Skip to content

Commit

Permalink
fix: normalize timestamp and only return intersection of snapshots wh…
Browse files Browse the repository at this point in the history
…ere each artist is represented (#2)
  • Loading branch information
brandongregoryscott authored Aug 24, 2024
1 parent 5076670 commit f90cb8f
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions apps/api/src/services/artist-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
ArtistSnapshot,
ArtistSnapshotRow,
} from "@repo/common";
import { Artist } from "@repo/common";
import { getDb } from "../database";
import {
fromUnixTime,
Expand All @@ -12,8 +11,9 @@ import {
getQuarter,
getWeek,
getYear,
startOfDay,
} from "date-fns";
import { uniqBy } from "lodash";
import { countBy, uniqBy } from "lodash";

interface ListArtistSnapshotsOptions {
ids: string[];
Expand Down Expand Up @@ -82,43 +82,37 @@ const ArtistService = {

switch (resolution) {
case "yearly":
snapshots = uniqBy(
return intersectionByNormalizedTimestamp(
ids.length,
snapshots,
(snapshot) =>
`${snapshot.id}_${getYear(snapshot.timestamp)}`
getYear
);
break;
case "quarterly":
snapshots = uniqBy(
return intersectionByNormalizedTimestamp(
ids.length,
snapshots,
(snapshot) =>
`${snapshot.id}_${getQuarter(snapshot.timestamp)}`
getQuarter
);
break;
case "monthly":
snapshots = uniqBy(
return intersectionByNormalizedTimestamp(
ids.length,
snapshots,
(snapshot) =>
`${snapshot.id}_${getMonth(snapshot.timestamp)}`
getMonth
);
break;
case "daily":
snapshots = uniqBy(
return intersectionByNormalizedTimestamp(
ids.length,
snapshots,
(snapshot) =>
`${snapshot.id}_${getDayOfYear(snapshot.timestamp)}`
getDayOfYear
);
break;
case "weekly":
default:
snapshots = uniqBy(
return intersectionByNormalizedTimestamp(
ids.length,
snapshots,
(snapshot) =>
`${snapshot.id}_${getWeek(snapshot.timestamp)}`
getWeek
);
}

return snapshots;
},
};

Expand All @@ -127,8 +121,26 @@ const queryPlaceholder = (count: number): string =>

const normalizeArtistSnapshot = (row: ArtistSnapshotRow): ArtistSnapshot => ({
...row,
timestamp: fromUnixTime(row.timestamp).toISOString(),
timestamp: startOfDay(fromUnixTime(row.timestamp)).toISOString(),
});

const intersectionByNormalizedTimestamp = (
count: number,
snapshots: ArtistSnapshot[],
getInterval: (timestamp: string) => number
): ArtistSnapshot[] => {
const countByTimestamp = countBy(
snapshots,
(snapshot) => snapshot.timestamp
);

return uniqBy(
snapshots.filter(
(snapshot) => countByTimestamp[snapshot.timestamp] === count
),
(snapshot) => `${snapshot.id}_${getInterval(snapshot.timestamp)}`
);
};

export type { Resolution };
export { ArtistService };

0 comments on commit f90cb8f

Please sign in to comment.