|
@@ -4,10 +4,10 @@ import moment from 'moment';
|
|
|
import { exec } from 'node-utils/shell';
|
|
import { exec } from 'node-utils/shell';
|
|
|
import path from 'path';
|
|
import path from 'path';
|
|
|
|
|
|
|
|
|
|
+import { HttpStatusException } from '../../common/lib/http-status.exception';
|
|
|
import { Logger } from '../../common/util/logger.class';
|
|
import { Logger } from '../../common/util/logger.class';
|
|
|
-import { HttpStatusException } from './lib/http-status.exception';
|
|
|
|
|
|
|
|
|
|
-const DATA_DIR = process.env.DATA_DIR;
|
|
|
|
|
|
|
+const DATA_DIR = process.env.DATA_DIR || 'data';
|
|
|
const DATA_BUFFER_FILE = path.resolve(DATA_DIR, 'buffer.csv');
|
|
const DATA_BUFFER_FILE = path.resolve(DATA_DIR, 'buffer.csv');
|
|
|
const DATA_BUFFER_REMAINS = path.resolve(DATA_DIR, 'remains.csv');
|
|
const DATA_BUFFER_REMAINS = path.resolve(DATA_DIR, 'remains.csv');
|
|
|
const DATA_REDUCED_FILE = path.resolve(DATA_DIR, 'reduced.csv');
|
|
const DATA_REDUCED_FILE = path.resolve(DATA_DIR, 'reduced.csv');
|
|
@@ -40,7 +40,7 @@ const CSV_COLS = {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export class Collector {
|
|
export class Collector {
|
|
|
- private intervalHdl: NodeJS.Timer;
|
|
|
|
|
|
|
+ private intervalHdl?: NodeJS.Timer;
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
(async () => {
|
|
(async () => {
|
|
@@ -158,6 +158,9 @@ export class Collector {
|
|
|
let valueBuffer: Array<BufferedData> = [];
|
|
let valueBuffer: Array<BufferedData> = [];
|
|
|
do {
|
|
do {
|
|
|
const line = lines.shift();
|
|
const line = lines.shift();
|
|
|
|
|
+
|
|
|
|
|
+ if (!line) break;
|
|
|
|
|
+
|
|
|
const data = this.parseBufferedData(line);
|
|
const data = this.parseBufferedData(line);
|
|
|
Logger.debug('[DEBUG] BufferedData:', JSON.stringify(data));
|
|
Logger.debug('[DEBUG] BufferedData:', JSON.stringify(data));
|
|
|
valueBuffer.push(data);
|
|
valueBuffer.push(data);
|
|
@@ -186,6 +189,7 @@ export class Collector {
|
|
|
if (cur.hdd && Object.keys(cur.hdd).length) {
|
|
if (cur.hdd && Object.keys(cur.hdd).length) {
|
|
|
const hdd_sums = res.hdd ?? {};
|
|
const hdd_sums = res.hdd ?? {};
|
|
|
res.hdd = Object.keys(cur.hdd).reduce((res_hdd, mount) => {
|
|
res.hdd = Object.keys(cur.hdd).reduce((res_hdd, mount) => {
|
|
|
|
|
+ if (!cur.hdd) return res_hdd;
|
|
|
if (!res_hdd[mount]) {
|
|
if (!res_hdd[mount]) {
|
|
|
res_hdd[mount] = { sum: 0, peak: 0, max: 0 };
|
|
res_hdd[mount] = { sum: 0, peak: 0, max: 0 };
|
|
|
}
|
|
}
|
|
@@ -265,7 +269,7 @@ export class Collector {
|
|
|
const lastCol = CSV_COLS.buffer.ram;
|
|
const lastCol = CSV_COLS.buffer.ram;
|
|
|
|
|
|
|
|
// HDD (?)
|
|
// HDD (?)
|
|
|
- let hdd: BufferedDriveData;
|
|
|
|
|
|
|
+ let hdd: BufferedDriveData | undefined;
|
|
|
if (MONITOR_MOUNTS.length && line.length > lastCol + 1) {
|
|
if (MONITOR_MOUNTS.length && line.length > lastCol + 1) {
|
|
|
for (let i = 1; i <= MONITOR_MOUNTS.length; i++) {
|
|
for (let i = 1; i <= MONITOR_MOUNTS.length; i++) {
|
|
|
if (lastCol + i > line.length - 1) break;
|
|
if (lastCol + i > line.length - 1) break;
|
|
@@ -297,7 +301,7 @@ export class Collector {
|
|
|
const lastCol = CSV_COLS.reduced.ram.max;
|
|
const lastCol = CSV_COLS.reduced.ram.max;
|
|
|
|
|
|
|
|
// HDD (?)
|
|
// HDD (?)
|
|
|
- let hdd: ReducedDriveData;
|
|
|
|
|
|
|
+ let hdd: ReducedDriveData | undefined;
|
|
|
if (MONITOR_MOUNTS.length && line.length > lastCol + 1) {
|
|
if (MONITOR_MOUNTS.length && line.length > lastCol + 1) {
|
|
|
hdd = {};
|
|
hdd = {};
|
|
|
for (let i = 1; lastCol + i + 3 < line.length; i += 4) {
|
|
for (let i = 1; lastCol + i + 3 < line.length; i += 4) {
|
|
@@ -345,7 +349,7 @@ export class Collector {
|
|
|
moment(data.time).format(TIMESTAMP_FORMAT),
|
|
moment(data.time).format(TIMESTAMP_FORMAT),
|
|
|
data.cpu,
|
|
data.cpu,
|
|
|
`${(data.ram.used / this.byteFactors['M']).toFixed(2)}/${(data.ram.max / this.byteFactors['M']).toFixed(2)} MiB`,
|
|
`${(data.ram.used / this.byteFactors['M']).toFixed(2)}/${(data.ram.max / this.byteFactors['M']).toFixed(2)} MiB`,
|
|
|
- ...(data.hdd ? Object.keys(data.hdd).map(mount => `${mount} ${data.hdd[mount].used}/${data.hdd[mount].max}`) : [])
|
|
|
|
|
|
|
+ ...(data.hdd ? Object.keys(data.hdd).map(mount => `${mount} ${data.hdd?.[mount].used}/${data.hdd?.[mount].max}`) : [])
|
|
|
].join(';');
|
|
].join(';');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -359,9 +363,14 @@ export class Collector {
|
|
|
data.ram.max.toFixed(2),
|
|
data.ram.max.toFixed(2),
|
|
|
...(data.hdd
|
|
...(data.hdd
|
|
|
? Object.keys(data.hdd).reduce((res, mount) => {
|
|
? Object.keys(data.hdd).reduce((res, mount) => {
|
|
|
- res.push(mount, data.hdd[mount].avg.toFixed(2), data.hdd[mount].peak.toFixed(2), data.hdd[mount].max.toFixed(2));
|
|
|
|
|
|
|
+ res.push(
|
|
|
|
|
+ mount,
|
|
|
|
|
+ data.hdd?.[mount].avg.toFixed(2) || '0',
|
|
|
|
|
+ data.hdd?.[mount].peak.toFixed(2) || '0',
|
|
|
|
|
+ data.hdd?.[mount].max.toFixed(2) || '0'
|
|
|
|
|
+ );
|
|
|
return res;
|
|
return res;
|
|
|
- }, [])
|
|
|
|
|
|
|
+ }, [] as string[])
|
|
|
: [])
|
|
: [])
|
|
|
].join(';');
|
|
].join(';');
|
|
|
}
|
|
}
|
|
@@ -372,7 +381,7 @@ export class Collector {
|
|
|
|
|
|
|
|
private _trx: {
|
|
private _trx: {
|
|
|
file?: PathLike;
|
|
file?: PathLike;
|
|
|
- start: () => Promise<number>;
|
|
|
|
|
|
|
+ start: () => Promise<number | null>;
|
|
|
read: () => Promise<Array<ReducedData>>;
|
|
read: () => Promise<Array<ReducedData>>;
|
|
|
rollback: (hdl: number) => Promise<void>;
|
|
rollback: (hdl: number) => Promise<void>;
|
|
|
commit: (hdl: number) => Promise<void>;
|
|
commit: (hdl: number) => Promise<void>;
|
|
@@ -380,7 +389,8 @@ export class Collector {
|
|
|
start: async () => {
|
|
start: async () => {
|
|
|
if (this.trx.file) {
|
|
if (this.trx.file) {
|
|
|
Logger.warn(`[WARN] Old transaction file found - rolling back now before starting new transaction ...`);
|
|
Logger.warn(`[WARN] Old transaction file found - rolling back now before starting new transaction ...`);
|
|
|
- const hdl = Number(/trx_(\d+)\.csv/.exec(this.trx.file as string)[1]);
|
|
|
|
|
|
|
+ const m = /trx_(\d+)\.csv/.exec(this.trx.file as string);
|
|
|
|
|
+ const hdl = Number(m?.[1] ?? '0');
|
|
|
await this.trx.rollback(hdl);
|
|
await this.trx.rollback(hdl);
|
|
|
Logger.warn(`[WARN] Transaction rollback succeeded.`);
|
|
Logger.warn(`[WARN] Transaction rollback succeeded.`);
|
|
|
}
|
|
}
|
|
@@ -407,7 +417,7 @@ export class Collector {
|
|
|
if (filename !== this.trx.file) throw new HttpStatusException(`Transaction #${hdl} not found`, 404);
|
|
if (filename !== this.trx.file) throw new HttpStatusException(`Transaction #${hdl} not found`, 404);
|
|
|
|
|
|
|
|
if (fs.existsSync(this.trx.file)) {
|
|
if (fs.existsSync(this.trx.file)) {
|
|
|
- let tmpFile: string;
|
|
|
|
|
|
|
+ let tmpFile: string | undefined;
|
|
|
if (fs.existsSync(DATA_REDUCED_FILE)) {
|
|
if (fs.existsSync(DATA_REDUCED_FILE)) {
|
|
|
tmpFile = `reduced.tmp_${moment().unix().toFixed(0)}.csv`;
|
|
tmpFile = `reduced.tmp_${moment().unix().toFixed(0)}.csv`;
|
|
|
await fsp.rename(DATA_REDUCED_FILE, tmpFile);
|
|
await fsp.rename(DATA_REDUCED_FILE, tmpFile);
|