|
| 1 | +import { incrementCounter } from "../observability/metrics.js"; |
| 2 | +import { COUNTER_NAMES } from "../observability/metricTypes.js"; |
| 3 | +import type { DecisionResult } from "./engagementDecision.js"; |
| 4 | +import type { ConsentResult } from "./consentEvaluator.js"; |
| 5 | +import type { EnergyScore } from "./energyEvaluator.js"; |
| 6 | + |
| 7 | +export function recordConsentDecision(consent: ConsentResult): void { |
| 8 | + if (consent.state === "OPTOUT") { |
| 9 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_BLOCK_OPTOUT_TOTAL); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + if (consent.state === "BLOCKED") { |
| 14 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_BLOCK_POLICY_TOTAL); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +export function recordEngagementDecision( |
| 19 | + decision: DecisionResult, |
| 20 | + consent: ConsentResult, |
| 21 | + energy?: EnergyScore, |
| 22 | +): void { |
| 23 | + switch (decision.reason) { |
| 24 | + case "AUTH_INVALID": |
| 25 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_BLOCK_AUTH_INVALID_TOTAL); |
| 26 | + return; |
| 27 | + case "AI_APPROVAL_MISSING": |
| 28 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_BLOCK_AI_APPROVAL_MISSING_TOTAL); |
| 29 | + return; |
| 30 | + case "BLOCKED_BY_POLICY": |
| 31 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_BLOCK_POLICY_TOTAL); |
| 32 | + return; |
| 33 | + case "OPTOUT_PRESENT": |
| 34 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_BLOCK_OPTOUT_TOTAL); |
| 35 | + return; |
| 36 | + case "ALREADY_REPLIED": |
| 37 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_SKIP_ALREADY_REPLIED_TOTAL); |
| 38 | + return; |
| 39 | + case "NO_CONSENT": |
| 40 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_SKIP_NO_CONSENT_TOTAL); |
| 41 | + return; |
| 42 | + case "NO_WRITE_BUDGET": |
| 43 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_HOLD_NO_BUDGET_TOTAL); |
| 44 | + return; |
| 45 | + case "TARGET_MISSING": |
| 46 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_SKIP_TARGET_MISSING_TOTAL); |
| 47 | + return; |
| 48 | + case "DEAD_SIGNAL": |
| 49 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_SKIP_NO_CONSENT_TOTAL); |
| 50 | + return; |
| 51 | + case "LOW_ENERGY": |
| 52 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_HOLD_LOW_ENERGY_TOTAL); |
| 53 | + return; |
| 54 | + case "REVIEW_REQUIRED": |
| 55 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_REVIEW_TOTAL); |
| 56 | + return; |
| 57 | + case "ENGAGE_VALID_CONSENT": |
| 58 | + case "ENGAGE_STRONG_CONSENT": |
| 59 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_ENGAGE_TOTAL); |
| 60 | + return; |
| 61 | + default: |
| 62 | + if (decision.decision === "ENGAGE") { |
| 63 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_ENGAGE_TOTAL); |
| 64 | + } else if (decision.decision === "REVIEW") { |
| 65 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_REVIEW_TOTAL); |
| 66 | + } else if (decision.decision === "HOLD") { |
| 67 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_HOLD_LOW_ENERGY_TOTAL); |
| 68 | + } else if (decision.decision === "SKIP") { |
| 69 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_SKIP_NO_CONSENT_TOTAL); |
| 70 | + } |
| 71 | + return; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export function recordEnergyBand(energy: EnergyScore): void { |
| 76 | + if (energy.band === "E1") { |
| 77 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_HOLD_LOW_ENERGY_TOTAL); |
| 78 | + } else if (energy.band === "E0") { |
| 79 | + incrementCounter(COUNTER_NAMES.ENGAGEMENT_DECISION_SKIP_NO_CONSENT_TOTAL); |
| 80 | + } |
| 81 | +} |
0 commit comments