Skip to content

Commit

Permalink
category-counts: backup data to NFS as well
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Nov 5, 2024
1 parent d2e1135 commit cedd962
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions category-counts/main.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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'));
14 changes: 14 additions & 0 deletions elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}

0 comments on commit cedd962

Please sign in to comment.