|
| 1 | +from fastapi import APIRouter, HTTPException, Query |
| 2 | +from src.configs.influx_conf import InfluxConf |
| 3 | +from src.services.influx import InfluxService |
| 4 | +from src.models.raw import RawResponse |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | + |
| 8 | +router = APIRouter() |
| 9 | + |
| 10 | +class Influx(): |
| 11 | + conf = InfluxConf() |
| 12 | + service = InfluxService() |
| 13 | + |
| 14 | + |
| 15 | +@router.get("/", response_model=RawResponse) |
| 16 | +def get_raw_data( |
| 17 | + start_time: int = Query(..., description="Start time (Unix timestamp in seconds)"), |
| 18 | + end_time: int = Query(..., description="End time (Unix timestamp in seconds)"), |
| 19 | + cell_index: int = Query(..., description="Cell index (required)"), |
| 20 | + |
| 21 | + batch_number: int = Query(1, ge=1, description="Batch number"), |
| 22 | +): |
| 23 | + """ |
| 24 | + Query raw data entries between a given start and end time for a specific cell. |
| 25 | +
|
| 26 | + This endpoint returns raw entries directly from the database, |
| 27 | + limited to a maximum of 50 per request. |
| 28 | + """ |
| 29 | + try: |
| 30 | + query_params = { |
| 31 | + "start_time": start_time, |
| 32 | + "end_time": end_time, |
| 33 | + "cell_index": cell_index, |
| 34 | + "batch_number": batch_number, |
| 35 | + } |
| 36 | + |
| 37 | + results,has_next = Influx.service.query_raw_data(**query_params) |
| 38 | + |
| 39 | + for row in results: |
| 40 | + for k, v in row.items(): |
| 41 | + if isinstance(v, datetime): |
| 42 | + row[k] = v.isoformat() |
| 43 | + |
| 44 | + return {"data": results, "has_next": has_next} |
| 45 | + |
| 46 | + except Exception as e: |
| 47 | + raise HTTPException(status_code=500, detail=f"Error querying raw data: {str(e)}") |
0 commit comments