All files / src/lib/observability slo-utils.ts

0% Statements 0/106
100% Branches 0/0
0% Functions 0/1
0% Lines 0/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                                                                                                                                                                                     
/**
 * SLO Utility Functions (Client-Safe)
 *
 * Pure utility functions for SLO display that can be safely used in client components.
 * These functions have no server-side dependencies.
 */

/**
 * SLO status type
 */
export type SLOStatus = 'healthy' | 'warning' | 'critical';

/**
 * SLO window type
 */
export type SLOWindow = 'hourly' | 'daily' | 'weekly' | 'monthly';

/**
 * SLO category type
 */
export type SLOCategory = 'api' | 'checkout' | 'search' | 'overall';

/**
 * Trend data point for charting
 */
export interface SLOTrendPoint {
  timestamp: string;
  value: number;
  status: string;
}

/**
 * SLO calculation result (client-safe type definition)
 */
export interface SLOResult {
  /** SLO name */
  name: string;
  /** Current value as percentage */
  current: number;
  /** Target value as percentage */
  target: number;
  /** Error budget (100 - target) */
  budget: number;
  /** Remaining error budget */
  budgetRemaining: number;
  /** Budget consumed percentage */
  budgetConsumed: number;
  /** Current status */
  status: SLOStatus;
  /** Time window */
  window: SLOWindow;
  /** Category */
  category: SLOCategory;
  /** Timestamp of calculation */
  calculatedAt: Date;
}

/**
 * Get SLO status color for UI
 */
export function getSLOStatusColor(status: SLOStatus): string {
  switch (status) {
    case 'healthy':
      return 'green';
    case 'warning':
      return 'yellow';
    case 'critical':
      return 'red';
    default:
      return 'gray';
  }
}

/**
 * Get SLO status text
 */
export function getSLOStatusText(status: SLOStatus): string {
  switch (status) {
    case 'healthy':
      return 'Meeting Target';
    case 'warning':
      return 'At Risk';
    case 'critical':
      return 'Breaching';
    default:
      return 'Unknown';
  }
}

/**
 * Format window for display
 */
export function formatWindow(window: SLOWindow): string {
  switch (window) {
    case 'hourly':
      return 'Last Hour';
    case 'daily':
      return 'Last 24 Hours';
    case 'weekly':
      return 'Last 7 Days';
    case 'monthly':
      return 'Last 30 Days';
    default:
      return window;
  }
}