traxxx/src/entities.js

198 lines
5.0 KiB
JavaScript
Raw Normal View History

'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) {
if (!entity) {
return null;
}
const curatedEntity = entity.id ? {
id: entity.id,
name: entity.name,
url: entity.url,
description: entity.description,
slug: entity.slug,
type: entity.type,
parameters: includeParameters ? entity.parameters : null,
parent: curateEntity(entity.parent),
} : {};
if (entity.children) {
curatedEntity.children = entity.children.map(child => curateEntity({
...child,
parent: curatedEntity.id ? curatedEntity : null,
}, 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
entities.*
FROM
entities
WHERE
slug = ANY(?) AND entities.type = 'network'
UNION ALL
SELECT
entities.*
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 rawNetworks = await knex.raw(`
2020-08-13 14:10:58 +00:00
WITH RECURSIVE channels AS (
/* select configured channels and networks */
2020-08-13 14:10:58 +00:00
SELECT
entities.*
2020-08-13 14:10:58 +00:00
FROM
entities
WHERE
CASE WHEN :includeAll
THEN
/* select all top level networks and independent channels */
entities.parent_id IS NULL
ELSE
((entities.slug = ANY(:includedNetworks)
AND entities.type = 'network')
OR (entities.slug = ANY(:includedChannels)
AND entities.type = 'channel'))
END
AND NOT (
(entities.slug = ANY(:excludedNetworks)
AND entities.type = 'network')
OR (entities.slug = ANY(:excludedChannels)
AND entities.type = 'channel'))
2020-08-13 14:10:58 +00:00
UNION ALL
/* select recursive children of configured networks */
SELECT
entities.*
2020-08-13 14:10:58 +00:00
FROM
entities
INNER JOIN
channels ON channels.id = entities.parent_id
WHERE
NOT ((entities.slug = ANY(:excludedNetworks)
AND entities.type = 'network')
OR (entities.slug = ANY(:excludedChannels)
AND entities.type = 'channel'))
2020-08-13 14:10:58 +00:00
)
/* select recursive channels as children of networks */
SELECT
entities.*, json_agg(channels) as children
FROM
channels
LEFT JOIN
entities ON entities.id = channels.parent_id
WHERE
channels.type = 'channel'
GROUP BY
entities.id
`, {
includeAll: !config.include?.networks && !config.include?.channels,
includedNetworks: config.include?.networks || [],
includedChannels: config.include?.channels || [],
excludedNetworks: config.exclude?.networks || [],
excludedChannels: config.exclude?.channels || [],
2020-08-13 14:10:58 +00:00
});
const curatedNetworks = rawNetworks.rows.map(entity => curateEntity(entity, true));
return curatedNetworks;
}
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,
};