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(`Number of clusters: ${options.maxClusters}`)
await saveToDatabase(await getChannels())
await saveToDatabase(await createQueue())
logger.info('Done')
}
main()
async function getChannels() {
logger.info(`Loading channels...`)
async function createQueue() {
logger.info(`Create queue...`)
let channels = {}
let queue = {}
const files = await file.list(options.channels)
for (const filepath of files) {
@ -41,40 +41,37 @@ async function getChannels() {
const [__, region] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null]
const groupId = `${region}/${site}`
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}`
if (!channels[key]) {
const countryCode = item.xmltv_id.split('.')[1]
item.country = countryCode ? countryCode.toUpperCase() : null
item.channelsPath = filepath
if (!queue[key]) {
item.configPath = configPath
item.groups = []
channels[key] = item
queue[key] = item
}
if (!channels[key].groups.includes(groupId)) {
channels[key].groups.push(groupId)
if (!queue[key].groups.includes(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...')
await db.channels.load()
await db.channels.reset()
const chunks = split(shuffle(channels), options.maxClusters)
await db.queue.load()
await db.queue.reset()
const chunks = split(shuffle(items), options.maxClusters)
for (const [i, chunk] of chunks.entries()) {
for (const item of chunk) {
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')
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' }
)
})
it('can create channels database', () => {
const output = content('tests/__data__/output/database/channels.db')
it('can create queue', () => {
const output = content('tests/__data__/output/database/queue.db')
expect(output).toEqual(
expect.arrayContaining([
expect.objectContaining({
lang: 'ru',
country: 'US',
xmltv_id: 'CNNInternationalEurope.us',
site_id: '140',
name: 'CNN International Europe',
site: 'example.com',
channelsPath: 'tests/__data__/input/sites/example.com_ca-nl.channels.xml',
configPath: 'tests/__data__/input/sites/example.com.config.js',
groups: ['ca-nl/example.com'],
cluster_id: 1
@ -33,10 +30,7 @@ it('can create channels database', () => {
lang: 'en',
xmltv_id: 'CNNInternationalEurope2.us',
site_id: '141',
name: 'CNN International Europe 2',
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',
groups: ['ca-nl/example.com'],
cluster_id: 1