UNPKG

axiodb

Version:

The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor

127 lines 7.28 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 }); /* eslint-disable no-self-assign */ const outers_1 = require("outers"); const responseBuilder_helper_1 = __importDefault(require("../helper/responseBuilder.helper")); // ResponseBuilder, // import { FastifyRequest } from "fastify"; // import countFilesRecursive from "../../helper/filesCounterInFolder.helper"; const memory_operation_1 = __importDefault(require("../../Memory/memory.operation")); const fs_1 = __importDefault(require("fs")); class StatsController { constructor(AxioDBInstance) { this.AxioDBInstance = AxioDBInstance; } /** * Retrieves dashboard statistics for the AxioDB instance. * * This method gathers and compiles various statistics about the database system: * - Count of databases, collections, and documents * - Storage information (used storage and total machine storage) * - In-memory cache details * * All storage metrics are converted to MB for consistency. * * @returns {Promise<Object>} A formatted response object containing: * - HTTP status code * - Status message * - Data payload with the following statistics: * - totalDatabases: Number of databases in the instance * - totalCollections: Total number of collections across all databases * - totalDocuments: Total number of documents across all collections * - storageInfo: Object containing storage metrics (total used, machine total, and units) * - cacheStorage: Object containing cache storage metrics (current usage, maximum, and units) * * @throws Will return an error response with status 500 if the operation fails */ getDashBoardStat() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e; try { const InstanceInfo = yield this.AxioDBInstance.getInstanceInfo(); let totalCollections = 0; let totalDocuments = 0; const treeMap = []; // Extract Total Stats from the InstanceInfo if (InstanceInfo && InstanceInfo.data) { for (const db of InstanceInfo.data.ListOfDatabases) { const dbTree = { name: db, collections: [], }; const DB_instance = this.AxioDBInstance.createDB(db); const Collection_stats = yield (yield DB_instance).getCollectionInfo(); if (Collection_stats) { totalCollections += Collection_stats.data.ListOfCollections.length || 0; for (const collection of Collection_stats.data.ListOfCollections) { const Collection_Instance = yield (yield this.AxioDBInstance.createDB(db)).createCollection(collection); const Document_stats = yield Collection_Instance.totalDocuments(); totalDocuments += ((_a = Document_stats.data) === null || _a === void 0 ? void 0 : _a.total) || 0; dbTree.collections.push({ name: collection, documentCount: ((_b = Document_stats.data) === null || _b === void 0 ? void 0 : _b.total) || 0, }); } } treeMap.push(dbTree); } } // Get storage info and convert to MB let totalMachineStorage = 0; let totalUsedStorage = ((_c = InstanceInfo === null || InstanceInfo === void 0 ? void 0 : InstanceInfo.data) === null || _c === void 0 ? void 0 : _c.TotalSize) || 0; try { // Get disk info for the root directory const stats = fs_1.default.statfsSync("/"); const totalBytes = stats.blocks * stats.bsize; // Convert to MB totalMachineStorage = parseFloat((totalBytes / (1024 * 1024)).toFixed(2)); totalUsedStorage = parseFloat((totalUsedStorage / (1024 * 1024)).toFixed(2)); } catch (storageError) { console.error("Error fetching machine storage:", storageError); } const MatrixUnitsForUsedStorage = "MB"; const MatrixUnitsForMachineStorage = "MB"; const CacheStorageDetails = yield memory_operation_1.default.getCacheDetails(); const totalCacheSize = parseFloat((CacheStorageDetails.cacheSizeInBytes / (1024 * 1024)).toFixed(2)); const maxCacheSize = parseFloat((CacheStorageDetails.availableMemoryInBytes / (1024 * 1024)).toFixed(2)); const response = { totalDatabases: ((_e = (_d = InstanceInfo === null || InstanceInfo === void 0 ? void 0 : InstanceInfo.data) === null || _d === void 0 ? void 0 : _d.ListOfDatabases) === null || _e === void 0 ? void 0 : _e.length) || 0, totalCollections: totalCollections || 0, totalDocuments: totalDocuments || 0, storageInfo: { total: totalUsedStorage || 0, matrixUnit: MatrixUnitsForUsedStorage || "MB", machine: totalMachineStorage || 0, machineUnit: MatrixUnitsForMachineStorage || "B", }, cacheStorage: { Storage: totalCacheSize || 0, Max: maxCacheSize || 0, Unit: "MB", }, nodeTree: treeMap, }; return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.OK, "Dashboard stats fetched successfully", response); } catch (error) { console.error("Error fetching dashboard stats:", error); return (0, responseBuilder_helper_1.default)(outers_1.StatusCodes.INTERNAL_SERVER_ERROR, "Failed to fetch dashboard stats"); } }); } } exports.default = StatsController; //# sourceMappingURL=Stats.controller.js.map