/** * sigma_stats_service.js * * Service for retrieving and processing Sigma rule database statistics * Provides aggregated statistical information about the rule database */ const logger = require('../../utils/logger'); const { getStatsFromDatabase } = require('../../sigma_db/queries'); const { getFileName } = require('../../utils/file_utils'); const FILE_NAME = getFileName(__filename); /** * Get database statistics * Collects various statistics about the Sigma rule database * * @returns {Promise} Object with success flag and statistics or error message */ async function getSigmaStats() { logger.info(`${FILE_NAME}: Getting Sigma rule database statistics`); try { // Get statistics from database query function const statsResult = await getStatsFromDatabase(); if (!statsResult.success) { logger.error(`${FILE_NAME}: Failed to retrieve statistics: ${statsResult.message}`); return { success: false, message: statsResult.message }; } // Format the data in a consistent structure for both CLI and Slack const formattedStats = { lastUpdate: statsResult.stats.lastUpdate, totalRules: statsResult.stats.totalRules, databaseHealth: statsResult.stats.databaseHealth, operatingSystems: statsResult.stats.operatingSystems, severityLevels: statsResult.stats.severityLevels, mitreTactics: statsResult.stats.mitreTactics, topAuthors: statsResult.stats.topAuthors, // Add any other statistics needed }; logger.info(`${FILE_NAME}: Successfully collected database statistics`); return { success: true, stats: formattedStats, // Include raw response data for direct use by CLI. // We have one universal function in the CLI to receive responses, // and the CLI will then format each result differently responseData: formattedStats }; } catch (error) { logger.error(`${FILE_NAME}: Error processing statistics: ${error.message}`); logger.debug(`${FILE_NAME}: Error stack: ${error.stack}`); return { success: false, message: `Error processing statistics: ${error.message}` }; } } module.exports = { getSigmaStats };