Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
fix(COR-1865): Throw error if pageMetric not found in data
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-van-eekelen committed Dec 18, 2023
1 parent bcfb7fa commit cea968f
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions packages/app/src/utils/get-last-insertion-date-of-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ function hasLastValue(metric: any): boolean {
}

function hasValues(metric: any): boolean {
return Array.isArray(metric?.values) &&
typeof metric?.values[metric.values.length - 1]?.date_of_insertion_unix !== 'undefined';
return Array.isArray(metric?.values) && typeof metric?.values[metric.values.length - 1]?.date_of_insertion_unix !== 'undefined';
}

function hasInsertionDate(metric: any): boolean {
return typeof metric?.date_of_insertion_unix !== 'undefined';
}

function hasNestedLastValue(metric: any): boolean {
return Array.isArray(metric?.values) &&
typeof metric?.values[0]?.last_value?.date_of_insertion_unix !== 'undefined';
return Array.isArray(metric?.values) && typeof metric?.values[0]?.last_value?.date_of_insertion_unix !== 'undefined';
}

// functions for getting values
Expand All @@ -33,7 +31,7 @@ function getDateFromInsertionDate(metric: any): number {
}

function getDateFromNestedLastValue(metric: any): number {
return metric?.values.reduce((lastDate :number, innerValue: any) => {
return metric?.values.reduce((lastDate: number, innerValue: any) => {
const metricDate = getMetricDate(innerValue);
return Math.max(metricDate, lastDate);
}, 0);
Expand All @@ -55,14 +53,21 @@ function getMetricDate(metricOrUnixDate: any): number {
return 0;
}

export function getLastInsertionDateOfPage(
data: unknown,
pageMetrics: string[]
) {
return pageMetrics.reduce((lastDate, metricProperty) => {
export function getLastInsertionDateOfPage(data: unknown, pageMetrics: string[]) {
const foundMetrics: string[] = pageMetrics.filter((metricProperty) => {
if (get(data, metricProperty) !== undefined) {
return metricProperty;
}
});

if (foundMetrics?.length === 0) {
throw new Error(`Pagemetrics not found in data`);
}

return foundMetrics.reduce((lastDate, metricProperty) => {
const metric: any = get(data, metricProperty);
const metricDate = getMetricDate(metric);

return Math.max(metricDate, lastDate);
}, 0);
};
}

0 comments on commit cea968f

Please sign in to comment.