|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func (a *app) ensureSchedule(year int) error { |
| 16 | + if a.fileExists(filepath.Join("seasons", strconv.Itoa(year), "schedule.json")) { |
| 17 | + return nil |
| 18 | + } |
| 19 | + |
| 20 | + a.scheduleLockMu.Lock() |
| 21 | + lock := a.scheduleLocks[year] |
| 22 | + if lock == nil { |
| 23 | + lock = &sync.Mutex{} |
| 24 | + a.scheduleLocks[year] = lock |
| 25 | + } |
| 26 | + a.scheduleLockMu.Unlock() |
| 27 | + |
| 28 | + lock.Lock() |
| 29 | + defer lock.Unlock() |
| 30 | + |
| 31 | + if a.fileExists(filepath.Join("seasons", strconv.Itoa(year), "schedule.json")) { |
| 32 | + return nil |
| 33 | + } |
| 34 | + |
| 35 | + if a.processor == nil { |
| 36 | + return errors.New("session processor is not initialized") |
| 37 | + } |
| 38 | + return a.processor.EnsureSchedule(context.Background(), year) |
| 39 | +} |
| 40 | + |
| 41 | +func (a *app) ensureSessionData(year, round int, sessionType string, onStatus func(string)) error { |
| 42 | + path := filepath.Join("sessions", strconv.Itoa(year), strconv.Itoa(round), sessionType, "replay.json") |
| 43 | + if a.fileExists(path) { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + key := fmt.Sprintf("%d_%d_%s", year, round, sessionType) |
| 48 | + a.sessionLockMu.Lock() |
| 49 | + lock := a.sessionLocks[key] |
| 50 | + if lock == nil { |
| 51 | + lock = &sync.Mutex{} |
| 52 | + a.sessionLocks[key] = lock |
| 53 | + } |
| 54 | + a.sessionLockMu.Unlock() |
| 55 | + |
| 56 | + lock.Lock() |
| 57 | + defer lock.Unlock() |
| 58 | + |
| 59 | + if a.fileExists(path) { |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + if a.processor == nil { |
| 64 | + return errors.New("session processor is not initialized") |
| 65 | + } |
| 66 | + return a.processor.ProcessSession(context.Background(), year, round, sessionType, onStatus) |
| 67 | +} |
| 68 | + |
| 69 | +func (a *app) buildEvents(year int) (map[string]any, error) { |
| 70 | + raw, err := a.readJSONAny(filepath.Join("seasons", strconv.Itoa(year), "schedule.json")) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + root, ok := raw.(map[string]any) |
| 75 | + if !ok { |
| 76 | + return nil, errors.New("invalid schedule format") |
| 77 | + } |
| 78 | + b, _ := json.Marshal(root) |
| 79 | + _ = json.Unmarshal(b, &root) |
| 80 | + |
| 81 | + events, _ := root["events"].([]any) |
| 82 | + now := time.Now().UTC() |
| 83 | + lastPast := -1 |
| 84 | + |
| 85 | + for i, evtAny := range events { |
| 86 | + evt, ok := evtAny.(map[string]any) |
| 87 | + if !ok { |
| 88 | + continue |
| 89 | + } |
| 90 | + hasPast := false |
| 91 | + sessions, _ := evt["sessions"].([]any) |
| 92 | + for _, sAny := range sessions { |
| 93 | + s, ok := sAny.(map[string]any) |
| 94 | + if !ok { |
| 95 | + continue |
| 96 | + } |
| 97 | + dateStr := asString(s["date_utc"]) |
| 98 | + available := false |
| 99 | + if ts, ok := parseDateMaybe(dateStr); ok { |
| 100 | + available = ts.Before(now) |
| 101 | + if available { |
| 102 | + hasPast = true |
| 103 | + } |
| 104 | + if !strings.HasSuffix(dateStr, "Z") { |
| 105 | + s["date_utc"] = strings.ReplaceAll(dateStr, " ", "T") + "Z" |
| 106 | + } |
| 107 | + } |
| 108 | + s["available"] = available |
| 109 | + |
| 110 | + sessionType := normalizeSessionType(asString(s["session_type"])) |
| 111 | + if sessionType == "" { |
| 112 | + sessionType = normalizeSessionType(sessionNameToType[asString(s["name"])]) |
| 113 | + } |
| 114 | + if sessionType != "" { |
| 115 | + s["session_type"] = sessionType |
| 116 | + st := a.sessionDownloadStatus(year, asInt(evt["round_number"]), sessionType) |
| 117 | + s["download_state"] = st.DownloadState |
| 118 | + s["downloaded"] = st.Downloaded |
| 119 | + if strings.TrimSpace(st.LastError) != "" { |
| 120 | + s["last_error"] = st.LastError |
| 121 | + } |
| 122 | + if strings.TrimSpace(st.UpdatedAt) != "" { |
| 123 | + s["updated_at"] = st.UpdatedAt |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + if hasPast { |
| 128 | + evt["status"] = "available" |
| 129 | + lastPast = i |
| 130 | + } else { |
| 131 | + evt["status"] = "future" |
| 132 | + } |
| 133 | + } |
| 134 | + if lastPast >= 0 { |
| 135 | + if evt, ok := events[lastPast].(map[string]any); ok { |
| 136 | + evt["status"] = "latest" |
| 137 | + } |
| 138 | + } |
| 139 | + return root, nil |
| 140 | +} |
| 141 | + |
| 142 | +func parseDateMaybe(s string) (time.Time, bool) { |
| 143 | + s = strings.TrimSpace(s) |
| 144 | + if s == "" { |
| 145 | + return time.Time{}, false |
| 146 | + } |
| 147 | + if t, err := time.Parse(time.RFC3339, strings.ReplaceAll(s, " ", "T")); err == nil { |
| 148 | + return t.UTC(), true |
| 149 | + } |
| 150 | + if strings.HasSuffix(s, "Z") { |
| 151 | + if t, err := time.Parse("2006-01-02T15:04:05Z", strings.ReplaceAll(s, " ", "T")); err == nil { |
| 152 | + return t.UTC(), true |
| 153 | + } |
| 154 | + } |
| 155 | + return time.Time{}, false |
| 156 | +} |
| 157 | + |
| 158 | +func (a *app) readJSONAny(rel string) (any, error) { |
| 159 | + if a.store == nil { |
| 160 | + return nil, errors.New("sqlite store is not configured") |
| 161 | + } |
| 162 | + b, err := a.store.GetJSONArtifact(context.Background(), filepath.ToSlash(rel)) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + var out any |
| 167 | + if err := json.Unmarshal(b, &out); err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + return out, nil |
| 171 | +} |
| 172 | + |
| 173 | +func (a *app) fileExists(rel string) bool { |
| 174 | + if a.store == nil { |
| 175 | + return false |
| 176 | + } |
| 177 | + _, err := a.store.GetJSONArtifact(context.Background(), filepath.ToSlash(rel)) |
| 178 | + return err == nil |
| 179 | +} |
0 commit comments