|
| 1 | +package reporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + |
| 6 | + "cloud.google.com/go/firestore" |
| 7 | + "github.com/Hack4Impact-UMD/professor/db" |
| 8 | + "github.com/Hack4Impact-UMD/professor/firebase" |
| 9 | + "github.com/Hack4Impact-UMD/professor/util" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + maxLogBytes = 50 * 1024 // 50KB |
| 14 | + maxTestOutputBytes = 10 * 1024 // 10KB |
| 15 | + |
| 16 | + collectionPublic = "gradingJobs" |
| 17 | + collectionInternal = "gradingJobsInternal" |
| 18 | +) |
| 19 | + |
| 20 | +type FirestoreReporter struct { |
| 21 | + fsClient *firestore.Client |
| 22 | + tests map[string][]db.TestResult |
| 23 | +} |
| 24 | + |
| 25 | +func NewFirestoreReporter(fsClient *firestore.Client) (*FirestoreReporter, error) { |
| 26 | + if fsClient == nil { |
| 27 | + return nil, errors.New("fsClient argument for reporter is nil") |
| 28 | + } |
| 29 | + |
| 30 | + return &FirestoreReporter{ |
| 31 | + fsClient: fsClient, |
| 32 | + tests: make(map[string][]db.TestResult), |
| 33 | + }, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (r *FirestoreReporter) updatePublicDoc(jobId string, data map[string]any) error { |
| 37 | + return firebase.UpdateDoc(r.fsClient, collectionPublic, jobId, data) |
| 38 | +} |
| 39 | + |
| 40 | +func (r *FirestoreReporter) updateInternalDoc(jobId string, data map[string]any) error { |
| 41 | + return firebase.UpdateDoc(r.fsClient, collectionInternal, jobId, data) |
| 42 | +} |
| 43 | + |
| 44 | +func truncateLog(log string, maxBytes int) string { |
| 45 | + if len(log) <= maxBytes { |
| 46 | + return log |
| 47 | + } |
| 48 | + return "Tail:\n" + log[len(log)-maxBytes:] |
| 49 | +} |
| 50 | + |
| 51 | +func (r *FirestoreReporter) OnGradeStart(jobId string) { |
| 52 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 53 | + "status": db.StatusPending, |
| 54 | + "updated": firestore.ServerTimestamp, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func (r *FirestoreReporter) OnCloneStart(jobId, assessmentRepo, testRepo string) { |
| 59 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 60 | + "status": db.StatusCloning, |
| 61 | + "updated": firestore.ServerTimestamp, |
| 62 | + }) |
| 63 | + |
| 64 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 65 | + "testRepo": testRepo, |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func (r *FirestoreReporter) OnCloneEnd(jobId, assessmentRepo, testRepo string, err error) { |
| 70 | + if err != nil { |
| 71 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 72 | + "status": db.StatusFailed, |
| 73 | + "error": err.Error(), |
| 74 | + "completed": firestore.ServerTimestamp, |
| 75 | + "updated": firestore.ServerTimestamp, |
| 76 | + }) |
| 77 | + |
| 78 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 79 | + "error": err.Error(), |
| 80 | + }) |
| 81 | + |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 86 | + "updated": firestore.ServerTimestamp, |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func (r *FirestoreReporter) OnInstallStart(jobId string) { |
| 91 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 92 | + "status": db.StatusInstalling, |
| 93 | + "updated": firestore.ServerTimestamp, |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func (r *FirestoreReporter) OnInstallEnd(jobId, out string, err error) { |
| 98 | + if err != nil { |
| 99 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 100 | + "status": db.StatusFailed, |
| 101 | + "error": err.Error(), |
| 102 | + "completed": firestore.ServerTimestamp, |
| 103 | + "updated": firestore.ServerTimestamp, |
| 104 | + }) |
| 105 | + |
| 106 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 107 | + "error": err.Error(), |
| 108 | + "installLog": truncateLog(out, maxLogBytes), |
| 109 | + }) |
| 110 | + |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 115 | + "updated": firestore.ServerTimestamp, |
| 116 | + }) |
| 117 | + |
| 118 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 119 | + "installLog": truncateLog(out, maxLogBytes), |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +func (r *FirestoreReporter) OnBuildStart(jobId string) { |
| 124 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 125 | + "status": db.StatusBuilding, |
| 126 | + "updated": firestore.ServerTimestamp, |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +func (r *FirestoreReporter) OnBuildEnd(jobId, out string, err error) { |
| 131 | + if err != nil { |
| 132 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 133 | + "status": db.StatusFailed, |
| 134 | + "error": err.Error(), |
| 135 | + "completed": firestore.ServerTimestamp, |
| 136 | + "updated": firestore.ServerTimestamp, |
| 137 | + }) |
| 138 | + |
| 139 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 140 | + "error": err.Error(), |
| 141 | + "buildLog": truncateLog(out, maxLogBytes), |
| 142 | + }) |
| 143 | + |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 148 | + "updated": firestore.ServerTimestamp, |
| 149 | + }) |
| 150 | + |
| 151 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 152 | + "buildLog": truncateLog(out, maxLogBytes), |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +func (r *FirestoreReporter) OnServe(jobId string, err error) { |
| 157 | + if err != nil { |
| 158 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 159 | + "status": db.StatusFailed, |
| 160 | + "error": err.Error(), |
| 161 | + "completed": firestore.ServerTimestamp, |
| 162 | + "updated": firestore.ServerTimestamp, |
| 163 | + }) |
| 164 | + |
| 165 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 166 | + "error": err.Error(), |
| 167 | + }) |
| 168 | + |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 173 | + "status": db.StatusServing, |
| 174 | + "updated": firestore.ServerTimestamp, |
| 175 | + }) |
| 176 | +} |
| 177 | + |
| 178 | +func (r *FirestoreReporter) OnTestingStart(jobId string, suites []string, err error) { |
| 179 | + if err != nil { |
| 180 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 181 | + "status": db.StatusFailed, |
| 182 | + "error": err.Error(), |
| 183 | + "completed": firestore.ServerTimestamp, |
| 184 | + "updated": firestore.ServerTimestamp, |
| 185 | + }) |
| 186 | + |
| 187 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 188 | + "error": err.Error(), |
| 189 | + }) |
| 190 | + |
| 191 | + return |
| 192 | + } |
| 193 | + |
| 194 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 195 | + "status": db.StatusTesting, |
| 196 | + "updated": firestore.ServerTimestamp, |
| 197 | + }) |
| 198 | +} |
| 199 | + |
| 200 | +func (r *FirestoreReporter) OnTestStart(jobId, suite, testName string) { |
| 201 | + // No-op rn as it would incur a lot of extra writes, but could update timestamp later |
| 202 | +} |
| 203 | + |
| 204 | +func (r *FirestoreReporter) OnTestEnd(jobId, suite, testName string, passed bool, stdout, stderr string, testErrors []string, durationMs int64, err error) { |
| 205 | + result := db.TestResult{ |
| 206 | + Suite: suite, |
| 207 | + TestName: testName, |
| 208 | + Passed: passed, |
| 209 | + Stdout: truncateLog(stdout, maxTestOutputBytes), |
| 210 | + Stderr: truncateLog(stderr, maxTestOutputBytes), |
| 211 | + Errors: testErrors, |
| 212 | + DurationMs: durationMs, |
| 213 | + Points: 0, // TODO: implement point extraction to fill this |
| 214 | + } |
| 215 | + |
| 216 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 217 | + "tests": map[string]any{ |
| 218 | + suite: firestore.ArrayUnion(result), |
| 219 | + }, |
| 220 | + }) |
| 221 | + |
| 222 | + publicTestUpdates := map[string]any{ |
| 223 | + "suiteName": suite, |
| 224 | + "total": firestore.Increment(1), |
| 225 | + "durationMs": firestore.Increment(durationMs), |
| 226 | + } |
| 227 | + |
| 228 | + if passed { |
| 229 | + publicTestUpdates["passed"] = firestore.Increment(1) |
| 230 | + } else { |
| 231 | + publicTestUpdates["failed"] = firestore.Increment(1) |
| 232 | + } |
| 233 | + |
| 234 | + publicTestUpdates["points"] = firestore.Increment(result.Points) |
| 235 | + publicTestUpdates["totalPoints"] = firestore.Increment(result.Points) |
| 236 | + |
| 237 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 238 | + "completedTests": firestore.Increment(1), |
| 239 | + "updated": firestore.ServerTimestamp, |
| 240 | + "publicTests": map[string]any{ |
| 241 | + suite: publicTestUpdates, |
| 242 | + }, |
| 243 | + }) |
| 244 | +} |
| 245 | + |
| 246 | +func (r *FirestoreReporter) OnTestingEnd(jobId string, err error) { |
| 247 | + if err != nil { |
| 248 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 249 | + "status": db.StatusFailed, |
| 250 | + "error": err.Error(), |
| 251 | + "completed": firestore.ServerTimestamp, |
| 252 | + "updated": firestore.ServerTimestamp, |
| 253 | + }) |
| 254 | + |
| 255 | + _ = r.updateInternalDoc(jobId, map[string]any{ |
| 256 | + "error": err.Error(), |
| 257 | + }) |
| 258 | + |
| 259 | + return |
| 260 | + } |
| 261 | + |
| 262 | + _ = r.updatePublicDoc(jobId, map[string]any{ |
| 263 | + "status": db.StatusCompleted, |
| 264 | + "completed": firestore.ServerTimestamp, |
| 265 | + "updated": firestore.ServerTimestamp, |
| 266 | + }) |
| 267 | +} |
| 268 | + |
| 269 | +var _ util.GradingJobReporter = (*FirestoreReporter)(nil) |
0 commit comments