Skip to content

Commit

Permalink
NNS1-3486: new util function to convert from period to dateRange (#6028)
Browse files Browse the repository at this point in the history
# Motivation

We want to convert the selected radio value into a `DateRange` object
that can be used when fetching the translations.

# Changes

- New utility `convertPeriodToNanosecondRange` to convert a reporting
period into a transaction date range.

# Tests

- Unit tests

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary
  • Loading branch information
yhabib authored Dec 17, 2024
1 parent 35cb27c commit bc7b3b6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
10 changes: 3 additions & 7 deletions frontend/src/lib/services/reporting.services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getTransactions } from "$lib/api/icp-index.api";
import type { Account } from "$lib/types/account";
import type { TransactionsDateRange } from "$lib/types/reporting";
import { neuronStake } from "$lib/utils/neuron.utils";
import { SignIdentity } from "@dfinity/agent";
import type { TransactionWithId } from "@dfinity/ledger-icp";
Expand Down Expand Up @@ -94,11 +95,6 @@ export const getAccountTransactionsConcurrently = async ({
return entitiesAndTransactions;
};

type DateRange = {
from?: bigint;
to?: bigint;
};

export const getAllTransactionsFromAccountAndIdentity = async ({
accountId,
identity,
Expand All @@ -112,7 +108,7 @@ export const getAllTransactionsFromAccountAndIdentity = async ({
lastTransactionId?: bigint;
allTransactions?: TransactionWithId[];
currentPageIndex?: number;
range?: DateRange;
range?: TransactionsDateRange;
}): Promise<TransactionWithId[] | undefined> => {
// Based on
// https://github.com/dfinity/ic/blob/master/rs/ledger_suite/icp/index/src/lib.rs#L31
Expand Down Expand Up @@ -178,7 +174,7 @@ export const getAllTransactionsFromAccountAndIdentity = async ({
// Helper function to filter transactions by date range
const filterTransactionsByRange = (
transactions: TransactionWithId[],
range?: DateRange
range?: TransactionsDateRange
): TransactionWithId[] => {
if (isNullish(range)) return transactions;
return transactions.filter((tx) => {
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/lib/types/reporting.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export type ReportingDateRange = "all" | "last-year" | "year-to-date";

export type TransactionsDateRange = {
/** Start of the date range (inclusive) - timestamp in nanoseconds */
from?: bigint;
/** End of the date range (exclusive) - timestamp in nanoseconds */
to?: bigint;
};
29 changes: 29 additions & 0 deletions frontend/src/lib/utils/reporting.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { TransactionResults } from "$lib/services/reporting.services";
import type {
ReportingDateRange,
TransactionsDateRange,
} from "$lib/types/reporting";
import {
getFutureDateFromDelayInSeconds,
nanoSecondsToDateTime,
Expand Down Expand Up @@ -459,3 +463,28 @@ export const buildNeuronsDatasets = ({

return [{ metadata, data }];
};

export const convertPeriodToNanosecondRange = (
period: ReportingDateRange
): TransactionsDateRange => {
const now = new Date();
const currentYear = now.getFullYear();
const toNanoseconds = (milliseconds: number): bigint =>
BigInt(milliseconds) * BigInt(1_000_000);

switch (period) {
case "all":
return {};

case "last-year":
return {
from: toNanoseconds(new Date(currentYear - 1, 0, 1).getTime()),
to: toNanoseconds(new Date(currentYear, 0, 1).getTime()),
};

case "year-to-date":
return {
from: toNanoseconds(new Date(currentYear, 0, 1).getTime()),
};
}
};
34 changes: 34 additions & 0 deletions frontend/src/tests/lib/utils/reporting.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
buildNeuronsDatasets,
buildTransactionsDatasets,
combineDatasetsToCsv,
convertPeriodToNanosecondRange,
convertToCsv,
generateCsvFileToSave,
type CsvHeader,
Expand Down Expand Up @@ -554,4 +555,37 @@ describe("reporting utils", () => {
]);
});
});

describe("convertPeriodToNanosecondRange", () => {
const mockDate = new Date("2024-03-15T12:00:00Z");
const NANOS_IN_MS = BigInt(1_000_000);

beforeEach(() => {
vi.clearAllTimers();
vi.useFakeTimers();
vi.setSystemTime(mockDate);
});

it('returns empty object for "all" period', () => {
const result = convertPeriodToNanosecondRange("all");
expect(result).toEqual({});
});

it('returns correct range for "last-year"', () => {
const result = convertPeriodToNanosecondRange("last-year");

expect(result).toEqual({
from: BigInt(new Date("2023-01-01T00:00:00Z").getTime()) * NANOS_IN_MS,
to: BigInt(new Date("2024-01-01T00:00:00Z").getTime()) * NANOS_IN_MS,
});
});

it('returns correct range for "year-to-date"', () => {
const result = convertPeriodToNanosecondRange("year-to-date");

expect(result).toEqual({
from: BigInt(new Date("2024-01-01T00:00:00Z").getTime()) * NANOS_IN_MS,
});
});
});
});

0 comments on commit bc7b3b6

Please sign in to comment.