beddel 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/README.md +297 -0
  2. package/dist/agents/agentRegistry.d.ts +68 -0
  3. package/dist/agents/agentRegistry.d.ts.map +1 -0
  4. package/dist/agents/agentRegistry.js +222 -0
  5. package/dist/agents/agentRegistry.js.map +1 -0
  6. package/dist/agents/formatter-agent.d.ts +10 -0
  7. package/dist/agents/formatter-agent.d.ts.map +1 -0
  8. package/dist/agents/formatter-agent.js +49 -0
  9. package/dist/agents/formatter-agent.js.map +1 -0
  10. package/dist/agents/genkit-agent.d.ts +12 -0
  11. package/dist/agents/genkit-agent.d.ts.map +1 -0
  12. package/dist/agents/genkit-agent.js +119 -0
  13. package/dist/agents/genkit-agent.js.map +1 -0
  14. package/dist/agents/i18n-messages.d.ts +17 -0
  15. package/dist/agents/i18n-messages.d.ts.map +1 -0
  16. package/dist/agents/i18n-messages.js +92 -0
  17. package/dist/agents/i18n-messages.js.map +1 -0
  18. package/dist/agents/index.d.ts +10 -0
  19. package/dist/agents/index.d.ts.map +1 -0
  20. package/dist/agents/index.js +26 -0
  21. package/dist/agents/index.js.map +1 -0
  22. package/dist/agents/pipeline.d.ts +15 -0
  23. package/dist/agents/pipeline.d.ts.map +1 -0
  24. package/dist/agents/pipeline.js +45 -0
  25. package/dist/agents/pipeline.js.map +1 -0
  26. package/dist/agents/schema-factory.d.ts +40 -0
  27. package/dist/agents/schema-factory.d.ts.map +1 -0
  28. package/dist/agents/schema-factory.js +121 -0
  29. package/dist/agents/schema-factory.js.map +1 -0
  30. package/dist/agents/translation-validators.d.ts +26 -0
  31. package/dist/agents/translation-validators.d.ts.map +1 -0
  32. package/dist/agents/translation-validators.js +77 -0
  33. package/dist/agents/translation-validators.js.map +1 -0
  34. package/dist/agents/translator-agents.d.ts +184 -0
  35. package/dist/agents/translator-agents.d.ts.map +1 -0
  36. package/dist/agents/translator-agents.js +613 -0
  37. package/dist/agents/translator-agents.js.map +1 -0
  38. package/dist/agents/types/translation.types.d.ts +100 -0
  39. package/dist/agents/types/translation.types.d.ts.map +1 -0
  40. package/dist/agents/types/translation.types.js +3 -0
  41. package/dist/agents/types/translation.types.js.map +1 -0
  42. package/dist/agents/validator-agent.d.ts +42 -0
  43. package/dist/agents/validator-agent.d.ts.map +1 -0
  44. package/dist/agents/validator-agent.js +122 -0
  45. package/dist/agents/validator-agent.js.map +1 -0
  46. package/dist/audit/auditTrail.d.ts +55 -0
  47. package/dist/audit/auditTrail.d.ts.map +1 -0
  48. package/dist/audit/auditTrail.js +93 -0
  49. package/dist/audit/auditTrail.js.map +1 -0
  50. package/dist/compliance/gdprEngine.d.ts +44 -0
  51. package/dist/compliance/gdprEngine.d.ts.map +1 -0
  52. package/dist/compliance/gdprEngine.js +178 -0
  53. package/dist/compliance/gdprEngine.js.map +1 -0
  54. package/dist/compliance/lgpdEngine.d.ts +51 -0
  55. package/dist/compliance/lgpdEngine.d.ts.map +1 -0
  56. package/dist/compliance/lgpdEngine.js +221 -0
  57. package/dist/compliance/lgpdEngine.js.map +1 -0
  58. package/dist/config.d.ts +78 -0
  59. package/dist/config.d.ts.map +1 -0
  60. package/dist/config.js +77 -0
  61. package/dist/config.js.map +1 -0
  62. package/dist/errors.d.ts +17 -0
  63. package/dist/errors.d.ts.map +1 -0
  64. package/dist/errors.js +40 -0
  65. package/dist/errors.js.map +1 -0
  66. package/dist/firebase/tenantManager.d.ts +84 -0
  67. package/dist/firebase/tenantManager.d.ts.map +1 -0
  68. package/dist/firebase/tenantManager.js +378 -0
  69. package/dist/firebase/tenantManager.js.map +1 -0
  70. package/dist/index.d.ts +36 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +118 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/integration/secure-yaml-runtime.d.ts +68 -0
  75. package/dist/integration/secure-yaml-runtime.d.ts.map +1 -0
  76. package/dist/integration/secure-yaml-runtime.js +245 -0
  77. package/dist/integration/secure-yaml-runtime.js.map +1 -0
  78. package/dist/parser/secure-yaml-parser.d.ts +62 -0
  79. package/dist/parser/secure-yaml-parser.d.ts.map +1 -0
  80. package/dist/parser/secure-yaml-parser.js +234 -0
  81. package/dist/parser/secure-yaml-parser.js.map +1 -0
  82. package/dist/performance/autoscaling.d.ts +100 -0
  83. package/dist/performance/autoscaling.d.ts.map +1 -0
  84. package/dist/performance/autoscaling.js +339 -0
  85. package/dist/performance/autoscaling.js.map +1 -0
  86. package/dist/performance/benchmark.d.ts +104 -0
  87. package/dist/performance/benchmark.d.ts.map +1 -0
  88. package/dist/performance/benchmark.js +514 -0
  89. package/dist/performance/benchmark.js.map +1 -0
  90. package/dist/performance/index.d.ts +14 -0
  91. package/dist/performance/index.d.ts.map +1 -0
  92. package/dist/performance/index.js +35 -0
  93. package/dist/performance/index.js.map +1 -0
  94. package/dist/performance/monitor.d.ts +126 -0
  95. package/dist/performance/monitor.d.ts.map +1 -0
  96. package/dist/performance/monitor.js +324 -0
  97. package/dist/performance/monitor.js.map +1 -0
  98. package/dist/performance/streaming.d.ts +82 -0
  99. package/dist/performance/streaming.d.ts.map +1 -0
  100. package/dist/performance/streaming.js +287 -0
  101. package/dist/performance/streaming.js.map +1 -0
  102. package/dist/runtime/audit.d.ts +240 -0
  103. package/dist/runtime/audit.d.ts.map +1 -0
  104. package/dist/runtime/audit.js +641 -0
  105. package/dist/runtime/audit.js.map +1 -0
  106. package/dist/runtime/declarativeAgentRuntime.d.ts +123 -0
  107. package/dist/runtime/declarativeAgentRuntime.d.ts.map +1 -0
  108. package/dist/runtime/declarativeAgentRuntime.js +576 -0
  109. package/dist/runtime/declarativeAgentRuntime.js.map +1 -0
  110. package/dist/runtime/isolatedRuntime.d.ts +119 -0
  111. package/dist/runtime/isolatedRuntime.d.ts.map +1 -0
  112. package/dist/runtime/isolatedRuntime.js +425 -0
  113. package/dist/runtime/isolatedRuntime.js.map +1 -0
  114. package/dist/runtime/schemaCompiler.d.ts +35 -0
  115. package/dist/runtime/schemaCompiler.d.ts.map +1 -0
  116. package/dist/runtime/schemaCompiler.js +151 -0
  117. package/dist/runtime/schemaCompiler.js.map +1 -0
  118. package/dist/runtime/simpleRuntime.d.ts +57 -0
  119. package/dist/runtime/simpleRuntime.d.ts.map +1 -0
  120. package/dist/runtime/simpleRuntime.js +187 -0
  121. package/dist/runtime/simpleRuntime.js.map +1 -0
  122. package/dist/security/dashboard.d.ts +89 -0
  123. package/dist/security/dashboard.d.ts.map +1 -0
  124. package/dist/security/dashboard.js +300 -0
  125. package/dist/security/dashboard.js.map +1 -0
  126. package/dist/security/hardening.d.ts +130 -0
  127. package/dist/security/hardening.d.ts.map +1 -0
  128. package/dist/security/hardening.js +414 -0
  129. package/dist/security/hardening.js.map +1 -0
  130. package/dist/security/index.d.ts +128 -0
  131. package/dist/security/index.d.ts.map +1 -0
  132. package/dist/security/index.js +353 -0
  133. package/dist/security/index.js.map +1 -0
  134. package/dist/security/monitor.d.ts +88 -0
  135. package/dist/security/monitor.d.ts.map +1 -0
  136. package/dist/security/monitor.js +356 -0
  137. package/dist/security/monitor.js.map +1 -0
  138. package/dist/security/scanner.d.ts +104 -0
  139. package/dist/security/scanner.d.ts.map +1 -0
  140. package/dist/security/scanner.js +298 -0
  141. package/dist/security/scanner.js.map +1 -0
  142. package/dist/security/score.d.ts +150 -0
  143. package/dist/security/score.d.ts.map +1 -0
  144. package/dist/security/score.js +983 -0
  145. package/dist/security/score.js.map +1 -0
  146. package/dist/security/test-security.d.ts +22 -0
  147. package/dist/security/test-security.d.ts.map +1 -0
  148. package/dist/security/test-security.js +154 -0
  149. package/dist/security/test-security.js.map +1 -0
  150. package/dist/security/threatDetector.d.ts +39 -0
  151. package/dist/security/threatDetector.d.ts.map +1 -0
  152. package/dist/security/threatDetector.js +354 -0
  153. package/dist/security/threatDetector.js.map +1 -0
  154. package/dist/security/validation.d.ts +69 -0
  155. package/dist/security/validation.d.ts.map +1 -0
  156. package/dist/security/validation.js +286 -0
  157. package/dist/security/validation.js.map +1 -0
  158. package/dist/server/api/clientsRoute.d.ts +9 -0
  159. package/dist/server/api/clientsRoute.d.ts.map +1 -0
  160. package/dist/server/api/clientsRoute.js +71 -0
  161. package/dist/server/api/clientsRoute.js.map +1 -0
  162. package/dist/server/api/endpointsRoute.d.ts +8 -0
  163. package/dist/server/api/endpointsRoute.d.ts.map +1 -0
  164. package/dist/server/api/endpointsRoute.js +76 -0
  165. package/dist/server/api/endpointsRoute.js.map +1 -0
  166. package/dist/server/api/graphql.d.ts +9 -0
  167. package/dist/server/api/graphql.d.ts.map +1 -0
  168. package/dist/server/api/graphql.js +180 -0
  169. package/dist/server/api/graphql.js.map +1 -0
  170. package/dist/server/errors.d.ts +19 -0
  171. package/dist/server/errors.d.ts.map +1 -0
  172. package/dist/server/errors.js +42 -0
  173. package/dist/server/errors.js.map +1 -0
  174. package/dist/server/index.d.ts +7 -0
  175. package/dist/server/index.d.ts.map +1 -0
  176. package/dist/server/index.js +24 -0
  177. package/dist/server/index.js.map +1 -0
  178. package/dist/server/kvStore.d.ts +27 -0
  179. package/dist/server/kvStore.d.ts.map +1 -0
  180. package/dist/server/kvStore.js +128 -0
  181. package/dist/server/kvStore.js.map +1 -0
  182. package/dist/server/runtimeSecurity.d.ts +28 -0
  183. package/dist/server/runtimeSecurity.d.ts.map +1 -0
  184. package/dist/server/runtimeSecurity.js +85 -0
  185. package/dist/server/runtimeSecurity.js.map +1 -0
  186. package/dist/server/types.d.ts +53 -0
  187. package/dist/server/types.d.ts.map +1 -0
  188. package/dist/server/types.js +8 -0
  189. package/dist/server/types.js.map +1 -0
  190. package/dist/types/executionContext.d.ts +16 -0
  191. package/dist/types/executionContext.d.ts.map +1 -0
  192. package/dist/types/executionContext.js +3 -0
  193. package/dist/types/executionContext.js.map +1 -0
  194. package/package.json +77 -0
  195. package/src/agents/agentRegistry.ts +272 -0
  196. package/src/agents/image-agent.yaml +86 -0
  197. package/src/agents/joker-agent.yaml +47 -0
  198. package/src/agents/translator-agent.yaml +80 -0
  199. package/src/audit/auditTrail.ts +134 -0
  200. package/src/compliance/gdprEngine.ts +209 -0
  201. package/src/compliance/lgpdEngine.ts +268 -0
  202. package/src/config.ts +179 -0
  203. package/src/errors.ts +35 -0
  204. package/src/firebase/tenantManager.ts +443 -0
  205. package/src/index.ts +125 -0
  206. package/src/integration/secure-yaml-runtime.ts +341 -0
  207. package/src/parser/secure-yaml-parser.ts +273 -0
  208. package/src/performance/autoscaling.ts +495 -0
  209. package/src/performance/benchmark.ts +644 -0
  210. package/src/performance/index.ts +34 -0
  211. package/src/performance/monitor.ts +469 -0
  212. package/src/performance/streaming.ts +317 -0
  213. package/src/runtime/audit.ts +907 -0
  214. package/src/runtime/declarativeAgentRuntime.ts +836 -0
  215. package/src/runtime/isolatedRuntime.ts +572 -0
  216. package/src/runtime/schemaCompiler.ts +228 -0
  217. package/src/runtime/simpleRuntime.ts +201 -0
  218. package/src/security/dashboard.ts +462 -0
  219. package/src/security/hardening.ts +560 -0
  220. package/src/security/index.ts +439 -0
  221. package/src/security/monitor.ts +490 -0
  222. package/src/security/scanner.ts +368 -0
  223. package/src/security/score.ts +1138 -0
  224. package/src/security/threatDetector.ts +481 -0
  225. package/src/security/validation.ts +365 -0
  226. package/src/server/api/clientsRoute.ts +92 -0
  227. package/src/server/api/endpointsRoute.ts +97 -0
  228. package/src/server/api/graphql.ts +249 -0
  229. package/src/server/errors.ts +38 -0
  230. package/src/server/index.ts +6 -0
  231. package/src/server/kvStore.ts +152 -0
  232. package/src/server/runtimeSecurity.ts +102 -0
  233. package/src/server/types.ts +60 -0
  234. package/src/types/executionContext.ts +16 -0
  235. package/tools/seed.ts +365 -0
  236. package/tools/test-endpoints.ts +174 -0
@@ -0,0 +1,462 @@
1
+ import { SecurityEvent, AlertLevel } from "./monitor";
2
+ import { ThreatAnalysis } from "./threatDetector";
3
+
4
+ export interface DashboardConfig {
5
+ refreshInterval: number;
6
+ maxDisplayEvents: number;
7
+ highlightThreshold: number;
8
+ enableRealTimeUpdates: boolean;
9
+ }
10
+
11
+ export interface SecurityMetric {
12
+ timestamp: Date;
13
+ tenantId: string;
14
+ metricType: string;
15
+ value: number;
16
+ riskLevel: "low" | "medium" | "high" | "critical";
17
+ description: string;
18
+ }
19
+
20
+ export interface SecurityDashboardData {
21
+ summary: SecuritySummary;
22
+ activeAlerts: SecurityEvent[];
23
+ recentThreats: ThreatAnalysis[];
24
+ tenantMetrics: TenantMetrics;
25
+ complianceStatus: ComplianceStatus;
26
+ realTimeChart: RealTimeChartData;
27
+ }
28
+
29
+ export interface SecuritySummary {
30
+ totalEvents: number;
31
+ totalAlerts: number;
32
+ threatEvents: number;
33
+ blockedOperations: number;
34
+ securityScore: number;
35
+ lastUpdate: Date;
36
+ }
37
+
38
+ export interface TenantMetrics {
39
+ [tenantId: string]: {
40
+ totalOperations: number;
41
+ threatCount: number;
42
+ riskScore: number;
43
+ lastActivity: Date;
44
+ alerts: SecurityEvent[];
45
+ };
46
+ }
47
+
48
+ export interface ComplianceStatus {
49
+ lgpd: ComplianceMetric;
50
+ gdpr: ComplianceMetric;
51
+ audit: ComplianceMetric;
52
+ }
53
+
54
+ export interface ComplianceMetric {
55
+ status: "compliant" | "warning" | "violation";
56
+ events: number;
57
+ lastCheck: Date;
58
+ score: number;
59
+ }
60
+
61
+ export interface RealTimeChartData {
62
+ labels: string[];
63
+ datasets: {
64
+ label: string;
65
+ data: number[];
66
+ borderColor: string;
67
+ backgroundColor: string;
68
+ }[];
69
+ }
70
+
71
+ export class SecurityDashboard {
72
+ private config: DashboardConfig;
73
+ private events: SecurityEvent[] = [];
74
+ private metrics: SecurityMetric[] = [];
75
+ private startTime: Date;
76
+ private updateInterval: NodeJS.Timeout | null = null;
77
+
78
+ constructor(config: Partial<DashboardConfig> = {}) {
79
+ this.config = {
80
+ refreshInterval: 5000,
81
+ maxDisplayEvents: 50,
82
+ highlightThreshold: 0.7,
83
+ enableRealTimeUpdates: true,
84
+ ...config,
85
+ };
86
+ this.startTime = new Date();
87
+ }
88
+
89
+ public initialize(): void {
90
+ console.log("šŸ›”ļø Security Dashboard initialized");
91
+ if (this.config.enableRealTimeUpdates) {
92
+ this.startRealTimeUpdates();
93
+ }
94
+ }
95
+
96
+ private startRealTimeUpdates(): void {
97
+ if (this.updateInterval) {
98
+ clearInterval(this.updateInterval);
99
+ }
100
+
101
+ this.updateInterval = setInterval(() => {
102
+ this.generateMetrics();
103
+ }, this.config.refreshInterval);
104
+ }
105
+
106
+ public stopRealTimeUpdates(): void {
107
+ if (this.updateInterval) {
108
+ clearInterval(this.updateInterval);
109
+ this.updateInterval = null;
110
+ }
111
+ }
112
+
113
+ public addEvent(event: SecurityEvent): void {
114
+ this.events.push(event);
115
+
116
+ // Keep only recent events
117
+ if (this.events.length > this.config.maxDisplayEvents * 2) {
118
+ this.events = this.events.slice(-this.config.maxDisplayEvents * 2);
119
+ }
120
+ }
121
+
122
+ public addMetric(metric: SecurityMetric): void {
123
+ this.metrics.push(metric);
124
+
125
+ // Keep metrics for last 24 hours only
126
+ const cutoff = new Date(Date.now() - 24 * 60 * 60 * 1000);
127
+ this.metrics = this.metrics.filter((m) => m.timestamp > cutoff);
128
+ }
129
+
130
+ public generateMetrics(): void {
131
+ const now = new Date();
132
+
133
+ // Generate security summary
134
+ const summary: SecuritySummary = {
135
+ totalEvents: this.events.length,
136
+ totalAlerts: this.events.filter((e) => e.riskScore > 0.7).length,
137
+ threatEvents: this.events.filter((e) => e.riskScore > 0.8).length,
138
+ blockedOperations: this.events.filter(
139
+ (e) => e.alertLevel === AlertLevel.EMERGENCY
140
+ ).length,
141
+ securityScore: this.calculateSecurityScore(),
142
+ lastUpdate: now,
143
+ };
144
+
145
+ // Generate tenant metrics
146
+ const tenantMetrics = this.generateTenantMetrics();
147
+
148
+ // Generate compliance status
149
+ const complianceStatus = this.generateComplianceStatus();
150
+
151
+ // Generate real-time chart data
152
+ const realTimeChart = this.generateRealTimeChart();
153
+
154
+ // Create dashboard data
155
+ const dashboardData: SecurityDashboardData = {
156
+ summary,
157
+ activeAlerts: this.getActiveAlerts(),
158
+ recentThreats: this.getRecentThreats(),
159
+ tenantMetrics,
160
+ complianceStatus,
161
+ realTimeChart,
162
+ };
163
+
164
+ // Log dashboard update
165
+ this.logDashboardUpdate(dashboardData);
166
+ }
167
+
168
+ private calculateSecurityScore(): number {
169
+ if (this.events.length === 0) return 10.0;
170
+
171
+ const recentEvents = this.events.filter(
172
+ (e) => new Date(e.timestamp).getTime() > Date.now() - 24 * 60 * 60 * 1000
173
+ );
174
+
175
+ if (recentEvents.length === 0) return 9.5;
176
+
177
+ // Calculate based on threat ratios
178
+ const totalEvents = recentEvents.length;
179
+ const highRiskEvents = recentEvents.filter((e) => e.riskScore > 0.7).length;
180
+ const mediumRiskEvents = recentEvents.filter(
181
+ (e) => e.riskScore > 0.4 && e.riskScore <= 0.7
182
+ ).length;
183
+
184
+ const highRiskRatio = highRiskEvents / totalEvents;
185
+ const mediumRiskRatio = mediumRiskEvents / totalEvents;
186
+
187
+ // Start with perfect score
188
+ let score = 10.0;
189
+
190
+ // Penalties based on risk ratios
191
+ score -= highRiskRatio * 3.0; // High risk events cost 3 points
192
+ score -= mediumRiskRatio * 1.5; // Medium risk events cost 1.5 points
193
+
194
+ return Math.max(score, 5.0); // Minimum score of 5.0
195
+ }
196
+
197
+ private generateTenantMetrics(): TenantMetrics {
198
+ const metrics: TenantMetrics = {};
199
+ const tenantIds = Array.from(new Set(this.events.map((e) => e.tenantId)));
200
+
201
+ tenantIds.forEach((tenantId) => {
202
+ const tenantEvents = this.events.filter((e) => e.tenantId === tenantId);
203
+
204
+ metrics[tenantId] = {
205
+ totalOperations: tenantEvents.length,
206
+ threatCount: tenantEvents.filter((e) => e.riskScore > 0.6).length,
207
+ riskScore: this.calculateTenantRiskScore(tenantEvents),
208
+ lastActivity:
209
+ tenantEvents.length > 0
210
+ ? new Date(
211
+ Math.max(
212
+ ...tenantEvents.map((e) => new Date(e.timestamp).getTime())
213
+ )
214
+ )
215
+ : new Date(),
216
+ alerts: tenantEvents.slice(-5), // Last 5 events
217
+ };
218
+ });
219
+
220
+ return metrics;
221
+ }
222
+
223
+ private calculateTenantRiskScore(events: SecurityEvent[]): number {
224
+ if (events.length === 0) return 0.0;
225
+
226
+ const recentEvents = events.filter(
227
+ (e) => new Date(e.timestamp).getTime() > Date.now() - 6 * 60 * 60 * 1000 // Last 6 hours
228
+ );
229
+
230
+ if (recentEvents.length === 0) return 0.1;
231
+
232
+ const avgRiskScore =
233
+ recentEvents.reduce((sum, e) => sum + e.riskScore, 0) /
234
+ recentEvents.length;
235
+ const maxRiskScore = Math.max(...recentEvents.map((e) => e.riskScore));
236
+
237
+ // Weighted average with emphasis on maximum risk
238
+ return avgRiskScore * 0.7 + maxRiskScore * 0.3;
239
+ }
240
+
241
+ private generateComplianceStatus(): ComplianceStatus {
242
+ const now = new Date();
243
+
244
+ // LGPD compliance check
245
+ const lgpdEvents = this.events.filter(
246
+ (e) => e.metadata && e.metadata.lgpdRelevant && e.riskScore > 0.5
247
+ );
248
+
249
+ // GDPR compliance check
250
+ const gdprEvents = this.events.filter(
251
+ (e) => e.metadata && e.metadata.gdprRelevant && e.riskScore > 0.5
252
+ );
253
+
254
+ // Audit compliance check
255
+ const auditEvents = this.events.filter(
256
+ (e) => e.operation.includes("audit") || e.operation.includes("log")
257
+ );
258
+
259
+ return {
260
+ lgpd: {
261
+ status:
262
+ lgpdEvents.length > 0
263
+ ? lgpdEvents.some((e: SecurityEvent) => e.riskScore > 0.8)
264
+ ? "violation"
265
+ : "warning"
266
+ : "compliant",
267
+ events: lgpdEvents.length,
268
+ lastCheck: now,
269
+ score:
270
+ lgpdEvents.length > 0
271
+ ? lgpdEvents.some((e) => e.riskScore > 0.8)
272
+ ? 3.0
273
+ : 6.0
274
+ : 9.0,
275
+ },
276
+ gdpr: {
277
+ status:
278
+ gdprEvents.length > 0
279
+ ? gdprEvents.some((e) => e.riskScore > 0.8)
280
+ ? "violation"
281
+ : "warning"
282
+ : "compliant",
283
+ events: gdprEvents.length,
284
+ lastCheck: now,
285
+ score:
286
+ gdprEvents.length > 0
287
+ ? gdprEvents.some((e) => e.riskScore > 0.8)
288
+ ? 3.0
289
+ : 6.0
290
+ : 9.0,
291
+ },
292
+ audit: {
293
+ status: auditEvents.length > 0 ? "compliant" : "warning",
294
+ events: auditEvents.length,
295
+ lastCheck: now,
296
+ score: 8.5,
297
+ },
298
+ };
299
+ }
300
+
301
+ private generateRealTimeChart(): RealTimeChartData {
302
+ const now = Date.now();
303
+ const timeLabels: string[] = [];
304
+ const riskData: number[] = [];
305
+ const eventData: number[] = [];
306
+
307
+ // Generate data for last 30 minutes (5-minute intervals)
308
+ for (let i = 5; i >= 0; i--) {
309
+ const time = new Date(now - i * 5 * 60 * 1000);
310
+ timeLabels.push(time.toLocaleTimeString("pt-BR"));
311
+
312
+ const intervalStart = new Date(time.getTime() - 5 * 60 * 1000);
313
+ const intervalEvents = this.events.filter((e) => {
314
+ const eventTime = new Date(e.timestamp).getTime();
315
+ return (
316
+ eventTime >= intervalStart.getTime() && eventTime <= time.getTime()
317
+ );
318
+ });
319
+
320
+ const avgRiskScore =
321
+ intervalEvents.length > 0
322
+ ? intervalEvents.reduce((sum, e) => sum + e.riskScore, 0) /
323
+ intervalEvents.length
324
+ : 0;
325
+
326
+ riskData.push(Math.round(avgRiskScore * 100) / 100);
327
+ eventData.push(intervalEvents.length);
328
+ }
329
+
330
+ return {
331
+ labels: timeLabels,
332
+ datasets: [
333
+ {
334
+ label: "Average Risk Score",
335
+ data: riskData,
336
+ borderColor: "#8884d8",
337
+ backgroundColor: "#8884d840",
338
+ },
339
+ {
340
+ label: "Event Count",
341
+ data: eventData,
342
+ borderColor: "#82ca9d",
343
+ backgroundColor: "#82ca9d40",
344
+ },
345
+ ],
346
+ };
347
+ }
348
+
349
+ private getActiveAlerts(): SecurityEvent[] {
350
+ return this.events
351
+ .filter((e) => e.riskScore >= this.config.highlightThreshold)
352
+ .slice(-10); // Last 10 high-risk events
353
+ }
354
+
355
+ private getRecentThreats(): ThreatAnalysis[] {
356
+ // Return mock threat analysis for recent events
357
+ return this.events.slice(-5).map((event) => ({
358
+ riskScore: event.riskScore,
359
+ threatType: `potential_${event.operation}`,
360
+ confidence: 0.8,
361
+ recommendations: [
362
+ `Monitor tenant ${event.tenantId}`,
363
+ "Check access logs",
364
+ ],
365
+ }));
366
+ }
367
+
368
+ private logDashboardUpdate(data: SecurityDashboardData): void {
369
+ const { summary } = data;
370
+
371
+ console.log(
372
+ `\nšŸ›”ļø Security Dashboard Update - ${new Date().toISOString()}`
373
+ );
374
+ console.log(` šŸ“Š Total Events: ${summary.totalEvents}`);
375
+ console.log(` 🚨 Total Alerts: ${summary.totalAlerts}`);
376
+ console.log(` šŸŽÆ Security Score: ${summary.securityScore}/10`);
377
+ console.log(` šŸ”’ Threat Events: ${summary.threatEvents}`);
378
+ console.log(` 🚫 Blocked Operations: ${summary.blockedOperations}`);
379
+
380
+ // Log tenant summaries
381
+ const tenantEntries = Object.entries(data.tenantMetrics);
382
+ tenantEntries.forEach(([tenantId, metrics]) => {
383
+ console.log(
384
+ ` šŸ¢ ${tenantId}: ${metrics.riskScore.toFixed(1)} risk score, ${
385
+ metrics.threatCount
386
+ } threats`
387
+ );
388
+ });
389
+ }
390
+
391
+ public getDashboardData(): SecurityDashboardData {
392
+ this.generateMetrics();
393
+
394
+ return {
395
+ summary: {
396
+ totalEvents: this.events.length,
397
+ totalAlerts: this.events.filter((e) => e.riskScore > 0.7).length,
398
+ threatEvents: this.events.filter((e) => e.riskScore > 0.8).length,
399
+ blockedOperations: this.events.filter(
400
+ (e) => e.alertLevel === AlertLevel.EMERGENCY
401
+ ).length,
402
+ securityScore: this.calculateSecurityScore(),
403
+ lastUpdate: new Date(),
404
+ },
405
+ activeAlerts: this.getActiveAlerts(),
406
+ recentThreats: this.getRecentThreats(),
407
+ tenantMetrics: this.generateTenantMetrics(),
408
+ complianceStatus: this.generateComplianceStatus(),
409
+ realTimeChart: this.generateRealTimeChart(),
410
+ };
411
+ }
412
+
413
+ public getSecurityMetrics(): SecurityMetric[] {
414
+ return this.metrics.slice(-20); // Last 20 metrics
415
+ }
416
+
417
+ public getEventHistory(limit: number = 100): SecurityEvent[] {
418
+ return this.events.slice(-limit);
419
+ }
420
+
421
+ public exportDashboardReport(): string {
422
+ const data = this.getDashboardData();
423
+ const report = [
424
+ "# Security Dashboard Report",
425
+ `Generated: ${new Date().toISOString()}`,
426
+ "",
427
+ "## Executive Summary",
428
+ `- Security Score: ${data.summary.securityScore}/10`,
429
+ `- Total Events: ${data.summary.totalEvents}`,
430
+ `- Active Alerts: ${data.summary.totalAlerts}`,
431
+ `- Threat Events: ${data.summary.threatEvents}`,
432
+ ``,
433
+ "## Tenant Status",
434
+ ...Object.entries(data.tenantMetrics).map(
435
+ ([tenant, metrics]) =>
436
+ `- ${tenant}: ${metrics.riskScore.toFixed(1)} risk, ${
437
+ metrics.threatCount
438
+ } threats`
439
+ ),
440
+ ``,
441
+ "## Compliance Status",
442
+ `- LGPD: ${data.complianceStatus.lgpd.status} (${data.complianceStatus.lgpd.score}/10)`,
443
+ `- GDPR: ${data.complianceStatus.gdpr.status} (${data.complianceStatus.gdpr.score}/10)`,
444
+ `- Audit: ${data.complianceStatus.audit.status} (${data.complianceStatus.audit.score}/10)`,
445
+ ``,
446
+ "## Recent High-Risk Events",
447
+ ...data.activeAlerts
448
+ .slice(-5)
449
+ .map(
450
+ (alert, index) =>
451
+ `${index + 1}. ${alert.tenantId} - ${
452
+ alert.operation
453
+ } (Risk: ${alert.riskScore.toFixed(1)})`
454
+ ),
455
+ ];
456
+
457
+ return report.join("\n");
458
+ }
459
+ }
460
+
461
+ // Singleton instance
462
+ export const securityDashboard = new SecurityDashboard();