Make data archives more accessible, and store only one file per hour

This commit is contained in:
Matt Isenhower 2023-05-23 17:48:30 -07:00
parent ec2e450c25
commit ba54d94e09
5 changed files with 40 additions and 10 deletions

View File

@ -5,8 +5,26 @@ export function getTopOfCurrentHour(date = null) {
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return Math.trunc(date.getTime() / 1000) * 1000;
return date;
}
function leadingZero(value) {
return value.toString().padStart(2, '0');
}
export function getDateParts(date = null) {
date ??= new Date;
return {
year: date.getUTCFullYear(),
month: leadingZero(date.getUTCMonth() + 1),
day: leadingZero(date.getUTCDate()),
hour: leadingZero(date.getUTCHours()),
minute: leadingZero(date.getUTCMinutes()),
second: leadingZero(date.getUTCSeconds()),
};
}
export function getGearIcon(gear) {

View File

@ -9,16 +9,17 @@ import ImageProcessor from '../ImageProcessor.mjs';
import NsoClient from '../../splatnet/NsoClient.mjs';
import { locales, regionalLocales, defaultLocale } from '../../../src/common/i18n.mjs';
import { LocalizationProcessor } from '../LocalizationProcessor.mjs';
import { deriveId } from '../../common/util.mjs';
import { deriveId, getDateParts, getTopOfCurrentHour } from '../../common/util.mjs';
export default class DataUpdater
{
name = null;
filename = null;
directory = null;
calendarName = null;
calendarFilename = null;
outputDirectory = 'dist/data';
archiveOutputDirectory = 'storage/archive';
archiveOutputDirectory = 'dist/data/archive';
imagePaths = [];
derivedIds = [];
@ -168,16 +169,25 @@ export default class DataUpdater
await this.writeFile(this.getPath(this.filename), s);
// Write a secondary file for archival
let filename = `${this.filename}.${Date.now()}`;
await this.writeFile(this.getArchivePath(filename), s);
await this.writeFile(this.getArchivePath(this.filename), s);
}
getPath(filename) {
return `${this.outputDirectory}/${filename}.json`;
return `${this.outputDirectory}/${this.directory}/${filename}.json`;
}
getArchivePath(filename) {
return `${this.archiveOutputDirectory}/${filename}.json`;
// We only want to store one file per hour, so start with the top of the current hour
let date = getTopOfCurrentHour();
let { year, month, day, hour, minute, second } = getDateParts(date);
return [
this.archiveOutputDirectory,
year,
month,
day,
`${year}-${month}-${day}.${hour}-${minute}-${second}.${filename}.json`,
].join('/');
}
async formatDataForWrite(data) {

View File

@ -6,7 +6,8 @@ import DataUpdater from "./DataUpdater.mjs";
export default class XRankDetailUpdater extends DataUpdater
{
name = 'X-Rank Detail';
filename = 'xrank/xrank.detail';
filename = 'xrank.detail';
directory = 'xrank';
imagePaths = [
'$..image.url',

View File

@ -7,7 +7,8 @@ import XRankDetailUpdater from "./XRankDetailUpdater.mjs";
export default class XRankUpdater extends DataUpdater
{
name = 'X-Rank';
filename = 'xrank/xrank';
filename = 'xrank';
directory = 'xrank';
imagePaths = [
'$..image.url',

View File

@ -32,7 +32,7 @@ export default class StatusGenerator
setActivePinia(createPinia());
useTimeStore().setNow(getTopOfCurrentHour());
useTimeStore().setNow(getTopOfCurrentHour().getTime());
useSchedulesDataStore().setData(JSON.parse(await fs.readFile('dist/data/schedules.json')));
useGearDataStore().setData(JSON.parse(await fs.readFile('dist/data/gear.json')));