Add a day timeline, trend chart and goal history to the dashboard
The dashboard gains a per-kind timeline of the selected day, a 14-day trend chart per kind, and a goal history grid with streaks, sharing a tooltip and a fixed color per top series. The activities limit rises to 5000 so busy days fit on the timeline. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+59
-10
@@ -1,8 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { api, dur, kindName, type Activity, type Summary } from "../api";
|
||||
import { computed, reactive, ref, watch } from "vue";
|
||||
import { api, dur, kindName, seriesKey, seriesSlots, slotColor, type Activity, type Summary } from "../api";
|
||||
import DayTimeline from "../components/DayTimeline.vue";
|
||||
import GoalHistory from "../components/GoalHistory.vue";
|
||||
import TrendChart from "../components/TrendChart.vue";
|
||||
|
||||
const LIMIT = 500;
|
||||
const LIMIT = 5000;
|
||||
const TREND_DAYS = 14;
|
||||
const today = () => new Date().toLocaleDateString("en-CA"); // YYYY-MM-DD in local time
|
||||
const noon = (d: string) => new Date(d + "T12:00:00");
|
||||
|
||||
@@ -11,6 +15,9 @@ const sum = ref<Summary | null>(null);
|
||||
const activities = ref<Activity[]>([]);
|
||||
const loading = ref(false);
|
||||
const error = ref("");
|
||||
// Summaries for the trend charts, kept across day changes. The selected day's
|
||||
// is always refetched since today's is still growing.
|
||||
const history = reactive<Record<string, Summary>>({});
|
||||
|
||||
const daysAgo = computed(() => Math.round((+noon(today()) - +noon(date.value)) / 864e5));
|
||||
const title = computed(() => daysAgo.value === 0 ? "Today" : daysAgo.value === 1 ? "Yesterday"
|
||||
@@ -29,6 +36,7 @@ async function load() {
|
||||
if (n !== seq) return;
|
||||
sum.value = s;
|
||||
activities.value = acts;
|
||||
history[s.date] = s;
|
||||
} catch (e) {
|
||||
if (n === seq) error.value = (e as Error).message;
|
||||
} finally {
|
||||
@@ -37,11 +45,31 @@ async function load() {
|
||||
}
|
||||
watch(date, load, { immediate: true });
|
||||
|
||||
function shift(days: number) {
|
||||
const d = noon(date.value);
|
||||
d.setDate(d.getDate() + days);
|
||||
date.value = d.toLocaleDateString("en-CA");
|
||||
}
|
||||
const addDays = (d: string, n: number) => {
|
||||
const x = noon(d);
|
||||
x.setDate(x.getDate() + n);
|
||||
return x.toLocaleDateString("en-CA");
|
||||
};
|
||||
const trendDays = computed(() => {
|
||||
// Runs up to a week past the selected day, but never past today.
|
||||
let end = date.value;
|
||||
for (let i = 0; i < TREND_DAYS / 2 && end < today(); i++) end = addDays(end, 1);
|
||||
return Array.from({ length: TREND_DAYS }, (_, i) => addDays(end, i - TREND_DAYS + 1))
|
||||
.map(d => ({ date: d, sum: history[d] ?? null }));
|
||||
});
|
||||
const pending = new Set<string>();
|
||||
watch(() => trendDays.value.map(d => d.date), days => {
|
||||
for (const d of days) {
|
||||
if (d in history || d === date.value || pending.has(d)) continue;
|
||||
pending.add(d);
|
||||
api<Summary>("GET", "summary/daily?date=" + d)
|
||||
.then(s => { history[d] = s; }, () => {})
|
||||
.finally(() => pending.delete(d));
|
||||
}
|
||||
}, { immediate: true });
|
||||
|
||||
const shift = (days: number) => { date.value = addDays(date.value, days); };
|
||||
const slots = computed(() => seriesSlots(sum.value?.totals ?? []));
|
||||
|
||||
const top = computed(() => sum.value?.totals
|
||||
.filter(t => t.kind === "app_focus" || t.kind === "tab_focus")
|
||||
@@ -57,6 +85,11 @@ const timeline = computed(() => activities.value.filter(a => a.duration_s >= 60)
|
||||
|
||||
const time = (t: string) => new Date(t).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
||||
const pct = (v: number, max: number) => `${v / (max || 1) * 100}%`;
|
||||
// The goal meter's scale fits both the target and the actual time.
|
||||
const meter = (g: Summary["goals"][number]) => {
|
||||
const max = Math.max(g.target_s, g.actual_s, 1);
|
||||
return { fill: pct(g.actual_s, max), target: pct(g.target_s, max) };
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -87,13 +120,29 @@ const pct = (v: number, max: number) => `${v / (max || 1) * 100}%`;
|
||||
<section class="card"><h2>Goals</h2><div class="tablewrap"><table>
|
||||
<tr v-for="(g, i) in sum.goals" :key="i">
|
||||
<td class="label">{{ kindName(g.kind) }}{{ g.label ? " · " + g.label : "" }}
|
||||
<span class="muted">{{ g.op }} {{ dur(g.target_s) }}</span></td>
|
||||
<span class="muted">{{ g.op }} {{ dur(g.target_s) }}</span>
|
||||
<div class="meter" :class="g.met ? 'ok' : 'bad'">
|
||||
<div class="fill" :style="{ width: meter(g).fill }"></div>
|
||||
<div class="target" :style="{ left: meter(g).target }" title="Target"></div>
|
||||
</div></td>
|
||||
<td class="num">{{ dur(g.actual_s) }}</td>
|
||||
<td class="num" :class="g.met ? 'ok' : 'bad'">{{ g.met ? "✓ met" : "✗ missed" }}</td>
|
||||
</tr>
|
||||
<tr v-if="!sum.goals.length"><td class="muted">No goals yet. See <RouterLink to="/setup#goals">Setup</RouterLink>.</td></tr>
|
||||
</table></div></section>
|
||||
|
||||
<section class="card"><h2>Day at a glance</h2>
|
||||
<DayTimeline :activities="activities" :from="sum.from" :to="sum.to" :slots="slots" />
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<TrendChart :days="trendDays" :selected="date" @select="date = $event" />
|
||||
</section>
|
||||
|
||||
<section v-if="sum.goals.length" class="card"><h2>Goal history</h2>
|
||||
<GoalHistory :days="trendDays" :selected="date" @select="date = $event" />
|
||||
</section>
|
||||
|
||||
<section class="card"><h2>By kind</h2><div class="tablewrap"><table>
|
||||
<tr v-for="[k, s] in kinds" :key="k">
|
||||
<td class="label">{{ kindName(k) }}<div class="bar" :style="{ width: pct(s, kinds[0][1]) }"></div></td>
|
||||
@@ -105,7 +154,7 @@ const pct = (v: number, max: number) => `${v / (max || 1) * 100}%`;
|
||||
<section class="card"><h2>Top labels</h2><div class="tablewrap"><table>
|
||||
<tr v-for="t in labels" :key="t.kind + '\0' + t.label">
|
||||
<td class="label">{{ t.label || "—" }} <span class="badge">{{ kindName(t.kind) }}</span>
|
||||
<div class="bar" :style="{ width: pct(t.seconds, labels[0].seconds) }"></div></td>
|
||||
<div class="bar" :style="{ width: pct(t.seconds, labels[0].seconds), background: slotColor(slots.get(seriesKey(t.kind, t.label))) }"></div></td>
|
||||
<td class="num">{{ dur(t.seconds) }}</td>
|
||||
</tr>
|
||||
<tr v-if="!labels.length"><td class="muted">Nothing recorded.</td></tr>
|
||||
|
||||
Reference in New Issue
Block a user