Federated via graft
0

Configure Feed

Select the types of activity you want to include in your feed.

1// Package status tracks the outcome of each repo pair's most recent sync 2// pass, keeps a small dashboard over the activity log, and serves both over 3// HTTP — so an operator can tell the daemon is alive and see what it's 4// actually done without reading logs. 5package status 6 7import ( 8 "encoding/json" 9 "html/template" 10 "net/http" 11 "net/url" 12 "sort" 13 "strings" 14 "sync" 15 "time" 16 17 "graft/internal/state" 18) 19 20// PairResult is the outcome of one repo pair's most recent pass. An empty 21// error string means that scope is either disabled or succeeded. 22type PairResult struct { 23 Name string `json:"name"` 24 Series string `json:"series"` 25 LastRun time.Time `json:"last_run"` 26 GitError string `json:"git_error,omitempty"` 27 IssuesError string `json:"issues_error,omitempty"` 28 PatchError string `json:"patch_error,omitempty"` 29} 30 31func (r PairResult) ok() bool { 32 return r.GitError == "" && r.IssuesError == "" && r.PatchError == "" 33} 34 35// Tracker holds the latest result per repo pair, safe for concurrent use, 36// plus a handle on the state store for the activity dashboard. 37type Tracker struct { 38 startedAt time.Time 39 store *state.Store 40 sourceURL string 41 42 mu sync.Mutex 43 pairs map[string]PairResult 44} 45 46func NewTracker(store *state.Store, sourceURL string) *Tracker { 47 return &Tracker{startedAt: time.Now(), store: store, sourceURL: sourceURL, pairs: map[string]PairResult{}} 48} 49 50// Record stores the outcome of a pass for one pair. Pass nil for a scope 51// that succeeded or wasn't enabled. series is the dashboard row this pair's 52// status badge groups under (see config.RepoPair.Series). 53func (t *Tracker) Record(name, series string, gitErr, issuesErr, patchErr error) { 54 r := PairResult{Name: name, Series: series, LastRun: time.Now()} 55 if gitErr != nil { 56 r.GitError = gitErr.Error() 57 } 58 if issuesErr != nil { 59 r.IssuesError = issuesErr.Error() 60 } 61 if patchErr != nil { 62 r.PatchError = patchErr.Error() 63 } 64 t.mu.Lock() 65 defer t.mu.Unlock() 66 t.pairs[name] = r 67} 68 69func (t *Tracker) snapshot() []PairResult { 70 t.mu.Lock() 71 defer t.mu.Unlock() 72 out := make([]PairResult, 0, len(t.pairs)) 73 for _, r := range t.pairs { 74 out = append(out, r) 75 } 76 sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) 77 return out 78} 79 80type statusResponse struct { 81 StartedAt time.Time `json:"started_at"` 82 Pairs []PairResult `json:"pairs"` 83} 84 85// Handler serves: 86// 87// GET /healthz - 200 if every pair's last pass was clean, 503 otherwise 88// GET /api/status - the JSON snapshot Record() has been fed 89// GET / - the activity dashboard (calendar + recent commits) 90func (t *Tracker) Handler() http.Handler { 91 mux := http.NewServeMux() 92 93 mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { 94 for _, p := range t.snapshot() { 95 if !p.ok() { 96 w.WriteHeader(http.StatusServiceUnavailable) 97 return 98 } 99 } 100 w.WriteHeader(http.StatusOK) 101 }) 102 103 mux.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) { 104 resp := statusResponse{StartedAt: t.startedAt, Pairs: t.snapshot()} 105 w.Header().Set("Content-Type", "application/json") 106 json.NewEncoder(w).Encode(resp) 107 }) 108 109 mux.HandleFunc("/", t.serveDashboard) 110 111 return mux 112} 113 114const dashboardDays = 3 115 116func (t *Tracker) serveDashboard(w http.ResponseWriter, r *http.Request) { 117 since := time.Now().UTC().AddDate(0, 0, -(dashboardDays - 1)).Truncate(24 * time.Hour) 118 entries, err := t.store.ActivitySince(since) 119 if err != nil { 120 http.Error(w, "load activity: "+err.Error(), http.StatusInternalServerError) 121 return 122 } 123 124 pairs := groupPairStatus(t.snapshot()) 125 health := make(map[string]bool, len(pairs)) 126 for _, p := range pairs { 127 health[p.Name] = p.OK 128 } 129 130 data := dashboardData{ 131 Series: buildSeriesRows(entries, health), 132 Activity: recentActivity(entries), 133 SourceURL: t.sourceURL, 134 } 135 w.Header().Set("Content-Type", "text/html; charset=utf-8") 136 w.Header().Set("Cache-Control", "no-store") 137 if err := dashboardTmpl.Execute(w, data); err != nil { 138 http.Error(w, err.Error(), http.StatusInternalServerError) 139 } 140} 141 142type dashboardData struct { 143 Series []seriesRow 144 Activity []commitLine 145 SourceURL string 146} 147 148// pairStatus is one badge in the dashboard's top status card: one per 149// series (repo), OK only if every pair mirroring that repo's last pass was 150// clean — grouped so a repo with 3 mirrored sides (e.g. two Forgejos plus 151// an external instance) shows one badge, not three. 152type pairStatus struct { 153 Name string 154 OK bool 155} 156 157func groupPairStatus(results []PairResult) []pairStatus { 158 order := []string{} 159 ok := map[string]bool{} 160 for _, r := range results { 161 name := r.Series 162 if name == "" { 163 name = r.Name 164 } 165 if _, seen := ok[name]; !seen { 166 order = append(order, name) 167 ok[name] = true 168 } 169 if !r.ok() { 170 ok[name] = false 171 } 172 } 173 sort.Strings(order) 174 out := make([]pairStatus, 0, len(order)) 175 for _, name := range order { 176 out = append(out, pairStatus{Name: name, OK: ok[name]}) 177 } 178 return out 179} 180 181// seriesRow is one full-width strip of the activity heatmap: everything 182// mirrored for one underlying repository (grouped across its Forgejo and 183// Radicle sides via config.RepoPair.Series), one cell per actual event — 184// not one per day — so nothing is ever aggregated behind a single tooltip. 185type seriesRow struct { 186 Name string 187 OK bool 188 SubRows []subRow 189} 190 191// subRow is one mirrored side of a series — one real repo (a specific 192// Forgejo instance, or a specific Radicle node) — shown once the series is 193// expanded, with only that side's own events and its own link. A series 194// with 3 mirrored sides (two Forgejos plus Radicle) gets 3 sub-rows, never 195// one link picked arbitrarily to represent all of them. 196type subRow struct { 197 Label string 198 URL string 199 Events []event 200} 201 202// event is one activity_log row: one heatmap cell, one tooltip, one link. 203type event struct { 204 Kind string // "git", "issue", "patch" — drives the cell's color 205 Label string // "Commit", "Issue", "Patch" 206 Text string 207 URL string 208 Server string // which mirrored side this happened on, e.g. "f1", "alice" 209 When string // human-readable timestamp, for the tooltip 210} 211 212type commitLine struct { 213 When string 214 Pair string 215 PairURL string 216 Server string 217 Kind string // "Commit", "Issue", "Patch" 218 Message string 219 URL string 220} 221 222// serverLabel names which side of a series a repo_pair belongs to, e.g. 223// "federation-x-f1" under series "federation-x" becomes "f1". Several pairs 224// share one series row/badge, so without this an operator can't tell which 225// mirrored server (f1, f2, an external instance...) a given event actually 226// happened on. Empty if the pair name doesn't start with its series name. 227func serverLabel(repoPair, series string) string { 228 if series == "" { 229 return "" 230 } 231 suffix := strings.TrimPrefix(repoPair, series+"-") 232 if suffix == repoPair { 233 return "" 234 } 235 return suffix 236} 237 238// hostLabel shortens a URL's host to its canonical name for compact 239// display: the TLD is dropped ("f1.cyberwild.org" -> "f1.cyberwild", 240// "artefacts.bimr.net" -> "artefacts.bimr"), so every server — ours or an 241// external instance's — reads the same way, never a per-pair alias. 242func hostLabel(rawURL string) string { 243 u, err := url.Parse(rawURL) 244 if err != nil || u.Host == "" { 245 return "" 246 } 247 parts := strings.Split(u.Host, ".") 248 if len(parts) > 1 { 249 parts = parts[:len(parts)-1] 250 } 251 return strings.Join(parts, ".") 252} 253 254// buildSeriesRows groups entries by their Series (one row per monitored 255// repo, spanning both its Forgejo and Radicle sides) — Atlassian 256// Statuspage style: one full-width horizontal strip per series. Each entry 257// becomes its own cell, oldest first, so a busy day never hides events 258// behind one shared tooltip. 259func buildSeriesRows(entries []state.ActivityEntry, health map[string]bool) []seriesRow { 260 type subAcc struct { 261 url string 262 events []state.ActivityEntry 263 } 264 type seriesAcc struct { 265 subOrder []string 266 subs map[string]*subAcc 267 } 268 seriesAccs := map[string]*seriesAcc{} 269 var seriesOrder []string 270 for _, e := range entries { 271 name := e.Series 272 if name == "" { 273 name = e.RepoPair 274 } 275 sa, ok := seriesAccs[name] 276 if !ok { 277 sa = &seriesAcc{subs: map[string]*subAcc{}} 278 seriesAccs[name] = sa 279 seriesOrder = append(seriesOrder, name) 280 } 281 server := hostLabel(e.URL) 282 if server == "" { 283 server = serverLabel(e.RepoPair, e.Series) 284 } 285 if server == "" { 286 server = e.RepoPair 287 } 288 sub, ok := sa.subs[server] 289 if !ok { 290 sub = &subAcc{} 291 sa.subs[server] = sub 292 sa.subOrder = append(sa.subOrder, server) 293 } 294 if sub.url == "" { 295 sub.url = repoRootURL(e.URL) 296 } 297 sub.events = append(sub.events, e) 298 } 299 sort.Strings(seriesOrder) 300 301 rows := make([]seriesRow, 0, len(seriesOrder)) 302 for _, name := range seriesOrder { 303 sa := seriesAccs[name] 304 subOrder := append([]string(nil), sa.subOrder...) 305 sort.Strings(subOrder) 306 307 subRows := make([]subRow, 0, len(subOrder)) 308 for _, label := range subOrder { 309 sub := sa.subs[label] 310 // entries arrive newest-first (see ActivitySince); the 311 // heatmap reads left-to-right as oldest-to-newest. 312 es := make([]state.ActivityEntry, len(sub.events)) 313 copy(es, sub.events) 314 sort.Slice(es, func(i, j int) bool { return es[i].OccurredAt.Before(es[j].OccurredAt) }) 315 subRows = append(subRows, subRow{Label: label, URL: sub.url, Events: toEvents(es)}) 316 } 317 318 ok, known := health[name] 319 if !known { 320 ok = true 321 } 322 rows = append(rows, seriesRow{Name: name, OK: ok, SubRows: subRows}) 323 } 324 return rows 325} 326 327func toEvents(es []state.ActivityEntry) []event { 328 out := make([]event, 0, len(es)) 329 for _, e := range es { 330 server := hostLabel(e.URL) 331 if server == "" { 332 server = serverLabel(e.RepoPair, e.Series) 333 } 334 out = append(out, event{ 335 Kind: e.Kind, 336 Label: kindLabel(e.Kind), 337 Text: e.Summary, 338 URL: e.URL, 339 Server: server, 340 When: e.OccurredAt.Format("Mon, Jan 2, 15:04"), 341 }) 342 } 343 return out 344} 345 346func kindLabel(kind string) string { 347 switch kind { 348 case "git": 349 return "Commit" 350 case "issue": 351 return "Issue" 352 case "patch": 353 return "Patch" 354 default: 355 return kind 356 } 357} 358 359// recentActivity lists every mirrored event — commits, issues, and 360// patches/pull requests alike, newest first. graft doesn't mirror stars or 361// other social metadata: its sync scope is git content, issues, and 362// patches/PRs only (see config.SyncScope), so that's what shows up here. 363func recentActivity(entries []state.ActivityEntry) []commitLine { 364 out := make([]commitLine, 0, len(entries)) 365 for _, e := range entries { 366 name := e.Series 367 if name == "" { 368 name = e.RepoPair 369 } 370 server := hostLabel(e.URL) 371 if server == "" { 372 server = serverLabel(e.RepoPair, e.Series) 373 } 374 // The (server) parenthetical should point at that specific 375 // server's own repo, not the series' shared Radicle explorer 376 // link (which is fixed to whichever pair logged first). 377 pairURL := repoRootURL(e.URL) 378 if pairURL == "" { 379 pairURL = e.SeriesURL 380 } 381 out = append(out, commitLine{ 382 When: e.OccurredAt.Format("Jan 2, 15:04"), 383 Pair: name, 384 PairURL: pairURL, 385 Server: server, 386 Kind: kindLabel(e.Kind), 387 Message: e.Summary, 388 URL: e.URL, 389 }) 390 } 391 return out 392} 393 394// repoRootURL trims an event's specific-item URL (a commit, issue, or 395// patch/PR page) back down to its repository root, e.g. 396// ".../owner/repo/commit/<sha>" -> ".../owner/repo". Empty if rawURL is 397// empty or doesn't contain a recognized item path. 398func repoRootURL(rawURL string) string { 399 for _, marker := range []string{"/commit/", "/issues/", "/pulls/", "/patches/"} { 400 if idx := strings.Index(rawURL, marker); idx >= 0 { 401 return rawURL[:idx] 402 } 403 } 404 return "" 405} 406 407var dashboardTmpl = template.Must(template.New("dashboard").Parse(`<!doctype html> 408<html lang="en"> 409<head> 410<meta charset="utf-8"> 411<meta name="viewport" content="width=device-width, initial-scale=1"> 412<title>graft</title> 413<style> 414 :root { 415 color-scheme: light; 416 --surface: #ffffff; 417 --surface-sunk: #f4f5f7; 418 --text: #172b4d; 419 --text-muted: #6b778c; 420 --border: #dfe1e6; 421 --brand: #0052cc; 422 --brand-dark: #0747a6; 423 --kind-git: #0052cc; 424 --kind-issue: #00875a; 425 --kind-patch: #6554c0; 426 --bad: #de350b; 427 --good: #36b37e; 428 } 429 * { box-sizing: border-box; } 430 body { 431 margin: 0; 432 background: var(--surface-sunk); 433 color: var(--text); 434 font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; 435 } 436 main { max-width: 920px; margin: 0 auto; padding: 2.5rem 1.5rem 1rem; } 437 438 .top { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 1.5rem; } 439 h1 { font-size: 1.25rem; margin: 0; font-weight: 600; } 440 .sub { color: var(--text-muted); font-size: .85rem; } 441 442 .card { 443 background: var(--surface); 444 border: 1px solid var(--border); 445 border-radius: 4px; 446 padding: 1.25rem 1.5rem; 447 margin-bottom: 1.25rem; 448 } 449 450 .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); flex: none; display: inline-block; } 451 .dot.bad { background: var(--bad); } 452 453 .heatmap-head { display: flex; align-items: baseline; justify-content: space-between; } 454 .heatmap-head h2 { margin: 0; } 455 .legend { display: flex; gap: .9rem; margin-bottom: .9rem; } 456 .legend span { display: flex; align-items: center; gap: .35rem; font-size: .74rem; color: var(--text-muted); } 457 .legend .sw { width: 8px; height: 8px; border-radius: 2px; display: inline-block; } 458 .legend .sw[data-kind="git"] { background: var(--kind-git); } 459 .legend .sw[data-kind="issue"] { background: var(--kind-issue); } 460 .legend .sw[data-kind="patch"] { background: var(--kind-patch); } 461 .heatmap { display: flex; flex-direction: column; gap: .15rem; } 462 .series-group { border-bottom: 1px solid var(--border); } 463 .series-group:last-child { border-bottom: none; } 464 .series-group[open] { padding-bottom: .5rem; } 465 .series-label { 466 display: flex; align-items: center; gap: .45rem; padding: .55rem 0; 467 cursor: pointer; list-style: none; 468 } 469 .series-label::-webkit-details-marker { display: none; } 470 .series-label::before { 471 content: ""; width: 0; height: 0; flex: none; 472 border-style: solid; border-width: 4px 0 4px 5px; 473 border-color: transparent transparent transparent var(--text-muted); 474 transition: transform .12s ease; 475 } 476 .series-group[open] > .series-label::before { transform: rotate(90deg); } 477 .series-name { font-size: .82rem; font-weight: 600; color: var(--text); } 478 .series-count { font-size: .74rem; color: var(--text-muted); } 479 .subrows { display: flex; flex-direction: column; gap: .5rem; padding-left: 1.3rem; } 480 .subrow { display: flex; align-items: center; gap: .75rem; } 481 .subrow-name { 482 flex: 0 0 11rem; font-size: .78rem; color: var(--text-muted); text-decoration: none; 483 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 484 } 485 a.subrow-name { color: var(--brand); } 486 a.subrow-name:hover { text-decoration: underline; } 487 .days { display: flex; gap: 3px; flex-wrap: wrap; } 488 .cell { 489 flex: none; width: 10px; height: 22px; border-radius: 3px; 490 background: var(--border); position: relative; cursor: default; 491 } 492 .cell[data-kind="git"] { background: var(--kind-git); } 493 .cell[data-kind="issue"] { background: var(--kind-issue); } 494 .cell[data-kind="patch"] { background: var(--kind-patch); } 495 496 .tip { 497 display: none; position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%); 498 background: var(--text); color: #fff; border-radius: 6px; 499 font-size: .78rem; z-index: 20; padding: .5rem 0; 500 box-shadow: 0 4px 12px rgba(9,30,66,.25); 501 width: max-content; max-width: min(320px, 90vw); 502 } 503 .cell:nth-child(-n+2) .tip { left: 0; transform: none; } 504 .cell:nth-last-child(-n+2) .tip { left: auto; right: 0; transform: none; } 505 .cell:hover .tip, .tip:hover { display: block; } 506 .tip .tip-date { padding: 0 .75rem .35rem; font-weight: 600; border-bottom: 1px solid rgba(255,255,255,.15); margin-bottom: .35rem; } 507 .tip a.tip-row { 508 display: flex; gap: .4em; padding: .3rem .75rem; color: #fff; text-decoration: none; 509 white-space: normal; overflow-wrap: anywhere; line-height: 1.35; 510 } 511 .tip a.tip-row:hover { background: rgba(255,255,255,.12); } 512 .tip .tip-kind { color: #b3bac5; flex: none; } 513 .tip .tip-empty { padding: 0 .75rem; color: #b3bac5; } 514 515 h2 { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 0 0 .9rem; font-weight: 600; } 516 .commits { border-top: 1px solid var(--border); } 517 .commit { 518 display: flex; align-items: center; gap: .75rem; padding: .55rem 0; border-bottom: 1px solid var(--border); 519 font-size: .82rem; 520 } 521 .commit .when { color: var(--text-muted); flex: none; width: 9em; } 522 .commit .kind { color: var(--text-muted); flex: none; width: 4.5em; font-size: .74rem; text-transform: uppercase; letter-spacing: .03em; } 523 .commit .pair { 524 color: var(--text-muted); flex: 0 1 auto; max-width: 22em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 525 text-decoration: none; 526 } 527 .commit a.pair { color: var(--brand); } 528 .commit a.pair:hover { text-decoration: underline; } 529 .commit .msg { 530 flex: 1 1 auto; min-width: 0; 531 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, monospace; 532 color: inherit; text-decoration: none; 533 } 534 .commit a.msg { color: var(--brand); } 535 .commit a.msg:hover { text-decoration: underline; } 536 .empty { color: var(--text-muted); padding: .5rem 0; } 537 538 footer { 539 max-width: 920px; margin: 0 auto; padding: 1rem 1.5rem 2.5rem; 540 color: var(--text-muted); font-size: .78rem; display: flex; gap: 1rem; 541 } 542 footer a { color: var(--text-muted); } 543</style> 544</head> 545<body> 546<main> 547 <div class="top"> 548 <h1>graft</h1> 549 <span class="sub">Forgejo &lt;-&gt; Radicle sync — last 3 days</span> 550 </div> 551 552 <div class="card"> 553 <div class="heatmap-head"> 554 <h2>Activity by repo</h2> 555 <div class="legend"> 556 <span><i class="sw" data-kind="git"></i>Commit</span> 557 <span><i class="sw" data-kind="issue"></i>Issue</span> 558 <span><i class="sw" data-kind="patch"></i>Patch</span> 559 </div> 560 </div> 561 <div class="heatmap"> 562 {{range .Series}} 563 <details class="series-group"> 564 <summary class="series-label"> 565 <span class="dot{{if not .OK}} bad{{end}}"></span> 566 <span class="series-name">{{.Name}}</span> 567 <span class="series-count">{{len .SubRows}} mirrored side{{if ne (len .SubRows) 1}}s{{end}}</span> 568 </summary> 569 <div class="subrows"> 570 {{range .SubRows}} 571 <div class="subrow"> 572 {{if .URL}} 573 <a class="subrow-name" href="{{.URL}}" target="_blank" rel="noopener">{{.Label}}</a> 574 {{else}} 575 <span class="subrow-name">{{.Label}}</span> 576 {{end}} 577 <div class="days"> 578 {{range .Events}} 579 <div class="cell" data-kind="{{.Kind}}"> 580 <div class="tip"> 581 <div class="tip-date">{{.When}}</div> 582 {{if .URL}} 583 <a class="tip-row" href="{{.URL}}" target="_blank" rel="noopener"><span class="tip-kind">{{.Label}}</span>{{.Text}}</a> 584 {{else}} 585 <span class="tip-row"><span class="tip-kind">{{.Label}}</span>{{.Text}}</span> 586 {{end}} 587 </div> 588 </div> 589 {{else}} 590 <span class="tip-empty">nothing synced</span> 591 {{end}} 592 </div> 593 </div> 594 {{end}} 595 </div> 596 </details> 597 {{else}} 598 <span class="empty">nothing synced yet</span> 599 {{end}} 600 </div> 601 </div> 602 603 <div class="card"> 604 <h2>Recent activity</h2> 605 <div class="commits"> 606 {{range .Activity}} 607 <div class="commit"> 608 <span class="when">{{.When}}</span> 609 <span class="kind">{{.Kind}}</span> 610 {{if .PairURL}} 611 <a class="pair" href="{{.PairURL}}" target="_blank" rel="noopener">{{.Pair}}{{if .Server}} ({{.Server}}){{end}}</a> 612 {{else}} 613 <span class="pair">{{.Pair}}{{if .Server}} ({{.Server}}){{end}}</span> 614 {{end}} 615 {{if .URL}} 616 <a class="msg" href="{{.URL}}" target="_blank" rel="noopener">{{.Message}}</a> 617 {{else}} 618 <span class="msg">{{.Message}}</span> 619 {{end}} 620 </div> 621 {{else}} 622 <p class="empty">nothing mirrored yet</p> 623 {{end}} 624 </div> 625 </div> 626</main> 627<footer> 628 {{if .SourceURL}}<a href="{{.SourceURL}}" target="_blank" rel="noopener">{{.SourceURL}}</a>{{end}} 629 <span>GPLv3</span> 630</footer> 631</body> 632</html> 633`))