traxxx/src/tools/alerts-save.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-06-08 00:37:33 +00:00
'use strict';
const fs = require('fs');
const moment = require('moment');
const knex = require('../knex');
async function save() {
const filename = `alerts-${moment().format('YYYY-MM-DD_hh_mm')}.json`;
const alerts = await knex('alerts')
.select([
'alerts.*',
'users.username',
'actors.slug as actor_slug',
'actors_entities.slug as actor_entity_slug',
'actors_entities.type as actor_entity_type',
'entities.slug as entity_slug',
'entities.type as entity_type',
'stashes.slug as stash_slug',
'tags.slug as tag_slug',
])
.leftJoin('users', 'users.id', 'alerts.user_id')
.leftJoin('alerts_actors', 'alerts_actors.alert_id', 'alerts.id')
.leftJoin('alerts_entities', 'alerts_entities.alert_id', 'alerts.id')
.leftJoin('alerts_stashes', 'alerts_stashes.alert_id', 'alerts.id')
.leftJoin('alerts_tags', 'alerts_tags.alert_id', 'alerts.id')
.leftJoin('actors', 'actors.id', 'alerts_actors.actor_id')
.leftJoin('entities', 'entities.id', 'alerts_entities.entity_id')
.leftJoin('tags', 'tags.id', 'alerts_tags.tag_id')
.leftJoin('stashes', 'stashes.id', 'alerts_stashes.stash_id')
.leftJoin('entities as actors_entities', 'actors_entities.id', 'actors.entity_id');
let savedAlerts = 0;
await alerts.reduce(async (chain, alert) => {
await chain;
const curatedAlert = JSON.stringify({
username: alert.username,
notify: alert.notify,
email: alert.email,
createdAt: alert.created_at,
actorSlug: alert.actor_slug,
actorEntitySlug: alert.actor_entity_slug,
actorEntityType: alert.actor_entity_type,
entitySlug: alert.entity_slug,
entityType: alert.entity_type,
stashSlug: alert.stash_slug,
tagSlug: alert.tag_slug,
});
await fs.promises.appendFile(filename, `${curatedAlert}\n`);
console.log(`Saved ${alert.username} alert`);
savedAlerts += 1;
}, Promise.resolve());
console.log(`Saved ${savedAlerts} alerts to ${filename}`);
process.exit();
}
save();