64 lines
2.8 KiB
JavaScript
64 lines
2.8 KiB
JavaScript
// Generated, do not edit. sha256:88f1ff40757f8e5095efe909c3a45c2f437406121459c4091bd0b4d3c80eac8c
|
|
import { localDate } from '../../shared/time.js';
|
|
|
|
const DAY_MS = 86400000;
|
|
|
|
const dayMs = iso => {
|
|
const d = localDate(iso);
|
|
return d ? Date.UTC(Number(d.slice(0, 4)), Number(d.slice(5, 7)) - 1, Number(d.slice(8, 10))) : null;
|
|
};
|
|
|
|
export function activityItems({ accountIds, trades = [], transfers = [], interest = [], valuations = [],
|
|
schedules = [], accounts = [], reservations = [], interestDue = [] }) {
|
|
const ids = new Set(accountIds);
|
|
const items = [];
|
|
const add = (at, accountId, kind, dir, table = null, id = null, assetId = null) => {
|
|
const x = dayMs(at);
|
|
if (x != null && ids.has(accountId)) items.push({ x, accountId, kind, dir, table, id, assetId });
|
|
};
|
|
for (const r of trades) {
|
|
if (r.type === 'buy') add(r.occurredAt, r.toAccountId, 'buy', 1, 'trades', r.id, r.assetId);
|
|
else if (r.type === 'sell') add(r.occurredAt, r.fromAccountId, 'sell', -1, 'trades', r.id, r.assetId);
|
|
}
|
|
for (const r of transfers) {
|
|
add(r.occurredAt, r.fromAccountId, 'transferOut', -1, 'transfers', r.id, r.assetId);
|
|
add(r.occurredAt, r.toAccountId, 'transferIn', 1, 'transfers', r.id, r.assetId);
|
|
}
|
|
for (const r of interest) add(r.occurredAt, r.toAccountId, 'interest', 1, 'interest', r.id, r.assetId);
|
|
for (const r of valuations) add(r.rateAt || r.valuedOn, r.accountId, 'balance', 0, 'valuations', r.id, r.assetId);
|
|
for (const s of schedules) {
|
|
add(s.startsOn, s.accountId, 'agreementStart', 0);
|
|
if (s.endsOn) add(s.endsOn, s.accountId, 'agreementEnd', 0);
|
|
}
|
|
for (const r of reservations) add(r.occurredAt, r.fromAccountId ?? r.toAccountId, 'reservation', 0, 'reservations', r.id, r.assetId);
|
|
for (const p of interestDue) add(p.periodEnd, p.accountId, 'interestDue', 0);
|
|
for (const a of accounts) {
|
|
if (a.createdAt) add(a.createdAt, a.id, 'accountOpened', 0);
|
|
if (a.closedAt) add(a.closedAt, a.id, 'accountClosed', 0);
|
|
if (a.updatedAt) add(a.updatedAt, a.id, 'accountChanged', 0);
|
|
}
|
|
return items;
|
|
}
|
|
|
|
export function heartbeat(items, todayIso, fromIso = null) {
|
|
const from = fromIso ? dayMs(fromIso) : null;
|
|
if (from != null) items = items.filter(it => it.x >= from);
|
|
if (!items.length) return null;
|
|
const byDay = new Map();
|
|
for (const it of items) {
|
|
if (!byDay.has(it.x)) byDay.set(it.x, []);
|
|
byDay.get(it.x).push(it);
|
|
}
|
|
const today = dayMs(todayIso);
|
|
const last = Math.max(...byDay.keys(), today);
|
|
const first = Math.min(from ?? Math.min(...byDay.keys(), today) - DAY_MS, last - DAY_MS);
|
|
const points = [];
|
|
for (let x = first; x <= last; x += DAY_MS) {
|
|
const day = byDay.get(x) || [];
|
|
const up = day.filter(i => i.dir > 0).length;
|
|
const down = day.filter(i => i.dir < 0).length;
|
|
points.push([x, up || down ? (up >= down ? Math.sqrt(up) : -Math.sqrt(down)) : 0]);
|
|
}
|
|
return { points, byDay };
|
|
}
|