MATIH Platform is in active MVP development. Documentation reflects current implementation status.
8. Platform Services
Usage Metering

Usage Metering

The UsageMeteringService records individual usage events for all billable resources on the platform. Usage records track API calls, query executions, storage consumption, compute time, data transfer, and other measurable activities. These records are aggregated by the UsageAggregationService for billing calculations.


Metric Types

Metric TypeUnitDescription
API_CALLScountAPI request invocations
QUERY_EXECUTIONScountSQL/analytics query executions
STORAGE_BYTESbytesData storage consumption
COMPUTE_SECONDSsecondsCompute resource usage
DATA_TRANSFER_BYTESbytesNetwork data transfer
ACTIVE_USERScountUnique active users
ML_INFERENCE_REQUESTScountML model inference calls
DASHBOARD_VIEWScountDashboard view events
PIPELINE_RUNScountData pipeline executions
CONNECTOR_SYNCScountData connector sync operations

Record Usage

Endpoint: POST /api/v1/billing/tenants/:tenantId/usage

curl -X POST http://localhost:8087/api/v1/billing/tenants/550e8400/usage \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{
    "metricType": "QUERY_EXECUTIONS",
    "quantity": 1,
    "unit": "count",
    "resourceId": "query-12345",
    "resourceType": "sql_query",
    "userId": "660e8400-e29b-41d4-a716-446655440000",
    "metadata": "{\"queryType\": \"SELECT\", \"rowsReturned\": 1500}"
  }'

Batch Usage Recording

Endpoint: POST /api/v1/billing/tenants/:tenantId/usage/batch

Records multiple usage events in a single request for better throughput.

curl -X POST http://localhost:8087/api/v1/billing/tenants/550e8400/usage/batch \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '[
    {"metricType": "API_CALLS", "quantity": 100, "unit": "count"},
    {"metricType": "DATA_TRANSFER_BYTES", "quantity": 52428800, "unit": "bytes"}
  ]'

Usage Summary

Endpoint: GET /api/v1/billing/tenants/:tenantId/usage/summary

ParameterTypeDescription
startLocalDateTimeSummary period start
endLocalDateTimeSummary period end
curl "http://localhost:8087/api/v1/billing/tenants/550e8400/usage/summary?start=2026-02-01T00:00:00&end=2026-02-28T23:59:59" \
  -H "Authorization: Bearer ${TOKEN}"

UsageRecord Entity

FieldTypeDescription
idUUIDRecord identifier
tenantIdUUIDOwning tenant
userIdUUIDUser who generated the usage
metricTypeMetricTypeType of usage metric
resourceIdStringSpecific resource identifier
resourceTypeStringType of resource
quantityBigDecimalUsage quantity (precision: 20, scale: 6)
unitStringUnit of measurement
timestampLocalDateTimeWhen the usage occurred
metadataJSONAdditional context
aggregatedbooleanWhether this record has been aggregated
aggregationPeriodStringAggregation period identifier

Aggregation

The UsageAggregationService periodically aggregates raw usage records into UsageAggregate entries for efficient billing calculations. After aggregation, the aggregated flag is set to true on processed records.

Database Indexes

IndexColumnsPurpose
idx_usage_tenant_timestamptenant_id, timestampTenant usage queries
idx_usage_metric_timestampmetric_type, timestampMetric-type queries
idx_usage_aggregatedaggregatedFind un-aggregated records