1
1
"""This module contains FastAPI routers for dashboard endpoints."""
2
2
3
3
import json
4
+ import os
4
5
from datetime import date , datetime , timedelta , timezone
5
6
from typing import Annotated , Literal , Optional
6
7
18
19
from sqlalchemy .ext .asyncio import AsyncSession
19
20
20
21
from ..auth .dependencies import get_current_workspace_name
21
- from ..database import get_async_session
22
+ from ..database import get_async_session , get_sqlalchemy_async_engine
22
23
from ..users .models import WorkspaceDB
23
24
from ..utils import setup_logger
24
25
from ..workspaces .utils import get_workspace_by_workspace_name
48
49
)
49
50
from .topic_modeling import topic_model_queries
50
51
52
+ os .environ ["TOKENIZERS_PARALLELISM" ] = "false"
53
+
51
54
TAG_METADATA = {
52
55
"name" : "Dashboard" ,
53
56
"description" : "_Requires user login._ Dashboard data fetching operations." ,
@@ -328,7 +331,6 @@ async def refresh_insights_frequency(
328
331
329
332
background_tasks .add_task (
330
333
refresh_insights ,
331
- asession = asession ,
332
334
end_date = end_dt ,
333
335
request = request ,
334
336
start_date = start_dt ,
@@ -512,7 +514,6 @@ def get_freq_start_end_date(
512
514
513
515
async def refresh_insights (
514
516
* ,
515
- asession : AsyncSession = Depends (get_async_session ),
516
517
end_date : date ,
517
518
request : Request ,
518
519
start_date : date ,
@@ -538,61 +539,63 @@ async def refresh_insights(
538
539
workspace_db
539
540
The workspace database object.
540
541
"""
541
-
542
- redis = request .app .state .redis
543
- await redis .set (
544
- f"{ workspace_db .workspace_name } _insights_{ timeframe } _results" ,
545
- TopicsData (
546
- data = [],
547
- refreshTimeStamp = datetime .now (timezone .utc ).isoformat (),
548
- status = "in_progress" ,
549
- ).model_dump_json (),
550
- )
551
-
552
- step = None
553
- try :
554
- step = "Retrieve queries"
555
- time_period_queries = await get_raw_queries (
556
- asession = asession ,
557
- end_date = end_date ,
558
- start_date = start_date ,
559
- workspace_id = workspace_db .workspace_id ,
560
- )
561
-
562
- step = "Retrieve contents"
563
- content_data = await get_raw_contents (
564
- asession = asession , workspace_id = workspace_db .workspace_id
565
- )
566
-
567
- topic_output , embeddings_df = await topic_model_queries (
568
- content_data = content_data ,
569
- query_data = time_period_queries ,
570
- workspace_id = workspace_db .workspace_id ,
571
- )
572
-
573
- step = "Write to Redis"
574
- embeddings_json = embeddings_df .to_json (orient = "split" )
575
- embeddings_key = f"{ workspace_db .workspace_name } _embeddings_{ timeframe } "
576
- await redis .set (embeddings_key , embeddings_json )
577
- await redis .set (
578
- f"{ workspace_db .workspace_name } _insights_{ timeframe } _results" ,
579
- topic_output .model_dump_json (),
580
- )
581
- return
582
- except Exception as e : # pylint: disable=W0718
583
- error_msg = str (e )
584
- logger .error (error_msg )
542
+ async with AsyncSession (
543
+ get_sqlalchemy_async_engine (), expire_on_commit = False
544
+ ) as asession :
545
+ redis = request .app .state .redis
585
546
await redis .set (
586
547
f"{ workspace_db .workspace_name } _insights_{ timeframe } _results" ,
587
548
TopicsData (
588
549
data = [],
589
- error_message = error_msg ,
590
- failure_step = step ,
591
550
refreshTimeStamp = datetime .now (timezone .utc ).isoformat (),
592
- status = "error " ,
551
+ status = "in_progress " ,
593
552
).model_dump_json (),
594
553
)
595
554
555
+ step = None
556
+ try :
557
+ step = "Retrieve queries"
558
+ time_period_queries = await get_raw_queries (
559
+ asession = asession ,
560
+ end_date = end_date ,
561
+ start_date = start_date ,
562
+ workspace_id = workspace_db .workspace_id ,
563
+ )
564
+
565
+ step = "Retrieve contents"
566
+ content_data = await get_raw_contents (
567
+ asession = asession , workspace_id = workspace_db .workspace_id
568
+ )
569
+
570
+ topic_output , embeddings_df = await topic_model_queries (
571
+ content_data = content_data ,
572
+ query_data = time_period_queries ,
573
+ workspace_id = workspace_db .workspace_id ,
574
+ )
575
+
576
+ step = "Write to Redis"
577
+ embeddings_json = embeddings_df .to_json (orient = "split" )
578
+ embeddings_key = f"{ workspace_db .workspace_name } _embeddings_{ timeframe } "
579
+ await redis .set (embeddings_key , embeddings_json )
580
+ await redis .set (
581
+ f"{ workspace_db .workspace_name } _insights_{ timeframe } _results" ,
582
+ topic_output .model_dump_json (),
583
+ )
584
+ return
585
+ except Exception as e : # pylint: disable=W0718
586
+ error_msg = str (e )
587
+ logger .error (error_msg )
588
+ await redis .set (
589
+ f"{ workspace_db .workspace_name } _insights_{ timeframe } _results" ,
590
+ TopicsData (
591
+ data = [],
592
+ error_message = error_msg ,
593
+ failure_step = step ,
594
+ refreshTimeStamp = datetime .now (timezone .utc ).isoformat (),
595
+ status = "error" ,
596
+ ).model_dump_json (),
597
+ )
598
+
596
599
597
600
async def retrieve_overview (
598
601
* ,
0 commit comments