1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-29 06:30:07 +02:00
copilot/frontend/src/state/selectors.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

const find = require('lodash.find');
const forceArray = require('force-array');
2016-12-20 13:29:54 +02:00
const get = require('lodash.get');
const reselect = require('reselect');
const {
createSelector
} = reselect;
2016-12-20 13:29:54 +02:00
const account = (state) => get(state, 'account.data', {});
const accountUi = (state) => get(state, 'account.ui', {});
2016-12-20 13:29:54 +02:00
const orgUiSections = (state) => get(state, 'orgs.ui.sections', []);
2016-12-20 21:06:02 +02:00
const projectUiSections = (state) => get(state, 'projects.ui.sections', []);
2017-01-03 00:32:29 +02:00
const serviceUiSections = (state) => get(state, 'services.ui.sections', []);
2016-12-20 13:29:54 +02:00
const orgs = (state) => get(state, 'orgs.data', []);
const projects = (state) => get(state, 'projects.data', []);
2017-01-03 00:32:29 +02:00
const services = (state) => get(state, 'services.data', []);
2017-01-10 16:58:51 +02:00
const collapsedServices = (state) => get(state, 'services.ui.collapsed', []);
2017-01-11 23:11:48 +02:00
const collapsedInstances = (state) => get(state, 'instances.ui.collapsed', []);
2017-01-09 21:08:47 +02:00
const instances = (state) => get(state, 'instances.data', []);
2017-01-12 14:17:25 +02:00
const metricTypes = (state) => get(state, 'metrics.data.types', []);
2017-01-11 19:41:28 +02:00
const metricDatasets = (state) => get(state, 'metrics.data.datasets', []);
2017-01-03 00:32:29 +02:00
const projectById = (projectId) => createSelector(
2016-12-20 21:06:02 +02:00
projects,
2017-01-03 00:32:29 +02:00
(projects) => find(projects, ['id', projectId])
2016-12-20 21:06:02 +02:00
);
2017-01-03 00:32:29 +02:00
const orgById = (orgId) => createSelector(
orgs,
2017-01-03 00:32:29 +02:00
(orgs) => find(orgs, ['id', orgId])
);
const serviceById = (serviceId) => createSelector(
[services],
(services) => find(services, ['id', serviceId])
);
const projectsByOrgId = (orgId) => createSelector(
[projects, orgById(orgId)],
(projects, org) => projects.filter((p) => p.org === org.uuid)
);
const orgSections = (orgId) => createSelector(
[orgById(orgId), orgUiSections],
(org, sections) => sections.filter(
(section) => forceArray(org.hide).indexOf(section) < 0
)
);
2017-01-11 23:11:48 +02:00
const isCollapsed = (collapsed, uuid) => collapsed.indexOf(uuid) >= 0;
const datasets = (metrics, uuids) => uuids.map((uuid) => find(metrics, [
'uuid',
uuid
]));
2017-01-03 00:32:29 +02:00
const servicesByProjectId = (projectId) => createSelector(
2017-01-11 19:41:28 +02:00
[services, projectById(projectId), collapsedServices, metricDatasets],
(services, project, collapsed, metrics) =>
2017-01-03 00:32:29 +02:00
services.filter((s) => s.project === project.uuid)
.map((service) => ({
...service,
services: services.filter((s) => s.parent === service.uuid)
}))
.filter((s) => !s.parent)
2017-01-10 16:58:51 +02:00
.map((service) => ({
...service,
2017-01-11 23:11:48 +02:00
metrics: datasets(metrics, service.metrics),
collapsed: isCollapsed(collapsed, service.uuid),
2017-01-10 16:58:51 +02:00
services: service.services.map((service) => ({
...service,
2017-01-11 23:11:48 +02:00
metrics: datasets(metrics, service.metrics),
collapsed: isCollapsed(collapsed, service.uuid)
2017-01-10 16:58:51 +02:00
}))
}))
2017-01-03 00:32:29 +02:00
);
2017-01-09 21:08:47 +02:00
const instancesByServiceId = (serviceId) => createSelector(
2017-01-11 23:11:48 +02:00
[instances, serviceById(serviceId), collapsedInstances, metricDatasets],
(instances, service, collapsed, metrics) =>
2017-01-10 16:58:51 +02:00
instances.filter((i) => i.service === service.uuid)
2017-01-11 23:11:48 +02:00
.map((instance) => ({
...instance,
metrics: datasets(metrics, instance.metrics),
collapsed: isCollapsed(collapsed, instance.uuid)
}))
2017-01-09 21:08:47 +02:00
);
const metricsByServiceId = (serviceId) => createSelector(
2017-01-12 14:17:25 +02:00
[metricTypes, serviceById(serviceId)],
(metricTypes, service) =>
metricTypes.filter((i) => i.service === service.uuid)
);
2017-01-09 21:08:47 +02:00
module.exports = {
accountSelector: account,
accountUISelector: accountUi,
orgByIdSelector: orgById,
orgsSelector: orgs,
2017-01-03 00:32:29 +02:00
servicesSelector: services,
serviceByIdSelector: serviceById,
orgSectionsSelector: orgSections,
2016-12-20 21:06:02 +02:00
projectSectionsSelector: projectUiSections,
2017-01-03 00:32:29 +02:00
serviceSectionsSelector: serviceUiSections,
2016-12-20 21:06:02 +02:00
projectsByOrgIdSelector: projectsByOrgId,
2017-01-03 00:32:29 +02:00
projectByIdSelector: projectById,
2017-01-09 21:08:47 +02:00
servicesByProjectIdSelector: servicesByProjectId,
instancesByServiceIdSelector: instancesByServiceId,
metricsByServiceIdSelector: metricsByServiceId
};