MATIH Platform is in active MVP development. Documentation reflects current implementation status.
8. Platform Services
Realtime Cost

Realtime Cost

The RealtimeCostController and RealtimeCostTrackingService provide real-time cost monitoring and alerting. Cost metrics are updated continuously as usage events flow through the system, giving tenants immediate visibility into their current spending.


Real-Time Cost Metrics

Get Current Costs

Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/current

Returns the current cost accumulation for the active billing period.

curl http://localhost:8087/api/v1/billing/realtime-cost/tenants/550e8400/current \
  -H "Authorization: Bearer ${TOKEN}"

Cost by Resource Type

Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/by-resource

Returns real-time cost broken down by resource type (compute, storage, API calls, etc.).

Cost Timeline

Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/timeline

Returns cost accumulation over time for the current billing period, suitable for chart rendering.


RealtimeCostMetric Entity

FieldTypeDescription
idUUIDMetric identifier
tenantIdUUIDOwning tenant
metricTypeStringCost category
currentValueBigDecimalCurrent accumulated cost
previousValueBigDecimalPrevious period value for comparison
thresholdBigDecimalAlert threshold
unitStringCurrency unit
timestampLocalDateTimeLast update time

Cost Alerts

Configure Alert

Endpoint: POST /api/v1/billing/realtime-cost/tenants/:tenantId/alerts

curl -X POST http://localhost:8087/api/v1/billing/realtime-cost/tenants/550e8400/alerts \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{
    "thresholdAmount": 5000.00,
    "thresholdType": "ABSOLUTE",
    "notifyEmail": "finance@acme.com",
    "notifySlack": true
  }'

Alert Types

TypeDescription
ABSOLUTEAlert when total cost exceeds a fixed amount
PERCENTAGE_OF_BUDGETAlert when cost exceeds a percentage of the monthly budget
DAILY_RATEAlert when the daily cost rate exceeds a threshold
ANOMALYAlert when cost pattern deviates significantly from baseline

List Active Alerts

Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/alerts

Dismiss Alert

Endpoint: POST /api/v1/billing/realtime-cost/alerts/:alertId/dismiss


Cost Tracking Flow

Usage Events (Kafka)
       |
       v
UsageMeteringService --> RealtimeCostTrackingService
       |                         |
       v                         v
 UsageRecord (DB)        RealtimeCostMetric (DB)
                                 |
                                 v
                        Cost Alert Evaluation
                                 |
                                 v
                     Notification Service (email/slack)

As usage events are recorded, the real-time cost tracking service calculates the cost impact based on the active pricing plan and updates the running totals. When thresholds are crossed, alerts are triggered through the Notification Service.