Only update festival ranking data when needed

This commit is contained in:
Matt Isenhower 2022-11-15 13:44:39 -05:00
parent c14e4a0502
commit a34ea396f6
4 changed files with 46 additions and 4 deletions

View File

@ -28,7 +28,7 @@ export async function updateAll() {
for (let updater of updaters()) {
try {
await updater.update();
await updater.updateIfNeeded();
} catch (e) {
console.error(e);
}

View File

@ -54,6 +54,19 @@ export default class DataUpdater
return new SplatNet3Client(this.nsoClient, locale.code);
}
async shouldUpdate() {
return true;
}
async updateIfNeeded() {
if (!(await this.shouldUpdate())) {
this.console.info('No need to update data');
return;
}
return await this.update();
}
async update() {
this.console.info('Updating data...');

View File

@ -1,3 +1,5 @@
import fs from 'fs/promises';
import prefixedConsole from "../../common/prefixedConsole.mjs";
import DataUpdater from "./DataUpdater.mjs";
export default class FestivalRankingUpdater extends DataUpdater
@ -5,10 +7,11 @@ export default class FestivalRankingUpdater extends DataUpdater
name = 'FestivalRankingUpdater';
filename = 'festivals.ranking';
constructor(region = null, festID = null) {
constructor(region = null, festID = null, endTime = null) {
super(region);
this.festID = festID;
this.endTime = endTime;
this.filename += `.${region}.${festID}`;
}
@ -21,6 +24,32 @@ export default class FestivalRankingUpdater extends DataUpdater
'$..image3dThumbnail.url',
];
get console() {
this._console ??= prefixedConsole('Updater', this.region, this.name, this.festID);
return this._console;
}
async shouldUpdate() {
// Does the file exist?
try {
await fs.readFile(this.getPath(this.filename));
} catch (e) {
// File doesn't exist or other error, so we need to download the data
return true;
}
// How long until this festival ends/ended?
// We want to update this data until 4 hours after the Splatfest ends
let diff = Date.now() - new Date(this.endTime);
if (diff < 4 * 60 * 60 * 100) {
return true;
}
// Otherwise, no need to update
return false;
}
getData(locale) {
return this.splatnet(locale).getFestRankingData(this.festID);
}

View File

@ -40,8 +40,8 @@ export default class FestivalUpdater extends DataUpdater
Object.assign(node, detailResult.data.fest);
let rankingUpdater = new FestivalRankingUpdater(this.region, node.id);
await rankingUpdater.update();
let rankingUpdater = new FestivalRankingUpdater(this.region, node.id, node.endTime);
await rankingUpdater.updateIfNeeded();
}
return result;