UNPKG

@gameroom/cli

Version:

A command line tool for Gameroom

69 lines (62 loc) 2.54 kB
const cosmetic = require('cosmetic'), Table = require('tabley'), { enums: { timecard_status }, models: { User, Changelog } } = require('@gameroom/kit'), // barHorizontal = require('bar-horizontal'), { join, resolve } = require('path'), { correctionStatus, dateAddDays, dateLocaleString, dateString, getAll, grGreen, printTable, writeCSVFile } = require('../../helpers'), { spinner } = require('../../refs') module.exports = async({ start_at, end_at, days, weeks, id, write }) => { spinner.start() spinner.info(`generating ${grGreen('created units')} report`) spinner.info(`from ${grGreen(start_at.toLocaleString())} to ${grGreen(end_at.toLocaleString())}`) spinner.text = `getting ${grGreen('changelogs')}` const and = [ { key: 'changeable_type', value: 'Unit' }, { key: 'before', value: null }, { key: 'created_at', comparison: '>=', value: start_at }, { key: 'created_at', comparison: '<=', value: end_at }, ] if (id) and.push({ key: 'user_id', value: id }) let changelogs = await Changelog.getAll( { filter: { and } }, (r) => spinner.text = `getting ${grGreen('changelogs')} (${r.length})`, (e) => spinner.warn(`error getting ${grGreen('changelogs')} ${e}`)) spinner.succeed(`got ${changelogs.length} ${grGreen('changelogs')}`) // const data = {} // while (start_at < end_at) { // const d = dateAddDays(start_at, 1) // const day = changelogs.filter(i => start_at < i.created_at.toDate() && i.created_at.toDate() < d) // data[start_at.toLocaleDateString()] = day.length // start_at = d // } // spinner.stop() // barHorizontal(data, { labels: true }) const data = [] while (start_at < end_at) { const d = dateAddDays(start_at, 1) const day = changelogs.filter(i => start_at < i.created_at.toDate() && i.created_at.toDate() < d) data.push({ day: start_at.toLocaleDateString(), count: day.length }) start_at = d } const table = new Table(data, { title: grGreen('Created Units'), align: 'center', columns: [ { key: 'day', title: 'Day' }, { key: 'count', title: 'Created Units' } ], meta: [ { day: 'Total', count: changelogs.length } ], margin: 4 }) spinner.stop() table.print() console.log() spinner.start() if (write) { const path = await writeCSVFile(join(resolve(write === true ? '.' : write), 'gameroom-changelogs-report.csv'), csv_data) spinner.succeed(`wrote data to ${path}`) } spinner.succeed(`generated ${grGreen('created units')} report `).stop() }