Skip to content

Commit

Permalink
feat(StatsTable): allow creating custom stats
Browse files Browse the repository at this point in the history
  • Loading branch information
cshaa committed Aug 20, 2024
1 parent 648c086 commit 42753df
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/lib/data/stats-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,45 @@ export class StatsTable<StatsMap extends Record<string, Stat>> {
});
}

static fromSingleStat<Title extends string>(opts: {
statTitle: Title;
dataUnit?: DataUnit;
displayUnit?: DisplayUnitPreference;
ids: string[];
values: number[];
style?: TraceStyleSheet;
labels?: Iterable<[string, string | undefined]>;
}): StatsTable<{ [t in Title]: ValueStat }>;

static fromSingleStat<Title extends string, DataType>(opts: {
statTitle: Title;
ids: string[];
customData: DataType[];
style?: TraceStyleSheet;
labels?: Iterable<[string, string | undefined]>;
}): StatsTable<{ [t in Title]: CustomStat<DataType> }>;

static fromSingleStat<Title extends string>({
statTitle,
dataUnit,
displayUnit,
ids,
data,
values,
customData,
style,
labels,
}: {
statTitle: Title;
dataUnit?: DataUnit;
displayUnit?: DisplayUnitPreference;
ids: string[];
data: number[];
values?: number[];
customData?: any[];
style?: TraceStyleSheet;
labels?: Iterable<[string, string | undefined]>;
}): StatsTable<{ [t in Title]: ValueStat }> {
}):
| StatsTable<{ [t in Title]: ValueStat }>
| StatsTable<{ [t in Title]: CustomStat<any> }> {
const handles: VariantHandleArray = new Uint32Array(ids.length);

for (const [i, id] of enumerate(ids)) {
Expand All @@ -205,12 +227,17 @@ export class StatsTable<StatsMap extends Record<string, Stat>> {
randomSeed: randomUint(),
styles: oxidizeStyleSheet(style),
stats: [
{
title: statTitle,
dataUnit,
displayUnit: displayUnit ?? "auto",
data: new Map(zip(handles, data)),
},
values
? {
title: statTitle,
dataUnit,
displayUnit: displayUnit ?? "auto",
data: new Map(zip(handles, values)),
}
: {
title: statTitle,
data: new Map(zip(handles, customData!)),
},
],
});
}
Expand Down

0 comments on commit 42753df

Please sign in to comment.