Replace with queue.db

This commit is contained in:
Aleksandr Statciuk 2022-01-30 01:01:15 +03:00
parent 217144cf91
commit 12034df995
2 changed files with 20 additions and 29 deletions

View file

@ -17,17 +17,17 @@ async function main() {
logger.info('Starting...') logger.info('Starting...')
logger.info(`Number of clusters: ${options.maxClusters}`) logger.info(`Number of clusters: ${options.maxClusters}`)
await saveToDatabase(await getChannels()) await saveToDatabase(await createQueue())
logger.info('Done') logger.info('Done')
} }
main() main()
async function getChannels() { async function createQueue() {
logger.info(`Loading channels...`) logger.info(`Create queue...`)
let channels = {} let queue = {}
const files = await file.list(options.channels) const files = await file.list(options.channels)
for (const filepath of files) { for (const filepath of files) {
@ -41,40 +41,37 @@ async function getChannels() {
const [__, region] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null] const [__, region] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null]
const groupId = `${region}/${site}` const groupId = `${region}/${site}`
for (const item of items) { for (const item of items) {
if (!item.site || !item.site_id || !item.xmltv_id || !item.name) continue if (!item.site || !item.site_id || !item.xmltv_id) continue
const key = `${item.site}:${item.site_id}` const key = `${item.site}:${item.site_id}`
if (!channels[key]) { if (!queue[key]) {
const countryCode = item.xmltv_id.split('.')[1]
item.country = countryCode ? countryCode.toUpperCase() : null
item.channelsPath = filepath
item.configPath = configPath item.configPath = configPath
item.groups = [] item.groups = []
channels[key] = item queue[key] = item
} }
if (!channels[key].groups.includes(groupId)) { if (!queue[key].groups.includes(groupId)) {
channels[key].groups.push(groupId) queue[key].groups.push(groupId)
} }
} }
} }
channels = Object.values(channels) queue = Object.values(queue)
logger.info(`Found ${channels.length} channels`) logger.info(`Found ${queue.length} items`)
return channels return queue
} }
async function saveToDatabase(channels = []) { async function saveToDatabase(items = []) {
logger.info('Saving to the database...') logger.info('Saving to the database...')
await db.channels.load() await db.queue.load()
await db.channels.reset() await db.queue.reset()
const chunks = split(shuffle(channels), options.maxClusters) const chunks = split(shuffle(items), options.maxClusters)
for (const [i, chunk] of chunks.entries()) { for (const [i, chunk] of chunks.entries()) {
for (const item of chunk) { for (const item of chunk) {
item.cluster_id = i + 1 item.cluster_id = i + 1
await db.channels.insert(item) await db.queue.insert(item)
} }
} }
} }

View file

@ -7,24 +7,21 @@ beforeEach(() => {
fs.mkdirSync('tests/__data__/output') fs.mkdirSync('tests/__data__/output')
const stdout = execSync( const stdout = execSync(
'DB_DIR=tests/__data__/output/database node scripts/commands/create-database.js --channels=tests/__data__/input/sites/*.channels.xml --max-clusters=1', 'DB_DIR=tests/__data__/output/database node scripts/commands/create-queue.js --channels=tests/__data__/input/sites/*.channels.xml --max-clusters=1',
{ encoding: 'utf8' } { encoding: 'utf8' }
) )
}) })
it('can create channels database', () => { it('can create queue', () => {
const output = content('tests/__data__/output/database/channels.db') const output = content('tests/__data__/output/database/queue.db')
expect(output).toEqual( expect(output).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
lang: 'ru', lang: 'ru',
country: 'US',
xmltv_id: 'CNNInternationalEurope.us', xmltv_id: 'CNNInternationalEurope.us',
site_id: '140', site_id: '140',
name: 'CNN International Europe',
site: 'example.com', site: 'example.com',
channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml',
configPath: 'tests/__data__/input/sites/example.com.config.js', configPath: 'tests/__data__/input/sites/example.com.config.js',
groups: ['ca-nl/example.com'], groups: ['ca-nl/example.com'],
cluster_id: 1 cluster_id: 1
@ -33,10 +30,7 @@ it('can create channels database', () => {
lang: 'en', lang: 'en',
xmltv_id: 'CNNInternationalEurope2.us', xmltv_id: 'CNNInternationalEurope2.us',
site_id: '141', site_id: '141',
name: 'CNN International Europe 2',
site: 'example.com', site: 'example.com',
country: 'US',
channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml',
configPath: 'tests/__data__/input/sites/example.com.config.js', configPath: 'tests/__data__/input/sites/example.com.config.js',
groups: ['ca-nl/example.com'], groups: ['ca-nl/example.com'],
cluster_id: 1 cluster_id: 1