Skip to content

Commit

Permalink
fix: last seen deadlocks (#5264) (#5266)
Browse files Browse the repository at this point in the history
Patch unleash last seen at logic
  • Loading branch information
FredrikOseberg authored Nov 3, 2023
1 parent 9ee3be8 commit d0b820b
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/lib/services/client-metrics/last-seen/last-seen-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ export interface FeaturesTable {
environment: string;
}

const prepareLastSeenInput = (data: LastSeenInput[]) => {
const now = new Date();

const sortedData = data.sort(
(a, b) =>
a.featureName.localeCompare(b.featureName) ||
a.environment.localeCompare(b.environment),
);

const inserts = sortedData.map((item) => {
return {
feature_name: item.featureName,
environment: item.environment,
last_seen_at: now,
};
});

return inserts;
};

export default class LastSeenStore implements ILastSeenStore {
private db: Db;

Expand All @@ -32,18 +52,10 @@ export default class LastSeenStore implements ILastSeenStore {
}

async setLastSeen(data: LastSeenInput[]): Promise<void> {
const now = new Date();

try {
const inserts = data.map((item) => {
return {
feature_name: item.featureName,
environment: item.environment,
last_seen_at: now,
};
});
const inserts = prepareLastSeenInput(data);

const batchSize = 1000;
const batchSize = 500;

for (let i = 0; i < inserts.length; i += batchSize) {
const batch = inserts.slice(i, i + batchSize);
Expand Down

0 comments on commit d0b820b

Please sign in to comment.