Update load-cluster.js

This commit is contained in:
Aleksandr Statciuk 2022-01-30 01:34:09 +03:00
parent b15ed0a4c6
commit 0161ac02d0
3 changed files with 23 additions and 30 deletions

View file

@ -26,16 +26,16 @@ async function main() {
logger.info(`Loading cluster: ${options.clusterId}`)
logger.info(`Creating '${clusterLog}'...`)
await file.create(clusterLog)
await db.channels.load()
const channels = await db.channels.find({ cluster_id: options.clusterId })
const total = options.days * channels.length
await db.queue.load()
const items = await db.queue.find({ cluster_id: options.clusterId })
const total = options.days * items.length
logger.info(`Total ${total} requests`)
logger.info('Loading...')
const results = {}
let i = 1
for (const channel of channels) {
let config = require(file.resolve(channel.configPath))
for (const item of items) {
let config = require(file.resolve(item.configPath))
config = _.merge(config, {
days: options.days,
@ -46,11 +46,11 @@ async function main() {
}
})
await grabber.grab(channel, config, async (data, err) => {
await grabber.grab(item, config, async (data, err) => {
logger.info(
`[${i}/${total}] ${channel.site} - ${channel.xmltv_id} - ${data.date.format(
'MMM D, YYYY'
)} (${data.programs.length} programs)`
`[${i}/${total}] ${item.site} - ${item.xmltv_id} - ${data.date.format('MMM D, YYYY')} (${
data.programs.length
} programs)`
)
if (err) logger.error(err.message)
@ -68,7 +68,7 @@ async function main() {
})
}
db.channels.compact()
db.queue.compact()
logger.info(`Done in ${timer.format('HH[h] mm[m] ss[s]')}`)
}

View file

@ -0,0 +1,2 @@
{"channel":{"lang":"fr","xmltv_id":"CNNInternationalEurope.us","site_id":"53","site":"chaines-tv.orange.fr","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["fr/chaines-tv.orange.fr","bh/chaines-tv.orange.fr"],"cluster_id":1,"programCount":32,"_id":"0Wefq0oMR3feCcuY"},"programs":[],"date":"2022-01-29T00:00:00Z","error":null}
{"channel":{"lang":"ru","xmltv_id":"CNNInternationalEurope.us","site_id":"140","site":"magticom.ge","configPath":"tests/__data__/input/sites/example.com.config.js","groups":["ge/magticom.ge"],"cluster_id":1,"programCount":0,"_id":"1XzrxNkSF2AQNBrT"},"programs":[],"date":"2022-01-29T00:00:00Z","error":null}

View file

@ -11,10 +11,7 @@ beforeEach(() => {
fs.rmdirSync('tests/__data__/output', { recursive: true })
fs.mkdirSync('tests/__data__/output')
fs.mkdirSync('tests/__data__/temp/database', { recursive: true })
fs.copyFileSync(
'tests/__data__/input/database/channels.db',
'tests/__data__/temp/database/channels.db'
)
fs.copyFileSync('tests/__data__/input/database/queue.db', 'tests/__data__/temp/database/queue.db')
execSync(
'DB_DIR=tests/__data__/temp/database LOGS_DIR=tests/__data__/output/logs node scripts/commands/load-cluster.js --cluster-id=1 --timeout=10000',
@ -23,25 +20,19 @@ beforeEach(() => {
})
it('can load cluster', () => {
const output = content('tests/__data__/output/logs/load-cluster/cluster_1.log')
let output = content('tests/__data__/output/logs/load-cluster/cluster_1.log')
let expected = content('tests/__data__/expected/logs/load-cluster/cluster_1.log')
expect(Object.keys(output[0]).sort()).toEqual(['channel', 'date', 'error', 'programs'])
expect(output[0]).toMatchObject({
channel: {
_id: '0Wefq0oMR3feCcuY',
logo: 'https://example.com/logo.png'
},
date: dayjs.utc().startOf('d').format(),
error: null
output = output.map(i => {
i.date = null
return i
})
expected = expected.map(i => {
i.date = null
return i
})
expect(output[1]).toMatchObject({
channel: {
_id: '1XzrxNkSF2AQNBrT',
logo: 'https://www.magticom.ge/images/channels/MjAxOC8wOS8xMC9lZmJhNWU5Yy0yMmNiLTRkMTAtOWY5Ny01ODM0MzY0ZTg0MmEuanBn.jpg'
}
})
expect(output).toEqual(expected)
})
function content(filepath) {