iobroker.js-controller
Version:
Updated by reinstall.js on 2018-06-11T15:19:56.688Z
61 lines • 2.05 kB
JavaScript
import { getUsedObjectIDs } from './visUtils.js';
/**
* Calculate the number of data points for each project of given instance
*
* @param options - database and project information
*/
async function calcProject(options) {
const { visAdapter, instance, objects, projects } = options;
const result = [];
if (!projects?.length) {
return result;
}
for (const project of projects) {
if (!project?.isDir) {
continue;
}
if (!(await objects.fileExists(`${visAdapter}.${instance}`, `/${project.file}/vis-views.json`))) {
continue;
}
// calculate data points in one project
const data = await objects.readFile(`${visAdapter}.${instance}`, `/${project.file}/vis-views.json`);
let json;
try {
json = JSON.parse(data.file);
}
catch {
console.error(`Cannot parse "/${project.file}/vis-views.json`);
continue;
}
const dps = getUsedObjectIDs(json, false);
if (dps?.IDs) {
result.push({
id: `${visAdapter}.${instance}.datapoints.${project.file.replace(/[.\\s]/g, '_')}`,
val: dps.IDs.length,
});
}
}
return result;
}
/**
* Calculate the number of data points for all vis projects of given instance
*
* @param options - db and vis options
*/
export async function calcProjects(options) {
const { visAdapter, instance, objects } = options;
const projects = await objects.readDirAsync(`${visAdapter}.${instance}`, '/');
if (!projects?.length) {
return [{ id: `${visAdapter}.${instance}.datapoints.total`, val: 0 }];
}
const result = await calcProject({ objects, projects, instance, visAdapter });
if (result?.length) {
let total = 0;
for (const entry of result) {
total += entry.val;
}
result.push({ id: `${visAdapter}.${instance}.datapoints.total`, val: total });
}
return result;
}
//# sourceMappingURL=states.js.map