# Cariflex - Dashboard Grafana

## Datasource FlexMeasures

FlexMeasures n'a pas de datasource Grafana natif. Options :
1. **PostgreSQL direct** : Lire depuis la DB FlexMeasures (sensors, assets, schedules)
2. **API REST FlexMeasures** : Utiliser un datasource JSON générique
3. **InfluxDB** : Les données IoT sont déjà dans InfluxDB

## Dashboard JSON

Le dashboard Cariflex comprend :
- **Panel 1** : Production PV temps réel (10 assets)
- **Panel 2** : État de charge des batteries (10 assets)
- **Panel 3** : Consommation des bornes VE (10 assets)
- **Panel 4** : Flexibilité disponible (agrégée)
- **Panel 5** : Carte des actifs (Geomap)
- **Panel 6** : Schedules FlexMeasures
- **Panel 7** : Revenus flexibilité (PPA + GO + Services système)

## Panels

### PV Production
```json
{
  "title": "Production PV Cariflex",
  "type": "timeseries",
  "datasource": "InfluxDB-v2",
  "targets": [{
    "query": "from(bucket:\"smartcity\") |> range(start: -24h) |> filter(fn: (r) => r[\"_measurement\"] == \"mqtt_consumer\") |> filter(fn: (r) => r[\"topic\"] =~ /pv_/) |> aggregateWindow(every: 5m, fn: mean)"
  }]
}
```

### Battery SOC
```json
{
  "title": "État de Charge Batteries",
  "type": "gauge",
  "datasource": "InfluxDB-v2",
  "targets": [{
    "query": "from(bucket:\"smartcity\") |> range(start: -1h) |> filter(fn: (r) => r[\"_measurement\"] == \"mqtt_consumer\") |> filter(fn: (r) => r[\"topic\"] =~ /bat_/) |> last()"
  }]
}
```

### EV Charger Consumption
```json
{
  "title": "Consommation Bornes VE",
  "type": "timeseries",
  "datasource": "InfluxDB-v2",
  "targets": [{
    "query": "from(bucket:\"smartcity\") |> range(start: -24h) |> filter(fn: (r) => r[\"_measurement\"] == \"mqtt_consumer\") |> filter(fn: (r) => r[\"topic\"] =~ /chg_/) |> aggregateWindow(every: 5m, fn: mean)"
  }]
}

### Flexibilité Disponible
```json
{
  "title": "Flexibilité Disponible",
  "type": "stat",
  "datasource": "InfluxDB-v2",
  "targets": [{
    "query": "from(bucket:\"smartcity\") |> range(start: -1h) |> filter(fn: (r) => r[\"_measurement\"] == \"mqtt_consumer\") |> filter(fn: (r) => r[\"_field\"] == \"flexibility\") |> sum()"
  }]
}
```

## Déploiement

```bash
# Créer le dashboard via l'API Grafana
curl -X POST http://localhost:3001/api/dashboards/db \
  -u admin:admin \
  -H "Content-Type: application/json" \
  -d @cariflex-dashboard.json
```
