Skip to content

Commit

Permalink
Further consolidate search deletes with other search commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Dec 21, 2024
1 parent 509e2fe commit 25dac9b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
41 changes: 26 additions & 15 deletions api/routes/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { captureMessage } from '@sentry/node';
import { chunk, groupBy, sortBy } from 'es-toolkit';
import { chunk, groupBy, partition, sortBy } from 'es-toolkit';
import { isEmpty } from 'es-toolkit/compat';
import express from 'express';
import asyncHandler from 'express-async-handler';
Expand Down Expand Up @@ -286,8 +286,9 @@ async function statelyUpdate(
platformMembershipId: string | undefined,
destinyVersion: DestinyVersion,
) {
// We want to group save search and search updates together
const actionKey = (u: ProfileUpdate) => (u.action === 'save_search' ? 'search' : u.action);
// We want to group save/delete search and search updates together
const actionKey = (u: ProfileUpdate) =>
u.action === 'save_search' || u.action === 'delete_search' ? 'search' : u.action;

const sortedUpdates = sortBy(updates, [actionKey]).flatMap((u): ProfileUpdate[] => {
// Separate out tag_cleanup updates into individual updates
Expand Down Expand Up @@ -382,20 +383,22 @@ async function statelyUpdate(
// saved searches and used searches are collectively "searches"
case 'search': {
const searchUpdates = consolidateSearchUpdates(
group as (UsedSearchUpdate | SavedSearchUpdate)[],
group as (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[],
);
await updateSearches(txn, platformMembershipId!, destinyVersion, searchUpdates);
const [deletes, updates] = partition(searchUpdates, (u) => u.deleted);
if (deletes.length) {
await deleteSearchInStately(
txn,
platformMembershipId!,
destinyVersion,
deletes.map((u) => u.query),
);
}
if (updates.length) {
await updateSearches(txn, platformMembershipId!, destinyVersion, updates);
}
break;
}

case 'delete_search':
await deleteSearchInStately(
txn,
platformMembershipId!,
destinyVersion,
(group as DeleteSearchUpdate[]).map((u) => u.payload.query),
);
break;
}
}
});
Expand Down Expand Up @@ -815,18 +818,26 @@ async function updateItemHashTag(
metrics.timing('update.updateItemHashTag', start);
}

function consolidateSearchUpdates(updates: (UsedSearchUpdate | SavedSearchUpdate)[]) {
function consolidateSearchUpdates(
updates: (UsedSearchUpdate | SavedSearchUpdate | DeleteSearchUpdate)[],
) {
const updatesByQuery = groupBy(updates, (u) => u.payload.query);
return Object.values(updatesByQuery).map((group) => {
const u: UpdateSearch = {
query: group[0].payload.query,
type: group[0].payload.type ?? SearchType.Item,
incrementUsed: 0,
deleted: false,
};
for (const update of group) {
if (update.action === 'save_search') {
u.deleted = false;
u.saved = update.payload.saved;
} else if (update.action === 'delete_search') {
u.deleted = true;
u.incrementUsed = 0;
} else {
u.deleted = false;
u.incrementUsed++;
}
}
Expand Down
32 changes: 28 additions & 4 deletions api/stately/bulk-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ describe('deleteAllDataForUser', () => {
});
await updateLoadout(txn, platformMembershipId, 2, [loadout]);
await updateSearches(txn, platformMembershipId, 1, [
{ query: 'is:handcannon', type: SearchType.Item, incrementUsed: 1, saved: false },
{
query: 'is:handcannon',
type: SearchType.Item,
incrementUsed: 1,
saved: false,
deleted: false,
},
]);
await updateSearches(txn, platformMembershipId, 2, [
{ query: 'tag:junk', type: SearchType.Item, incrementUsed: 1, saved: false },
{
query: 'tag:junk',
type: SearchType.Item,
incrementUsed: 1,
saved: false,
deleted: false,
},
]);
await trackUntrackTriumphs(txn, platformMembershipId, [
{ recordHash: 3851137658, tracked: true },
Expand Down Expand Up @@ -92,10 +104,22 @@ describe('exportDataForUser', () => {
});
await updateLoadout(txn, platformMembershipId, 2, [loadout]);
await updateSearches(txn, platformMembershipId, 1, [
{ query: 'is:handcannon', type: SearchType.Item, incrementUsed: 1, saved: false },
{
query: 'is:handcannon',
type: SearchType.Item,
incrementUsed: 1,
saved: false,
deleted: false,
},
]);
await updateSearches(txn, platformMembershipId, 2, [
{ query: 'tag:junk', type: SearchType.Item, incrementUsed: 1, saved: false },
{
query: 'tag:junk',
type: SearchType.Item,
incrementUsed: 1,
saved: false,
deleted: false,
},
]);
await trackUntrackTriumphs(txn, platformMembershipId, [
{ recordHash: 3851137658, tracked: true },
Expand Down
2 changes: 2 additions & 0 deletions api/stately/searches-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function updateUsedSearch(
type,
saved: false,
incrementUsed: 1,
deleted: false,
},
]);
});
Expand All @@ -45,6 +46,7 @@ async function saveSearch(
type,
saved,
incrementUsed: 0,
deleted: false,
},
]);
});
Expand Down
1 change: 1 addition & 0 deletions api/stately/searches-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export interface UpdateSearch {
saved?: boolean;
/** How much to increment the used count by. */
incrementUsed: number;
deleted: boolean;
}

/**
Expand Down

0 comments on commit 25dac9b

Please sign in to comment.