|
1 | 1 | import { ManagedResourceItem, Condition } from '../../lib/shared/types'; |
2 | 2 | import { APIError } from '../../lib/api/error'; |
3 | | -import { HintSegmentCalculator, HintState } from './types'; |
| 3 | +import { HintSegmentCalculator, HintState, HoverDataCalculator } from './types'; |
| 4 | +import { HoverContentProps } from './HoverContent'; |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * Common colors used across all hints |
@@ -313,3 +314,132 @@ export const calculateCrossplaneHoverData = (allItems: ManagedResourceItem[]): C |
313 | 314 | }, |
314 | 315 | }; |
315 | 316 | }; |
| 317 | + |
| 318 | +/** |
| 319 | + * Calculate hover data for Crossplane using the generic HoverContent structure |
| 320 | + * Shows healthy resources (the positive segment) |
| 321 | + */ |
| 322 | +export const calculateCrossplaneHoverDataGeneric: HoverDataCalculator = ( |
| 323 | + allItems: ManagedResourceItem[], |
| 324 | + enabled: boolean, |
| 325 | + t: (key: string) => string, |
| 326 | +): Omit<HoverContentProps, 'enabled'> | null => { |
| 327 | + if (!enabled || allItems.length === 0) { |
| 328 | + return null; |
| 329 | + } |
| 330 | + |
| 331 | + const { resourceTypeStats, overallStats } = calculateCrossplaneHoverData(allItems); |
| 332 | + |
| 333 | + // Get the segments from the bar chart calculation to ensure color consistency |
| 334 | + const segmentData = calculateCrossplaneSegments(allItems, false, undefined, enabled, t); |
| 335 | + |
| 336 | + const legendItems = segmentData.segments.map(segment => ({ |
| 337 | + label: segment.label, |
| 338 | + count: segment.label === t('common.healthy') ? overallStats.healthy : |
| 339 | + segment.label === t('common.creating') ? overallStats.creating : |
| 340 | + overallStats.unhealthy, |
| 341 | + color: segment.color, |
| 342 | + })); |
| 343 | + |
| 344 | + // Focus on healthy percentage in radar chart (the positive aspect) |
| 345 | + const radarDataset = resourceTypeStats.map((stats) => ({ |
| 346 | + type: stats.type, |
| 347 | + healthy: stats.healthyPercentage, |
| 348 | + })); |
| 349 | + |
| 350 | + // Use the color of the healthy segment (first segment in the bar chart) |
| 351 | + const healthyColor = segmentData.segments.find(s => s.label === t('common.healthy'))?.color || HINT_COLORS.healthy; |
| 352 | + |
| 353 | + return { |
| 354 | + totalCount: overallStats.total, |
| 355 | + totalLabel: t('Hints.CrossplaneHint.hoverContent.totalResources'), |
| 356 | + legendItems, |
| 357 | + radarDataset, |
| 358 | + radarDimensions: [{ accessor: 'type' }], |
| 359 | + radarMeasures: [ |
| 360 | + { |
| 361 | + accessor: 'healthy', |
| 362 | + color: healthyColor, |
| 363 | + hideDataLabel: true, |
| 364 | + label: t('Hints.CrossplaneHint.hoverContent.healthy') + ' (%)', |
| 365 | + }, |
| 366 | + ], |
| 367 | + }; |
| 368 | +}; |
| 369 | + |
| 370 | +/** |
| 371 | + * Calculate hover data for GitOps showing resource type management coverage |
| 372 | + * Shows managed resources (the positive segment) |
| 373 | + */ |
| 374 | +export const calculateGitOpsHoverDataGeneric: HoverDataCalculator = ( |
| 375 | + allItems: ManagedResourceItem[], |
| 376 | + enabled: boolean, |
| 377 | + t: (key: string) => string, |
| 378 | +): Omit<HoverContentProps, 'enabled'> | null => { |
| 379 | + if (!enabled || allItems.length === 0) { |
| 380 | + return null; |
| 381 | + } |
| 382 | + |
| 383 | + // Group by resource type and calculate flux management coverage |
| 384 | + const typeStats: Record<string, { total: number; managed: number }> = {}; |
| 385 | + let totalManaged = 0; |
| 386 | + |
| 387 | + allItems.forEach((item: ManagedResourceItem) => { |
| 388 | + const type = item.kind || 'Unknown'; |
| 389 | + |
| 390 | + if (!typeStats[type]) { |
| 391 | + typeStats[type] = { total: 0, managed: 0 }; |
| 392 | + } |
| 393 | + |
| 394 | + typeStats[type].total++; |
| 395 | + |
| 396 | + // Check if the resource is managed by Flux |
| 397 | + if ( |
| 398 | + item?.metadata?.labels && |
| 399 | + Object.prototype.hasOwnProperty.call(item.metadata.labels, 'kustomize.toolkit.fluxcd.io/name') |
| 400 | + ) { |
| 401 | + typeStats[type].managed++; |
| 402 | + totalManaged++; |
| 403 | + } |
| 404 | + }); |
| 405 | + |
| 406 | + const totalUnmanaged = allItems.length - totalManaged; |
| 407 | + |
| 408 | + // Get the segments from the bar chart calculation to ensure color consistency |
| 409 | + const segmentData = calculateGitOpsSegments(allItems, false, undefined, enabled, t); |
| 410 | + |
| 411 | + const legendItems = segmentData.segments.map(segment => ({ |
| 412 | + label: segment.label, |
| 413 | + count: segment.label === t('common.progress') ? totalManaged : totalUnmanaged, |
| 414 | + color: segment.color, |
| 415 | + })); |
| 416 | + |
| 417 | + // Focus on managed percentage in radar chart (the positive aspect) |
| 418 | + const radarDataset = Object.keys(typeStats).map((type) => { |
| 419 | + const stats = typeStats[type]; |
| 420 | + const managedPercentage = Math.round((stats.managed / stats.total) * 100); |
| 421 | + return { |
| 422 | + type, |
| 423 | + managed: managedPercentage, |
| 424 | + }; |
| 425 | + }); |
| 426 | + |
| 427 | + // Use the color of the progress/managed segment (first segment in the bar chart) |
| 428 | + const managedColor = segmentData.segments.find(s => s.label === t('common.progress'))?.color || HINT_COLORS.managed; |
| 429 | + |
| 430 | + return { |
| 431 | + totalCount: allItems.length, |
| 432 | + totalLabel: t('Hints.GitOpsHint.hoverContent.totalResources'), |
| 433 | + legendItems, |
| 434 | + radarDataset, |
| 435 | + radarDimensions: [{ accessor: 'type' }], |
| 436 | + radarMeasures: [ |
| 437 | + { |
| 438 | + accessor: 'managed', |
| 439 | + color: managedColor, |
| 440 | + hideDataLabel: true, |
| 441 | + label: t('Hints.GitOpsHint.hoverContent.managed') + ' (%)', |
| 442 | + }, |
| 443 | + ], |
| 444 | + }; |
| 445 | +}; |
0 commit comments