UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

111 lines (110 loc) 7.46 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const DSTE_1 = __importDefault(require("../core/dste/DSTE")); const DatabaseEngine_1 = __importDefault(require("../database/DatabaseEngine")); const DataframeManager_1 = __importDefault(require("./DataframeManager")); class UsageDataManager { getUsageDetails() { return __awaiter(this, void 0, void 0, function* () { const now = DSTE_1.default.now(); const from = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); const prevMonthFrom = new Date(now.getTime() - 60 * 24 * 60 * 60 * 1000); const yearAgo = new Date(now.getFullYear(), now.getMonth() - 11, 1); const collection = 'usage'; // Aggregate status counts for current and previous month const getStatusCounts = (start, end) => __awaiter(this, void 0, void 0, function* () { const results = yield DatabaseEngine_1.default.aggregate(collection, [ { $match: { startedAt: { $gte: start, $lte: end } } }, { $group: { _id: '$status', count: { $sum: 1 } } } ]); let success = 0, failed = 0, total = 0; results.forEach((r) => { total += r.count; if (r._id === 'success') success = r.count; if (r._id === 'failed') failed = r.count; }); return { total, success, failed }; }); const statusesRequests = yield getStatusCounts(from, now); const prevStatusesRequests = yield getStatusCounts(prevMonthFrom, from); // Monthly success and fails for last 12 months const monthlySuccessPipeline = [ { $match: { status: 'success', startedAt: { $gte: yearAgo, $lte: now } } }, { $addFields: { year: { $year: '$startedAt' }, month: { $month: '$startedAt' } } }, { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: 1 } } }, { $project: { _id: 0, x: { $concat: [{ $toString: '$_id.year' }, '-', { $toString: '$_id.month' }] }, y: '$count' } }, { $sort: { x: 1 } } ]; const monthlyFailsPipeline = [ { $match: { status: 'failed', startedAt: { $gte: yearAgo, $lte: now } } }, { $addFields: { year: { $year: '$startedAt' }, month: { $month: '$startedAt' } } }, { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: 1 } } }, { $project: { _id: 0, x: { $concat: [{ $toString: '$_id.year' }, '-', { $toString: '$_id.month' }] }, y: '$count' } }, { $sort: { x: 1 } } ]; const rawMonthlySuccess = yield DatabaseEngine_1.default.aggregate(collection, monthlySuccessPipeline); const rawMonthlyFails = yield DatabaseEngine_1.default.aggregate(collection, monthlyFailsPipeline); // Top lines per month for last 12 months const topLinesPipeline = [ { $match: { startedAt: { $gte: yearAgo, $lte: now } } }, { $addFields: { year: { $year: '$startedAt' }, month: { $month: '$startedAt' } } }, { $group: { _id: { year: '$year', month: '$month' }, itemsCount: { $max: '$itemsCount' } } }, { $project: { _id: 0, x: { $concat: [{ $toString: '$_id.year' }, '-', { $toString: '$_id.month' }] }, y: '$itemsCount' } }, { $sort: { x: 1 } } ]; const topLines = yield DatabaseEngine_1.default.aggregate(collection, topLinesPipeline); // Top times per month for last 12 months const topTimePipeline = [ { $match: { startedAt: { $gte: yearAgo, $lte: now } } }, { $addFields: { durationMs: { $subtract: ['$finishedAt', '$startedAt'] }, year: { $year: '$startedAt' }, month: { $month: '$startedAt' } } }, { $group: { _id: { year: '$year', month: '$month' }, maxDuration: { $max: '$durationMs' } } }, { $project: { _id: 0, x: { $concat: [{ $toString: '$_id.year' }, '-', { $toString: '$_id.month' }] }, y: '$maxDuration' } }, { $sort: { x: 1 } } ]; const topTime = yield DatabaseEngine_1.default.aggregate(collection, topTimePipeline); // Monthly consumers: for each consumer, per month count const consumerPipeline = [ { $match: { startedAt: { $gte: yearAgo, $lte: now } } }, { $addFields: { year: { $year: '$startedAt' }, month: { $month: '$startedAt' } } }, { $group: { _id: { consumer: '$consumer', year: '$year', month: '$month' }, count: { $sum: 1 } } }, { $project: { _id: 0, consumer: '$_id.consumer', x: { $concat: [{ $toString: '$_id.year' }, '-', { $toString: '$_id.month' }] }, y: '$count' } }, { $sort: { consumer: 1, x: 1 } } ]; const consumersData = yield DatabaseEngine_1.default.aggregate(collection, consumerPipeline); // transform to consumer array const consumerMap = {}; consumersData.forEach((r) => { consumerMap[r.consumer] = consumerMap[r.consumer] || []; consumerMap[r.consumer].push({ x: r.x, y: r.y }); }); const consumers = Object.entries(consumerMap).map(([name, data]) => ({ name, data: DataframeManager_1.default.fill(data !== null && data !== void 0 ? data : [], yearAgo, now) })); // Recent executions const recentExecution = yield DatabaseEngine_1.default.query(collection, { startedAt: { $gte: from, $lte: now } }, { sort: { startedAt: -1 }, limit: 10 }); return { statusesRequests, prevStatusesRequests, monthlySuccess: DataframeManager_1.default.fill(rawMonthlySuccess !== null && rawMonthlySuccess !== void 0 ? rawMonthlySuccess : [], yearAgo, now), monthlyFails: DataframeManager_1.default.fill(rawMonthlyFails !== null && rawMonthlyFails !== void 0 ? rawMonthlyFails : [], yearAgo, now), consumers: consumers, topLine: DataframeManager_1.default.fill(topLines !== null && topLines !== void 0 ? topLines : [], yearAgo, now), topTime: DataframeManager_1.default.fill(topTime !== null && topTime !== void 0 ? topTime : [], yearAgo, now), recentExecution }; }); } } exports.default = new UsageDataManager();