42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
// Generated, do not edit. sha256:e8b81d0ee6b08d9e5a1155c5a9b2d57a27a3e3e4cf29c9e9449b7eaea522f722
|
|||
|
|
export const STANDING_GROUPINGS = Object.freeze(['account', 'accountKind', 'asset', 'assetKind']);
|
||
|
|
|
||
|
|
export const standingShowsToday = params => !(params && params.showToday === false);
|
||
|
|
|
||
|
|
export const standingGroupingOf = v => (STANDING_GROUPINGS.includes(v) ? v : 'account');
|
||
|
|
|
||
|
|
const KEYS = {
|
||
|
|
account: r => (r.account && r.account.id != null ? `a${r.account.id}` : 'none'),
|
||
|
|
accountKind: r => (r.account && r.account.kind ? r.account.kind : 'none'),
|
||
|
|
asset: r => `s${r.asset.id}`,
|
||
|
|
assetKind: r => r.asset.kind,
|
||
|
|
};
|
||
|
|
|
||
|
|
export function groupStanding(rows, by) {
|
||
|
|
const keyOf = KEYS[standingGroupingOf(by)];
|
||
|
|
const groups = new Map();
|
||
|
|
for (const r of rows) {
|
||
|
|
const key = keyOf(r);
|
||
|
|
let g = groups.get(key);
|
||
|
|
if (!g) { g = { key, sample: r, value: 0, origins: [] }; groups.set(key, g); }
|
||
|
|
g.value += r.value || 0;
|
||
|
|
g.origins.push({ origin: r.origin, asset: r.asset });
|
||
|
|
}
|
||
|
|
return [...groups.values()].sort((a, b) => b.value - a.value);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function standingToday(rows, by, today) {
|
||
|
|
const out = new Map();
|
||
|
|
const grouping = standingGroupingOf(by);
|
||
|
|
if (!today?.perAsset || (grouping !== 'asset' && grouping !== 'assetKind')) return out;
|
||
|
|
const seen = new Set();
|
||
|
|
for (const r of rows) {
|
||
|
|
if (seen.has(r.asset.id)) continue;
|
||
|
|
seen.add(r.asset.id);
|
||
|
|
const amount = today.perAsset.get(r.asset.id);
|
||
|
|
if (amount == null) continue;
|
||
|
|
const key = KEYS[grouping](r);
|
||
|
|
out.set(key, (out.get(key) || 0) + amount);
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|