#๐Ÿ”’ hi need somehelp regarding fastapi

18 messages ยท Page 1 of 1 (latest)

sharp ore
#
@app.get("/alerts")
async def alerts(duration: int = Query(...), unit: str = Query(...), token: str = Depends(oauth2_scheme), current_user: dict = Depends(get_current_user)):
    unit_mapping = {'minute': 60, 'hour': 3600, 'day': 86400, 'month': 2592000, 'year': 31536000}
    if unit not in unit_mapping:
        raise HTTPException(status_code=400, detail="Invalid unit specified. Valid units are 'minute', 'hour', 'day', 'month', 'year'.")

    try:
        # Calculate the duration in seconds based on the selected unit
        duration_seconds = duration * unit_mapping[unit]

        query = """
        SELECT timestamp, fields.organization, fields.department, src_ip, dest_ip, src_port, dest_port, alert.signature
        FROM suricata_d
        WHERE alert.signature_id >= 1
        AND parseDateTimeBestEffort("timestamp") >= toUnixTimestamp(toDateTime(now()) - INTERVAL %(duration_seconds)s second)
        ORDER BY timestamp ASC
        """

        cache_key = f"alerts:{duration}:{unit}"
        cached_data = await get_data_from_cache(cache_key)
        if cached_data:
            print("From Redis Cache")
            return cached_data

        print("From Clickhouse")
        data = await async_execute(query, params={"duration_seconds": duration_seconds}, with_column_types=True)

        columns = [column[0] for column in data[1]]
        df = pd.DataFrame.from_records(data[0], columns=columns)
        result = json.loads(df.to_json(orient='records'))

        await cache_data(cache_key, result)

        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

tiny fulcrumBOT
#

@sharp ore

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sharp ore
#

@south mural hi

#

the above is working but i think its may be slow for duration if i select 1 year data can we make or improve its performance ?

#

@feral yew

feral yew
#

hey, im not able to assist, im otherwise occupied

sharp ore
#

occupied ?

feral yew
manic urchin
#

Why bother with pandas?

#

Define a pydantic model and use that

feral yew
#

i agree, use a pydantic data model instead, and let it handle the validation part

#

and you have a in memory database as a cache, so everything else will be really fast

manic urchin
#

only use pandas if you need to do some calculations on the data beforehand

sharp ore
#

@manic urchin can you give example

manic urchin
sharp ore
#

Is it make api better?

tiny fulcrumBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.