2020-06-08 01:41:12 +00:00
'use strict' ;
const config = require ( 'config' ) ;
const logger = require ( './logger' ) ( _ _filename ) ;
const argv = require ( './argv' ) ;
const knex = require ( './knex' ) ;
const whereOr = require ( './utils/where-or' ) ;
function curateEntity ( entity , includeParameters = false ) {
const curatedEntity = {
id : entity . id ,
name : entity . name ,
url : entity . url ,
description : entity . description ,
slug : entity . slug ,
2020-06-15 01:58:35 +00:00
type : entity . type ,
2020-06-08 01:41:12 +00:00
parameters : includeParameters ? entity . parameters : null ,
2020-06-27 22:15:13 +00:00
parent : entity . parent _id && entity . parent ,
2020-06-17 02:07:24 +00:00
children : ( entity . children || [ ] ) . map ( child => curateEntity ( {
... child ,
parent : entity ,
2020-06-27 00:57:30 +00:00
} , includeParameters ) ) ,
2020-06-08 01:41:12 +00:00
} ;
return curatedEntity ;
}
async function curateEntities ( entities , includeParameters ) {
return Promise . all ( entities . map ( async entity => curateEntity ( entity , includeParameters ) ) ) ;
}
2020-06-27 00:57:30 +00:00
async function fetchChannelsFromArgv ( ) {
2020-06-17 02:07:24 +00:00
const rawNetworks = await knex . raw ( `
2020-06-27 00:57:30 +00:00
/* networks from argument with channels as children */
WITH RECURSIVE children AS (
2020-06-15 01:58:35 +00:00
SELECT
id , parent _id , name , slug , type , url , description , parameters
FROM
entities
WHERE
2020-06-27 00:57:30 +00:00
slug = ANY ( ? ) AND entities . type = 'network'
2020-06-15 01:58:35 +00:00
UNION ALL
SELECT
entities . id , entities . parent _id , entities . name , entities . slug , entities . type , entities . url , entities . description , entities . parameters
FROM
entities
INNER JOIN
2020-06-27 00:57:30 +00:00
children ON children . id = entities . parent _id
2020-06-15 01:58:35 +00:00
)
SELECT
2020-06-27 00:57:30 +00:00
entities . * , row _to _json ( parents ) as parent , json _agg ( children ) as children
2020-06-15 01:58:35 +00:00
FROM
2020-06-27 00:57:30 +00:00
children
2020-06-15 01:58:35 +00:00
LEFT JOIN
2020-06-27 00:57:30 +00:00
entities ON entities . id = children . parent _id
2020-06-15 01:58:35 +00:00
LEFT JOIN
entities AS parents ON parents . id = entities . parent _id
WHERE
2020-06-27 00:57:30 +00:00
children . type = 'channel'
2020-06-15 01:58:35 +00:00
GROUP BY
2020-06-27 00:57:30 +00:00
children . parent _id , entities . id , entities . name , parents . id
2020-06-15 01:58:35 +00:00
UNION ALL
2020-06-17 02:07:24 +00:00
2020-06-27 00:57:30 +00:00
/* channels from argument as the child of network with parent */
2020-06-15 01:58:35 +00:00
SELECT
2020-06-17 02:07:24 +00:00
entities . * , row _to _json ( parents ) as parent , json _agg ( row _to _json ( children ) )
2020-06-15 01:58:35 +00:00
FROM
entities AS children
LEFT JOIN
entities ON entities . id = children . parent _id
LEFT JOIN
entities AS parents ON parents . id = entities . parent _id
WHERE
2020-06-27 00:57:30 +00:00
children . slug = ANY ( ? ) AND children . type = 'channel'
2020-06-15 01:58:35 +00:00
GROUP BY
2020-06-17 02:07:24 +00:00
entities . id , parents . id ;
2020-06-27 00:57:30 +00:00
` , [argv.networks || [], argv.channels || []]);
2020-06-08 01:41:12 +00:00
2020-06-17 02:07:24 +00:00
const curatedNetworks = await curateEntities ( rawNetworks . rows , true ) ;
logger . info ( ` Found ${ curatedNetworks . length } networks in database ` ) ;
2020-06-08 01:41:12 +00:00
2020-06-17 02:07:24 +00:00
return curatedNetworks ;
2020-06-08 01:41:12 +00:00
}
2020-06-27 00:57:30 +00:00
async function fetchChannelsFromConfig ( ) {
2020-06-08 01:41:12 +00:00
const rawSites = await knex ( 'entities' )
2020-06-27 00:57:30 +00:00
. select ( knex . raw ( 'entities.*, row_to_json(parents) as parent' ) )
. leftJoin ( 'entities as parents' , 'parents.id' , 'entities.parent_id' )
2020-06-08 01:41:12 +00:00
. where ( ( builder ) => {
if ( config . include ) {
builder . whereIn ( 'entities.slug' , config . include ) ;
}
} )
. whereNot ( ( builder ) => {
builder . whereIn ( 'entities.slug' , config . exclude || [ ] ) ;
} ) ;
2020-06-15 01:58:35 +00:00
const curatedSites = await curateEntities ( rawSites , true ) ;
logger . info ( ` Found ${ curatedSites . length } entities in database ` ) ;
2020-06-08 01:41:12 +00:00
return curatedSites ;
}
async function fetchIncludedEntities ( ) {
2020-06-27 00:57:30 +00:00
if ( argv . networks || argv . channels ) {
return fetchChannelsFromArgv ( ) ;
2020-06-08 01:41:12 +00:00
}
2020-06-27 00:57:30 +00:00
return fetchChannelsFromConfig ( ) ;
2020-06-08 01:41:12 +00:00
}
2020-06-27 00:57:30 +00:00
async function fetchChannels ( queryObject ) {
2020-06-08 01:41:12 +00:00
const sites = await knex ( 'sites' )
. where ( builder => whereOr ( queryObject , 'sites' , builder ) )
. select (
'sites.*' ,
'networks.name as network_name' , 'networks.slug as network_slug' , 'networks.url as network_url' , 'networks.description as network_description' , 'networks.parameters as network_parameters' ,
)
. leftJoin ( 'networks' , 'sites.network_id' , 'networks.id' )
. limit ( 100 ) ;
2020-06-15 01:58:35 +00:00
return curateEntities ( sites ) ;
2020-06-08 01:41:12 +00:00
}
2020-06-27 00:57:30 +00:00
async function fetchChannelsFromReleases ( ) {
2020-06-08 01:41:12 +00:00
const sites = await knex ( 'releases' )
. select ( 'site_id' , '' )
. leftJoin ( 'sites' , 'sites.id' , 'releases.site_id' )
. groupBy ( 'sites.id' )
. limit ( 100 ) ;
2020-06-15 01:58:35 +00:00
return curateEntities ( sites ) ;
2020-06-08 01:41:12 +00:00
}
module . exports = {
curateEntity ,
curateEntities ,
fetchIncludedEntities ,
2020-06-27 00:57:30 +00:00
fetchChannels ,
fetchChannelsFromConfig ,
fetchChannelsFromArgv ,
fetchChannelsFromReleases ,
2020-06-08 01:41:12 +00:00
} ;