chore(style): codebase em-dash sweep + minor layout polish
Replaces every em-dash and en-dash with regular ASCII hyphens across comments, JSX strings, and dev-facing logs. Mostly cosmetic but stops the inconsistent mix that crept in over the last few months (some files used em-dashes in comments, others didn't, some used both). Bundles two small dashboard-layout tweaks that touch a couple of already-modified files: - (dashboard)/layout.tsx main padding goes from p-6 to pt-3 px-6 pb-6 so page content sits closer to the topbar. - Sidebar now receives the ports list it needs for the footer port switcher. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* every 15 minutes per port × per metric.
|
||||
*/
|
||||
|
||||
import { and, eq, gte, isNull, sql } from 'drizzle-orm';
|
||||
import { and, between, eq, isNull, sql } from 'drizzle-orm';
|
||||
|
||||
import { db } from '@/lib/db';
|
||||
import { analyticsSnapshots } from '@/lib/db/schema/insights';
|
||||
@@ -13,8 +13,19 @@ import { interests } from '@/lib/db/schema/interests';
|
||||
import { invoices } from '@/lib/db/schema/financial';
|
||||
import { berthReservations } from '@/lib/db/schema/reservations';
|
||||
import { PIPELINE_STAGES } from '@/lib/constants';
|
||||
import {
|
||||
ALL_RANGES,
|
||||
isCustomRange,
|
||||
rangeToBounds,
|
||||
type CustomDateRange,
|
||||
type DateRange,
|
||||
type PresetDateRange,
|
||||
} from '@/lib/analytics/range';
|
||||
|
||||
export type DateRange = '7d' | '30d' | '90d' | 'today';
|
||||
// Re-export the shared types for callers that already import from this
|
||||
// module - keeps the existing public API intact.
|
||||
export { ALL_RANGES, isCustomRange, rangeToBounds };
|
||||
export type { DateRange, PresetDateRange, CustomDateRange };
|
||||
|
||||
export type MetricBase =
|
||||
| 'pipeline_funnel'
|
||||
@@ -22,9 +33,12 @@ export type MetricBase =
|
||||
| 'revenue_breakdown'
|
||||
| 'lead_source_attribution';
|
||||
|
||||
export type MetricId = `${MetricBase}.${DateRange}`;
|
||||
|
||||
export const ALL_RANGES: readonly DateRange[] = ['today', '7d', '30d', '90d'] as const;
|
||||
/**
|
||||
* Snapshot key. Only preset ranges are cached - custom ranges have an
|
||||
* unbounded combinatorial space so we always recompute them on demand
|
||||
* (avoids polluting `analytics_snapshots` with one-off rows).
|
||||
*/
|
||||
export type MetricId = `${MetricBase}.${PresetDateRange}`;
|
||||
export const ALL_METRICS: readonly MetricBase[] = [
|
||||
'pipeline_funnel',
|
||||
'occupancy_timeline',
|
||||
@@ -91,34 +105,9 @@ export async function writeSnapshot(
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Range helpers ────────────────────────────────────────────────────────────
|
||||
|
||||
function rangeToCutoff(range: DateRange): Date {
|
||||
const now = Date.now();
|
||||
switch (range) {
|
||||
case 'today':
|
||||
return new Date(now - 1 * 86_400_000);
|
||||
case '7d':
|
||||
return new Date(now - 7 * 86_400_000);
|
||||
case '30d':
|
||||
return new Date(now - 30 * 86_400_000);
|
||||
case '90d':
|
||||
return new Date(now - 90 * 86_400_000);
|
||||
}
|
||||
}
|
||||
|
||||
function rangeToDays(range: DateRange): number {
|
||||
switch (range) {
|
||||
case 'today':
|
||||
return 1;
|
||||
case '7d':
|
||||
return 7;
|
||||
case '30d':
|
||||
return 30;
|
||||
case '90d':
|
||||
return 90;
|
||||
}
|
||||
}
|
||||
// Range helpers (rangeToBounds, rangeToDays, rangeSpanDays) moved to
|
||||
// @/lib/analytics/range - that file is client-safe (no DB imports) so it
|
||||
// can be used from React components AND this server module.
|
||||
|
||||
// ─── Computations ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -126,9 +115,9 @@ export async function computePipelineFunnel(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<PipelineFunnelData> {
|
||||
const cutoff = rangeToCutoff(range);
|
||||
const { from, to } = rangeToBounds(range);
|
||||
|
||||
// Stage counts EXCLUDE lost/cancelled outcomes — those never become
|
||||
// Stage counts EXCLUDE lost/cancelled outcomes - those never become
|
||||
// conversions, so polluting the funnel with them gives meaningless math.
|
||||
// Lost is reported separately in the `lost` block.
|
||||
const stageRows = await db
|
||||
@@ -138,7 +127,7 @@ export async function computePipelineFunnel(
|
||||
and(
|
||||
eq(interests.portId, portId),
|
||||
isNull(interests.archivedAt),
|
||||
gte(interests.createdAt, cutoff),
|
||||
between(interests.createdAt, from, to),
|
||||
sql`(${interests.outcome} IS NULL OR ${interests.outcome} = 'won')`,
|
||||
),
|
||||
)
|
||||
@@ -161,7 +150,7 @@ export async function computePipelineFunnel(
|
||||
and(
|
||||
eq(interests.portId, portId),
|
||||
isNull(interests.archivedAt),
|
||||
gte(interests.createdAt, cutoff),
|
||||
between(interests.createdAt, from, to),
|
||||
sql`${interests.outcome} IS NOT NULL AND ${interests.outcome} != 'won'`,
|
||||
),
|
||||
)
|
||||
@@ -182,8 +171,9 @@ export async function computeOccupancyTimeline(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<OccupancyTimelineData> {
|
||||
const days = rangeToDays(range);
|
||||
// Total berths per port (current count — assumes no churn).
|
||||
const { from, to } = rangeToBounds(range);
|
||||
const days = Math.max(1, Math.ceil((to.getTime() - from.getTime()) / 86_400_000));
|
||||
// Total berths per port (current count - assumes no churn).
|
||||
const totalRow = await db.execute<{ total: number }>(
|
||||
sql`SELECT count(*)::int AS total FROM berths WHERE port_id = ${portId}`,
|
||||
);
|
||||
@@ -191,10 +181,12 @@ export async function computeOccupancyTimeline(
|
||||
|
||||
// For each day in range, count berths that have an active reservation
|
||||
// covering that day. A reservation is "covering" if start_date <= day
|
||||
// AND (end_date IS NULL OR end_date >= day).
|
||||
// AND (end_date IS NULL OR end_date >= day). Walk forward from `from`
|
||||
// so custom ranges produce the right calendar days, not just N
|
||||
// most-recent days from "now".
|
||||
const points: OccupancyTimelineData['points'] = [];
|
||||
for (let i = days - 1; i >= 0; i--) {
|
||||
const day = new Date(Date.now() - i * 86_400_000);
|
||||
for (let i = 0; i < days; i++) {
|
||||
const day = new Date(from.getTime() + i * 86_400_000);
|
||||
const dayStr = day.toISOString().slice(0, 10);
|
||||
const occRow = await db
|
||||
.select({ occupied: sql<number>`count(distinct ${berthReservations.berthId})::int` })
|
||||
@@ -218,7 +210,7 @@ export async function computeRevenueBreakdown(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<RevenueBreakdownData> {
|
||||
const cutoff = rangeToCutoff(range);
|
||||
const { from, to } = rangeToBounds(range);
|
||||
const rows = await db
|
||||
.select({
|
||||
status: invoices.status,
|
||||
@@ -230,7 +222,7 @@ export async function computeRevenueBreakdown(
|
||||
and(
|
||||
eq(invoices.portId, portId),
|
||||
isNull(invoices.archivedAt),
|
||||
gte(invoices.createdAt, cutoff),
|
||||
between(invoices.createdAt, from, to),
|
||||
),
|
||||
)
|
||||
.groupBy(invoices.status, invoices.currency);
|
||||
@@ -248,7 +240,7 @@ export async function computeLeadSourceAttribution(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<LeadSourceAttributionData> {
|
||||
const cutoff = rangeToCutoff(range);
|
||||
const { from, to } = rangeToBounds(range);
|
||||
const rows = await db
|
||||
.select({ source: interests.source, count: sql<number>`count(*)::int` })
|
||||
.from(interests)
|
||||
@@ -256,7 +248,7 @@ export async function computeLeadSourceAttribution(
|
||||
and(
|
||||
eq(interests.portId, portId),
|
||||
isNull(interests.archivedAt),
|
||||
gte(interests.createdAt, cutoff),
|
||||
between(interests.createdAt, from, to),
|
||||
),
|
||||
)
|
||||
.groupBy(interests.source);
|
||||
@@ -272,11 +264,15 @@ export async function computeLeadSourceAttribution(
|
||||
}
|
||||
|
||||
// ─── Public read API (cache → compute → write back) ──────────────────────────
|
||||
//
|
||||
// Custom ranges always recompute (cache key would be unbounded). Preset
|
||||
// ranges go cache → compute → write-back as before.
|
||||
|
||||
export async function getPipelineFunnel(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<PipelineFunnelData> {
|
||||
if (isCustomRange(range)) return computePipelineFunnel(portId, range);
|
||||
const metricId = `pipeline_funnel.${range}` as const;
|
||||
const cached = await readSnapshot<PipelineFunnelData>(portId, metricId);
|
||||
if (cached) return cached;
|
||||
@@ -289,6 +285,7 @@ export async function getOccupancyTimeline(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<OccupancyTimelineData> {
|
||||
if (isCustomRange(range)) return computeOccupancyTimeline(portId, range);
|
||||
const metricId = `occupancy_timeline.${range}` as const;
|
||||
const cached = await readSnapshot<OccupancyTimelineData>(portId, metricId);
|
||||
if (cached) return cached;
|
||||
@@ -301,6 +298,7 @@ export async function getRevenueBreakdown(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<RevenueBreakdownData> {
|
||||
if (isCustomRange(range)) return computeRevenueBreakdown(portId, range);
|
||||
const metricId = `revenue_breakdown.${range}` as const;
|
||||
const cached = await readSnapshot<RevenueBreakdownData>(portId, metricId);
|
||||
if (cached) return cached;
|
||||
@@ -313,6 +311,7 @@ export async function getLeadSourceAttribution(
|
||||
portId: string,
|
||||
range: DateRange,
|
||||
): Promise<LeadSourceAttributionData> {
|
||||
if (isCustomRange(range)) return computeLeadSourceAttribution(portId, range);
|
||||
const metricId = `lead_source_attribution.${range}` as const;
|
||||
const cached = await readSnapshot<LeadSourceAttributionData>(portId, metricId);
|
||||
if (cached) return cached;
|
||||
|
||||
Reference in New Issue
Block a user