UNPKG

rrdtool.ts

Version:

RRDTool High Level library for Node.js written in TypeScript

62 lines (61 loc) 2.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseInfo = exports.create = exports.now = void 0; const merge_1 = __importDefault(require("lodash/merge")); const db_1 = require("./db"); const proc_1 = __importDefault(require("./proc")); const fs_1 = __importDefault(require("fs")); const now = () => { return Math.floor(Date.now() / 1000); }; exports.now = now; const create = async (filename, definitions, options) => { if (!fs_1.default.existsSync(filename)) { await proc_1.default.create(filename, definitions, options); } return new db_1.RrdtoolDatabase(filename); }; exports.create = create; const parseInfo = (str) => { const obj = (props, value) => { if (props.length === 0) return value; const name = props[0]; const index = Number(name); const v = props.length === 1 ? value : obj(props.slice(1), value); if (Number.isNaN(index)) { const o = {}; o[name] = v; return o; } else { const o = []; o[index] = v; return o; } }; const getValue = (str) => { if (str.startsWith('"') && str.endsWith('"') && str.length > 1) { return str.slice(1, str.length - 1); } if (str.toLowerCase() === "null") return null; return Number(str); }; let info = {}; const re = /([\w\[\]\.]+) =(.+)/g; str.replace(re, (_, name, value) => { const v = getValue(value.trim()); if (!Number.isNaN(v) && v !== null) { const props = name.split(/[.\[\]]/).filter(s => s); info = (0, merge_1.default)(info, obj(props, v)); } return ""; }); info["ds"] = [...Object.keys(info["ds"])].map(k => (Object.assign({ name: k }, info["ds"][k]))); return info; }; exports.parseInfo = parseInfo;