HomeEntrypoint.vala 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Invercargill.Expressions;
  4. using Spry;
  5. using Spry.Authentication;
  6. using Statum;
  7. using ClankerSaddle.Data;
  8. using ClankerSaddle.Services;
  9. namespace ClankerSaddle {
  10. /** A saddle card on the dashboard. */
  11. public class DashboardSaddleView : Object {
  12. public int64 id { get; set; }
  13. public string name { get; set; default = ""; }
  14. public string status { get; set; default = "offline"; }
  15. public string host { get; set; default = ""; }
  16. public int64 current_task_id { get; set; }
  17. public string current_task_title { get; set; default = ""; }
  18. /**
  19. * Set when the saddle's current task is a goal agent's run:
  20. * the card then links the GOAL (runs are internal — never a
  21. * task destination at a glance).
  22. */
  23. public int64 current_goal_id { get; set; }
  24. public string current_goal_title { get; set; default = ""; }
  25. }
  26. /** One entry in the dashboard's cross-task activity timeline. */
  27. public class DashboardActivityView : Object {
  28. public string kind { get; set; default = ""; }
  29. public int64 at_epoch { get; set; }
  30. public string when_display { get; set; default = ""; }
  31. public int64 task_id { get; set; }
  32. public string task_title { get; set; default = ""; }
  33. /** Plan 12: set on goal events — the row links the goal page. */
  34. public int64 goal_id { get; set; }
  35. public string goal_title { get; set; default = ""; }
  36. public string text { get; set; default = ""; }
  37. /**
  38. * The referenced task earned its 🤠 badge — any of its sessions
  39. * carries a `saddle yeehaw` entry (derived, nothing stored).
  40. */
  41. public bool task_yeehaw { get; set; }
  42. }
  43. /** An active task with its latest progress line. */
  44. public class DashboardActiveView : Object {
  45. public int64 id { get; set; }
  46. public string title { get; set; default = ""; }
  47. public string status { get; set; default = ""; }
  48. public string latest_progress { get; set; default = ""; }
  49. public string progress_display { get; set; default = ""; }
  50. public string task_branch { get; set; default = ""; }
  51. }
  52. /**
  53. * A pinned OPEN blocking item (plan 10 phase 3; plan 12 unified;
  54. * plan 15 C1 single-row): an unanswered question, RFC, review or
  55. * the goal agent's block (question/RFC, plan submission,
  56. * completion report — all rows of the goal-run task) rendered as
  57. * a link-through card. Goal items carry `item_type` "goal" and
  58. * link the goal page; task items carry "task" and link the task
  59. * page where the answer form lives. No inline answer forms on the
  60. * dashboard (settled).
  61. */
  62. public class DashboardBlockView : Object {
  63. /** "task" or "goal" — the pile interleaves both. */
  64. public string item_type { get; set; default = "task"; }
  65. public int64 id { get; set; }
  66. public int64 task_id { get; set; }
  67. public string task_title { get; set; default = ""; }
  68. /** Goal items only: the goal the block belongs to. */
  69. public int64 goal_id { get; set; }
  70. public string goal_title { get; set; default = ""; }
  71. public string block_type { get; set; default = "question"; }
  72. public string question { get; set; default = ""; }
  73. public string created_display { get; set; default = ""; }
  74. /** The created stamp's epoch — the unified newest-first key. */
  75. public int64 at_epoch { get; set; }
  76. }
  77. /** Public data of the dashboard's `dashboard` slot. */
  78. public class DashboardPublic : Object {
  79. public int64 draft_count { get; set; }
  80. public int64 queued_count { get; set; }
  81. public int64 ready_count { get; set; }
  82. public int64 active_count { get; set; }
  83. public int64 blocked_count { get; set; }
  84. public int64 done_count { get; set; }
  85. public int64 failed_count { get; set; }
  86. public int64 idle_saddle_count { get; set; }
  87. public Statum.Model.ActionDto? refresh { get; set; }
  88. /** The VAPID public key the browser subscribes with ("" when push is unavailable). */
  89. public string push_vapid_key { get; set; default = ""; }
  90. /** The signed-in user holds at least one push subscription. */
  91. public bool push_subscribed { get; set; }
  92. public Statum.Model.ActionDto? push_subscribe { get; set; }
  93. public Statum.Model.ActionDto? push_unsubscribe { get; set; }
  94. }
  95. /**
  96. * Builds the `dashboard` slot state shared by the home entrypoint and
  97. * the {@link RefreshDashboardAction} pulse refresh, so both hydrate
  98. * identical state: queue counters, saddle cards, the pinned OPEN
  99. * blocking items as link-through cards (newest first — plan 10
  100. * phase 3; goal items arrive with the run task's own blocks,
  101. * plan 15 C1), active tasks with their latest progress, and the
  102. * cross-task activity timeline ({@link TimelineService}: last ~50
  103. * events, newest first).
  104. */
  105. public class DashboardState : Object {
  106. /**
  107. * Loads the dashboard aggregate and shapes its PAGE slot. The
  108. * `db` session comes from the caller (entrypoint or action scope).
  109. */
  110. public static async Statum.Slot build(StateService state_service, TaskService tasks,
  111. SaddleService saddles, TimelineService timeline,
  112. InvercargillSql.Orm.OrmSession db,
  113. ActionRegistry? actions = null, PushService? push = null,
  114. int64 viewer_id = 0) throws GLib.Error {
  115. var pub = new DashboardPublic();
  116. var titles = new Dictionary<string, string>();
  117. var all_tasks = yield tasks.list_tasks(null, null);
  118. foreach (var task in all_tasks) {
  119. titles.set(task.id.to_string(), task.title);
  120. // Goal-agent runs are internal tasks — they never count
  121. // against the human-facing queue summary (the goal page
  122. // owns their visibility).
  123. if (task.kind == TASK_KIND_GOAL_RUN) {
  124. continue;
  125. }
  126. switch (task.status) {
  127. case STATUS_DRAFT:
  128. pub.draft_count++;
  129. break;
  130. case STATUS_QUEUED:
  131. pub.queued_count++;
  132. break;
  133. case STATUS_ASSIGNED:
  134. case STATUS_WORKING:
  135. pub.active_count++;
  136. break;
  137. case STATUS_BLOCKED:
  138. case STATUS_REVIEW_REQUESTED:
  139. pub.blocked_count++;
  140. break;
  141. case STATUS_DONE:
  142. pub.done_count++;
  143. break;
  144. case STATUS_FAILED:
  145. pub.failed_count++;
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. int64 ready_count = 0;
  152. foreach (var task in yield tasks.get_ready_tasks()) {
  153. if (task.kind != TASK_KIND_GOAL_RUN) {
  154. ready_count++;
  155. }
  156. }
  157. pub.ready_count = ready_count;
  158. var saddle_rows = new Vector<DashboardSaddleView>();
  159. foreach (var saddle in yield saddles.list_saddles()) {
  160. if (saddle.status == "idle") {
  161. pub.idle_saddle_count++;
  162. }
  163. var view = new DashboardSaddleView() {
  164. id = saddle.id,
  165. name = saddle.name,
  166. status = saddle.status,
  167. host = saddle.host,
  168. current_task_id = saddle.current_task_id
  169. };
  170. var work = yield saddles.current_work_view(saddle);
  171. if (work != null) {
  172. view.current_task_title = ((!)work).task_title;
  173. if (((!)work).goal_id > 0) {
  174. view.current_goal_id = ((!)work).goal_id;
  175. view.current_goal_title = ((!)work).goal_title;
  176. }
  177. }
  178. saddle_rows.add(view);
  179. }
  180. var active = new Vector<DashboardActiveView>();
  181. var pinned = new Vector<DashboardBlockView>();
  182. foreach (var task in yield tasks.list_tasks(null, null)) {
  183. // Goal-agent runs are internal — never an "active
  184. // task" row (the goal page shows the agent's
  185. // activity). Their BLOCKS still pin below (plan 15
  186. // C1): the run task's rows are the goal's pile cards.
  187. bool internal_run = task.kind == TASK_KIND_GOAL_RUN;
  188. if (!internal_run
  189. && (task.status == STATUS_ASSIGNED || task.status == STATUS_WORKING
  190. || task.status == STATUS_BLOCKED || task.status == STATUS_REVIEW_REQUESTED)) {
  191. var row = new DashboardActiveView() {
  192. id = task.id,
  193. title = task.title,
  194. status = task.status,
  195. task_branch = task.task_branch ?? ""
  196. };
  197. var progress = yield tasks.list_progress(task.id);
  198. if (progress.length > 0) {
  199. row.latest_progress = progress.first().summary;
  200. row.progress_display = format_stamp(progress.first().created_at);
  201. }
  202. active.add(row);
  203. }
  204. // Every unanswered block pins as a link-through card —
  205. // questions, RFCs and reviews alike. Plan 15 C1: the
  206. // goal agent's blocks ARE the run task's blocks (one
  207. // row each) — they render as goal items (goal badge,
  208. // goal link) straight from this same loop; there is no
  209. // goal-side pass and no mirror to consult.
  210. int64 block_goal_id = 0;
  211. string block_goal_title = "";
  212. if (internal_run && task.goal_id > 0) {
  213. block_goal_id = task.goal_id;
  214. var goal_row = yield db.query<Goal>()
  215. .where(expr("id == $0", elem<int64?>(task.goal_id)))
  216. .first_async();
  217. block_goal_title = goal_row != null ? ((!)goal_row).title : "";
  218. }
  219. foreach (var block in yield tasks.list_blocks(task.id)) {
  220. if (block.answer != null) {
  221. continue;
  222. }
  223. var view = new DashboardBlockView() {
  224. item_type = block_goal_id > 0 ? "goal" : "task",
  225. id = block.id,
  226. task_id = task.id,
  227. task_title = task.title,
  228. goal_id = block_goal_id,
  229. goal_title = block_goal_title,
  230. block_type = block.block_type,
  231. question = block.question,
  232. created_display = format_stamp(block.created_at)
  233. };
  234. if (block.created_at != null) {
  235. view.at_epoch = ((!)block.created_at).to_unix();
  236. }
  237. pinned.add(view);
  238. }
  239. }
  240. // Newest first (plan 10 phase 3): the most recent question,
  241. // RFC, review or goal block leads the unified pile.
  242. var pinned_sorted = pinned.sort((a, b) => {
  243. if (a.at_epoch != b.at_epoch) {
  244. return a.at_epoch > b.at_epoch ? -1 : 1;
  245. }
  246. return a.id > b.id ? -1 : 1;
  247. });
  248. if (actions != null) {
  249. pub.refresh = ((!)actions).author<RefreshDashboardAction>("GET");
  250. pub.push_subscribe = ((!)actions).author<PushSubscribeAction>();
  251. pub.push_unsubscribe = ((!)actions).author<PushUnsubscribeAction>();
  252. }
  253. // Web Push bell state (plan 11 task E): the VAPID key the
  254. // browser needs + whether this user already subscribed.
  255. pub.push_vapid_key = PushService.vapid_public_key;
  256. if (push != null && viewer_id > 0) {
  257. pub.push_subscribed = yield ((!)push).user_subscribed(db, viewer_id);
  258. }
  259. var activity = new Vector<DashboardActivityView>();
  260. // The 🤠 badge rides every timeline task reference: one
  261. // batch presence set for the whole activity window.
  262. var yeehaw_ids = yield tasks.yeehaw_task_ids();
  263. foreach (var item in yield timeline.recent_window()) {
  264. activity.add(new DashboardActivityView() {
  265. kind = item.kind,
  266. at_epoch = item.at_epoch,
  267. when_display = format_stamp(new DateTime.from_unix_local(item.at_epoch)),
  268. task_id = item.task_id,
  269. task_title = item.task_title,
  270. goal_id = item.goal_id,
  271. goal_title = item.goal_title,
  272. text = item.text,
  273. task_yeehaw = yeehaw_ids.has(item.task_id.to_string())
  274. });
  275. }
  276. var props = (PropertyDictionary) GObjectMapping.to_properties(pub);
  277. props["saddles"] = StateJson.list<DashboardSaddleView>(saddle_rows.to_immutable_buffer());
  278. props["activity"] = StateJson.list<DashboardActivityView>(activity.to_immutable_buffer());
  279. props["active_tasks"] = StateJson.list<DashboardActiveView>(active.to_immutable_buffer());
  280. props["pinned"] = StateJson.list<DashboardBlockView>(pinned_sorted.to_immutable_buffer());
  281. return state_service.new_slot(Scope.PAGE, new Statum.State() {
  282. type_name = "dashboard",
  283. public_data = props,
  284. private_data = new PropertyDictionary()
  285. });
  286. }
  287. }
  288. /**
  289. * Entrypoint for /: the dashboard — queue summary, the pinned OPEN
  290. * blocking items as link-through cards (newest first), active tasks
  291. * with their latest progress, saddle cards, and the cross-task
  292. * activity timeline (last ~50 events, newest first). The page
  293. * subscribes to the global pulse and re-runs its sealed refresh
  294. * action whenever task or saddle state changes anywhere in the app.
  295. */
  296. public class HomeEntrypoint : StatumEntrypoint {
  297. private TaskService task_service = Inversion.inject<TaskService>();
  298. private SaddleService saddle_service = Inversion.inject<SaddleService>();
  299. private TimelineService timeline_service = Inversion.inject<TimelineService>();
  300. private InvercargillSql.Orm.OrmSession db = Inversion.inject<InvercargillSql.Orm.OrmSession>();
  301. private PushService push_service = Inversion.inject<PushService>();
  302. private UserService users = Inversion.inject<UserService>();
  303. public override async DirectiveBuilder handle() throws GLib.Error {
  304. var guard = yield SpryAuth.require_login(directives(), request, users);
  305. if (guard != null) {
  306. return (!)guard;
  307. }
  308. var me = SpryAuth.current(request);
  309. var state = yield DashboardState.build(state_service, task_service,
  310. saddle_service, timeline_service, db, action_registry,
  311. push_service, me != null ? ((!)me).user_id : 0);
  312. return directives().set(state).subscribe_global(StatePulse.SLOT_TYPE);
  313. }
  314. }
  315. }