Lifecycle planning, and consistent empty and loading states
- Inventory: end of support and replacement cost per asset; a Lifecycle page showing what ages out year by year and what replacing it will cost; support ending within 90 days in the weekly digest - Lists: a shared empty state saying what is missing, why, and the one thing to do; "nothing matches" with a clear button; skeleton rows instead of empty-looking lists while loading Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92726568b7
commit
846af02851
50 files changed
+4434
-233
No files matched your search
@@ -820,6 +820,8 @@ export const assets = sqliteTable("assets", {
|
||||
purchaseDate: text("purchase_date"), // YYYY-MM-DD
|
||||
purchasePrice: real("purchase_price"), // in the currency set under Settings → Inventory
|
||||
warrantyUntil: text("warranty_until"), // YYYY-MM-DD
|
||||
endOfSupport: text("end_of_support"), // YYYY-MM-DD — when the maker stops patching it, or you plan to retire it; what the lifecycle report goes by
|
||||
replacementCost: real("replacement_cost"), // what replacing it is expected to cost, in the same currency; the lifecycle report falls back to the purchase price
|
||||
location: text("location"), // room, rack, shelf
|
||||
rackUnit: text("rack_unit"), // e.g. "U12" or "U12-13"
|
||||
powerWatts: integer("power_watts"), // typical draw, for the running cost
|
||||
|
||||
@@ -5,7 +5,7 @@ import { db } from "../db/client.js";
|
||||
import { assetKinds, assets, servers } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { isRealDate } from "../services/inventory.js";
|
||||
import { isRealDate, lifecycleReport } from "../services/inventory.js";
|
||||
import { blanksToFill, differences, hardwareOf } from "../services/inventoryAutofill.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -26,6 +26,8 @@ const assetSchema = z.object({
|
||||
purchaseDate: date,
|
||||
purchasePrice: z.number().min(0).max(1_000_000_000).nullable().optional(),
|
||||
warrantyUntil: date,
|
||||
endOfSupport: date,
|
||||
replacementCost: z.number().min(0).max(1_000_000_000).nullable().optional(),
|
||||
location: text(120),
|
||||
rackUnit: text(30),
|
||||
powerWatts: z.number().int().min(0).max(100_000).nullable().optional(),
|
||||
@@ -46,6 +48,8 @@ function values(d: z.infer<typeof assetSchema>) {
|
||||
purchaseDate: clean(d.purchaseDate),
|
||||
purchasePrice: d.purchasePrice ?? null,
|
||||
warrantyUntil: clean(d.warrantyUntil),
|
||||
endOfSupport: clean(d.endOfSupport),
|
||||
replacementCost: d.replacementCost ?? null,
|
||||
location: clean(d.location),
|
||||
rackUnit: clean(d.rackUnit),
|
||||
powerWatts: d.powerWatts ?? null,
|
||||
@@ -83,6 +87,14 @@ inventoryRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
});
|
||||
}));
|
||||
|
||||
// What ages out when, for planning what to replace: this year, the next few, later, and what has no date.
|
||||
inventoryRouter.get("/lifecycle", asyncHandler(async (req, res) => {
|
||||
const asked = Number(req.query.years);
|
||||
const years = Number.isInteger(asked) && asked >= 1 && asked <= 10 ? asked : 5;
|
||||
const [rows, settings] = await Promise.all([db.select().from(assets), getSettings()]);
|
||||
res.json({ ...lifecycleReport(rows, new Date(), years), currency: settings.inventory.currency });
|
||||
}));
|
||||
|
||||
const configSchema = z.object({
|
||||
currency: z.string().trim().max(8),
|
||||
pricePerKwh: z.number().min(0).max(1000).nullable(),
|
||||
|
||||
@@ -30,6 +30,84 @@ export function warrantyStatus(warrantyUntil: string | null, now: Date = new Dat
|
||||
return days <= warnDays ? "expiring" : "ok";
|
||||
}
|
||||
|
||||
// ─── Lifecycle planning ─────────────────────────────────────────────────────
|
||||
|
||||
export interface LifecycleItem {
|
||||
id: number;
|
||||
name: string;
|
||||
kind: string;
|
||||
/** YYYY-MM-DD — when it ages out. */
|
||||
endsOn: string;
|
||||
/** What that date is: the end of support the maker gives (or you set), or, with none set, the end of the warranty. */
|
||||
basis: "support" | "warranty";
|
||||
daysLeft: number;
|
||||
/** What replacing it is expected to cost, and where that figure comes from; null when there is none. */
|
||||
cost: number | null;
|
||||
costBasis: "replacement" | "purchase" | null;
|
||||
}
|
||||
|
||||
export interface LifecycleYear {
|
||||
year: number;
|
||||
items: LifecycleItem[];
|
||||
/** The sum of the figures that exist. */
|
||||
totalCost: number;
|
||||
/** How many have no figure, so the total is understood to be at least that. */
|
||||
unpriced: number;
|
||||
}
|
||||
|
||||
export interface LifecycleReport {
|
||||
/** Already past their date. */
|
||||
past: LifecycleItem[];
|
||||
/** The years from this one on. */
|
||||
years: LifecycleYear[];
|
||||
/** Further off than the years shown. */
|
||||
later: LifecycleItem[];
|
||||
/** With no date at all: no end of support and no warranty. */
|
||||
undated: { id: number; name: string; kind: string }[];
|
||||
}
|
||||
|
||||
type LifecycleSource = { id: number; name: string; kind: string; endOfSupport: string | null; warrantyUntil: string | null; replacementCost: number | null; purchasePrice: number | null };
|
||||
|
||||
/**
|
||||
* What ages out when, for planning what to replace. The date is the end of support where there is one, otherwise the end of the warranty (said
|
||||
* so, since that is only a stand-in); the cost is the replacement cost where there is one, otherwise what it was bought for. Things already past
|
||||
* their date, those in each of the next `yearsAhead` calendar years, those later, and those with no date at all are kept apart.
|
||||
*/
|
||||
export function lifecycleReport(assets: LifecycleSource[], now: Date = new Date(), yearsAhead = 5): LifecycleReport {
|
||||
const thisYear = now.getUTCFullYear();
|
||||
const years: LifecycleYear[] = Array.from({ length: yearsAhead }, (_, i) => ({ year: thisYear + i, items: [], totalCost: 0, unpriced: 0 }));
|
||||
const past: LifecycleItem[] = [];
|
||||
const later: LifecycleItem[] = [];
|
||||
const undated: LifecycleReport["undated"] = [];
|
||||
for (const a of assets) {
|
||||
const support = a.endOfSupport && isRealDate(a.endOfSupport) ? a.endOfSupport : null;
|
||||
const warranty = a.warrantyUntil && isRealDate(a.warrantyUntil) ? a.warrantyUntil : null;
|
||||
const endsOn = support ?? warranty;
|
||||
if (!endsOn) {
|
||||
undated.push({ id: a.id, name: a.name, kind: a.kind });
|
||||
continue;
|
||||
}
|
||||
const cost = a.replacementCost ?? a.purchasePrice ?? null;
|
||||
const item: LifecycleItem = { id: a.id, name: a.name, kind: a.kind, endsOn, basis: support ? "support" : "warranty", daysLeft: warrantyDaysLeft(endsOn, now) ?? 0, cost, costBasis: a.replacementCost !== null ? "replacement" : a.purchasePrice !== null ? "purchase" : null };
|
||||
if (item.daysLeft < 0) past.push(item);
|
||||
else {
|
||||
const bucket = years.find((y) => y.year === Number(endsOn.slice(0, 4)));
|
||||
if (bucket) bucket.items.push(item);
|
||||
else later.push(item);
|
||||
}
|
||||
}
|
||||
const byDate = (x: LifecycleItem, y: LifecycleItem) => x.endsOn.localeCompare(y.endsOn) || x.name.localeCompare(y.name);
|
||||
past.sort(byDate);
|
||||
later.sort(byDate);
|
||||
for (const y of years) {
|
||||
y.items.sort(byDate);
|
||||
y.totalCost = Math.round(y.items.reduce((n, i) => n + (i.cost ?? 0), 0));
|
||||
y.unpriced = y.items.filter((i) => i.cost === null).length;
|
||||
}
|
||||
undated.sort((a, b) => a.name.localeCompare(b.name));
|
||||
return { past, years, later, undated };
|
||||
}
|
||||
|
||||
/** Kilowatt-hours a steady draw uses in a year. */
|
||||
export function yearlyKwh(watts: number): number {
|
||||
return Math.round((watts / 1000) * 24 * 365 * 100) / 100;
|
||||
|
||||
@@ -185,11 +185,16 @@ export async function buildDigest(now: Date = new Date()): Promise<Digest> {
|
||||
expiring.push({ daysLeft: d.daysLeft, text: d.daysLeft < 0 ? `Domain ${d.name} expired ${plural(Math.abs(d.daysLeft), "day")} ago` : `Domain ${d.name} expires in ${plural(d.daysLeft, "day")} (${d.expiresAt})` });
|
||||
}
|
||||
// A warranty about to end is worth knowing while there is still time to claim on it; one that ended long ago is not news.
|
||||
for (const a of await db.select({ name: assets.name, warrantyUntil: assets.warrantyUntil }).from(assets)) {
|
||||
for (const a of await db.select({ name: assets.name, warrantyUntil: assets.warrantyUntil, endOfSupport: assets.endOfSupport }).from(assets)) {
|
||||
const days = warrantyDaysLeft(a.warrantyUntil, now);
|
||||
if (days !== null && days >= -30 && days <= 60) {
|
||||
expiring.push({ daysLeft: days, text: days < 0 ? `Warranty on ${a.name} ended ${plural(Math.abs(days), "day")} ago` : `Warranty on ${a.name} ends in ${plural(days, "day")} (${a.warrantyUntil})` });
|
||||
}
|
||||
// The end of support gives more notice: it is when something has to be replaced, not claimed on.
|
||||
const supportDays = warrantyDaysLeft(a.endOfSupport, now);
|
||||
if (supportDays !== null && supportDays >= -30 && supportDays <= 90) {
|
||||
expiring.push({ daysLeft: supportDays, text: supportDays < 0 ? `Support for ${a.name} ended ${plural(Math.abs(supportDays), "day")} ago` : `Support for ${a.name} ends in ${plural(supportDays, "day")} (${a.endOfSupport})` });
|
||||
}
|
||||
}
|
||||
if (expiring.length > 0) {
|
||||
expiring.sort((a, b) => a.daysLeft - b.daysLeft);
|
||||
|
||||
Reference in new issue
Block a user