Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate loadout shares #258

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 0 additions & 98 deletions api/db/loadout-share-queries.test.ts

This file was deleted.

84 changes: 0 additions & 84 deletions api/db/loadout-share-queries.ts

This file was deleted.

29 changes: 6 additions & 23 deletions api/routes/loadout-share.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import crypto from 'crypto';
import asyncHandler from 'express-async-handler';
import base32 from 'hi-base32';
import { transaction } from '../db/index.js';
import { getLoadoutShare, recordAccess } from '../db/loadout-share-queries.js';
import { metrics } from '../metrics/index.js';
import { ApiApp } from '../shapes/app.js';
import {
Expand Down Expand Up @@ -54,7 +52,7 @@ export const loadoutShareHandler = asyncHandler(async (req, res) => {
});
}

const validationResult = validateLoadout('loadout_share', loadout, appId);
const validationResult = validateLoadout('loadout_share', loadout);
if (validationResult) {
res.status(400).send(validationResult);
return;
Expand Down Expand Up @@ -117,25 +115,10 @@ export const getLoadoutShareHandler = asyncHandler(async (req, res) => {
});

export async function loadLoadoutShare(shareId: string) {
// First look in Stately
try {
const loadout = await getLoadoutShareStately(shareId);
if (loadout) {
// Record when this was viewed and increment the view counter. Not using it much for now but I'd like to know.
await recordAccessStately(shareId);
return loadout;
}
} catch (e) {
console.error('Failed to load loadout share from Stately', e);
}

// Fall back to Postgres
return transaction(async (client) => {
const loadout = await getLoadoutShare(client, shareId);
if (loadout) {
// Record when this was viewed and increment the view counter. Not using it much for now but I'd like to know.
await recordAccess(client, shareId);
}
const loadout = await getLoadoutShareStately(shareId);
if (loadout) {
// Record when this was viewed and increment the view counter. Not using it much for now but I'd like to know.
await recordAccessStately(shareId);
return loadout;
});
}
}
8 changes: 4 additions & 4 deletions api/routes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function validateUpdates(
break;

case 'loadout':
result = validateUpdateLoadout(update.payload, appId);
result = validateUpdateLoadout(update.payload);
break;

case 'tag':
Expand Down Expand Up @@ -514,11 +514,11 @@ async function updateLoadout(
metrics.timing('update.loadout', start);
}

function validateUpdateLoadout(loadout: Loadout, appId: string): ProfileUpdateResult {
return validateLoadout('update', loadout, appId) ?? { status: 'Success' };
function validateUpdateLoadout(loadout: Loadout): ProfileUpdateResult {
return validateLoadout('update', loadout) ?? { status: 'Success' };
}

export function validateLoadout(metricPrefix: string, loadout: Loadout, appId: string) {
export function validateLoadout(metricPrefix: string, loadout: Loadout) {
if (!loadout.name) {
metrics.increment(`${metricPrefix}.validation.loadoutNameMissing.count`);
return {
Expand Down
4 changes: 4 additions & 0 deletions api/shapes/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface ProfileResponse {
/** Hashes of tracked triumphs */
triumphs?: number[];
searches?: Search[];

syncTokens?: {
[key: string]: string;
};
}

/**
Expand Down
6 changes: 6 additions & 0 deletions api/stately/init/migrate-loadout-shares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// import { migrateLoadoutShareChunk } from '../migrator/loadout-shares.js';

// while (true) {
// await migrateLoadoutShareChunk();
// console.log('Migrated loadout shares');
// }
24 changes: 23 additions & 1 deletion api/stately/loadout-share-queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { keyPath, StatelyError } from '@stately-cloud/client';
import { keyPath, StatelyError, WithPutOptions } from '@stately-cloud/client';
import { Loadout } from '../shapes/loadouts.js';
import { client } from './client.js';
import { LoadoutShare as StatelyLoadoutShare } from './generated/index.js';
Expand Down Expand Up @@ -56,6 +56,28 @@ export async function addLoadoutShare(
}
}

/**
* Put loadout shares - this is meant for migrations.
*/
export async function addLoadoutSharesForMigration(
shares: {
platformMembershipId: string;
shareId: string;
loadout: Loadout;
}[],
): Promise<void> {
const statelyShares = shares.map(
({ platformMembershipId, shareId, loadout }): WithPutOptions<StatelyLoadoutShare> => ({
item: convertLoadoutShareToStately(loadout, platformMembershipId, shareId),
// Preserve the original timestamps
overwriteMetadataTimestamps: true,
}),
);

// We overwrite here - shares are immutable, so this is fine.
await client.putBatch(...statelyShares);
}

/**
* Touch the last_accessed_at and visits fields to keep track of access.
*/
Expand Down
2 changes: 2 additions & 0 deletions api/stately/loadouts-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ export function convertLoadoutCommonFieldsToStately(
unequipped: (loadout.unequipped || []).map(convertLoadoutItemToStately),
notes: loadout.notes,
parameters: convertLoadoutParametersToStately(loadout.parameters),
createdAt: BigInt(loadout.createdAt ?? 0n),
lastUpdatedAt: BigInt(loadout.lastUpdatedAt ?? 0n),
};
}

Expand Down
15 changes: 15 additions & 0 deletions api/stately/migrator/loadout-shares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// import { transaction } from '../../db/index.js';
// import { deleteLoadoutShares, getLoadoutShares } from '../../db/loadout-share-queries.js';
// import { addLoadoutSharesForMigration } from '../loadout-share-queries.js';

// export async function migrateLoadoutShareChunk() {
// await transaction(async (db) => {
// const loadouts = await getLoadoutShares(db, 50);
// await Promise.all(loadouts.map((loadout) => addLoadoutSharesForMigration([loadout])));
// console.log('Added to stately');
// await deleteLoadoutShares(
// db,
// loadouts.map((loadout) => loadout.shareId),
// );
// });
// }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@google-cloud/profiler": "^6.0.2",
"@sentry/node": "^7.119.2",
"@sentry/tracing": "^7.114.0",
"@stately-cloud/client": "^0.17.1",
"@stately-cloud/client": "^0.19.0",
"bungie-api-ts": "^5.1.0",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading