-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: merge bot list query requests (#596)
* 重构 bots 查询接口,将空间机器人需要的多张表信息一次返回,减少空间页面额外的循环请求
- Loading branch information
Showing
4 changed files
with
143 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
migrations/supabase/migrations/20241218093541_remote_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
alter table "public"."profiles" drop constraint "profile_pkey"; | ||
|
||
drop index if exists "public"."profile_pkey"; | ||
|
||
alter table "public"."profiles" alter column "sub" set not null; | ||
|
||
CREATE INDEX idx_file_sha ON public.rag_docs USING btree (file_sha); | ||
|
||
CREATE UNIQUE INDEX profiles_pkey ON public.profiles USING btree (id); | ||
|
||
alter table "public"."profiles" add constraint "profiles_pkey" PRIMARY KEY using index "profiles_pkey"; | ||
|
||
set check_function_bodies = off; | ||
|
||
create or replace view "public"."bots_with_profiles_and_github" as SELECT bots.id, | ||
bots.created_at, | ||
bots.updated_at, | ||
bots.avatar, | ||
bots.description, | ||
bots.name, | ||
bots.public, | ||
bots.starters, | ||
bots.uid, | ||
bots.repo_name, | ||
profiles.picture, | ||
profiles.nickname, | ||
CASE | ||
WHEN (github_repo_config.robot_id IS NOT NULL) THEN true | ||
ELSE false | ||
END AS github_installed | ||
FROM ((bots | ||
LEFT JOIN profiles ON (((bots.uid)::text = (profiles.sub)::text))) | ||
LEFT JOIN github_repo_config ON ((bots.id = (github_repo_config.robot_id)::uuid))); | ||
|
||
|
||
CREATE OR REPLACE FUNCTION public.get_bots_with_profiles_and_github() | ||
RETURNS TABLE(id uuid, created_at timestamp without time zone, updated_at timestamp without time zone, avatar text, description text, name text, public boolean, starters text, uid text, domain_whitelist text[], repo_name text, picture text, nickname text, github_installed boolean) | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT | ||
b.id::uuid, | ||
b.created_at::timestamp, | ||
b.updated_at::timestamp, | ||
b.avatar::text, | ||
b.description::text, | ||
b.name::text, | ||
b.public::boolean, | ||
b.starters::text, | ||
b.uid::text, | ||
b.domain_whitelist::text[], | ||
b.repo_name::text, | ||
p.picture::text, | ||
p.nickname::text, | ||
CASE | ||
WHEN grc.robot_id IS NOT NULL THEN TRUE | ||
ELSE FALSE | ||
END AS github_installed | ||
FROM | ||
bots b | ||
LEFT JOIN | ||
profiles p ON b.uid = p.sub | ||
LEFT JOIN | ||
github_repo_config grc ON b.id::varchar = grc.robot_id; | ||
END; | ||
$function$ | ||
; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Optional | ||
|
||
from github import Github | ||
from core.dao.repositoryConfigDAO import RepositoryConfigDAO | ||
from petercat_utils import get_client | ||
from github import Github, Auth | ||
|
||
|
||
def query_list( | ||
name: Optional[str] = None, | ||
user_id: Optional[str] = None, | ||
access_token: Optional[str] = None, | ||
personal: Optional[str] = None, | ||
): | ||
try: | ||
supabase = get_client() | ||
query = ( | ||
supabase.rpc("get_bots_with_profiles_and_github") | ||
if personal == "true" | ||
else supabase.table("bots").select( | ||
"id, created_at, updated_at, avatar, description, name, public, starters, uid, repo_name" | ||
) | ||
) | ||
if name: | ||
query = query.filter("name", "like", f"%{name}%") | ||
|
||
if personal == "true": | ||
if not access_token or not user_id: | ||
return {"data": [], "personal": personal} | ||
|
||
auth = Auth.Token(token=access_token) | ||
github_user = Github(auth=auth).get_user() | ||
orgs_ids = [org.id for org in github_user.get_orgs()] | ||
bot_ids = [] | ||
|
||
repository_config_dao = RepositoryConfigDAO() | ||
bots = repository_config_dao.query_bot_id_by_owners( | ||
orgs_ids + [github_user.id] | ||
) | ||
|
||
if bots: | ||
bot_ids = [bot["robot_id"] for bot in bots if bot["robot_id"]] | ||
|
||
or_clause = f"uid.eq.{user_id}" + ( | ||
f",id.in.({','.join(map(str, bot_ids))})" if bot_ids else "" | ||
) | ||
query = query.or_(or_clause) | ||
else: | ||
query = query.eq("public", True) | ||
|
||
query = query.order("updated_at", desc=True) | ||
data = query.execute() | ||
if data.data: | ||
unique_data = {} | ||
for item in data.data: | ||
unique_data[item["id"]] = item | ||
deduplicated_list = list(unique_data.values()) | ||
return deduplicated_list | ||
return data.data | ||
except Exception as e: | ||
print(f"query list error: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters