| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- using Invercargill;
- using Invercargill.DataStructures;
- using Invercargill.Expressions;
- using Spry;
- using Spry.Authentication;
- using Statum;
- using ClankerSaddle.Data;
- using ClankerSaddle.Services;
- namespace ClankerSaddle {
- /** A saddle card on the dashboard. */
- public class DashboardSaddleView : Object {
- public int64 id { get; set; }
- public string name { get; set; default = ""; }
- public string status { get; set; default = "offline"; }
- public string host { get; set; default = ""; }
- public int64 current_task_id { get; set; }
- public string current_task_title { get; set; default = ""; }
- /**
- * Set when the saddle's current task is a goal agent's run:
- * the card then links the GOAL (runs are internal — never a
- * task destination at a glance).
- */
- public int64 current_goal_id { get; set; }
- public string current_goal_title { get; set; default = ""; }
- }
- /** One entry in the dashboard's cross-task activity timeline. */
- public class DashboardActivityView : Object {
- public string kind { get; set; default = ""; }
- public int64 at_epoch { get; set; }
- public string when_display { get; set; default = ""; }
- public int64 task_id { get; set; }
- public string task_title { get; set; default = ""; }
- /** Plan 12: set on goal events — the row links the goal page. */
- public int64 goal_id { get; set; }
- public string goal_title { get; set; default = ""; }
- public string text { get; set; default = ""; }
- /**
- * The referenced task earned its 🤠 badge — any of its sessions
- * carries a `saddle yeehaw` entry (derived, nothing stored).
- */
- public bool task_yeehaw { get; set; }
- }
- /** An active task with its latest progress line. */
- public class DashboardActiveView : Object {
- public int64 id { get; set; }
- public string title { get; set; default = ""; }
- public string status { get; set; default = ""; }
- public string latest_progress { get; set; default = ""; }
- public string progress_display { get; set; default = ""; }
- public string task_branch { get; set; default = ""; }
- }
- /**
- * A pinned OPEN blocking item (plan 10 phase 3; plan 12 unified;
- * plan 15 C1 single-row): an unanswered question, RFC, review or
- * the goal agent's block (question/RFC, plan submission,
- * completion report — all rows of the goal-run task) rendered as
- * a link-through card. Goal items carry `item_type` "goal" and
- * link the goal page; task items carry "task" and link the task
- * page where the answer form lives. No inline answer forms on the
- * dashboard (settled).
- */
- public class DashboardBlockView : Object {
- /** "task" or "goal" — the pile interleaves both. */
- public string item_type { get; set; default = "task"; }
- public int64 id { get; set; }
- public int64 task_id { get; set; }
- public string task_title { get; set; default = ""; }
- /** Goal items only: the goal the block belongs to. */
- public int64 goal_id { get; set; }
- public string goal_title { get; set; default = ""; }
- public string block_type { get; set; default = "question"; }
- public string question { get; set; default = ""; }
- public string created_display { get; set; default = ""; }
- /** The created stamp's epoch — the unified newest-first key. */
- public int64 at_epoch { get; set; }
- }
- /** Public data of the dashboard's `dashboard` slot. */
- public class DashboardPublic : Object {
- public int64 draft_count { get; set; }
- public int64 queued_count { get; set; }
- public int64 ready_count { get; set; }
- public int64 active_count { get; set; }
- public int64 blocked_count { get; set; }
- public int64 done_count { get; set; }
- public int64 failed_count { get; set; }
- public int64 idle_saddle_count { get; set; }
- public Statum.Model.ActionDto? refresh { get; set; }
- /** The VAPID public key the browser subscribes with ("" when push is unavailable). */
- public string push_vapid_key { get; set; default = ""; }
- /** The signed-in user holds at least one push subscription. */
- public bool push_subscribed { get; set; }
- public Statum.Model.ActionDto? push_subscribe { get; set; }
- public Statum.Model.ActionDto? push_unsubscribe { get; set; }
- }
- /**
- * Builds the `dashboard` slot state shared by the home entrypoint and
- * the {@link RefreshDashboardAction} pulse refresh, so both hydrate
- * identical state: queue counters, saddle cards, the pinned OPEN
- * blocking items as link-through cards (newest first — plan 10
- * phase 3; goal items arrive with the run task's own blocks,
- * plan 15 C1), active tasks with their latest progress, and the
- * cross-task activity timeline ({@link TimelineService}: last ~50
- * events, newest first).
- */
- public class DashboardState : Object {
- /**
- * Loads the dashboard aggregate and shapes its PAGE slot. The
- * `db` session comes from the caller (entrypoint or action scope).
- */
- public static async Statum.Slot build(StateService state_service, TaskService tasks,
- SaddleService saddles, TimelineService timeline,
- InvercargillSql.Orm.OrmSession db,
- ActionRegistry? actions = null, PushService? push = null,
- int64 viewer_id = 0) throws GLib.Error {
- var pub = new DashboardPublic();
- var titles = new Dictionary<string, string>();
- var all_tasks = yield tasks.list_tasks(null, null);
- foreach (var task in all_tasks) {
- titles.set(task.id.to_string(), task.title);
- // Goal-agent runs are internal tasks — they never count
- // against the human-facing queue summary (the goal page
- // owns their visibility).
- if (task.kind == TASK_KIND_GOAL_RUN) {
- continue;
- }
- switch (task.status) {
- case STATUS_DRAFT:
- pub.draft_count++;
- break;
- case STATUS_QUEUED:
- pub.queued_count++;
- break;
- case STATUS_ASSIGNED:
- case STATUS_WORKING:
- pub.active_count++;
- break;
- case STATUS_BLOCKED:
- case STATUS_REVIEW_REQUESTED:
- pub.blocked_count++;
- break;
- case STATUS_DONE:
- pub.done_count++;
- break;
- case STATUS_FAILED:
- pub.failed_count++;
- break;
- default:
- break;
- }
- }
- int64 ready_count = 0;
- foreach (var task in yield tasks.get_ready_tasks()) {
- if (task.kind != TASK_KIND_GOAL_RUN) {
- ready_count++;
- }
- }
- pub.ready_count = ready_count;
- var saddle_rows = new Vector<DashboardSaddleView>();
- foreach (var saddle in yield saddles.list_saddles()) {
- if (saddle.status == "idle") {
- pub.idle_saddle_count++;
- }
- var view = new DashboardSaddleView() {
- id = saddle.id,
- name = saddle.name,
- status = saddle.status,
- host = saddle.host,
- current_task_id = saddle.current_task_id
- };
- var work = yield saddles.current_work_view(saddle);
- if (work != null) {
- view.current_task_title = ((!)work).task_title;
- if (((!)work).goal_id > 0) {
- view.current_goal_id = ((!)work).goal_id;
- view.current_goal_title = ((!)work).goal_title;
- }
- }
- saddle_rows.add(view);
- }
- var active = new Vector<DashboardActiveView>();
- var pinned = new Vector<DashboardBlockView>();
- foreach (var task in yield tasks.list_tasks(null, null)) {
- // Goal-agent runs are internal — never an "active
- // task" row (the goal page shows the agent's
- // activity). Their BLOCKS still pin below (plan 15
- // C1): the run task's rows are the goal's pile cards.
- bool internal_run = task.kind == TASK_KIND_GOAL_RUN;
- if (!internal_run
- && (task.status == STATUS_ASSIGNED || task.status == STATUS_WORKING
- || task.status == STATUS_BLOCKED || task.status == STATUS_REVIEW_REQUESTED)) {
- var row = new DashboardActiveView() {
- id = task.id,
- title = task.title,
- status = task.status,
- task_branch = task.task_branch ?? ""
- };
- var progress = yield tasks.list_progress(task.id);
- if (progress.length > 0) {
- row.latest_progress = progress.first().summary;
- row.progress_display = format_stamp(progress.first().created_at);
- }
- active.add(row);
- }
- // Every unanswered block pins as a link-through card —
- // questions, RFCs and reviews alike. Plan 15 C1: the
- // goal agent's blocks ARE the run task's blocks (one
- // row each) — they render as goal items (goal badge,
- // goal link) straight from this same loop; there is no
- // goal-side pass and no mirror to consult.
- int64 block_goal_id = 0;
- string block_goal_title = "";
- if (internal_run && task.goal_id > 0) {
- block_goal_id = task.goal_id;
- var goal_row = yield db.query<Goal>()
- .where(expr("id == $0", elem<int64?>(task.goal_id)))
- .first_async();
- block_goal_title = goal_row != null ? ((!)goal_row).title : "";
- }
- foreach (var block in yield tasks.list_blocks(task.id)) {
- if (block.answer != null) {
- continue;
- }
- var view = new DashboardBlockView() {
- item_type = block_goal_id > 0 ? "goal" : "task",
- id = block.id,
- task_id = task.id,
- task_title = task.title,
- goal_id = block_goal_id,
- goal_title = block_goal_title,
- block_type = block.block_type,
- question = block.question,
- created_display = format_stamp(block.created_at)
- };
- if (block.created_at != null) {
- view.at_epoch = ((!)block.created_at).to_unix();
- }
- pinned.add(view);
- }
- }
- // Newest first (plan 10 phase 3): the most recent question,
- // RFC, review or goal block leads the unified pile.
- var pinned_sorted = pinned.sort((a, b) => {
- if (a.at_epoch != b.at_epoch) {
- return a.at_epoch > b.at_epoch ? -1 : 1;
- }
- return a.id > b.id ? -1 : 1;
- });
- if (actions != null) {
- pub.refresh = ((!)actions).author<RefreshDashboardAction>("GET");
- pub.push_subscribe = ((!)actions).author<PushSubscribeAction>();
- pub.push_unsubscribe = ((!)actions).author<PushUnsubscribeAction>();
- }
- // Web Push bell state (plan 11 task E): the VAPID key the
- // browser needs + whether this user already subscribed.
- pub.push_vapid_key = PushService.vapid_public_key;
- if (push != null && viewer_id > 0) {
- pub.push_subscribed = yield ((!)push).user_subscribed(db, viewer_id);
- }
- var activity = new Vector<DashboardActivityView>();
- // The 🤠 badge rides every timeline task reference: one
- // batch presence set for the whole activity window.
- var yeehaw_ids = yield tasks.yeehaw_task_ids();
- foreach (var item in yield timeline.recent_window()) {
- activity.add(new DashboardActivityView() {
- kind = item.kind,
- at_epoch = item.at_epoch,
- when_display = format_stamp(new DateTime.from_unix_local(item.at_epoch)),
- task_id = item.task_id,
- task_title = item.task_title,
- goal_id = item.goal_id,
- goal_title = item.goal_title,
- text = item.text,
- task_yeehaw = yeehaw_ids.has(item.task_id.to_string())
- });
- }
- var props = (PropertyDictionary) GObjectMapping.to_properties(pub);
- props["saddles"] = StateJson.list<DashboardSaddleView>(saddle_rows.to_immutable_buffer());
- props["activity"] = StateJson.list<DashboardActivityView>(activity.to_immutable_buffer());
- props["active_tasks"] = StateJson.list<DashboardActiveView>(active.to_immutable_buffer());
- props["pinned"] = StateJson.list<DashboardBlockView>(pinned_sorted.to_immutable_buffer());
- return state_service.new_slot(Scope.PAGE, new Statum.State() {
- type_name = "dashboard",
- public_data = props,
- private_data = new PropertyDictionary()
- });
- }
- }
- /**
- * Entrypoint for /: the dashboard — queue summary, the pinned OPEN
- * blocking items as link-through cards (newest first), active tasks
- * with their latest progress, saddle cards, and the cross-task
- * activity timeline (last ~50 events, newest first). The page
- * subscribes to the global pulse and re-runs its sealed refresh
- * action whenever task or saddle state changes anywhere in the app.
- */
- public class HomeEntrypoint : StatumEntrypoint {
- private TaskService task_service = Inversion.inject<TaskService>();
- private SaddleService saddle_service = Inversion.inject<SaddleService>();
- private TimelineService timeline_service = Inversion.inject<TimelineService>();
- private InvercargillSql.Orm.OrmSession db = Inversion.inject<InvercargillSql.Orm.OrmSession>();
- private PushService push_service = Inversion.inject<PushService>();
- private UserService users = Inversion.inject<UserService>();
- public override async DirectiveBuilder handle() throws GLib.Error {
- var guard = yield SpryAuth.require_login(directives(), request, users);
- if (guard != null) {
- return (!)guard;
- }
- var me = SpryAuth.current(request);
- var state = yield DashboardState.build(state_service, task_service,
- saddle_service, timeline_service, db, action_registry,
- push_service, me != null ? ((!)me).user_id : 0);
- return directives().set(state).subscribe_global(StatePulse.SLOT_TYPE);
- }
- }
- }
|