More detailed logs

This commit is contained in:
Aleksandr Statciuk 2022-01-31 06:55:05 +03:00
parent 16ca23efe5
commit 9367c061e9
9 changed files with 60 additions and 40 deletions

View file

@ -56,7 +56,7 @@ async function createQueue() {
site_id: item.site_id,
lang: item.lang,
date: undefined,
error: 'Wrong channel ID'
error: 'The channel has the wrong xmltv_id'
})
continue
}

View file

@ -16,19 +16,17 @@ main()
async function generateGuides() {
logger.info(`Generating guides/...`)
const grouped = groupByGroup(await loadQueue())
logger.info('Loading "database/programs.db"...')
await db.programs.load()
await api.channels.load()
const grouped = groupByGroup(await loadQueue())
for (const key in grouped) {
const [__, site] = key.split('/')
const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml`
let items = grouped[key]
const errors = []
for (const item of items) {
const criticalErrors = []
const channels = []
let programs = []
for (const item of grouped[key]) {
if (item.error) {
const error = {
xmltv_id: item.channel.xmltv_id,
@ -38,34 +36,52 @@ async function generateGuides() {
date: item.date,
error: item.error
}
errors.push(error)
}
}
await logErrors(key, errors)
criticalErrors.push(error)
await logError(key, error)
} else {
const itemPrograms = await loadProgramsForItem(item)
if (!itemPrograms.length) {
await logError(key, {
xmltv_id: item.channel.xmltv_id,
site: item.channel.site,
site_id: item.channel.site_id,
lang: item.channel.lang,
date: item.date,
error: 'Programs not found'
})
continue
}
const programs = await loadProgramsForItems(items)
let channels = Object.keys(_.groupBy(programs, 'channel'))
const channel = api.channels.find({ id: item.channel.xmltv_id })
if (!channel) {
await logError(key, {
xmltv_id: item.channel.xmltv_id,
site: item.channel.site,
site_id: item.channel.site_id,
lang: item.channel.lang,
date: item.date,
error: 'The channel has the wrong xmltv_id'
})
continue
}
logger.info(`Creating "${filepath}"...`)
channels = channels
.map(id => {
const channel = api.channels.find({ id })
if (!channel) return null
return {
channels.push({
xmltv_id: channel.id,
name: channel.name,
logo: channel.logo,
site
}
})
.filter(i => i)
site: item.channel.site
})
programs = programs.concat(itemPrograms)
}
}
logger.info(`Creating "${filepath}"...`)
const output = grabber.convertToXMLTV({ channels, programs })
await file.create(filepath, output)
let status = 0
if (errors.length > 0 || !channels.length) {
if (criticalErrors.length > 0 || !channels.length) {
status = 1
}
@ -103,10 +119,8 @@ async function loadQueue() {
return await db.queue.find({}).sort({ xmltv_id: 1 })
}
async function loadProgramsForItems(items = []) {
const qids = items.map(i => i._id)
return await db.programs.find({ _qid: { $in: qids } }).sort({ channel: 1, start: 1 })
async function loadProgramsForItem(item) {
return await db.programs.find({ _qid: item._id }).sort({ channel: 1, start: 1 })
}
async function setUp() {
@ -119,9 +133,11 @@ async function logGuide(data) {
await file.append(GUIDES_PATH, JSON.stringify(data) + '\r\n')
}
async function logErrors(key, errors) {
if (!errors.length) return false
errors = errors.map(e => JSON.stringify(e)).join('\r\n')
async function logError(key, data) {
const filepath = `${LOGS_DIR}/errors/${key}.log`
if (!(await file.exists(filepath))) {
await file.create(filepath)
}
await file.create(`${LOGS_DIR}/errors/${key}.log`, errors)
await file.append(filepath, JSON.stringify(data) + '\r\n')
}

View file

@ -1,2 +0,0 @@
{"xmltv_id":"BravoEast.us","site":"directv.com","site_id":"237","lang":"en","date":"2022-01-21T00:00:00Z","error":"Invalid header value char"}
{"xmltv_id":"Perviykanal.ru","site":"yandex.ru","site_id":"1","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Some error"}

View file

@ -1 +1 @@
{"xmltv_id":"CNNInternationalEurope2.us","site":"example.com","site_id":"141","lang":"en","error":"Wrong channel ID"}
{"xmltv_id":"CNNInternationalEurope2.us","site":"example.com","site_id":"141","lang":"en","error":"The channel has the wrong xmltv_id"}

View file

@ -0,0 +1 @@
{"xmltv_id":"CNNInternationalEurope.us","site":"magticom.ge","site_id":"140","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Programs not found"}

View file

@ -1 +1 @@
{"xmltv_id":"Perviykanal.ru","site":"yandex.ru","site_id":"1","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Some error"}
{"xmltv_id":"Perviykanal.ru","site":"yandex.ru","site_id":"1","lang":"ru","date":"2022-01-21T00:00:00Z","error":"Some error"}

View file

@ -1 +1 @@
{"xmltv_id":"BravoEast.us","site":"directv.com","site_id":"237","lang":"en","date":"2022-01-21T00:00:00Z","error":"Invalid header value char"}
{"xmltv_id":"BravoEast.us","site":"directv.com","site_id":"237","lang":"en","date":"2022-01-21T00:00:00Z","error":"Invalid header value char"}

View file

@ -35,7 +35,7 @@ it('can create queue', () => {
)
})
it('can create errors log', () => {
it('can log errors', () => {
let output = content('tests/__data__/output/logs/errors/ca-nl/example.com.log')
let expected = content('tests/__data__/expected/logs/errors/ca-nl/example.com.log')

View file

@ -51,6 +51,11 @@ it('can log errors', () => {
const expected2 = content('tests/__data__/expected/logs/errors/us/directv.com.log')
expect(output2).toBe(expected2)
const output3 = content('tests/__data__/output/logs/errors/ge/magticom.ge.log')
const expected3 = content('tests/__data__/expected/logs/errors/ge/magticom.ge.log')
expect(output3).toBe(expected3)
})
function content(filepath) {