146 lines
3.9 KiB
JavaScript
146 lines
3.9 KiB
JavaScript
'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,
|
|
type: entity.type,
|
|
parameters: includeParameters ? entity.parameters : null,
|
|
parent: entity.parent_id && entity.parent,
|
|
children: (entity.children || []).map(child => curateEntity({
|
|
...child,
|
|
parent: entity,
|
|
}, includeParameters)),
|
|
};
|
|
|
|
return curatedEntity;
|
|
}
|
|
|
|
async function curateEntities(entities, includeParameters) {
|
|
return Promise.all(entities.map(async entity => curateEntity(entity, includeParameters)));
|
|
}
|
|
|
|
async function fetchChannelsFromArgv() {
|
|
const rawNetworks = await knex.raw(`
|
|
/* networks from argument with channels as children */
|
|
WITH RECURSIVE children AS (
|
|
SELECT
|
|
id, parent_id, name, slug, type, url, description, parameters
|
|
FROM
|
|
entities
|
|
WHERE
|
|
slug = ANY(?) AND entities.type = 'network'
|
|
UNION ALL
|
|
SELECT
|
|
entities.id, entities.parent_id, entities.name, entities.slug, entities.type, entities.url, entities.description, entities.parameters
|
|
FROM
|
|
entities
|
|
INNER JOIN
|
|
children ON children.id = entities.parent_id
|
|
)
|
|
SELECT
|
|
entities.*, row_to_json(parents) as parent, json_agg(children) as children
|
|
FROM
|
|
children
|
|
LEFT JOIN
|
|
entities ON entities.id = children.parent_id
|
|
LEFT JOIN
|
|
entities AS parents ON parents.id = entities.parent_id
|
|
WHERE
|
|
children.type = 'channel'
|
|
GROUP BY
|
|
children.parent_id, entities.id, entities.name, parents.id
|
|
|
|
UNION ALL
|
|
|
|
/* channels from argument as the child of network with parent */
|
|
SELECT
|
|
entities.*, row_to_json(parents) as parent, json_agg(row_to_json(children))
|
|
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
|
|
children.slug = ANY(?) AND children.type = 'channel'
|
|
GROUP BY
|
|
entities.id, parents.id;
|
|
`, [argv.networks || [], argv.channels || []]);
|
|
|
|
const curatedNetworks = await curateEntities(rawNetworks.rows, true);
|
|
logger.info(`Found ${curatedNetworks.length} networks in database`);
|
|
|
|
return curatedNetworks;
|
|
}
|
|
|
|
async function fetchChannelsFromConfig() {
|
|
const rawSites = await knex('entities')
|
|
.select(knex.raw('entities.*, row_to_json(parents) as parent'))
|
|
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
|
.where((builder) => {
|
|
if (config.include) {
|
|
builder.whereIn('entities.slug', config.include);
|
|
}
|
|
})
|
|
.whereNot((builder) => {
|
|
builder.whereIn('entities.slug', config.exclude || []);
|
|
});
|
|
|
|
const curatedSites = await curateEntities(rawSites, true);
|
|
logger.info(`Found ${curatedSites.length} entities in database`);
|
|
|
|
return curatedSites;
|
|
}
|
|
|
|
async function fetchIncludedEntities() {
|
|
if (argv.networks || argv.channels) {
|
|
return fetchChannelsFromArgv();
|
|
}
|
|
|
|
return fetchChannelsFromConfig();
|
|
}
|
|
|
|
async function fetchChannels(queryObject) {
|
|
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);
|
|
|
|
return curateEntities(sites);
|
|
}
|
|
|
|
async function fetchChannelsFromReleases() {
|
|
const sites = await knex('releases')
|
|
.select('site_id', '')
|
|
.leftJoin('sites', 'sites.id', 'releases.site_id')
|
|
.groupBy('sites.id')
|
|
.limit(100);
|
|
|
|
return curateEntities(sites);
|
|
}
|
|
|
|
module.exports = {
|
|
curateEntity,
|
|
curateEntities,
|
|
fetchIncludedEntities,
|
|
fetchChannels,
|
|
fetchChannelsFromConfig,
|
|
fetchChannelsFromArgv,
|
|
fetchChannelsFromReleases,
|
|
};
|