UNPKG

@gsb-core/core

Version:

GSB core services and classes for platform-independent web applications

155 lines 6.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowMonitorService = void 0; const gsb_entity_service_1 = require("../../services/entity/gsb-entity.service"); const gsb_config_1 = require("../../config/gsb-config"); const gsb_workflow_model_1 = require("../../models/gsb-workflow.model"); const query_params_1 = require("../../types/query-params"); const query_1 = require("../../types/query"); class WorkflowMonitorService { constructor() { this.pollInterval = 60000; // 1 minute this.entityService = gsb_entity_service_1.GsbEntityService.getInstance(); this.statusCache = new Map(); this.startPolling(); } startPolling() { // Clear existing timer if any if (this.pollTimer) { clearInterval(this.pollTimer); } // Start polling this.pollTimer = setInterval(() => { this.updateAllWorkflowStatuses(); }, this.pollInterval); // Initial update this.updateAllWorkflowStatuses(); } async updateAllWorkflowStatuses() { try { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); // Get all workflow instances const queryParams = new query_params_1.QueryParams('GsbWorkflowInstance'); queryParams.calcTotalCount = true; queryParams.startIndex = 0; queryParams.count = 1000; const sortCol = new query_1.SelectCol('lastUpdateDate'); const sort = new query_1.SortCol(); sort.col = sortCol; sort.sortType = 'desc'; queryParams.sortCols = [sort]; const response = await this.entityService.query(queryParams, token, tenantCode); const instances = response.items; const workflowStats = new Map(); // Process instances instances === null || instances === void 0 ? void 0 : instances.forEach(instance => { const stats = workflowStats.get(instance.workflow_id || '') || { total: 0, errors: 0, active: 0, executionTimes: [] }; stats.total++; if (instance.status === gsb_workflow_model_1.TaskStatus.Error) { stats.errors++; } if (instance.status === gsb_workflow_model_1.TaskStatus.Started) { stats.active++; } if (instance.startDate && instance.lastUpdateDate) { const executionTime = new Date(instance.lastUpdateDate).getTime() - new Date(instance.startDate).getTime(); stats.executionTimes.push(executionTime); } workflowStats.set(instance.workflow_id || '', stats); }); // Update status cache for (const [workflowId, stats] of workflowStats) { const status = { id: workflowId, name: 'Workflow', // Will be updated when we get workflow details status: this.calculateStatus(stats), lastCheck: new Date(), activeInstances: stats.active, errorCount: stats.errors, performance: { avgExecutionTime: this.calculateAverage(stats.executionTimes), successRate: ((stats.total - stats.errors) / stats.total) * 100 } }; this.statusCache.set(workflowId, status); } } catch (error) { console.error('Error updating workflow statuses:', error); } } calculateStatus(stats) { const errorRate = stats.errors / stats.total; if (errorRate >= 0.25) { // 25% or more errors return 'Down'; } else if (errorRate >= 0.1) { // 10% or more errors return 'Degraded'; } return 'Operational'; } calculateAverage(numbers) { if (numbers.length === 0) return 0; return numbers.reduce((a, b) => a + b, 0) / numbers.length; } async getWorkflowStatus(workflowId) { return this.statusCache.get(workflowId) || null; } async getAllWorkflowStatuses() { return Array.from(this.statusCache.values()); } async getOverallWorkflowStatus() { const statuses = await this.getAllWorkflowStatuses(); if (statuses.length === 0) return 'Operational'; if (statuses.some(s => s.status === 'Down')) { return 'Down'; } if (statuses.some(s => s.status === 'Degraded')) { return 'Degraded'; } return 'Operational'; } /** * Get workflow instances for a specific workflow */ async getWorkflowInstances(workflowId) { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); try { const queryParams = new query_params_1.QueryParams('GsbWorkflowInstance'); queryParams.calcTotalCount = true; queryParams.startIndex = 0; queryParams.count = 1000; // Add workflow ID filter queryParams.filter('workflow_id', workflowId); // Sort by last update date const sortCol = new query_1.SelectCol('lastUpdateDate'); const sort = new query_1.SortCol(); sort.col = sortCol; sort.sortType = 'desc'; queryParams.sortCols = [sort]; const response = await this.entityService.query(queryParams, token, tenantCode); return response.items || []; } catch (error) { console.error('Error fetching workflow instances:', error); return []; } } destroy() { if (this.pollTimer) { clearInterval(this.pollTimer); } } } exports.WorkflowMonitorService = WorkflowMonitorService; //# sourceMappingURL=workflow-monitor.service.js.map