Files
cloud-services/services/cost/README.md
2026-02-02 21:38:02 -05:00

166 lines
7.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Cost Service
Per-vehicle cost estimation service for capacity planning and cloud vs on-prem comparison.
## Overview
This service estimates the cost of running cloud services per VIN by:
1. Querying vehicle activity from ClickHouse (message counts)
2. Estimating resource usage based on activity level
3. Applying cost rates for cloud vs on-prem hosting
4. Storing aggregated cost data for reporting
## Cost Estimation Methodology
### Resource Estimates Per Active VIN
| Activity Level | Messages/15min | CPU (cores) | Memory (GB) |
|---------------|----------------|-------------|-------------|
| Low | < 100 | 0.80 | 1.20 |
| Medium | 100-1000 | 1.20 | 1.80 |
| High | > 1000 | 1.60 | 2.40 |
These estimates account for the full data pipeline per vehicle:
- Data ingestion (MQTT/HTTP endpoints)
- Kafka message processing
- Stream processing and transformations
- ClickHouse storage and queries
- Redis caching
- MongoDB document storage
- API serving
### Base Infrastructure Costs
Shared infrastructure costs are distributed across active vehicles each collection interval:
| Component | Cloud ($/15min) | On-Prem ($/15min) |
|-----------|-----------------|-------------------|
| Storage, Event Hubs, Defender, monitoring | $10.00 | $0.90 |
### Cost Rates (per hour)
| Resource | Cloud (Azure) | On-Prem |
|----------|---------------|---------|
| CPU/core | $0.30 | $0.02 |
| Memory/GB| $0.08 | $0.005 |
#### Cloud Rates (Fudged Higher)
- Based on Azure D-series VM pricing + 50% managed services overhead
- Includes: AKS compute, managed Kafka (Event Hubs), CosmosDB, Azure Storage, networking, monitoring
- Intentionally conservative (higher) to show true cloud TCO
#### On-Prem Rates (Fudged Lower)
- Based on 3-year hardware amortization only
- Assumes: owned hardware, minimal ops overhead
- Intentionally optimistic (lower) to show on-prem savings
- Does NOT include: datacenter costs, staff, power, cooling, network, maintenance
### Savings Calculation
```
Per-VIN Cost = (CPU_cores × rate + Memory_GB × rate) × hours + (base_infra / active_vins)
Cloud Cost = Per-VIN costs summed across fleet
On-Prem Cost = Same formula with on-prem rates
Savings = Cloud Cost - On-Prem Cost
Savings % = (Savings / Cloud Cost) × 100
```
Expected savings: **~85-88%** with on-prem hosting (hardware costs only).
### Projected Annual Costs (5000 vehicles)
Based on ~$100k/month cloud spend:
| Metric | Cloud | On-Prem |
|--------|-------|---------|
| Monthly Cost | ~$100,000 | ~$9,500 |
| Annual Cost | ~$1,200,000 | ~$114,000 |
| Per Vehicle/Month | ~$20.00 | ~$1.90 |
| Annual Savings | ~$1,086,000 (90%) | - |
## API Endpoints
### GET /cost/vin/{vin}
Cost summary for a specific VIN.
### GET /cost/fleet
Fleet-wide cost summary with top cost VINs.
### GET /cost/summary?period=day|week|month
High-level cost summary for a time period.
### GET /cost/comparison
Cloud vs on-prem cost comparison with projected annual savings.
### GET /cost/report
Plain text report for terminal viewing.
## Accessing the Report
The service is deployed internally on cec-prd-cluster-1 (no public ingress). To view the report:
```bash
# Quick one-liner
kubectl --context cec-prd-cluster-1 run curl-test --image=curlimages/curl --rm -it --restart=Never -- curl -s http://cost.default.svc.cluster.local:8077/cost/report
# Or port-forward and curl locally
kubectl --context cec-prd-cluster-1 port-forward svc/cost 8077:8077 &
curl http://localhost:8077/cost/report
```
## Example Report Output
```
╔══════════════════════════════════════════════════════════════════╗
║ COST SERVICE REPORT ║
╠══════════════════════════════════════════════════════════════════╣
║ Period: 2026-01-03 to 2026-02-03
╠══════════════════════════════════════════════════════════════════╣
║ FLEET OVERVIEW ║
║ ─────────────────────────────────────────────────────────────── ║
║ Active Vehicles: 3143
║ Cloud Cost: $5658.12
║ On-Prem Cost: $389.98
║ Savings: $5268.15 (93.1%)
╠══════════════════════════════════════════════════════════════════╣
║ COST RATES ║
║ ─────────────────────────────────────────────────────────────── ║
║ Cloud: CPU $0.30/core-hr Memory $0.080/GB-hr
║ On-Prem: CPU $0.02/core-hr Memory $0.005/GB-hr
║ Base Infra: Cloud $10.00/15min On-Prem $0.90/15min
╠══════════════════════════════════════════════════════════════════╣
║ ANNUAL PROJECTION (based on current usage) ║
║ ─────────────────────────────────────────────────────────────── ║
║ Cloud Annual: $67897.44
║ On-Prem Annual: $4679.70
║ Annual Savings: $63217.74
╚══════════════════════════════════════════════════════════════════╝
TOP COST VEHICLES:
VIN Cloud $ On-Prem $ Savings %
─────────────────── ────────── ────────── ────────
VCF1EBU27PG008191 6.84 0.48 93.0%
VCF1ZBU28PG005207 6.84 0.48 93.0%
VCF1ZBU29PG005488 6.84 0.48 93.0%
VCF1ZBU26PG004962 6.76 0.48 93.0%
VCF1EBU20PG008145 6.75 0.47 93.0%
...
```
*Note: Report generated 2026-02-03. Costs accumulate over time as the collector runs every 15 minutes.*
## Configuration
| Env Var | Description | Default |
|---------|-------------|---------|
| CLICKHOUSE_HOST | Local CH for storing cost data | localhost |
| REMOTE_CLICKHOUSE_HOST | Dev cluster CH for VIN activity | - |
| COLLECTOR_INTERVAL_MINUTES | How often to collect metrics | 15 |
## Limitations
- Resource estimates are approximations, not actual measurements
- Cost rates are simplified and don't reflect all real-world factors
- On-prem costs exclude significant operational overhead
- Designed for business case illustration, not precise billing