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 host := u.Host 248 // A Radicle Explorer URL's host is always the shared frontend 249 // (e.g. radicle.cyberwild.org) — the actual node lives in the path, 250 // /nodes/<node-host>/<rid>/... — so different Radicle nodes get 251 // distinct labels instead of collapsing into one shared one. 252 if segs := strings.Split(strings.TrimPrefix(u.Path, "/"), "/"); len(segs) >= 2 && segs[0] == "nodes" { 253 host = segs[1] 254 } 255 return host 256} 257 258// buildSeriesRows groups entries by their Series (one row per monitored 259// repo, spanning both its Forgejo and Radicle sides) — Atlassian 260// Statuspage style: one full-width horizontal strip per series. Each entry 261// becomes its own cell, oldest first, so a busy day never hides events 262// behind one shared tooltip. 263func buildSeriesRows(entries []state.ActivityEntry, health map[string]bool) []seriesRow { 264 type subAcc struct { 265 url string 266 events []state.ActivityEntry 267 } 268 type seriesAcc struct { 269 subOrder []string 270 subs map[string]*subAcc 271 } 272 seriesAccs := map[string]*seriesAcc{} 273 var seriesOrder []string 274 for _, e := range entries { 275 name := e.Series 276 if name == "" { 277 name = e.RepoPair 278 } 279 sa, ok := seriesAccs[name] 280 if !ok { 281 sa = &seriesAcc{subs: map[string]*subAcc{}} 282 seriesAccs[name] = sa 283 seriesOrder = append(seriesOrder, name) 284 } 285 server := hostLabel(e.URL) 286 if server == "" { 287 server = serverLabel(e.RepoPair, e.Series) 288 } 289 if server == "" { 290 server = e.RepoPair 291 } 292 sub, ok := sa.subs[server] 293 if !ok { 294 sub = &subAcc{} 295 sa.subs[server] = sub 296 sa.subOrder = append(sa.subOrder, server) 297 } 298 if sub.url == "" { 299 sub.url = repoRootURL(e.URL) 300 } 301 sub.events = append(sub.events, e) 302 } 303 sort.Strings(seriesOrder) 304 305 rows := make([]seriesRow, 0, len(seriesOrder)) 306 for _, name := range seriesOrder { 307 sa := seriesAccs[name] 308 subOrder := append([]string(nil), sa.subOrder...) 309 sort.Strings(subOrder) 310 311 subRows := make([]subRow, 0, len(subOrder)) 312 for _, label := range subOrder { 313 sub := sa.subs[label] 314 // entries arrive newest-first (see ActivitySince); the 315 // heatmap reads left-to-right as oldest-to-newest. 316 es := make([]state.ActivityEntry, len(sub.events)) 317 copy(es, sub.events) 318 sort.Slice(es, func(i, j int) bool { return es[i].OccurredAt.Before(es[j].OccurredAt) }) 319 subRows = append(subRows, subRow{Label: label, URL: sub.url, Events: toEvents(es)}) 320 } 321 322 ok, known := health[name] 323 if !known { 324 ok = true 325 } 326 rows = append(rows, seriesRow{Name: name, OK: ok, SubRows: subRows}) 327 } 328 return rows 329} 330 331func toEvents(es []state.ActivityEntry) []event { 332 out := make([]event, 0, len(es)) 333 for _, e := range es { 334 server := hostLabel(e.URL) 335 if server == "" { 336 server = serverLabel(e.RepoPair, e.Series) 337 } 338 out = append(out, event{ 339 Kind: e.Kind, 340 Label: kindLabel(e.Kind), 341 Text: e.Summary, 342 URL: e.URL, 343 Server: server, 344 When: e.OccurredAt.Format("Mon, Jan 2, 15:04"), 345 }) 346 } 347 return out 348} 349 350func kindLabel(kind string) string { 351 switch kind { 352 case "git": 353 return "Commit" 354 case "issue": 355 return "Issue" 356 case "patch": 357 return "Patch" 358 default: 359 return kind 360 } 361} 362 363// recentActivity lists every mirrored event — commits, issues, and 364// patches/pull requests alike, newest first. graft doesn't mirror stars or 365// other social metadata: its sync scope is git content, issues, and 366// patches/PRs only (see config.SyncScope), so that's what shows up here. 367func recentActivity(entries []state.ActivityEntry) []commitLine { 368 out := make([]commitLine, 0, len(entries)) 369 for _, e := range entries { 370 name := e.Series 371 if name == "" { 372 name = e.RepoPair 373 } 374 server := hostLabel(e.URL) 375 if server == "" { 376 server = serverLabel(e.RepoPair, e.Series) 377 } 378 // The (server) parenthetical should point at that specific 379 // server's own repo, not the series' shared Radicle explorer 380 // link (which is fixed to whichever pair logged first). 381 pairURL := repoRootURL(e.URL) 382 if pairURL == "" { 383 pairURL = e.SeriesURL 384 } 385 out = append(out, commitLine{ 386 When: e.OccurredAt.Format("Jan 2, 15:04"), 387 Pair: name, 388 PairURL: pairURL, 389 Server: server, 390 Kind: kindLabel(e.Kind), 391 Message: e.Summary, 392 URL: e.URL, 393 }) 394 } 395 return out 396} 397 398// repoRootURL trims an event's specific-item URL (a commit, issue, or 399// patch/PR page) back down to its repository root, e.g. 400// ".../owner/repo/commit/<sha>" -> ".../owner/repo". Empty if rawURL is 401// empty or doesn't contain a recognized item path. 402func repoRootURL(rawURL string) string { 403 // Forgejo uses singular "/commit/"; Radicle Explorer uses plural 404 // "/commits/" — both must be recognized, or every Radicle-hosted 405 // commit link falls back to a non-clickable label. 406 for _, marker := range []string{"/commit/", "/commits/", "/issues/", "/pulls/", "/patches/"} { 407 if idx := strings.Index(rawURL, marker); idx >= 0 { 408 return rawURL[:idx] 409 } 410 } 411 return "" 412} 413 414var dashboardTmpl = template.Must(template.New("dashboard").Parse(`<!doctype html> 415<html lang="en"> 416<head> 417<meta charset="utf-8"> 418<meta name="viewport" content="width=device-width, initial-scale=1"> 419<title>graft</title> 420<style> 421 :root { 422 color-scheme: light; 423 --surface: #ffffff; 424 --surface-sunk: #f4f5f7; 425 --text: #172b4d; 426 --text-muted: #6b778c; 427 --border: #dfe1e6; 428 --brand: #0052cc; 429 --brand-dark: #0747a6; 430 --kind-git: #0052cc; 431 --kind-issue: #00875a; 432 --kind-patch: #6554c0; 433 --bad: #de350b; 434 --good: #36b37e; 435 } 436 * { box-sizing: border-box; } 437 body { 438 margin: 0; 439 background: var(--surface-sunk); 440 color: var(--text); 441 font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; 442 } 443 main { max-width: 920px; margin: 0 auto; padding: 2.5rem 1.5rem 1rem; } 444 445 .top { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 1.5rem; } 446 h1 { font-size: 1.25rem; margin: 0; font-weight: 600; } 447 .sub { color: var(--text-muted); font-size: .85rem; } 448 449 .card { 450 background: var(--surface); 451 border: 1px solid var(--border); 452 border-radius: 4px; 453 padding: 1.25rem 1.5rem; 454 margin-bottom: 1.25rem; 455 } 456 457 .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); flex: none; display: inline-block; } 458 .dot.bad { background: var(--bad); } 459 460 .heatmap-head { display: flex; align-items: baseline; justify-content: space-between; } 461 .heatmap-head h2 { margin: 0; } 462 .legend { display: flex; gap: .9rem; margin-bottom: .9rem; } 463 .legend span { display: flex; align-items: center; gap: .35rem; font-size: .74rem; color: var(--text-muted); } 464 .legend .sw { width: 8px; height: 8px; border-radius: 2px; display: inline-block; } 465 .legend .sw[data-kind="git"] { background: var(--kind-git); } 466 .legend .sw[data-kind="issue"] { background: var(--kind-issue); } 467 .legend .sw[data-kind="patch"] { background: var(--kind-patch); } 468 .heatmap { display: flex; flex-direction: column; gap: .15rem; } 469 .series-group { border-bottom: 1px solid var(--border); } 470 .series-group:last-child { border-bottom: none; } 471 .series-group[open] { padding-bottom: .5rem; } 472 .series-label { 473 display: flex; align-items: center; gap: .45rem; padding: .55rem 0; 474 cursor: pointer; list-style: none; 475 } 476 .series-label::-webkit-details-marker { display: none; } 477 .series-label::before { 478 content: ""; width: 0; height: 0; flex: none; 479 border-style: solid; border-width: 4px 0 4px 5px; 480 border-color: transparent transparent transparent var(--text-muted); 481 transition: transform .12s ease; 482 } 483 .series-group[open] > .series-label::before { transform: rotate(90deg); } 484 .series-name { font-size: .82rem; font-weight: 600; color: var(--text); } 485 .series-count { font-size: .74rem; color: var(--text-muted); } 486 .subrows { display: flex; flex-direction: column; gap: .5rem; padding-left: 1.3rem; } 487 .subrow { display: flex; align-items: center; gap: .75rem; } 488 .subrow-name { 489 flex: 0 0 11rem; font-size: .78rem; color: var(--text-muted); text-decoration: none; 490 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 491 } 492 a.subrow-name { color: var(--brand); } 493 a.subrow-name:hover { text-decoration: underline; } 494 .days { display: flex; gap: 3px; flex-wrap: wrap; } 495 .cell { 496 flex: none; width: 10px; height: 22px; border-radius: 3px; 497 background: var(--border); position: relative; cursor: default; 498 } 499 .cell[data-kind="git"] { background: var(--kind-git); } 500 .cell[data-kind="issue"] { background: var(--kind-issue); } 501 .cell[data-kind="patch"] { background: var(--kind-patch); } 502 503 .tip { 504 display: none; position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%); 505 background: var(--text); color: #fff; border-radius: 6px; 506 font-size: .78rem; z-index: 20; padding: .5rem 0; 507 box-shadow: 0 4px 12px rgba(9,30,66,.25); 508 width: max-content; max-width: min(320px, 90vw); 509 } 510 .cell:nth-child(-n+2) .tip { left: 0; transform: none; } 511 .cell:nth-last-child(-n+2) .tip { left: auto; right: 0; transform: none; } 512 .cell:hover .tip, .tip:hover { display: block; } 513 .tip .tip-date { padding: 0 .75rem .35rem; font-weight: 600; border-bottom: 1px solid rgba(255,255,255,.15); margin-bottom: .35rem; } 514 .tip a.tip-row { 515 display: flex; gap: .4em; padding: .3rem .75rem; color: #fff; text-decoration: none; 516 white-space: normal; overflow-wrap: anywhere; line-height: 1.35; 517 } 518 .tip a.tip-row:hover { background: rgba(255,255,255,.12); } 519 .tip .tip-kind { color: #b3bac5; flex: none; } 520 .tip .tip-empty { padding: 0 .75rem; color: #b3bac5; } 521 522 h2 { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 0 0 .9rem; font-weight: 600; } 523 .commits { border-top: 1px solid var(--border); } 524 .commit { 525 display: flex; align-items: center; gap: .75rem; padding: .55rem 0; border-bottom: 1px solid var(--border); 526 font-size: .82rem; 527 } 528 .commit .when { color: var(--text-muted); flex: none; width: 9em; } 529 .commit .kind { color: var(--text-muted); flex: none; width: 4.5em; font-size: .74rem; text-transform: uppercase; letter-spacing: .03em; } 530 .commit .pair { 531 color: var(--text-muted); flex: 0 1 auto; max-width: 22em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 532 text-decoration: none; 533 } 534 .commit a.pair { color: var(--brand); } 535 .commit a.pair:hover { text-decoration: underline; } 536 .commit .msg { 537 flex: 1 1 auto; min-width: 0; 538 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, monospace; 539 color: inherit; text-decoration: none; 540 } 541 .commit a.msg { color: var(--brand); } 542 .commit a.msg:hover { text-decoration: underline; } 543 .empty { color: var(--text-muted); padding: .5rem 0; } 544 545 footer { 546 max-width: 920px; margin: 0 auto; padding: 1rem 1.5rem 2.5rem; 547 color: var(--text-muted); font-size: .78rem; display: flex; gap: 1rem; 548 } 549 footer a { color: var(--text-muted); } 550</style> 551</head> 552<body> 553<main> 554 <div class="top"> 555 <h1>graft</h1> 556 <span class="sub">Forgejo &lt;-&gt; Radicle sync — last 3 days</span> 557 </div> 558 559 <div class="card"> 560 <div class="heatmap-head"> 561 <h2>Activity by repo</h2> 562 <div class="legend"> 563 <span><i class="sw" data-kind="git"></i>Commit</span> 564 <span><i class="sw" data-kind="issue"></i>Issue</span> 565 <span><i class="sw" data-kind="patch"></i>Patch</span> 566 </div> 567 </div> 568 <div class="heatmap"> 569 {{range .Series}} 570 <details class="series-group"> 571 <summary class="series-label"> 572 <span class="dot{{if not .OK}} bad{{end}}"></span> 573 <span class="series-name">{{.Name}}</span> 574 <span class="series-count">{{len .SubRows}} mirrored side{{if ne (len .SubRows) 1}}s{{end}}</span> 575 </summary> 576 <div class="subrows"> 577 {{range .SubRows}} 578 <div class="subrow"> 579 {{if .URL}} 580 <a class="subrow-name" href="{{.URL}}" target="_blank" rel="noopener">{{.Label}}</a> 581 {{else}} 582 <span class="subrow-name">{{.Label}}</span> 583 {{end}} 584 <div class="days"> 585 {{range .Events}} 586 <div class="cell" data-kind="{{.Kind}}"> 587 <div class="tip"> 588 <div class="tip-date">{{.When}}</div> 589 {{if .URL}} 590 <a class="tip-row" href="{{.URL}}" target="_blank" rel="noopener"><span class="tip-kind">{{.Label}}</span>{{.Text}}</a> 591 {{else}} 592 <span class="tip-row"><span class="tip-kind">{{.Label}}</span>{{.Text}}</span> 593 {{end}} 594 </div> 595 </div> 596 {{else}} 597 <span class="tip-empty">nothing synced</span> 598 {{end}} 599 </div> 600 </div> 601 {{end}} 602 </div> 603 </details> 604 {{else}} 605 <span class="empty">nothing synced yet</span> 606 {{end}} 607 </div> 608 </div> 609 610 <div class="card"> 611 <h2>Recent activity</h2> 612 <div class="commits"> 613 {{range .Activity}} 614 <div class="commit"> 615 <span class="when">{{.When}}</span> 616 <span class="kind">{{.Kind}}</span> 617 {{if .PairURL}} 618 <a class="pair" href="{{.PairURL}}" target="_blank" rel="noopener">{{.Pair}}{{if .Server}} ({{.Server}}){{end}}</a> 619 {{else}} 620 <span class="pair">{{.Pair}}{{if .Server}} ({{.Server}}){{end}}</span> 621 {{end}} 622 {{if .URL}} 623 <a class="msg" href="{{.URL}}" target="_blank" rel="noopener">{{.Message}}</a> 624 {{else}} 625 <span class="msg">{{.Message}}</span> 626 {{end}} 627 </div> 628 {{else}} 629 <p class="empty">nothing mirrored yet</p> 630 {{end}} 631 </div> 632 </div> 633</main> 634<footer> 635 {{if .SourceURL}}<a href="{{.SourceURL}}" target="_blank" rel="noopener">{{.SourceURL}}</a>{{end}} 636 <span>GPLv3</span> 637</footer> 638</body> 639</html> 640`))