From d085431fa7f65114b7968d801a9caf8f9844aebf Mon Sep 17 00:00:00 2001 From: Valeryi Date: Wed, 20 Dec 2023 14:02:00 +0000 Subject: [PATCH] quickfix --- .../dashboard_service/dashboards/dash_prime.py | 2 +- .../dashboard_service/utils.py | 18 ++++++++++++++++++ .../webgui/selfhost_fastapi_completions.py | 8 ++++---- .../webgui/selfhost_statistics.py | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/self_hosting_machinery/dashboard_service/dashboards/dash_prime.py b/self_hosting_machinery/dashboard_service/dashboards/dash_prime.py index 9387750c..0534a004 100644 --- a/self_hosting_machinery/dashboard_service/dashboards/dash_prime.py +++ b/self_hosting_machinery/dashboard_service/dashboards/dash_prime.py @@ -15,7 +15,7 @@ def robot_human_ratio(robot: int, human: int) -> float: return 1 if robot == 0: return 0 - return round(robot / robot + human, 2) + return round(robot / (robot + human), 2) def barplot_rh( diff --git a/self_hosting_machinery/dashboard_service/utils.py b/self_hosting_machinery/dashboard_service/utils.py index 8465b59a..e9e317f0 100644 --- a/self_hosting_machinery/dashboard_service/utils.py +++ b/self_hosting_machinery/dashboard_service/utils.py @@ -48,6 +48,19 @@ def fetch_records(): return pd.DataFrame(fetch_records()) +def try_get_user_to_team_dict(stats_service: StatisticsService) -> Dict[str, str]: + res = {} + try: + prep = stats_service.session.prepare( + "SELECT * FROM users_access_control" + ) + for record in stats_service.session.execute(prep): + res.setdefault(record["account"], record["team"]) + except Exception: + pass + return res + + def robot_human_df_from_ts(stats_service: StatisticsService, ts: int) -> pd.DataFrame: prep = stats_service.session.prepare( "SELECT * FROM telemetry_robot_human WHERE ts_end >= ? ALLOW FILTERING" @@ -107,6 +120,7 @@ def retrieve_all_data_tables(stats_service: StatisticsService) -> StatsDataTable current_year = datetime.now().year start_of_year = datetime(current_year, 1, 1, 0, 0, 0, 0) timestamp_start_of_year = int(start_of_year.timestamp()) + user_to_team_dict = try_get_user_to_team_dict(stats_service) extra = {} network_df = network_df_from_ts(stats_service, timestamp_start_of_year) @@ -120,6 +134,10 @@ def retrieve_all_data_tables(stats_service: StatisticsService) -> StatsDataTable robot_human_df['dt_end'] = pd.to_datetime(robot_human_df['ts_end'], unit='s') comp_counters_df['dt_end'] = pd.to_datetime(comp_counters_df['ts_end'], unit='s') + network_df['team'] = network_df['tenant_name'].map(lambda x: user_to_team_dict.get(x, "unassigned")) + robot_human_df['team'] = robot_human_df['tenant_name'].map(lambda x: user_to_team_dict.get(x, "unassigned")) + comp_counters_df['team'] = comp_counters_df['tenant_name'].map(lambda x: user_to_team_dict.get(x, "unassigned")) + network_df.sort_values(by='dt_end', inplace=True) robot_human_df.sort_values(by='dt_end', inplace=True) comp_counters_df.sort_values(by='dt_end', inplace=True) diff --git a/self_hosting_machinery/webgui/selfhost_fastapi_completions.py b/self_hosting_machinery/webgui/selfhost_fastapi_completions.py index ff115fad..df1648a4 100644 --- a/self_hosting_machinery/webgui/selfhost_fastapi_completions.py +++ b/self_hosting_machinery/webgui/selfhost_fastapi_completions.py @@ -345,7 +345,7 @@ async def _secret_key_activate(self): "human_readable_message": "API key verified", } - async def _completions(self, post: NlpCompletion, account: str = "XXX"): + async def _completions(self, post: NlpCompletion, account: str = "user"): ticket = Ticket("comp-") req = post.clamp() model_name, err_msg = static_resolve_model(post.model, self._inference_queue) @@ -371,7 +371,7 @@ async def _completions(self, post: NlpCompletion, account: str = "XXX"): media_type=("text/event-stream" if post.stream else "application/json"), ) - async def _contrast(self, post: DiffCompletion, request: Request, account: str = "XXX"): + async def _contrast(self, post: DiffCompletion, request: Request, account: str = "user"): if post.function != "diff-anywhere": if post.cursor_file not in post.sources: raise HTTPException(status_code=400, detail="cursor_file='%s' is not in sources=%s" % (post.cursor_file, list(post.sources.keys()))) @@ -418,7 +418,7 @@ async def _contrast(self, post: DiffCompletion, request: Request, account: str = await q.put(ticket) return StreamingResponse(diff_streamer(ticket, post, self._timeout, req["created"])) - async def _chat(self, post: ChatContext, request: Request, account: str = "XXX"): + async def _chat(self, post: ChatContext, request: Request, account: str = "user"): ticket = Ticket("comp-") model_name, err_msg = static_resolve_model(post.model, self._inference_queue) @@ -478,7 +478,7 @@ async def _models(self): "data": data, } - async def _chat_completions(self, post: ChatContext, account: str = "XXX"): + async def _chat_completions(self, post: ChatContext, account: str = "user"): prefix, postfix = "data: ", "\n\n" if post.model in litellm.model_list: diff --git a/self_hosting_machinery/webgui/selfhost_statistics.py b/self_hosting_machinery/webgui/selfhost_statistics.py index e782d4c7..0422be47 100644 --- a/self_hosting_machinery/webgui/selfhost_statistics.py +++ b/self_hosting_machinery/webgui/selfhost_statistics.py @@ -110,7 +110,7 @@ async def _dash_teams_post(self, data: DashTeamsGenDashData): media_type='application/json', status_code=500) - async def _telemetry_basic(self, data: TelemetryBasicData, request: Request, account: str = "XXX"): + async def _telemetry_basic(self, data: TelemetryBasicData, request: Request, account: str = "user"): if not self._stats_service.is_ready: return self._stats_service_not_available_response @@ -175,7 +175,7 @@ async def _telemetry_basic(self, data: TelemetryBasicData, request: Request, acc return JSONResponse({"retcode": "OK"}) - async def _telemetry_snippets(self, data: TelemetryBasicData, request: Request, account: str = "XXX"): + async def _telemetry_snippets(self, data: TelemetryBasicData, request: Request, account: str = "user"): if not self._stats_service.is_ready: return self._stats_service_not_available_response