2023-05-05 10:10:54 +02:00
|
|
|
import {
|
|
|
|
IClientMetricsEnv,
|
|
|
|
IClientMetricsEnvVariant,
|
|
|
|
} from '../types/stores/client-metrics-store-v2';
|
2022-08-19 10:38:26 +02:00
|
|
|
import { startOfHour } from 'date-fns';
|
|
|
|
|
|
|
|
const createMetricKey = (metric: IClientMetricsEnv): string => {
|
|
|
|
return [
|
|
|
|
metric.featureName,
|
|
|
|
metric.appName,
|
|
|
|
metric.environment,
|
|
|
|
metric.timestamp.getTime(),
|
|
|
|
].join();
|
|
|
|
};
|
|
|
|
|
2023-05-05 10:10:54 +02:00
|
|
|
const mergeRecords = (
|
|
|
|
firstRecord: Record<string, number>,
|
|
|
|
secondRecord: Record<string, number>,
|
|
|
|
): Record<string, number> => {
|
|
|
|
const result: Record<string, number> = {};
|
|
|
|
|
|
|
|
for (const key in firstRecord) {
|
|
|
|
result[key] = firstRecord[key] + (secondRecord[key] ?? 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in secondRecord) {
|
|
|
|
if (!(key in result)) {
|
|
|
|
result[key] = secondRecord[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2022-08-19 10:38:26 +02:00
|
|
|
export const collapseHourlyMetrics = (
|
|
|
|
metrics: IClientMetricsEnv[],
|
|
|
|
): IClientMetricsEnv[] => {
|
2022-08-19 11:45:41 +02:00
|
|
|
const grouped = new Map<string, IClientMetricsEnv>();
|
|
|
|
metrics.forEach((metric) => {
|
|
|
|
const hourlyMetric = {
|
|
|
|
...metric,
|
|
|
|
timestamp: startOfHour(metric.timestamp),
|
|
|
|
};
|
|
|
|
const key = createMetricKey(hourlyMetric);
|
|
|
|
if (!grouped[key]) {
|
|
|
|
grouped[key] = hourlyMetric;
|
|
|
|
} else {
|
|
|
|
grouped[key].yes = metric.yes + (grouped[key].yes || 0);
|
|
|
|
grouped[key].no = metric.no + (grouped[key].no || 0);
|
2023-05-05 10:10:54 +02:00
|
|
|
|
|
|
|
if (metric.variants) {
|
|
|
|
grouped[key].variants = mergeRecords(
|
|
|
|
metric.variants,
|
2023-06-09 14:48:32 +02:00
|
|
|
grouped[key].variants ?? {},
|
2023-05-05 10:10:54 +02:00
|
|
|
);
|
|
|
|
}
|
2022-08-19 11:45:41 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return Object.values(grouped);
|
2022-08-19 10:38:26 +02:00
|
|
|
};
|
2023-05-05 10:10:54 +02:00
|
|
|
|
|
|
|
export const spreadVariants = (
|
|
|
|
metrics: IClientMetricsEnv[],
|
|
|
|
): IClientMetricsEnvVariant[] => {
|
|
|
|
return metrics.flatMap((item) => {
|
|
|
|
if (!item.variants) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return Object.entries(item.variants).map(([variant, count]) => ({
|
|
|
|
featureName: item.featureName,
|
|
|
|
appName: item.appName,
|
|
|
|
environment: item.environment,
|
|
|
|
timestamp: item.timestamp,
|
|
|
|
variant,
|
|
|
|
count,
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|