Unverified Commit 07c82890 authored by Clément Berthou's avatar Clément Berthou Committed by GitHub
Browse files

fix: fix invalid date ranges computations (#86)

parent a1a2c595
......@@ -9,6 +9,7 @@ import type {
MatomoActionConfigObject,
MatomoEventCategory,
} from "../matomo-types";
import { RELEASE_DATE_3_2 } from "./loader-utils";
const FOOTPRINT_COEF = 19;
const PAPER_EQUIVALENCE_COEF = 218;
......@@ -66,8 +67,11 @@ const getCarbonfootprintPaperEquivalence = (carbonFootprint: number): number =>
export const markedToDeleteLoaders = (
config: MatomoActionConfigObject
): Loader[] =>
getMatomoLastWeeksRange(new Date()).map((date) => ({
aggregator: markedToDeleteAggregator(),
query: actionQuery({ ...config, date }),
}));
): Loader[] => {
return getMatomoLastWeeksRange(new Date(), new Date(RELEASE_DATE_3_2)).map(
(date) => ({
aggregator: markedToDeleteAggregator(),
query: actionQuery({ ...config, date }),
})
);
};
......@@ -8,6 +8,7 @@ import type {
MatomoActionConfigObject,
MatomoEventCategory,
} from "../matomo-types";
import { RELEASE_DATE_3_2 } from "./loader-utils";
const totalFileDropAggregator = () => (
response: MatomoEventCategory[]
......@@ -58,7 +59,9 @@ const getTotalFileDrop = (fileDropVolumes: string[]): number => {
export const totalFileDropLoaders = (
config: MatomoActionConfigObject
): Loader[] =>
getMatomoLastWeeksRange(new Date()).map((date) => ({
aggregator: totalFileDropAggregator(),
query: actionQuery({ ...config, date }),
}));
getMatomoLastWeeksRange(new Date(), new Date(RELEASE_DATE_3_2)).map(
(date) => ({
aggregator: totalFileDropAggregator(),
query: actionQuery({ ...config, date }),
})
);
import { parseISO } from "date-fns";
import { getLastWeeksRanges } from "./date";
import { getLastWeeksRanges, getMatomoLastWeeksRange } from "./date";
describe("date", () => {
describe("getLastWeeksRanges", () => {
......@@ -13,4 +13,15 @@ describe("date", () => {
]);
});
});
describe("getMatomoLastWeeksRanges", () => {
it("should compute the last months ranges", () => {
const now = parseISO("2021-06-20");
const start = parseISO("2021-06-10");
expect(getMatomoLastWeeksRange(now, start)).toEqual([
["2021-06-10", "2021-06-16"],
["2021-06-17", "2021-06-23"],
]);
});
});
});
import {
addDays,
differenceInWeeks,
endOfWeek,
formatISO,
......@@ -7,8 +8,6 @@ import {
} from "date-fns";
import { range } from "lodash";
import { RELEASE_DATE_3_2 } from "../matomo/loaders/loader-utils";
export const getLastWeeksRanges = (weekCount: number) => (
now: Date
): [string, string][] =>
......@@ -26,6 +25,25 @@ export const getLastWeeksRanges = (weekCount: number) => (
]
);
export const getMatomoLastWeeksRange = getLastWeeksRanges(
differenceInWeeks(Date.now(), new Date(RELEASE_DATE_3_2))
);
export const getLastRangesChunks = (chunkSize: number) => (
chunksCount: number
) => (startDate: Date): [string, string][] =>
range(chunksCount)
.map((index) => addDays(startDate, index * chunkSize))
.map((date) => [date, addDays(date, chunkSize - 1)])
.map(
(dateRange) =>
dateRange.map((date) =>
formatISO(date, { representation: "date" })
) as [string, string]
);
const DAYS_IN_WEEK = 7;
const getLastWeeksChunks = getLastRangesChunks(DAYS_IN_WEEK);
export const getMatomoLastWeeksRange = (
now: Date,
baseDate: Date
): [string, string][] =>
getLastWeeksChunks(differenceInWeeks(now, baseDate) + 1)(baseDate);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment