Create update-guides.log

This commit is contained in:
Aleksandr Statciuk 2022-01-14 20:37:21 +03:00
parent 3e4a24b42f
commit 61dbd2a1b3
2 changed files with 31 additions and 5 deletions

View file

@ -2,9 +2,12 @@ const { db, logger, file, xml } = require('../core')
const _ = require('lodash') const _ = require('lodash')
const DB_DIR = process.env.DB_DIR || 'scripts/database' const DB_DIR = process.env.DB_DIR || 'scripts/database'
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs'
const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages' const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages'
async function main() { async function main() {
await setUp()
await generateGuides() await generateGuides()
} }
@ -15,11 +18,12 @@ async function generateGuides() {
const channels = await db.channels.find({}).sort({ xmltv_id: 1 }) const channels = await db.channels.find({}).sort({ xmltv_id: 1 })
const programs = await loadPrograms() const programs = await loadPrograms()
const grouped = _.groupBy(programs, i => `${i.gid}/${i.site}.epg.xml`) const grouped = _.groupBy(programs, i => `${i.gid}_${i.site}`)
for (let relativePath in grouped) { for (let key in grouped) {
const filepath = `${PUBLIC_DIR}/guides/${relativePath}` const [gid, site] = key.split('_') || [null, null]
const groupProgs = grouped[relativePath] const filepath = `${PUBLIC_DIR}/guides/${gid}/${site}.epg.xml`
const groupProgs = grouped[key]
const groupChannels = Object.keys(_.groupBy(groupProgs, i => `${i.site}_${i.channel}`)).map( const groupChannels = Object.keys(_.groupBy(groupProgs, i => `${i.site}_${i.channel}`)).map(
key => { key => {
let [site, channel] = key.split('_') let [site, channel] = key.split('_')
@ -31,6 +35,12 @@ async function generateGuides() {
const output = xml.create({ channels: groupChannels, programs: groupProgs }) const output = xml.create({ channels: groupChannels, programs: groupProgs })
await file.create(filepath, output) await file.create(filepath, output)
await log({
gid,
filepath,
count: groupChannels.length
})
} }
} }
@ -56,3 +66,14 @@ async function loadPrograms() {
return programs return programs
} }
async function setUp() {
const logPath = `${LOGS_DIR}/update-guides.log`
logger.info(`Creating '${logPath}'...`)
await file.create(logPath)
}
async function log(data) {
await file.append(`${LOGS_DIR}/update-guides.log`, JSON.stringify(data) + '\n')
}

View file

@ -16,7 +16,7 @@ beforeEach(() => {
) )
execSync( execSync(
'DB_DIR=tests/__data__/temp/database PUBLIC_DIR=tests/__data__/output node scripts/commands/update-guides.js', 'DB_DIR=tests/__data__/temp/database PUBLIC_DIR=tests/__data__/output LOGS_DIR=tests/__data__/output/logs node scripts/commands/update-guides.js',
{ encoding: 'utf8' } { encoding: 'utf8' }
) )
}) })
@ -35,6 +35,11 @@ it('can generate /guides', () => {
const expected2 = content('tests/__data__/expected/guides/za/dstv.com.epg.xml') const expected2 = content('tests/__data__/expected/guides/za/dstv.com.epg.xml')
expect(output2).toBe(expected2) expect(output2).toBe(expected2)
const output3 = content('tests/__data__/output/logs/update-guides.log')
const expected3 = content('tests/__data__/expected/logs/update-guides.log')
expect(output3).toBe(expected3)
}) })
function content(filepath) { function content(filepath) {