diff --git a/category-counts/main.ts b/category-counts/main.ts index 2317a78..a5a1210 100644 --- a/category-counts/main.ts +++ b/category-counts/main.ts @@ -1,4 +1,4 @@ -import {bot, log} from "../botbase"; +import {bot, emailOnError, log} from "../botbase"; import {ApiQueryCategoryInfoParams} from "types-mediawiki/api_params"; import {ElasticDataStore} from "../elasticsearch"; import {getKey, normalizeCategory} from "./util"; @@ -40,4 +40,12 @@ import {getKey, normalizeCategory} from "./util"; } } } -})(); + + // Backup data to NFS + process.chdir(__dirname); + fs.writeFileSync( + `backups/category-counts-backup-${new bot.Date().format('YYYY-MM-DD', 'utc')}.json`, + JSON.stringify(countStore.dump(), null, 2) + ); + +})().catch(err => emailOnError(err, 'cat-counts')); diff --git a/elasticsearch.ts b/elasticsearch.ts index 405f2e7..1e56b69 100644 --- a/elasticsearch.ts +++ b/elasticsearch.ts @@ -62,4 +62,18 @@ export class ElasticDataStore { id: id }); } + async dump() { + // XXX: Obviously there should be better ways to do this + const count = await elastic.search({ + index: this.index, + size: 1 + }).then(result => result.body.hits.total.value); + + const hits = await elastic.search({ + index: this.index, + size: count + }).then(result => result.body.hits.hits); + + return Object.fromEntries(hits.map(hit => [hit._id, hit._source])); + } }