diff --git a/fly.toml b/fly.toml index f921ac9..3a4af6e 100644 --- a/fly.toml +++ b/fly.toml @@ -19,7 +19,16 @@ primary_region = 'den' [[vm]] size = 'shared-cpu-2x' +# [mounts] +# source = "usapi_data" +# destination = "/data" +# initial_size = "20gb" +# auto_extend_size_increment = "10gb" +# auto_extend_size_threshold = 80 +# auto_extend_size_limit = "200gb" + [env] +DATA_ROOT_PATH = "/data" FILE_DEFAULT_TTL = "600" S3_ENDPOINT="s3.us-west-002.backblazeb2.com" S3_PORT="443" diff --git a/src/app.module.ts b/src/app.module.ts index 3c75863..26fda79 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -29,6 +29,7 @@ import { APP_INTERCEPTOR } from '@nestjs/core'; import { JunkDrawerModule } from './junk-drawer/junk-drawer.module'; import { EmailModule } from './email/email.module'; import { ContactModule } from './contact/contact.module'; +import { HoardingModule } from './hoarding/hoarding.module'; @Module({ imports: [ @@ -104,6 +105,7 @@ import { ContactModule } from './contact/contact.module'; JunkDrawerModule, EmailModule, ContactModule, + HoardingModule, ], controllers: [AppController], providers: [ diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 3f9c99d..6758aea 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -17,7 +17,7 @@ export default () => ({ channel: process.env.IRC_CHANNEL ?? '#usdev', password: process.env.IRC_PASSWORD ?? '', nick: - process.env.IRC_NICK ?? process.env.NODE_ENV === 'production' + (process.env.IRC_NICK ?? process.env.NODE_ENV === 'production') ? 'us-bot' : 'us-dev', }, @@ -35,6 +35,9 @@ export default () => ({ process.env.FILE_DEFAULT_TTL ?? (30 * 24 * 60 * 60).toString(), ), }, + s3: { + bucketName: process.env.S3_BUCKET ?? 'api.us.dev', + }, focoLive: { airtable: { apiKey: process.env.FOCO_LIVE_AIRTABLE_APIKEY ?? '', diff --git a/src/hoarding/hoarding.module.ts b/src/hoarding/hoarding.module.ts new file mode 100644 index 0000000..1c17c1a --- /dev/null +++ b/src/hoarding/hoarding.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common'; +import { HoardingService } from './hoarding.service'; +import { MinioService } from 'src/minio/minio.service'; + +@Module({ + providers: [HoardingService, MinioService], +}) +export class HoardingModule {} diff --git a/src/hoarding/hoarding.service.ts b/src/hoarding/hoarding.service.ts new file mode 100644 index 0000000..85798c4 --- /dev/null +++ b/src/hoarding/hoarding.service.ts @@ -0,0 +1,38 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { Cron } from '@nestjs/schedule'; +import axios from 'axios'; +import { MinioService } from 'src/minio/minio.service'; + +@Injectable() +export class HoardingService { + private readonly logger: Logger = new Logger(HoardingService.name); + constructor(public readonly minioService: MinioService) { + this.hoardChessStats(); + } + + @Cron('0 0 1 * * *') + async hoardChessStats() { + await this.hoardUserChessStats('letmutchessplayer'); + await this.hoardUserChessStats('archyotype'); + } + + async hoardUserChessStats(user: string) { + this.logger.log(`Hoarding chess.com stats for ${user}`); + const chessState = (id: string) => + `https://api.chess.com/pub/player/${id}/stats`; + const stats = await axios.get(chessState(user)); + + const time = new Date().getTime(); + await this.minioService.uploadBuffer( + this.minioService.defaultBucketName, + `hoarding/chess.com-stats/${user}/${time}-${user}.json`, + Buffer.from( + JSON.stringify({ + time, + data: stats.data, + }), + ), + ); + } +}