File size: 3,129 Bytes
69601d4
 
 
 
 
 
 
 
 
b79eb6d
69601d4
1b179fe
69601d4
d7a51f0
69601d4
 
 
 
 
 
 
 
1b179fe
69601d4
 
 
 
 
d7a51f0
b79eb6d
3f68b77
1b179fe
b79eb6d
1b179fe
 
69601d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7a51f0
b79eb6d
3f68b77
1b179fe
b79eb6d
1b179fe
 
69601d4
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from ..models.requestModels import GenerateChartInput, PanelChartDetails
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse
from ..utils.functions import verifyToken
from fastapi import APIRouter, Depends
from urllib.request import urlopen
from typing import Annotated
from . import pipeline
import psutil
import json
import time
import os
import gc

router = APIRouter()
security = HTTPBearer()

@router.post("/generateChart")
async def generateChart(chartDetails: GenerateChartInput, credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]):
    try:
        if verifyToken(token = credentials.credentials):
            fileUrl = os.environ["FILE_URL"].format(projectId = chartDetails.projectId, fileName = "metadata.json").replace(".parquet", "") + f"?cb={int(time.time())}"
            response = pipeline.generateChart(
                inputQuery = chartDetails.inputQuery,
                projectId = chartDetails.projectId,
                metadata = json.loads(urlopen(fileUrl).read())
            )
            gc.collect()
            memory = psutil.virtual_memory()
            cpuUsage = psutil.cpu_percent(interval=1, percpu=True)
            totalUsage = psutil.cpu_percent(interval=1)
            print(f"RAM Usage Percentage: {memory.percent}%")
            print(f"Total CPU Usage: {totalUsage}")
            print(f"CPU Usage Per Core: {cpuUsage}")
            return JSONResponse(status_code = 200, content = response)
        else:
            return JSONResponse(status_code = 498, content = {"status": "ERROR", "errorDetail": "Invalid Token"})    
    except Exception as e:
        raise HTTPException(status_code = 500, detail = f"Endpoint says: {e}")
    
@router.post("/generatePanelChart")
async def generatePanelChart(panelChartDetails: PanelChartDetails, credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]):
    try:
        if verifyToken(token = credentials.credentials):
            response = pipeline.generateChartFromPanel(
                projectId = panelChartDetails.projectId,
                chartType = panelChartDetails.chartType,
                xAxis = panelChartDetails.xAxis,
                yAxis = panelChartDetails.yAxis,
                aggregationMetric = panelChartDetails.aggregationMetric,
                dataSource = panelChartDetails.dataSource
            )
            gc.collect()
            memory = psutil.virtual_memory()
            cpuUsage = psutil.cpu_percent(interval=1, percpu=True)
            totalUsage = psutil.cpu_percent(interval=1)
            print(f"RAM Usage Percentage: {memory.percent}%")
            print(f"Total CPU Usage: {totalUsage}")
            print(f"CPU Usage Per Core: {cpuUsage}")
            return JSONResponse(status_code = 200, content = response)
        else:
            return JSONResponse(status_code = 498, content = {"status": "ERROR", "errorDetail": "Invalid Token"})    
    except Exception as e:
        raise HTTPException(status_code = 500, detail = f"Endpoint says: {e}")