feat: full plan execution - CI/CD, critical fixes, UX polish, secondary/advanced features, E2E + docs
Phase 0 (CI/CD): fix root typecheck to cover api+worker+web; reconcile migration story into idempotent db:migrate (db:sync + db:triggers); add Gitea Actions quality/deploy/smoke workflow; rewrite README/AGENTS/DEPLOY docs; add requireWorkspaceAccess + recordActivityForEntity conventions. Phase 1 (critical fixes): calendar delete + drag/resize DnD; canvas card CRUD + bulk save + debounced autosave; logout route; graph edge workspaceId derivation; real analytics endpoints (drop Math.random); task board droppable columns + reorder persistence; Tiptap notes editor with sanitized HTML rendering; remove insecure passkey auth; domain/owner scoping (IDOR) on all by-ID routes + search/ export/realtime scoping; command palette routing + agent mention fetch; agent activity SSE handler; graph fly-to with tracked positions. Phase 2 (UX polish): login on design system; Sonner toasts app-wide; shared Loading/Empty/Error state components; working density/sidebarPos/reduce-motion settings; Inter typography; consolidated status-colors lib; unified detail routes; dashboard sort/realtime/responsive fixes; mobile responsive; a11y (radiogroups, sanitized snippets, badge labels). Phase 3 (features): daily notes timezone fix + delete + autosave + mood/energy create; active-domain store + topbar picker; graph domain picker + navigable entity links; tag assign/remove UI + server-side tag filter; real CSV export + import validation; custom fields on tasks. Phase 4 (advanced): migrate job worker into apps/worker (webhook delivery with HMAC, recurring spawn, ai_dispatch disabled); webhook queue helper + entity event enqueuing + test endpoint fix; recurring scheduledJobs pipeline; agents CRUD + permission editing + activity filters; real notifications feed; MCP polish (validation, error codes, domain scoping, dead sql leftover). Phase 5 (E2E + docs): rewrite Playwright suite for the Vite SPA (15 specs, new auth helpers, chromium-only in CI); add ephemeral-Postgres e2e CI job; rewrite docs/API.md for the real Hono API.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Hono } from "hono";
|
||||
import { db, tasks, habits, habitCompletions } from "@project-e/db";
|
||||
import { and, eq, gte, isNull } from "drizzle-orm";
|
||||
import { db, tasks, habits, habitCompletions, projects } from "@project-e/db";
|
||||
import { and, eq, gte, inArray, isNull, or } from "drizzle-orm";
|
||||
import { requireAuth, resolveActiveDomain, AuthError } from "../middleware/auth";
|
||||
|
||||
export const analyticsRoutes = new Hono();
|
||||
@@ -65,9 +65,17 @@ analyticsRoutes.get("/habits", async (c) => {
|
||||
.from(habits)
|
||||
.where(and(eq(habits.domainId, domainId), isNull(habits.deletedAt)));
|
||||
|
||||
const allLogs = await db.select()
|
||||
.from(habitCompletions)
|
||||
.where(gte(habitCompletions.date, startDate));
|
||||
const habitIds = allHabits.map((h) => h.id);
|
||||
|
||||
// Only count completions belonging to habits in this domain (not all completions globally)
|
||||
const allLogs = habitIds.length > 0
|
||||
? await db.select()
|
||||
.from(habitCompletions)
|
||||
.where(and(
|
||||
inArray(habitCompletions.habitId, habitIds),
|
||||
gte(habitCompletions.date, startDate),
|
||||
))
|
||||
: [];
|
||||
|
||||
const habitConsistency = allHabits.length > 0
|
||||
? Math.round((allLogs.length / (allHabits.length * range)) * 100)
|
||||
@@ -93,7 +101,7 @@ analyticsRoutes.get("/habits", async (c) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/analytics/projects?range=... — Project progress
|
||||
// GET /api/analytics/projects?range=... — Per-project progress
|
||||
analyticsRoutes.get("/projects", async (c) => {
|
||||
try {
|
||||
const user = await requireAuth(c);
|
||||
@@ -105,24 +113,48 @@ analyticsRoutes.get("/projects", async (c) => {
|
||||
domainId = active.id;
|
||||
}
|
||||
|
||||
const startDate = new Date();
|
||||
startDate.setDate(startDate.getDate() - range);
|
||||
const allProjects = await db.select()
|
||||
.from(projects)
|
||||
.where(and(eq(projects.domainId, domainId), isNull(projects.deletedAt)));
|
||||
|
||||
const allTasks = await db.select()
|
||||
.from(tasks)
|
||||
.where(and(
|
||||
eq(tasks.domainId, domainId),
|
||||
gte(tasks.createdAt, startDate),
|
||||
isNull(tasks.deletedAt),
|
||||
));
|
||||
const projectIds = allProjects.map((p) => p.id);
|
||||
|
||||
const completedTasks = allTasks.filter(t => t.status === "done");
|
||||
const taskCompletionRate = allTasks.length > 0 ? Math.round((completedTasks.length / allTasks.length) * 100) : 0;
|
||||
// Count tasks per project (any status, including non-done) for the domain
|
||||
const taskRows = projectIds.length > 0
|
||||
? await db.select({ projectId: tasks.projectId, status: tasks.status })
|
||||
.from(tasks)
|
||||
.where(and(
|
||||
isNull(tasks.deletedAt),
|
||||
inArray(tasks.projectId, projectIds),
|
||||
))
|
||||
: [];
|
||||
|
||||
const counts = new Map<string, { totalTasks: number; completedTasks: number }>();
|
||||
for (const t of taskRows) {
|
||||
if (!t.projectId) continue;
|
||||
const entry = counts.get(t.projectId) ?? { totalTasks: 0, completedTasks: 0 };
|
||||
entry.totalTasks += 1;
|
||||
if (t.status === "done") entry.completedTasks += 1;
|
||||
counts.set(t.projectId, entry);
|
||||
}
|
||||
|
||||
const projectsData = allProjects.map((p) => {
|
||||
const stats = counts.get(p.id) ?? { totalTasks: 0, completedTasks: 0 };
|
||||
const progress = stats.totalTasks > 0
|
||||
? Math.round((stats.completedTasks / stats.totalTasks) * 100) / 100
|
||||
: 0;
|
||||
return {
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
totalTasks: stats.totalTasks,
|
||||
completedTasks: stats.completedTasks,
|
||||
progress,
|
||||
};
|
||||
});
|
||||
|
||||
return c.json({
|
||||
taskCompletionRate,
|
||||
totalTasks: allTasks.length,
|
||||
completedTasks: completedTasks.length,
|
||||
projects: projectsData,
|
||||
totalProjects: allProjects.length,
|
||||
period: range,
|
||||
}, {
|
||||
headers: { "Cache-Control": "private, max-age=300, stale-while-revalidate=600" },
|
||||
@@ -133,3 +165,76 @@ analyticsRoutes.get("/projects", async (c) => {
|
||||
return c.json({ error: { code: "INTERNAL_ERROR", message: "Failed to get project analytics" } }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/analytics/daily?range=... — Daily task creation & completion time series
|
||||
analyticsRoutes.get("/daily", async (c) => {
|
||||
try {
|
||||
const user = await requireAuth(c);
|
||||
const url = new URL(c.req.url);
|
||||
const range = parseInt(url.searchParams.get("range") || "30");
|
||||
let domainId = url.searchParams.get("domain") || undefined;
|
||||
if (!domainId) {
|
||||
const active = await resolveActiveDomain(user);
|
||||
domainId = active.id;
|
||||
}
|
||||
|
||||
// Buckets cover the last `range` days ending today, matching the frontend's expectation.
|
||||
const firstDay = new Date();
|
||||
firstDay.setDate(firstDay.getDate() - (range - 1));
|
||||
firstDay.setHours(0, 0, 0, 0);
|
||||
|
||||
const domainTasks = await db.select()
|
||||
.from(tasks)
|
||||
.where(and(
|
||||
eq(tasks.domainId, domainId),
|
||||
isNull(tasks.deletedAt),
|
||||
or(
|
||||
gte(tasks.createdAt, firstDay),
|
||||
gte(tasks.completedAt, firstDay),
|
||||
),
|
||||
));
|
||||
|
||||
// Bucket by local calendar date (yyyy-MM-dd) so keys line up with the frontend's
|
||||
// date-fns day generation (which uses local time as well).
|
||||
const localDateKey = (d: Date) => {
|
||||
const y = d.getFullYear();
|
||||
const m = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
return `${y}-${m}-${day}`;
|
||||
};
|
||||
|
||||
const createdByDay = new Map<string, number>();
|
||||
const completedByDay = new Map<string, number>();
|
||||
for (const t of domainTasks) {
|
||||
const createdKey = localDateKey(t.createdAt);
|
||||
createdByDay.set(createdKey, (createdByDay.get(createdKey) || 0) + 1);
|
||||
if (t.status === "done" && t.completedAt) {
|
||||
const completedKey = localDateKey(t.completedAt);
|
||||
completedByDay.set(completedKey, (completedByDay.get(completedKey) || 0) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
const items: Array<{ date: string; created: number; completed: number }> = [];
|
||||
const cursor = new Date(firstDay);
|
||||
for (let i = 0; i < range; i++) {
|
||||
const key = localDateKey(cursor);
|
||||
items.push({
|
||||
date: key,
|
||||
created: createdByDay.get(key) || 0,
|
||||
completed: completedByDay.get(key) || 0,
|
||||
});
|
||||
cursor.setDate(cursor.getDate() + 1);
|
||||
}
|
||||
|
||||
return c.json({
|
||||
items,
|
||||
period: range,
|
||||
}, {
|
||||
headers: { "Cache-Control": "private, max-age=300, stale-while-revalidate=600" },
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) return c.json({ error: { code: error.code, message: error.message } }, error.status as any);
|
||||
console.error("[analytics] GET /daily error:", error);
|
||||
return c.json({ error: { code: "INTERNAL_ERROR", message: "Failed to get daily analytics" } }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user