Skip to content
24 changes: 24 additions & 0 deletions live-editing/configs/PivotGridConfigGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ export class PivotGridConfigGenerator implements IConfigGenerator {
component: 'PivotStylingComponent'
}));

const evChargingFiles = [
'/src/app/data/ev-charging-data.ts',
'/src/app/pivot-grid/pivot-ev-charging/calendar.ts',
'/src/app/pivot-grid/pivot-ev-charging/pivot-config.ts',
'/src/app/pivot-grid/pivot-ev-charging/kpis.ts'
];

configs.push(new Config({
additionalFiles: evChargingFiles,
appConfig: BaseAppConfig,
component: 'PivotEvChargingComponent'
}));

configs.push(new Config({
additionalFiles: [
...evChargingFiles,
'/src/app/pivot-grid/pivot-ev-charging/pivot-ev-charging.component.ts',
'/src/app/pivot-grid/pivot-ev-charging/pivot-ev-charging.component.html',
'/src/app/pivot-grid/pivot-ev-charging/pivot-ev-charging.component.scss'
],
appConfig: BaseAppConfig,
component: 'PivotEvChargingDocsComponent'
}));

return configs;
}
}
25 changes: 25 additions & 0 deletions src/app/data/ev-charging-data.ts

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions src/app/pivot-grid/pivot-ev-charging/calendar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { CHARGING_DATA, ChargingRecord } from '../../data/ev-charging-data';

/**
* Dates relative to today, so the sample always looks recent.
* The story is anchored on the last complete quarter (in November 2026 that is Q3 2026).
* The data covers the whole previous year and the current year up to the end of that quarter.
*
* The generated template covers 2025-01 to 2026-09 with Q3 2026 as its anchor; it is re-dated here:
* - previous-year months use the 2025 template months,
* - current-year months use the 2026 template months (Oct-Dec, which the template lacks, use 2025 + 8% growth),
* - the Tessera outage (template: Aug-Sep 2026) moves to the last two months of the anchor quarter.
*/
const MON = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const MONTH = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

const today = new Date();
let year = today.getFullYear();
let q = Math.floor(today.getMonth() / 3); // the previous quarter, 1-based (0 = Q4 of last year)
if (q === 0) { q = 4; year -= 1; }

export const CAL = {
year,
prevYear: year - 1,
q,
/** 'Q3 2026' */
anchor: `Q${q} ${year}`,
/** previous quarter label as used in comparisons: 'Q2', or 'Q4 2025' across a year boundary */
prevQ: q === 1 ? { y: year - 1, q: 4, label: `Q4 ${year - 1}` } : { y: year, q: q - 1, label: `Q${q - 1}` },
firstMonth: 3 * q - 2,
lastMonth: 3 * q
};
export const pad = (n: number) => String(n).padStart(2, '0');
export const ym = (y: number, m: number) => `${y}-${pad(m)}-01`;
export const monShort = (m: number) => MON[m - 1];
export const monLong = (m: number) => MONTH[m - 1];
export const qFirst = (y: number, qq: number) => ym(y, 3 * qq - 2);
export const qLast = (y: number, qq: number) => ym(y, 3 * qq);
/** Start of the current (incomplete) quarter, for the live estimate. */
export const CURRENT_QUARTER_START = new Date(CAL.lastMonth === 12 ? CAL.year + 1 : CAL.year, CAL.lastMonth === 12 ? 0 : CAL.lastMonth, 1);

/* ---------- re-date the template ---------- */
const GROWTH = 1.08;
const ACTIVITY: (keyof ChargingRecord)[] = ['Sessions', 'EnergyKwh', 'RevenueEur', 'EnergyCostEur', 'ChargingHours'];
const index = new Map<string, ChargingRecord>();
const key = (r: { Site: string; DayType: string }, month: string) => `${r.Site}|${r.DayType}|${month}`;
for (const r of CHARGING_DATA) index.set(key(r, r.Month), r);
const sites = [...new Map(CHARGING_DATA.map(r => [`${r.Site}|${r.DayType}`, r])).values()];
const downRatio = (r: ChargingRecord | undefined) => (r && r.AvailableHours ? r.DowntimeHours / r.AvailableHours : undefined);
const shiftYears = (date: string, dy: number) => `${+date.slice(0, 4) + dy}${date.slice(4)}`;

function rebase(): ChargingRecord[] {
const out: ChargingRecord[] = [];
const dy = CAL.year - 2026;
const outage = [CAL.lastMonth - 1, CAL.lastMonth];
const months: [number, number][] = [];
for (let m = 1; m <= 12; m++) months.push([CAL.prevYear, m]);
for (let m = 1; m <= CAL.lastMonth; m++) months.push([CAL.year, m]);
for (const s of sites) {
for (const [y, m] of months) {
let src: ChargingRecord | undefined, scale = 1;
if (y === CAL.prevYear) src = index.get(key(s, ym(2025, m)));
else if (m <= 9) src = index.get(key(s, ym(2026, m)));
else { src = index.get(key(s, ym(2025, m))) ?? index.get(key(s, ym(2026, 9))); scale = GROWTH; }
if (!src) continue; // site not open yet in that month of the template
const r = { ...src, Month: ym(y, m), OpenedDate: shiftYears(src.OpenedDate, dy) } as ChargingRecord;
for (const k of ACTIVITY) (r as any)[k] = (src[k] as number) * scale;
if (r.Vendor === 'Tessera') {
// outage only in the last two months of the anchor quarter
const inOutage = y === CAL.year && outage.includes(m);
const ratio = inOutage
? downRatio(index.get(key(s, ym(2026, m === outage[0] ? 8 : 9))))
: src.Month === ym(2026, 8) || src.Month === ym(2026, 9) ? downRatio(index.get(key(s, ym(2026, 7)))) : undefined;
if (ratio !== undefined) r.DowntimeHours = r.AvailableHours * ratio;
}
out.push(r);
}
}
return out;
}

export const DATA: ChargingRecord[] = rebase();
128 changes: 128 additions & 0 deletions src/app/pivot-grid/pivot-ev-charging/kpis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { ChargingRecord } from '../../data/ev-charging-data';
import { maturity } from './pivot-config';
import { CAL, CURRENT_QUARTER_START, monLong, monShort, qFirst, qLast, ym } from './calendar';

/** KPI cards shown above the grid for each saved view, computed from the same records. */
/** Card status: a 4 px accent on the left (warning, error) and a red value (error). */
export type Status = 'none' | 'warning' | 'error';
export interface Kpi {
/** Shown in uppercase: what and when, e.g. 'Network uptime · Q3 2026'. */
label: string;
value: string;
/** Change chip: green ▲ when good, red ▼ when bad. */
change?: { text: string; good: boolean; up: boolean };
/** Grey text after the chip (or alone). */
comparison: string;
status: Status;
/** Optional link to another saved view. */
action?: { label: string; view: string };
/** Value updates live (shown with a green dot). */
live?: boolean;
/** Smaller unit after the value. */
unit?: string;
}

type Pred = (r: ChargingRecord) => boolean;
const sum = (rows: ChargingRecord[], k: keyof ChargingRecord) => rows.reduce((a, r) => a + (r[k] as number), 0);
const uptime = (rows: ChargingRecord[]) => 1 - sum(rows, 'DowntimeHours') / sum(rows, 'AvailableHours');
const util = (rows: ChargingRecord[]) => sum(rows, 'ChargingHours') / sum(rows, 'AvailableHours');
const pct = (v: number) => (v * 100).toFixed(1) + '%';
const pp = (v: number) => (v >= 0 ? '+' : '−') + Math.abs(v * 100).toFixed(1) + ' pp';
/** A change chip; `higherIsGood` decides the colour. */
const chg = (d: number, text: string, higherIsGood = true) => ({ text, up: d >= 0, good: (d >= 0) === higherIsGood });
const ppAbs = (v: number) => Math.abs(v * 100).toFixed(1) + ' pp';
const nf = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 });
const uptimeStatus = (v: number): Status => (v < 0.97 ? 'error' : v < 0.98 ? 'warning' : 'none');

const between = (from: string, to: string): Pred => r => r.Month >= from && r.Month <= to;
// periods relative to today (see data/calendar.ts): the anchor quarter is the last complete one
const Y = CAL.year, PY = CAL.prevYear;
const AQ = between(qFirst(Y, CAL.q), qLast(Y, CAL.q));
const PQ = between(qFirst(CAL.prevQ.y, CAL.prevQ.q), qLast(CAL.prevQ.y, CAL.prevQ.q));
const YTD = between(ym(Y, 1), ym(Y, CAL.lastMonth));
const PREV_YEAR = between(ym(PY, 1), ym(PY, 12));
/** pp change; hidden when it rounds to 0.0 pp, so no green ▲ for no change. */
const ppChg = (d: number) => (Math.abs(d) < 0.0005 ? undefined : chg(d, ppAbs(d)));
const kwhFmt = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 });
const dec1 = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
const and = (...ps: Pred[]): Pred => r => ps.every(p => p(r));

export function kpis(view: string, data: ChargingRecord[], now = Date.now()): Kpi[] {
const where = (...ps: Pred[]) => data.filter(and(...ps));
const de: Pred = r => r.Country === 'Germany';

if (view === 'overview') {
const netQ = uptime(where(AQ)), netP = uptime(where(PQ));
const deQ = uptime(where(AQ, de)), deP = uptime(where(PQ, de));
const uY = util(where(YTD)), uP = util(where(PREV_YEAR));
// Live: energy to date = complete months + an estimate for the elapsed part of the current quarter.
const eAnchor = sum(where(AQ), 'EnergyKwh');
const qStart = CURRENT_QUARTER_START.getTime();
const elapsed = Math.max(0, (now - qStart) / 1000);
// The live year is the current quarter's year (in Q1 it is a new year: only the estimate counts).
const liveYear = CURRENT_QUARTER_START.getFullYear();
const newYear = liveYear > Y;
// Same period one year earlier: the same complete months, plus the same share of that year's version of the current quarter.
const cmpYear = liveYear - 1, nq = Math.floor(CURRENT_QUARTER_START.getMonth() / 3) + 1;
const cmpQuarterSeconds = (new Date(cmpYear, 3 * nq, 1).getTime() - new Date(cmpYear, 3 * nq - 3, 1).getTime()) / 1000;
const eCmpQ = sum(where(between(qFirst(cmpYear, nq), qLast(cmpYear, nq))), 'EnergyKwh');
const eCmpDone = newYear ? 0 : sum(where(between(ym(cmpYear, 1), ym(cmpYear, CAL.lastMonth))), 'EnergyKwh');
const share = Math.min(1, elapsed / cmpQuarterSeconds);
const ePrev = eCmpDone + eCmpQ * share;
// Estimate for the current quarter: last year's same quarter, grown like the anchor quarter grew year on year.
const growth = eAnchor / sum(where(between(qFirst(PY, CAL.q), qLast(PY, CAL.q))), 'EnergyKwh');
const eLive = (newYear ? 0 : sum(where(YTD), 'EnergyKwh')) + eCmpQ * growth * share;
return [
{ label: `Network uptime · ${CAL.anchor}`, value: pct(netQ), change: ppChg(netQ - netP), comparison: `vs ${CAL.prevQ.label}`, status: uptimeStatus(netQ) },
{ label: `Germany uptime · ${CAL.anchor}`, value: pct(deQ), change: ppChg(deQ - deP), comparison: `vs ${CAL.prevQ.label} · target 97%`, status: uptimeStatus(deQ),
action: { label: 'See vendors', view: 'vendor' } },
{ label: `Utilization · ${Y} to date`, value: pct(uY), change: ppChg(uY - uP), comparison: `vs ${PY}`, status: 'none' },
{ label: `Energy delivered · ${liveYear} to date`, value: kwhFmt.format(eLive), unit: 'kWh', change: chg(eLive - ePrev, Math.abs((eLive / ePrev - 1) * 100).toFixed(1) + '%'),
comparison: `vs same period ${cmpYear}`, status: 'none', live: true }
];
}

if (view === 'vendor') {
const last = between(ym(Y, CAL.lastMonth), ym(Y, CAL.lastMonth)), first = between(ym(Y, CAL.firstMonth), ym(Y, CAL.firstMonth));
const tess: Pred = r => r.Vendor === 'Tessera';
const tLast = uptime(where(de, tess, last)), tFirst = uptime(where(de, tess, first));
const others = [...new Set(where(de).map(r => r.Vendor))].filter(v => v !== 'Tessera')
.map(v => uptime(where(de, last, r => r.Vendor === v)));
// Live: the outage started in the second month of the anchor quarter and is still going on.
const outStart = CAL.lastMonth - 1;
const outage = between(ym(Y, outStart), ym(Y, CAL.lastMonth));
const lastMonthSeconds = (new Date(Y, CAL.lastMonth, 1).getTime() - new Date(Y, CAL.lastMonth - 1, 1).getTime()) / 1000;
const rateNow = sum(where(de, tess, last), 'DowntimeHours') / lastMonthSeconds; // downtime hours per second
const ratePrev = sum(where(de, tess, PQ), 'DowntimeHours') / ((new Date(CAL.prevQ.y, 3 * CAL.prevQ.q, 1).getTime() - new Date(CAL.prevQ.y, 3 * CAL.prevQ.q - 3, 1).getTime()) / 1000);
const downLive = sum(where(de, tess, outage), 'DowntimeHours') + rateNow * Math.max(0, (now - CURRENT_QUARTER_START.getTime()) / 1000);
const sites = new Set(where(de, tess).map(r => r.Site)).size;
const lastLabel = `${monShort(CAL.lastMonth)} ${Y}`;
return [
{ label: `Tessera uptime · ${lastLabel}`, value: pct(tLast), change: ppChg(tLast - tFirst), comparison: `since ${monLong(CAL.firstMonth)}`, status: uptimeStatus(tLast) },
{ label: `Other vendors · ${lastLabel}`, value: pct(Math.min(...others)), comparison: 'lowest of Altior and Halden', status: uptimeStatus(Math.min(...others)) },
{ label: `Tessera downtime · since ${monShort(outStart)}`, value: dec1.format(downLive), unit: 'h', change: chg(1, (rateNow / ratePrev).toFixed(1) + '×', false),
comparison: `hourly rate vs ${CAL.prevQ.label}`, status: 'error', live: true },
// the sample data has sites, not connectors (the design shows connectors: 140)
{ label: 'Tessera sites · Germany', value: String(sites), comparison: `all 350 kW · ${CAL.anchor}`, status: 'none',
action: { label: 'Weekend pressure', view: 'weekends' } }
];
}

// weekends: the anchor quarter, motorway corridor, mature sites (the view's filters)
const scope = and(AQ, r => r.LocationType === 'Motorway corridor', r => maturity(r) === 'Mature');
const we: Pred = r => r.DayType === 'Weekend', wd: Pred = r => r.DayType === 'Weekday';
const uWe = util(where(scope, we)), uWd = util(where(scope, wd));
// German cities at weekends (the grid shows city level for Germany)
const byCity = [...new Set(where(scope, de).map(r => r.City))].map(c => ({ c, u: util(where(scope, we, de, r => r.City === c)) }));
const top = [...byCity].sort((a, b) => b.u - a.u)[0];
const hot = byCity.filter(x => x.u >= 0.25).length;
const kwh = (rows: ChargingRecord[]) => sum(rows, 'EnergyKwh') / sum(rows, 'Sessions');
return [
{ label: `Weekend utilization · ${CAL.anchor}`, value: pct(uWe), change: ppChg(uWe - uWd), comparison: 'vs weekdays', status: uWe >= 0.25 ? 'warning' : 'none' },
{ label: 'Busiest city · weekends', value: pct(top.u), comparison: `${top.c}${top.u >= 0.25 ? ' · over 25%' : ''}`, status: top.u >= 0.25 ? 'warning' : 'none' },
{ label: 'German cities at 25%+ · weekends', value: `${hot} of ${byCity.length}`, comparison: 'expansion candidates', status: 'warning' },
{ label: 'Energy per session · weekends', value: kwh(where(scope, we)).toFixed(1) + ' kWh',
change: chg(kwh(where(scope, we)) - kwh(where(scope, wd)), Math.abs(kwh(where(scope, we)) - kwh(where(scope, wd))).toFixed(1) + ' kWh'),
comparison: 'vs weekdays', status: 'none', action: { label: 'Back to overview', view: 'overview' } }
];
}
Loading