From 55b0f34daba775cc802f5e04763049798a2fcc2b Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 15 Dec 2021 17:38:36 +0800 Subject: [PATCH 001/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=96=B0=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/ai_model_manage.go | 2 +- models/dbsql/dateset_foreigntable_for_es.sql | 155 +++++++++++++ models/dbsql/issue_foreigntable_for_es.sql | 0 models/dbsql/pr_foreigntable_for_es.sql | 0 models/dbsql/repo_foreigntable_for_es.sql | 335 +++++++++++++++++++++++++++ models/dbsql/user_foreigntable_for_es.sql | 282 ++++++++++++++++++++++ options/locale/locale_en-US.ini | 2 + options/locale/locale_zh-CN.ini | 1 + routers/repo/ai_model_manage.go | 66 ++++++ routers/routes/routes.go | 1 + 10 files changed, 843 insertions(+), 1 deletion(-) create mode 100644 models/dbsql/dateset_foreigntable_for_es.sql create mode 100644 models/dbsql/issue_foreigntable_for_es.sql create mode 100644 models/dbsql/pr_foreigntable_for_es.sql create mode 100644 models/dbsql/repo_foreigntable_for_es.sql create mode 100644 models/dbsql/user_foreigntable_for_es.sql diff --git a/models/ai_model_manage.go b/models/ai_model_manage.go index 712e72d98..581b19a9c 100644 --- a/models/ai_model_manage.go +++ b/models/ai_model_manage.go @@ -135,7 +135,7 @@ func QueryModelByName(name string, repoId int64) []*AiModelManage { sess := x.NewSession() defer sess.Close() sess.Select("*").Table("ai_model_manage"). - Where("name='" + name + "' and repo_id=" + fmt.Sprint(repoId)).OrderBy("version desc") + Where("name='" + name + "' and repo_id=" + fmt.Sprint(repoId)).OrderBy("created_unix desc") aiModelManageList := make([]*AiModelManage, 0) sess.Find(&aiModelManageList) return aiModelManageList diff --git a/models/dbsql/dateset_foreigntable_for_es.sql b/models/dbsql/dateset_foreigntable_for_es.sql new file mode 100644 index 000000000..0cc41e6d7 --- /dev/null +++ b/models/dbsql/dateset_foreigntable_for_es.sql @@ -0,0 +1,155 @@ +CREATE FOREIGN TABLE public."dataset_es" +( + id bigint NOT NULL, + title character varying(255), + status integer, + category character varying(255), + description text, + download_times bigint, + license character varying(255), + task character varying(255), + release_id bigint, + user_id bigint, + repo_id bigint, + created_unix bigint, + updated_unix bigint, + file_name text +)SERVER multicorn_es +OPTIONS + ( + host '192.168.207.94', + port '9200', + index 'user_es-index', + rowid_column 'id' + ) +; + +CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS +$def$ + BEGIN + INSERT INTO public.dataset( + id, + title, + status, + category, + description, + download_times, + license, task, + release_id, + user_id, + repo_id, + created_unix, + updated_unix) + VALUES ( + NEW.id, + NEW.title, + NEW.status, + NEW.category, + NEW.description, + NEW.download_times, + NEW.license, + NEW.task, + NEW.release_id, + NEW.user_id, + NEW.repo_id, + NEW.created_unix, + NEW.updated_unix + ); + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_dataset on public.dataset; + +CREATE TRIGGER es_insert_dataset + AFTER INSERT ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data(); + +CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name() RETURNS trigger AS +$def$ + BEGIN + if (TG_OP = 'DELETE') then + update public.dataset_es SET file_name=(select string_agg(name, ',') from public.attachment where dataset_id=OLD.dataset_id order by created_unix desc) where id=OLD.dataset_id; + elsif (TG_OP = 'INSERT') then + update public.dataset_es SET file_name=(select string_agg(name, ',') from public.attachment where dataset_id=NEW.dataset_id order by created_unix desc) where id=NEW.dataset_id; + end if; + + return null; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_udpate_dataset_file_name on public.attachment; +CREATE TRIGGER es_udpate_dataset_file_name + AFTER INSERT OR DELETE ON public.attachment + FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name(); + + +CREATE OR REPLACE FUNCTION public.update_dataset_description() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.dataset_es SET description=NEW.description where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_dataset_description on public.dataset; + +CREATE TRIGGER es_update_dataset_description + AFTER UPDATE OF "description" ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE update_dataset_description(); + + +CREATE OR REPLACE FUNCTION public.update_dataset_title() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.dataset_es SET title=NEW.title where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_dataset_title on public.dataset; + +CREATE TRIGGER es_update_dataset_title + AFTER UPDATE OF "title" ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE update_dataset_title(); + + +CREATE OR REPLACE FUNCTION public.update_dataset_category() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.dataset_es SET category=NEW.category where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_dataset_category on public.dataset; + +CREATE TRIGGER es_update_dataset_category + AFTER UPDATE OF "category" ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE update_dataset_category(); + + +CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS +$def$ + declare + BEGIN + DELETE FROM public.dataset_es where id=OLD.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset; +CREATE TRIGGER es_delete_dataset + AFTER DELETE ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE delete_dataset(); + + diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql new file mode 100644 index 000000000..e69de29bb diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql new file mode 100644 index 000000000..e69de29bb diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql new file mode 100644 index 000000000..0b09f576d --- /dev/null +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -0,0 +1,335 @@ +CREATE FOREIGN TABLE public.repository_es ( + id bigint NOT NULL, + owner_id bigint, + owner_name character varying(255), + lower_name character varying(255) NOT NULL, + name character varying(255) NOT NULL, + description text, + website character varying(2048), + original_service_type integer, + original_url character varying(2048), + default_branch character varying(255), + num_watches integer, + num_stars integer, + num_forks integer, + num_issues integer, + num_closed_issues integer, + num_pulls integer, + num_closed_pulls integer, + num_milestones integer DEFAULT 0 NOT NULL, + num_closed_milestones integer DEFAULT 0 NOT NULL, + is_private boolean, + is_empty boolean, + is_archived boolean, + is_mirror boolean, + status integer DEFAULT 0 NOT NULL, + is_fork boolean DEFAULT false NOT NULL, + fork_id bigint, + is_template boolean DEFAULT false NOT NULL, + template_id bigint, + size bigint DEFAULT 0 NOT NULL, + is_fsck_enabled boolean DEFAULT true NOT NULL, + close_issues_via_commit_in_any_branch boolean DEFAULT false NOT NULL, + topics json, + avatar character varying(64), + created_unix bigint, + updated_unix bigint, + contract_address character varying(255), + block_chain_status integer DEFAULT 0 NOT NULL, + balance character varying(255) DEFAULT '0'::character varying NOT NULL, + clone_cnt bigint DEFAULT 0 NOT NULL, + license character varying(100), + download_cnt bigint DEFAULT 0 NOT NULL, + num_commit bigint DEFAULT 0 NOT NULL, + git_clone_cnt bigint DEFAULT 0 NOT NULL, + lang character varying(2048) +) SERVER multicorn_es +OPTIONS + ( + host '192.168.207.94', + port '9200', + index 'repository-es-index', + rowid_column 'id' + ) +; + +CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS +$def$ + BEGIN + INSERT INTO public.repository_es (id, + owner_id, + owner_name, + lower_name, + name, + description, + website, + original_service_type, + original_url, + default_branch, + num_watches, + num_stars, + num_forks, + num_issues, + num_closed_issues, + num_pulls, + num_closed_pulls, + num_milestones, + num_closed_milestones, + is_private, + is_empty, + is_archived, + is_mirror, + status, + is_fork, + fork_id, + is_template, + template_id, + size, + is_fsck_enabled, + close_issues_via_commit_in_any_branch, + topics, + avatar, + created_unix, + updated_unix, + contract_address, + block_chain_status, + balance, + clone_cnt, + num_commit, + git_clone_cnt) VALUES + (NEW.id, + NEW.owner_id, + NEW.owner_name, + NEW.lower_name, + NEW.name, + NEW.description, + NEW.website, + NEW.original_service_type, + NEW.original_url, + NEW.default_branch, + NEW.num_watches, + NEW.num_stars, + NEW.num_forks, + NEW.num_issues, + NEW.num_closed_issues, + NEW.num_pulls, + NEW.num_closed_pulls, + NEW.num_milestones, + NEW.num_closed_milestones, + NEW.is_private, + NEW.is_empty, + NEW.is_archived, + NEW.is_mirror, + NEW.status, + NEW.is_fork, + NEW.fork_id, + NEW.is_template, + NEW.template_id, + NEW.size, + NEW.is_fsck_enabled, + NEW.close_issues_via_commit_in_any_branch, + NEW.topics, + NEW.avatar, + NEW.created_unix, + NEW.updated_unix, + NEW.contract_address, + NEW.block_chain_status, + NEW.balance, + NEW.clone_cnt, + NEW.num_commit, + NEW.git_clone_cnt); + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_repository on public.repository; + +CREATE TRIGGER es_insert_repository + AFTER INSERT ON public.repository + FOR EACH ROW EXECUTE PROCEDURE insert_repository_data(); + + +CREATE OR REPLACE FUNCTION public.update_repository_description() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET description=NEW.description where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_description on public.repository; + +CREATE TRIGGER es_update_repository_description + AFTER UPDATE OF "description" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_description(); + + +CREATE OR REPLACE FUNCTION public.update_repository_name() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET name=NEW.name,lower_name=NEW.lower_name where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_name on public.repository; +CREATE TRIGGER es_update_repository_name + AFTER UPDATE OF "name","lower_name" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_name(); + + +CREATE OR REPLACE FUNCTION public.update_repository_ownername() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET owner_name=NEW.owner_name where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_ownername on public.repository; +CREATE TRIGGER es_update_repository_ownername + AFTER UPDATE OF "owner_name" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_ownername(); + +CREATE OR REPLACE FUNCTION public.update_repository_website() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET website=NEW.website where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_website on public.repository; +CREATE TRIGGER es_update_repository_website + AFTER UPDATE OF "website" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_website(); + + +CREATE OR REPLACE FUNCTION public.update_repository_topics() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET topics=NEW.topics where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_topics on public.repository; +CREATE TRIGGER es_update_repository_topics + AFTER UPDATE OF "topics" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_topics(); + + + +CREATE OR REPLACE FUNCTION public.update_repository_updated_unix() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET updated_unix=NEW.updated_unix where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_updated_unix on public.repository; +CREATE TRIGGER es_update_repository_updated_unix + AFTER UPDATE OF "updated_unix" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_updated_unix(); + + +CREATE OR REPLACE FUNCTION public.update_repository_num_watches() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET num_watches=NEW.num_watches where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_num_watches on public.repository; +CREATE TRIGGER es_update_repository_num_watches + AFTER UPDATE OF "num_watches" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_num_watches(); + + +CREATE OR REPLACE FUNCTION public.update_repository_num_stars() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET num_stars=NEW.num_stars where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_repository_num_stars on public.repository; +CREATE TRIGGER es_update_repository_num_stars + AFTER UPDATE OF "num_stars" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_num_stars(); + + + +CREATE OR REPLACE FUNCTION public.update_repository_num_forks() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.repository_es SET num_forks=NEW.num_forks where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + + +DROP TRIGGER IF EXISTS es_update_repository_num_forks on public.repository; +CREATE TRIGGER es_update_repository_num_forks + AFTER UPDATE OF "num_forks" ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository_num_forks(); + + +CREATE OR REPLACE FUNCTION public.delete_repository() RETURNS trigger AS +$def$ + declare + BEGIN + DELETE FROM public.repository_es where id=OLD.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_delete_repository on public.repository; +CREATE TRIGGER es_delete_repository + AFTER DELETE ON public.repository + FOR EACH ROW EXECUTE PROCEDURE delete_repository(); + + +CREATE OR REPLACE FUNCTION public.udpate_repository_lang() RETURNS trigger AS +$def$ + BEGIN + if (TG_OP = 'DELETE') then + update public.repository_es SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=OLD.repo_id) where id=OLD.repo_id; + elsif (TG_OP = 'UPDATE') then + update public.repository_es SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id; + elsif (TG_OP = 'INSERT') then + update public.repository_es SET lang=(select string_agg(language, ',') from public.language_stat where repo_id=NEW.repo_id) where id=NEW.repo_id; + end if; + + return null; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat; +CREATE TRIGGER es_udpate_repository_lang + AFTER INSERT OR UPDATE OR DELETE ON public.language_stat + FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang(); \ No newline at end of file diff --git a/models/dbsql/user_foreigntable_for_es.sql b/models/dbsql/user_foreigntable_for_es.sql new file mode 100644 index 000000000..dc362fce3 --- /dev/null +++ b/models/dbsql/user_foreigntable_for_es.sql @@ -0,0 +1,282 @@ +CREATE FOREIGN TABLE public."user_es" +( + id bigint NOT NULL , + lower_name character varying(255) NULL, + name character varying(255) NULL, + full_name character varying(255), + email character varying(255), + keep_email_private boolean, + email_notifications_preference character varying(20) , + passwd character varying(255) , + passwd_hash_algo character varying(255) , + must_change_password boolean NOT NULL DEFAULT false, + login_type integer, + login_source bigint NOT NULL DEFAULT 0, + login_name character varying(255) , + type integer, + location character varying(255), + website character varying(255), + rands character varying(10), + salt character varying(10), + language character varying(5), + description character varying(255), + created_unix bigint, + updated_unix bigint, + last_login_unix bigint, + last_repo_visibility boolean, + max_repo_creation integer, + is_active boolean, + is_admin boolean, + is_restricted boolean NOT NULL DEFAULT false, + allow_git_hook boolean, + allow_import_local boolean, + allow_create_organization boolean DEFAULT true, + prohibit_login boolean NOT NULL DEFAULT false, + avatar character varying(2048) , + avatar_email character varying(255), + use_custom_avatar boolean, + num_followers integer, + num_following integer NOT NULL DEFAULT 0, + num_stars integer, + num_repos integer, + num_teams integer, + num_members integer, + visibility integer NOT NULL DEFAULT 0, + repo_admin_change_team_access boolean NOT NULL DEFAULT false, + diff_view_style character varying(255), + theme character varying(255), + token character varying(1024) , + public_key character varying(255), + private_key character varying(255), + is_operator boolean NOT NULL DEFAULT false +) SERVER multicorn_es +OPTIONS + ( + host '192.168.207.94', + port '9200', + index 'user_es-index', + rowid_column 'id' + ) +; + +CREATE OR REPLACE FUNCTION public.insert_user_data() RETURNS trigger AS +$def$ + BEGIN + INSERT INTO public."user_es"( + id, + lower_name, + name, + full_name, + email, + keep_email_private, + email_notifications_preference, + must_change_password, + login_type, + login_source, + login_name, + type, + location, + website, + rands, + language, + description, + created_unix, + updated_unix, + last_login_unix, + last_repo_visibility, + max_repo_creation, + is_active, + is_restricted, + allow_git_hook, + allow_import_local, + allow_create_organization, + prohibit_login, + avatar, + avatar_email, + use_custom_avatar, + num_followers, + num_following, + num_stars, + num_repos, + num_teams, + num_members, + visibility, + repo_admin_change_team_access, + diff_view_style, + theme, + is_operator) + VALUES ( + NEW.id, + NEW.lower_name, + NEW.name, + NEW.full_name, + NEW.email, + NEW.keep_email_private, + NEW.email_notifications_preference, + NEW.must_change_password, + NEW.login_type, + NEW.login_source, + NEW.login_name, + NEW.type, + NEW.location, + NEW.website, + NEW.rands, + NEW.language, + NEW.description, + NEW.created_unix, + NEW.updated_unix, + NEW.last_login_unix, + NEW.last_repo_visibility, + NEW.max_repo_creation, + NEW.is_active, + NEW.is_restricted, + NEW.allow_git_hook, + NEW.allow_import_local, + NEW.allow_create_organization, + NEW.prohibit_login, + NEW.avatar, + NEW.avatar_email, + NEW.use_custom_avatar, + NEW.num_followers, + NEW.num_following, + NEW.num_stars, + NEW.num_repos, + NEW.num_teams, + NEW.num_members, + NEW.visibility, + NEW.repo_admin_change_team_access, + NEW.diff_view_style, + NEW.theme, + NEW.is_operator + ); + + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_user on public.user; + +CREATE TRIGGER es_insert_user + AFTER INSERT ON public.user + FOR EACH ROW EXECUTE PROCEDURE insert_user_data(); + + +CREATE OR REPLACE FUNCTION public.update_user_description() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.user_es SET description=NEW.description where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_user_description on public.user; + +CREATE TRIGGER es_update_user_description + AFTER UPDATE OF "description" ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user_description(); + + +CREATE OR REPLACE FUNCTION public.update_user_name() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.user_es SET name=NEW.name where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_user_name on public.user; + +CREATE TRIGGER es_update_user_name + AFTER UPDATE OF "name" ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user_name(); + + +CREATE OR REPLACE FUNCTION public.update_user_full_name() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.user_es SET full_name=NEW.full_name where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_user_full_name on public.user; + +CREATE TRIGGER es_update_user_full_name + AFTER UPDATE OF "full_name" ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user_full_name(); + + + +CREATE OR REPLACE FUNCTION public.update_user_location() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.user_es SET location=NEW.location where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_user_location on public.user; + +CREATE TRIGGER es_update_user_location + AFTER UPDATE OF "location" ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user_location(); + + +CREATE OR REPLACE FUNCTION public.update_user_website() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.user_es SET website=NEW.website where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_user_website on public.user; + +CREATE TRIGGER es_update_user_website + AFTER UPDATE OF "website" ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user_website(); + + +CREATE OR REPLACE FUNCTION public.update_user_email() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.user_es SET email=NEW.email where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_user_email on public.user; + +CREATE TRIGGER es_update_user_email + AFTER UPDATE OF "email" ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user_email(); + + + +CREATE OR REPLACE FUNCTION public.delete_user() RETURNS trigger AS +$def$ + declare + BEGIN + DELETE FROM public.user_es where id=OLD.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_delete_user on public.user; +CREATE TRIGGER es_delete_user + AFTER DELETE ON public.user + FOR EACH ROW EXECUTE PROCEDURE delete_user(); \ No newline at end of file diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 48a010b73..01bfe7fcd 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -779,6 +779,8 @@ datasets.desc = Enable Dataset cloudbrain_helper=Use GPU/NPU resources to open notebooks, model training tasks, etc. model_manager = Model +model_noright=No right +model_rename=Duplicate model name, please modify model name. debug=Debug stop=Stop diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 1eee752a1..ee85a6d30 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -784,6 +784,7 @@ cloudbrain_helper=使用GPU/NPU资源,开启Notebook、模型训练任务等 model_manager = 模型 model_noright=无权限操作 +model_rename=模型名称重复,请修改模型名称 debug=调试 stop=停止 diff --git a/routers/repo/ai_model_manage.go b/routers/repo/ai_model_manage.go index ad68f5b99..da505d84e 100644 --- a/routers/repo/ai_model_manage.go +++ b/routers/repo/ai_model_manage.go @@ -105,6 +105,22 @@ func saveModelByParameters(jobId string, versionName string, name string, versio return nil } +func SaveNewNameModel(ctx *context.Context) { + name := ctx.Query("Name") + if name == "" { + ctx.Error(500, fmt.Sprintf("name or version is null.")) + return + } + + aimodels := models.QueryModelByName(name, ctx.Repo.Repository.ID) + if len(aimodels) > 0 { + ctx.ServerError("Name error.", errors.New(ctx.Tr("repo.model_noright"))) + } + SaveModel(ctx) + + log.Info("save model end.") +} + func SaveModel(ctx *context.Context) { log.Info("save model start.") JobId := ctx.Query("JobId") @@ -569,3 +585,53 @@ func ModifyModelInfo(ctx *context.Context) { } } + +func QueryModelListForPredict(ctx *context.Context) { + repoId := ctx.Repo.Repository.ID + modelResult, count, err := models.QueryModel(&models.AiModelQueryOptions{ + ListOptions: models.ListOptions{ + Page: -1, + PageSize: -1, + }, + RepoID: repoId, + Type: -1, + New: -1, + }) + if err != nil { + ctx.ServerError("Cloudbrain", err) + return + } + log.Info("query return count=" + fmt.Sprint(count)) + + nameList := make([]string, 0) + + nameMap := make(map[string][]*models.AiModelManage) + for _, model := range modelResult { + if _, value := nameMap[model.Name]; !value { + models := make([]*models.AiModelManage, 0) + models = append(models, model) + nameMap[model.Name] = models + nameList = append(nameList, model.Name) + } else { + nameMap[model.Name] = append(nameMap[model.Name], model) + } + } + + mapInterface := make(map[string]interface{}) + mapInterface["nameList"] = nameList + mapInterface["nameMap"] = nameMap + ctx.JSON(http.StatusOK, mapInterface) +} + +func QueryModelFileForPredict(ctx *context.Context) { + id := ctx.Query("ID") + model, err := models.QueryModelById(id) + if err != nil { + log.Error("no such model!", err.Error()) + ctx.ServerError("no such model:", err) + return + } + prefix := model.Path[len(setting.Bucket)+2:] + fileinfos, err := storage.GetAllObjectByBucketAndPrefix(setting.Bucket, prefix) + ctx.JSON(http.StatusOK, fileinfos) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 30e486b98..0f3bea9a0 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -975,6 +975,7 @@ func RegisterRoutes(m *macaron.Macaron) { }, context.RepoRef()) m.Group("/modelmanage", func() { m.Post("/create_model", reqRepoModelManageWriter, repo.SaveModel) + m.Post("/create_new_model", reqRepoModelManageWriter, repo.SaveNewNameModel) m.Delete("/delete_model", repo.DeleteModel) m.Put("/modify_model", repo.ModifyModelInfo) m.Get("/show_model", reqRepoModelManageReader, repo.ShowModelTemplate) From 1c664e99f99a11cdb86e7aad420fb6e1ca4d9714 Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 16 Dec 2021 16:33:54 +0800 Subject: [PATCH 002/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dateset_foreigntable_for_es.sql | 6 +- models/dbsql/issue_foreigntable_for_es.sql | 200 +++++++++++++++++++++++++++ models/dbsql/pr_foreigntable_for_es.sql | 123 ++++++++++++++++ models/user_business_analysis.go | 2 +- 4 files changed, 327 insertions(+), 4 deletions(-) diff --git a/models/dbsql/dateset_foreigntable_for_es.sql b/models/dbsql/dateset_foreigntable_for_es.sql index 0cc41e6d7..55522af3b 100644 --- a/models/dbsql/dateset_foreigntable_for_es.sql +++ b/models/dbsql/dateset_foreigntable_for_es.sql @@ -27,7 +27,7 @@ OPTIONS CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS $def$ BEGIN - INSERT INTO public.dataset( + INSERT INTO public.dataset_es( id, title, status, @@ -70,9 +70,9 @@ CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name() RETURNS trigger AS $def$ BEGIN if (TG_OP = 'DELETE') then - update public.dataset_es SET file_name=(select string_agg(name, ',') from public.attachment where dataset_id=OLD.dataset_id order by created_unix desc) where id=OLD.dataset_id; + update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=OLD.dataset_id order by created_unix desc) where id=OLD.dataset_id; elsif (TG_OP = 'INSERT') then - update public.dataset_es SET file_name=(select string_agg(name, ',') from public.attachment where dataset_id=NEW.dataset_id order by created_unix desc) where id=NEW.dataset_id; + update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=NEW.dataset_id order by created_unix desc) where id=NEW.dataset_id; end if; return null; diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index e69de29bb..01ffeed9c 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -0,0 +1,200 @@ +CREATE FOREIGN TABLE public.issue_es +( + id bigint NOT NULL, + repo_id bigint, + index bigint, + poster_id bigint, + original_author character varying(255), + original_author_id bigint, + name character varying(255) , + content text, + comment text, + milestone_id bigint, + priority integer, + is_closed boolean, + is_pull boolean, + pr_id bigint, + num_comments integer, + ref character varying(255), + deadline_unix bigint, + created_unix bigint, + updated_unix bigint, + closed_unix bigint, + is_locked boolean NOT NULL, + amount bigint, + is_transformed boolean NOT NULL, +)SERVER multicorn_es +OPTIONS + ( + host '192.168.207.94', + port '9200', + index 'user_es-index', + rowid_column 'id' + ) +; + +CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS +$def$ + BEGIN + INSERT INTO public.issue_es( + id, + repo_id, + index, + poster_id, + original_author, + original_author_id, + name, + content, + milestone_id, + priority, + is_closed, + is_pull, + num_comments, + ref, + deadline_unix, + created_unix, + updated_unix, + closed_unix, + is_locked, + amount, + is_transformed) + VALUES ( + NEW.id, + NEW.repo_id, + NEW.index, + NEW.poster_id, + NEW.original_author, + NEW.original_author_id, + NEW.name, + NEW.content, + NEW.milestone_id, + NEW.priority, + NEW.is_closed, + NEW.is_pull, + NEW.num_comments, + NEW.ref, + NEW.deadline_unix, + NEW.created_unix, + NEW.updated_unix, + NEW.closed_unix, + NEW.is_locked, + NEW.amount, + NEW.is_transformed + ); + + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_issue on public.issue; + +CREATE TRIGGER es_insert_issue + AFTER INSERT ON public.issue + FOR EACH ROW EXECUTE PROCEDURE insert_issue_data(); + + + +CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS +$def$ + BEGIN + if (TG_OP = 'DELETE') then + update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=OLD.issue_id order by created_unix desc) where id=OLD.issue_id; + elsif (TG_OP = 'INSERT') then + update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id order by created_unix desc) where id=NEW.issue_id; + elsif (TG_OP = 'UPDATE') then + update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id order by created_unix desc) where id=NEW.issue_id; + end if; + + return null; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment; +CREATE TRIGGER es_udpate_issue_comment + AFTER INSERT OR DELETE OR UPDATE ON public.comment + FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment(); + + +CREATE OR REPLACE FUNCTION public.update_issue_content() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.issue_es SET content=NEW.content where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_issue_content on public.issue; + +CREATE TRIGGER es_update_issue_content + AFTER UPDATE OF "content" ON public.issue + FOR EACH ROW EXECUTE PROCEDURE update_issue_content(); + + +CREATE OR REPLACE FUNCTION public.update_issue_name() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.issue_es SET name=NEW.name where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_issue_name on public.issue; + +CREATE TRIGGER es_update_issue_name + AFTER UPDATE OF "name" ON public.issue + FOR EACH ROW EXECUTE PROCEDURE update_issue_name(); + + +CREATE OR REPLACE FUNCTION public.update_issue_is_closed() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.issue_es SET is_closed=NEW.is_closed where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_issue_is_closed on public.issue; + +CREATE TRIGGER es_update_issue_is_closed + AFTER UPDATE OF "is_closed" ON public.issue + FOR EACH ROW EXECUTE PROCEDURE update_issue_is_closed(); + +CREATE OR REPLACE FUNCTION public.update_issue_num_comments() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.issue_es SET num_comments=NEW.num_comments where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_issue_num_comments on public.issue; + +CREATE TRIGGER es_update_issue_num_comments + AFTER UPDATE OF "num_comments" ON public.issue + FOR EACH ROW EXECUTE PROCEDURE update_issue_num_comments(); + + +CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS +$def$ + declare + BEGIN + DELETE FROM public.issue_es where id=OLD.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_delete_issue on public.issue; +CREATE TRIGGER es_delete_issue + AFTER DELETE ON public.issue + FOR EACH ROW EXECUTE PROCEDURE delete_issue(); \ No newline at end of file diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql index e69de29bb..183844d87 100644 --- a/models/dbsql/pr_foreigntable_for_es.sql +++ b/models/dbsql/pr_foreigntable_for_es.sql @@ -0,0 +1,123 @@ +CREATE FOREIGN TABLE public.pull_request_es +( + id bigint NOT NULL, + type integer, + status integer, + conflicted_files json, + commits_ahead integer, + commits_behind integer, + issue_id bigint, + index bigint, + head_repo_id bigint, + base_repo_id bigint, + head_branch character varying(255), + base_branch character varying(255), + merge_base character varying(40), + has_merged boolean, + merged_commit_id character varying(40), + merger_id bigint, + merged_unix bigint, + is_transformed boolean NOT NULL DEFAULT false, + amount integer NOT NULL DEFAULT 0 +)SERVER multicorn_es +OPTIONS + ( + host '192.168.207.94', + port '9200', + index 'user_es-index', + rowid_column 'id' + ) +; + +CREATE OR REPLACE FUNCTION public.insert_pull_request_data() RETURNS trigger AS +$def$ + BEGIN + INSERT INTO public.pull_request_es( + id, + type, + status, + conflicted_files, + commits_ahead, + commits_behind, + issue_id, + index, + head_repo_id, + base_repo_id, + head_branch, + base_branch, + merge_base, + has_merged, + merged_commit_id, + merger_id, + merged_unix, + is_transformed, + amount) + VALUES ( + NEW.id, + NEW.type, + NEW.status, + NEW.conflicted_files, + NEW.commits_ahead, + NEW.commits_behind, + NEW.issue_id, + NEW.index, + NEW.head_repo_id, + NEW.base_repo_id, + NEW.head_branch, + NEW.base_branch, + NEW.merge_base, + NEW.has_merged, + NEW.merged_commit_id, + NEW.merger_id, + NEW.merged_unix, + NEW.is_transformed, + NEW.amount + ); + + --update issue + UPDATE public.issue_es SET pr_id=NEW.id where id=NEW.issue_id; + + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_pull_request on public.pull_request; + +CREATE TRIGGER es_insert_pull_request + AFTER INSERT ON public.pull_request + FOR EACH ROW EXECUTE PROCEDURE insert_pull_request_data(); + + + +CREATE OR REPLACE FUNCTION public.update_pull_request_has_merged() RETURNS trigger AS +$def$ + declare + BEGIN + UPDATE public.pull_request_es SET has_merged=NEW.has_merged where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_pull_request_has_merged on public.pull_request; + +CREATE TRIGGER es_update_pull_request_has_merged + AFTER UPDATE OF "has_merged" ON public.pull_request + FOR EACH ROW EXECUTE PROCEDURE update_pull_request_has_merged(); + + +CREATE OR REPLACE FUNCTION public.delete_pull_request() RETURNS trigger AS +$def$ + declare + BEGIN + DELETE FROM public.pull_request_es where id=OLD.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_delete_pull_request on public.pull_request; +CREATE TRIGGER es_delete_pull_request + AFTER DELETE ON public.pull_request + FOR EACH ROW EXECUTE PROCEDURE delete_pull_request(); \ No newline at end of file diff --git a/models/user_business_analysis.go b/models/user_business_analysis.go index 9b9d5e0ad..0809f9544 100644 --- a/models/user_business_analysis.go +++ b/models/user_business_analysis.go @@ -1135,7 +1135,7 @@ func queryLoginCount(start_unix int64, end_unix int64) map[int64]int { break } } - + log.Info("user login size=" + fmt.Sprint(len(resultMap))) return resultMap } From e585c9a90a12feac58833b67fb853e0d090fe7c1 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 17 Dec 2021 09:47:01 +0800 Subject: [PATCH 003/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=93=E5=8D=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/issue_foreigntable_for_es.sql | 8 ++++---- models/user_business_analysis.go | 4 +--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index 01ffeed9c..db235c3b5 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -22,7 +22,7 @@ CREATE FOREIGN TABLE public.issue_es closed_unix bigint, is_locked boolean NOT NULL, amount bigint, - is_transformed boolean NOT NULL, + is_transformed boolean NOT NULL )SERVER multicorn_es OPTIONS ( @@ -99,11 +99,11 @@ CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS $def$ BEGIN if (TG_OP = 'DELETE') then - update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=OLD.issue_id order by created_unix desc) where id=OLD.issue_id; + update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=OLD.issue_id) where id=OLD.issue_id; elsif (TG_OP = 'INSERT') then - update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id order by created_unix desc) where id=NEW.issue_id; + update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id) where id=NEW.issue_id; elsif (TG_OP = 'UPDATE') then - update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id order by created_unix desc) where id=NEW.issue_id; + update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id) where id=NEW.issue_id; end if; return null; diff --git a/models/user_business_analysis.go b/models/user_business_analysis.go index 0809f9544..c958e47e6 100644 --- a/models/user_business_analysis.go +++ b/models/user_business_analysis.go @@ -1,7 +1,6 @@ package models import ( - "encoding/json" "fmt" "sort" "strconv" @@ -1099,8 +1098,7 @@ func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 } } - userMapJson, _ := json.Marshal(userMap) - log.Info("userMapJson=" + string(userMapJson)) + log.Info("user openi index size=" + fmt.Sprint(len(userMap))) return userMap } From 8c7191f77a1d9359887d95084eac09dd3d526d97 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 17 Dec 2021 10:20:07 +0800 Subject: [PATCH 004/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/routes/routes.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 0f3bea9a0..1ce5472d1 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -985,6 +985,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/show_model_child_api", repo.ShowOneVersionOtherModel) m.Get("/query_train_job", reqRepoCloudBrainReader, repo.QueryTrainJobList) m.Get("/query_train_job_version", reqRepoCloudBrainReader, repo.QueryTrainJobVersionList) + m.Get("/query_model_for_predict", reqRepoCloudBrainReader, repo.QueryModelListForPredict) + m.Get("/query_modelfile_for_predict", reqRepoCloudBrainReader, repo.QueryModelFileForPredict) m.Group("/:ID", func() { m.Get("", repo.ShowSingleModel) m.Get("/downloadsingle", repo.DownloadSingleModelFile) From d1836f4228fc540c460de38bf98ad1b775e2ee7d Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 21 Dec 2021 10:35:35 +0800 Subject: [PATCH 005/315] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=96=B0=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E6=8F=90=E7=A4=BA=E4=BF=A1=E6=81=AF=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/repo_foreigntable_for_es.sql | 14 +++++++++++++- routers/repo/ai_model_manage.go | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 0b09f576d..3637853cd 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -1,3 +1,4 @@ +DROP FOREIGN TABLE public.repository_es; CREATE FOREIGN TABLE public.repository_es ( id bigint NOT NULL, owner_id bigint, @@ -30,7 +31,7 @@ CREATE FOREIGN TABLE public.repository_es ( size bigint DEFAULT 0 NOT NULL, is_fsck_enabled boolean DEFAULT true NOT NULL, close_issues_via_commit_in_any_branch boolean DEFAULT false NOT NULL, - topics json, + topics text, avatar character varying(64), created_unix bigint, updated_unix bigint, @@ -145,6 +146,7 @@ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS es_insert_repository on public.repository; + CREATE TRIGGER es_insert_repository AFTER INSERT ON public.repository FOR EACH ROW EXECUTE PROCEDURE insert_repository_data(); @@ -160,7 +162,17 @@ $def$ $def$ LANGUAGE plpgsql; + DROP TRIGGER IF EXISTS es_update_repository_description on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_name on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_ownername on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_website on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_topics on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_updated_unix on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_num_watches on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_num_stars on public.repository; +DROP TRIGGER IF EXISTS es_update_repository_num_forks on public.repository; +DROP TRIGGER IF EXISTS es_delete_repository on public.repository; CREATE TRIGGER es_update_repository_description AFTER UPDATE OF "description" ON public.repository diff --git a/routers/repo/ai_model_manage.go b/routers/repo/ai_model_manage.go index da505d84e..22b551206 100644 --- a/routers/repo/ai_model_manage.go +++ b/routers/repo/ai_model_manage.go @@ -114,7 +114,8 @@ func SaveNewNameModel(ctx *context.Context) { aimodels := models.QueryModelByName(name, ctx.Repo.Repository.ID) if len(aimodels) > 0 { - ctx.ServerError("Name error.", errors.New(ctx.Tr("repo.model_noright"))) + ctx.Error(500, ctx.Tr("repo.model_rename")) + return } SaveModel(ctx) From dbc578adfa4c39272a56569cab6e23027668ebd4 Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 22 Dec 2021 14:25:57 +0800 Subject: [PATCH 006/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=B8=8D=E5=87=86=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dataset_foreigntable_for_es.sql | 152 ++++++++++++++++ models/dbsql/dateset_foreigntable_for_es.sql | 155 ---------------- models/dbsql/issue_foreigntable_for_es.sql | 128 +++++++------- models/dbsql/pr_foreigntable_for_es.sql | 66 +++++-- models/dbsql/repo_foreigntable_for_es.sql | 255 +++++++++++---------------- models/dbsql/user_foreigntable_for_es.sql | 200 +++++++++++---------- models/user_business_analysis.go | 64 +++---- routers/home.go | 13 +- 8 files changed, 521 insertions(+), 512 deletions(-) create mode 100644 models/dbsql/dataset_foreigntable_for_es.sql delete mode 100644 models/dbsql/dateset_foreigntable_for_es.sql diff --git a/models/dbsql/dataset_foreigntable_for_es.sql b/models/dbsql/dataset_foreigntable_for_es.sql new file mode 100644 index 000000000..9e1e886b6 --- /dev/null +++ b/models/dbsql/dataset_foreigntable_for_es.sql @@ -0,0 +1,152 @@ +DROP FOREIGN TABLE public.dataset_es; +CREATE FOREIGN TABLE public.dataset_es +( + id bigint NOT NULL, + title character varying(255), + status integer, + category character varying(255), + description text, + download_times bigint, + license character varying(255), + task character varying(255), + release_id bigint, + user_id bigint, + repo_id bigint, + created_unix bigint, + updated_unix bigint, + file_name text +)SERVER multicorn_es +OPTIONS + ( + host '192.168.207.94', + port '9200', + index 'user_es-index', + rowid_column 'id', + default_sort '_id' + ) +; +DELETE FROM public.dataset_es; + INSERT INTO public.dataset_es( + id, + title, + status, + category, + description, + download_times, + license, task, + release_id, + user_id, + repo_id, + created_unix, + updated_unix,file_name) + SELECT + id, + title, + status, + category, + description, + download_times, + license, + task, + release_id, + user_id, + repo_id, + created_unix, + updated_unix,(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment a where a.dataset_id=b.id) + FROM public.dataset b; + +CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS +$def$ + BEGIN + INSERT INTO public.dataset_es( + id, + title, + status, + category, + description, + download_times, + license, task, + release_id, + user_id, + repo_id, + created_unix, + updated_unix) + VALUES ( + NEW.id, + NEW.title, + NEW.status, + NEW.category, + NEW.description, + NEW.download_times, + NEW.license, + NEW.task, + NEW.release_id, + NEW.user_id, + NEW.repo_id, + NEW.created_unix, + NEW.updated_unix + ); + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_dataset on public.dataset; + +CREATE TRIGGER es_insert_dataset + AFTER INSERT ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data(); + + +CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name_delete() RETURNS trigger AS +$def$ + BEGIN + update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=OLD.dataset_id) where id=OLD.dataset_id; + return NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_udpate_dataset_file_name_delete on public.attachment; +CREATE TRIGGER es_udpate_dataset_file_name_delete + AFTER DELETE ON public.attachment + FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name_delete(); + + +CREATE OR REPLACE FUNCTION public.update_dataset() RETURNS trigger AS +$def$ + BEGIN + UPDATE public.dataset_es + SET description=NEW.description, + title=NEW.title, + category=NEW.category, + file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=NEW.id) + where id=NEW.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_update_dataset on public.dataset; + +CREATE TRIGGER es_update_dataset + AFTER UPDATE ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE update_dataset(); + + +CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS +$def$ + declare + BEGIN + DELETE FROM public.dataset_es where id=OLD.id; + return new; + END +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset; +CREATE TRIGGER es_delete_dataset + AFTER DELETE ON public.dataset + FOR EACH ROW EXECUTE PROCEDURE delete_dataset(); + + diff --git a/models/dbsql/dateset_foreigntable_for_es.sql b/models/dbsql/dateset_foreigntable_for_es.sql deleted file mode 100644 index 55522af3b..000000000 --- a/models/dbsql/dateset_foreigntable_for_es.sql +++ /dev/null @@ -1,155 +0,0 @@ -CREATE FOREIGN TABLE public."dataset_es" -( - id bigint NOT NULL, - title character varying(255), - status integer, - category character varying(255), - description text, - download_times bigint, - license character varying(255), - task character varying(255), - release_id bigint, - user_id bigint, - repo_id bigint, - created_unix bigint, - updated_unix bigint, - file_name text -)SERVER multicorn_es -OPTIONS - ( - host '192.168.207.94', - port '9200', - index 'user_es-index', - rowid_column 'id' - ) -; - -CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS -$def$ - BEGIN - INSERT INTO public.dataset_es( - id, - title, - status, - category, - description, - download_times, - license, task, - release_id, - user_id, - repo_id, - created_unix, - updated_unix) - VALUES ( - NEW.id, - NEW.title, - NEW.status, - NEW.category, - NEW.description, - NEW.download_times, - NEW.license, - NEW.task, - NEW.release_id, - NEW.user_id, - NEW.repo_id, - NEW.created_unix, - NEW.updated_unix - ); - RETURN NEW; - END; -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_insert_dataset on public.dataset; - -CREATE TRIGGER es_insert_dataset - AFTER INSERT ON public.dataset - FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data(); - -CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name() RETURNS trigger AS -$def$ - BEGIN - if (TG_OP = 'DELETE') then - update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=OLD.dataset_id order by created_unix desc) where id=OLD.dataset_id; - elsif (TG_OP = 'INSERT') then - update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=NEW.dataset_id order by created_unix desc) where id=NEW.dataset_id; - end if; - - return null; - END; -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_udpate_dataset_file_name on public.attachment; -CREATE TRIGGER es_udpate_dataset_file_name - AFTER INSERT OR DELETE ON public.attachment - FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name(); - - -CREATE OR REPLACE FUNCTION public.update_dataset_description() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.dataset_es SET description=NEW.description where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_dataset_description on public.dataset; - -CREATE TRIGGER es_update_dataset_description - AFTER UPDATE OF "description" ON public.dataset - FOR EACH ROW EXECUTE PROCEDURE update_dataset_description(); - - -CREATE OR REPLACE FUNCTION public.update_dataset_title() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.dataset_es SET title=NEW.title where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_dataset_title on public.dataset; - -CREATE TRIGGER es_update_dataset_title - AFTER UPDATE OF "title" ON public.dataset - FOR EACH ROW EXECUTE PROCEDURE update_dataset_title(); - - -CREATE OR REPLACE FUNCTION public.update_dataset_category() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.dataset_es SET category=NEW.category where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_dataset_category on public.dataset; - -CREATE TRIGGER es_update_dataset_category - AFTER UPDATE OF "category" ON public.dataset - FOR EACH ROW EXECUTE PROCEDURE update_dataset_category(); - - -CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS -$def$ - declare - BEGIN - DELETE FROM public.dataset_es where id=OLD.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset; -CREATE TRIGGER es_delete_dataset - AFTER DELETE ON public.dataset - FOR EACH ROW EXECUTE PROCEDURE delete_dataset(); - - diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index db235c3b5..d05dae93a 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -1,3 +1,4 @@ +DROP FOREIGN TABLE public.issue_es; CREATE FOREIGN TABLE public.issue_es ( id bigint NOT NULL, @@ -29,10 +30,60 @@ OPTIONS host '192.168.207.94', port '9200', index 'user_es-index', - rowid_column 'id' + rowid_column 'id', + default_sort '_id' ) ; +DELETE FROM public.issue_es; +INSERT INTO public.issue_es( + id, + repo_id, + index, + poster_id, + original_author, + original_author_id, + name, + content, + milestone_id, + priority, + is_closed, + is_pull, + num_comments, + ref, + deadline_unix, + created_unix, + updated_unix, + closed_unix, + is_locked, + amount, + is_transformed,comment) + SELECT + id, + repo_id, + index, + poster_id, + original_author, + original_author_id, + name, + content, + milestone_id, + priority, + is_closed, + is_pull, + num_comments, + ref, + deadline_unix, + created_unix, + updated_unix, + closed_unix, + is_locked, + amount, + is_transformed, + (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id) + FROM public.issue b; + + CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS $def$ BEGIN @@ -79,7 +130,7 @@ $def$ NEW.closed_unix, NEW.is_locked, NEW.amount, - NEW.is_transformed + NEW.is_transformed ); RETURN NEW; @@ -100,9 +151,7 @@ $def$ BEGIN if (TG_OP = 'DELETE') then update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=OLD.issue_id) where id=OLD.issue_id; - elsif (TG_OP = 'INSERT') then - update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id) where id=NEW.issue_id; - elsif (TG_OP = 'UPDATE') then + elsif (TG_OP = 'UPDATE') then update public.issue_es SET comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.issue_id) where id=NEW.issue_id; end if; @@ -113,75 +162,32 @@ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS es_udpate_issue_comment on public.comment; CREATE TRIGGER es_udpate_issue_comment - AFTER INSERT OR DELETE OR UPDATE ON public.comment + AFTER DELETE OR UPDATE ON public.comment FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment(); -CREATE OR REPLACE FUNCTION public.update_issue_content() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.issue_es SET content=NEW.content where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_issue_content on public.issue; - -CREATE TRIGGER es_update_issue_content - AFTER UPDATE OF "content" ON public.issue - FOR EACH ROW EXECUTE PROCEDURE update_issue_content(); - - -CREATE OR REPLACE FUNCTION public.update_issue_name() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.issue_es SET name=NEW.name where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_issue_name on public.issue; - -CREATE TRIGGER es_update_issue_name - AFTER UPDATE OF "name" ON public.issue - FOR EACH ROW EXECUTE PROCEDURE update_issue_name(); - - -CREATE OR REPLACE FUNCTION public.update_issue_is_closed() RETURNS trigger AS +CREATE OR REPLACE FUNCTION public.update_issue() RETURNS trigger AS $def$ declare BEGIN - UPDATE public.issue_es SET is_closed=NEW.is_closed where id=NEW.id; + UPDATE public.issue_es + SET content=NEW.content, + name=NEW.name, + is_closed=NEW.is_closed, + num_comments=NEW.num_comments, + comment=(select array_to_string(array_agg(content order by created_unix desc),',') from public.comment where issue_id=NEW.id) + where id=NEW.id; return new; END $def$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS es_update_issue_is_closed on public.issue; - -CREATE TRIGGER es_update_issue_is_closed - AFTER UPDATE OF "is_closed" ON public.issue - FOR EACH ROW EXECUTE PROCEDURE update_issue_is_closed(); - -CREATE OR REPLACE FUNCTION public.update_issue_num_comments() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.issue_es SET num_comments=NEW.num_comments where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; +DROP TRIGGER IF EXISTS es_update_issue on public.issue; -DROP TRIGGER IF EXISTS es_update_issue_num_comments on public.issue; +CREATE TRIGGER es_update_issue + AFTER UPDATE ON public.issue + FOR EACH ROW EXECUTE PROCEDURE update_issue(); -CREATE TRIGGER es_update_issue_num_comments - AFTER UPDATE OF "num_comments" ON public.issue - FOR EACH ROW EXECUTE PROCEDURE update_issue_num_comments(); CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql index 183844d87..1ebf4093b 100644 --- a/models/dbsql/pr_foreigntable_for_es.sql +++ b/models/dbsql/pr_foreigntable_for_es.sql @@ -1,3 +1,4 @@ +DROP FOREIGN TABLE public.pull_request_es; CREATE FOREIGN TABLE public.pull_request_es ( id bigint NOT NULL, @@ -25,10 +26,56 @@ OPTIONS host '192.168.207.94', port '9200', index 'user_es-index', - rowid_column 'id' + rowid_column 'id', + default_sort '_id' ) ; +delete from public.pull_request_es; + INSERT INTO public.pull_request_es( + id, + type, + status, + conflicted_files, + commits_ahead, + commits_behind, + issue_id, + index, + head_repo_id, + base_repo_id, + head_branch, + base_branch, + merge_base, + has_merged, + merged_commit_id, + merger_id, + merged_unix, + is_transformed, + amount) + SELECT + id, + type, + status, + conflicted_files, + commits_ahead, + commits_behind, + issue_id, + index, + head_repo_id, + base_repo_id, + head_branch, + base_branch, + merge_base, + has_merged, + merged_commit_id, + merger_id, + merged_unix, + is_transformed, + amount + FROM public.pull_request; + + + CREATE OR REPLACE FUNCTION public.insert_pull_request_data() RETURNS trigger AS $def$ BEGIN @@ -74,9 +121,6 @@ $def$ NEW.amount ); - --update issue - UPDATE public.issue_es SET pr_id=NEW.id where id=NEW.issue_id; - RETURN NEW; END; $def$ @@ -90,21 +134,23 @@ CREATE TRIGGER es_insert_pull_request -CREATE OR REPLACE FUNCTION public.update_pull_request_has_merged() RETURNS trigger AS +CREATE OR REPLACE FUNCTION public.update_pull_request() RETURNS trigger AS $def$ declare BEGIN - UPDATE public.pull_request_es SET has_merged=NEW.has_merged where id=NEW.id; + UPDATE public.pull_request_es + SET has_merged=NEW.has_merged + where id=NEW.id; return new; END $def$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS es_update_pull_request_has_merged on public.pull_request; +DROP TRIGGER IF EXISTS es_update_pull_request on public.pull_request; -CREATE TRIGGER es_update_pull_request_has_merged - AFTER UPDATE OF "has_merged" ON public.pull_request - FOR EACH ROW EXECUTE PROCEDURE update_pull_request_has_merged(); +CREATE TRIGGER es_update_pull_request + AFTER UPDATE ON public.pull_request + FOR EACH ROW EXECUTE PROCEDURE update_pull_request(); CREATE OR REPLACE FUNCTION public.delete_pull_request() RETURNS trigger AS diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 3637853cd..027a7de76 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -1,4 +1,4 @@ -DROP FOREIGN TABLE public.repository_es; +drop FOREIGN table if exists public.repository_es; CREATE FOREIGN TABLE public.repository_es ( id bigint NOT NULL, owner_id bigint, @@ -50,9 +50,96 @@ OPTIONS host '192.168.207.94', port '9200', index 'repository-es-index', - rowid_column 'id' + rowid_column 'id', + default_sort '_id' ) ; +delete from public.repository_es; + INSERT INTO public.repository_es (id, + owner_id, + owner_name, + lower_name, + name, + description, + website, + original_service_type, + original_url, + default_branch, + num_watches, + num_stars, + num_forks, + num_issues, + num_closed_issues, + num_pulls, + num_closed_pulls, + num_milestones, + num_closed_milestones, + is_private, + is_empty, + is_archived, + is_mirror, + status, + is_fork, + fork_id, + is_template, + template_id, + size, + is_fsck_enabled, + close_issues_via_commit_in_any_branch, + topics, + avatar, + created_unix, + updated_unix, + contract_address, + block_chain_status, + balance, + clone_cnt, + num_commit, + git_clone_cnt) + SELECT + id, + owner_id, + owner_name, + lower_name, + name, + description, + website, + original_service_type, + original_url, + default_branch, + num_watches, + num_stars, + num_forks, + num_issues, + num_closed_issues, + num_pulls, + num_closed_pulls, + num_milestones, + num_closed_milestones, + is_private, + is_empty, + is_archived, + is_mirror, + status, + is_fork, + fork_id, + is_template, + template_id, + size, + is_fsck_enabled, + close_issues_via_commit_in_any_branch, + topics, + avatar, + created_unix, + updated_unix, + contract_address, + block_chain_status, + balance, + clone_cnt, + num_commit, + git_clone_cnt + FROM public.repository; + CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS $def$ @@ -152,162 +239,30 @@ CREATE TRIGGER es_insert_repository FOR EACH ROW EXECUTE PROCEDURE insert_repository_data(); -CREATE OR REPLACE FUNCTION public.update_repository_description() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET description=NEW.description where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - - -DROP TRIGGER IF EXISTS es_update_repository_description on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_name on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_ownername on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_website on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_topics on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_updated_unix on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_num_watches on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_num_stars on public.repository; -DROP TRIGGER IF EXISTS es_update_repository_num_forks on public.repository; -DROP TRIGGER IF EXISTS es_delete_repository on public.repository; - -CREATE TRIGGER es_update_repository_description - AFTER UPDATE OF "description" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_description(); - - -CREATE OR REPLACE FUNCTION public.update_repository_name() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET name=NEW.name,lower_name=NEW.lower_name where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_name on public.repository; -CREATE TRIGGER es_update_repository_name - AFTER UPDATE OF "name","lower_name" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_name(); - - -CREATE OR REPLACE FUNCTION public.update_repository_ownername() RETURNS trigger AS +CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS $def$ - declare - BEGIN - UPDATE public.repository_es SET owner_name=NEW.owner_name where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_ownername on public.repository; -CREATE TRIGGER es_update_repository_ownername - AFTER UPDATE OF "owner_name" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_ownername(); - -CREATE OR REPLACE FUNCTION public.update_repository_website() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET website=NEW.website where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_website on public.repository; -CREATE TRIGGER es_update_repository_website - AFTER UPDATE OF "website" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_website(); - - -CREATE OR REPLACE FUNCTION public.update_repository_topics() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET topics=NEW.topics where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_topics on public.repository; -CREATE TRIGGER es_update_repository_topics - AFTER UPDATE OF "topics" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_topics(); - - - -CREATE OR REPLACE FUNCTION public.update_repository_updated_unix() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET updated_unix=NEW.updated_unix where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_updated_unix on public.repository; -CREATE TRIGGER es_update_repository_updated_unix - AFTER UPDATE OF "updated_unix" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_updated_unix(); - - -CREATE OR REPLACE FUNCTION public.update_repository_num_watches() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET num_watches=NEW.num_watches where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_num_watches on public.repository; -CREATE TRIGGER es_update_repository_num_watches - AFTER UPDATE OF "num_watches" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_num_watches(); - - -CREATE OR REPLACE FUNCTION public.update_repository_num_stars() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.repository_es SET num_stars=NEW.num_stars where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_repository_num_stars on public.repository; -CREATE TRIGGER es_update_repository_num_stars - AFTER UPDATE OF "num_stars" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_num_stars(); - - - -CREATE OR REPLACE FUNCTION public.update_repository_num_forks() RETURNS trigger AS -$def$ - declare BEGIN - UPDATE public.repository_es SET num_forks=NEW.num_forks where id=NEW.id; + update public.repository_es SET description=NEW.description, + name=NEW.name, + lower_name=NEW.lower_name, + owner_name=NEW.owner_name, + website=NEW.website, + updated_unix=NEW.updated_unix, + num_watches=NEW.num_watches, + num_stars=NEW.num_stars, + num_forks=NEW.num_forks, + topics=NEW.topics + where id=NEW.id; return new; END $def$ LANGUAGE plpgsql; +DROP TRIGGER IF EXISTS es_update_repository on public.repository; +CREATE TRIGGER es_update_repository + AFTER UPDATE ON public.repository + FOR EACH ROW EXECUTE PROCEDURE update_repository(); -DROP TRIGGER IF EXISTS es_update_repository_num_forks on public.repository; -CREATE TRIGGER es_update_repository_num_forks - AFTER UPDATE OF "num_forks" ON public.repository - FOR EACH ROW EXECUTE PROCEDURE update_repository_num_forks(); - CREATE OR REPLACE FUNCTION public.delete_repository() RETURNS trigger AS $def$ diff --git a/models/dbsql/user_foreigntable_for_es.sql b/models/dbsql/user_foreigntable_for_es.sql index dc362fce3..5708a5b4c 100644 --- a/models/dbsql/user_foreigntable_for_es.sql +++ b/models/dbsql/user_foreigntable_for_es.sql @@ -1,4 +1,5 @@ -CREATE FOREIGN TABLE public."user_es" +DROP FOREIGN table if exists public.user_es; +CREATE FOREIGN TABLE public.user_es ( id bigint NOT NULL , lower_name character varying(255) NULL, @@ -55,9 +56,98 @@ OPTIONS host '192.168.207.94', port '9200', index 'user_es-index', - rowid_column 'id' + rowid_column 'id', + default_sort '_id' ) ; +delete from public.user_es; + INSERT INTO public.user_es( + id, + lower_name, + name, + full_name, + email, + keep_email_private, + email_notifications_preference, + must_change_password, + login_type, + login_source, + login_name, + type, + location, + website, + rands, + language, + description, + created_unix, + updated_unix, + last_login_unix, + last_repo_visibility, + max_repo_creation, + is_active, + is_restricted, + allow_git_hook, + allow_import_local, + allow_create_organization, + prohibit_login, + avatar, + avatar_email, + use_custom_avatar, + num_followers, + num_following, + num_stars, + num_repos, + num_teams, + num_members, + visibility, + repo_admin_change_team_access, + diff_view_style, + theme, + is_operator) + SELECT + id, + lower_name, + name, + full_name, + email, + keep_email_private, + email_notifications_preference, + must_change_password, + login_type, + login_source, + login_name, + type, + location, + website, + rands, + language, + description, + created_unix, + updated_unix, + last_login_unix, + last_repo_visibility, + max_repo_creation, + is_active, + is_restricted, + allow_git_hook, + allow_import_local, + allow_create_organization, + prohibit_login, + avatar, + avatar_email, + use_custom_avatar, + num_followers, + num_following, + num_stars, + num_repos, + num_teams, + num_members, + visibility, + repo_admin_change_team_access, + diff_view_style, + theme, + is_operator + FROM public.user; CREATE OR REPLACE FUNCTION public.insert_user_data() RETURNS trigger AS $def$ @@ -162,107 +252,27 @@ CREATE TRIGGER es_insert_user FOR EACH ROW EXECUTE PROCEDURE insert_user_data(); -CREATE OR REPLACE FUNCTION public.update_user_description() RETURNS trigger AS +CREATE OR REPLACE FUNCTION public.update_user() RETURNS trigger AS $def$ - declare - BEGIN - UPDATE public.user_es SET description=NEW.description where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_user_description on public.user; - -CREATE TRIGGER es_update_user_description - AFTER UPDATE OF "description" ON public.user - FOR EACH ROW EXECUTE PROCEDURE update_user_description(); - - -CREATE OR REPLACE FUNCTION public.update_user_name() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.user_es SET name=NEW.name where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_user_name on public.user; - -CREATE TRIGGER es_update_user_name - AFTER UPDATE OF "name" ON public.user - FOR EACH ROW EXECUTE PROCEDURE update_user_name(); - - -CREATE OR REPLACE FUNCTION public.update_user_full_name() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.user_es SET full_name=NEW.full_name where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_user_full_name on public.user; - -CREATE TRIGGER es_update_user_full_name - AFTER UPDATE OF "full_name" ON public.user - FOR EACH ROW EXECUTE PROCEDURE update_user_full_name(); - - - -CREATE OR REPLACE FUNCTION public.update_user_location() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.user_es SET location=NEW.location where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_user_location on public.user; - -CREATE TRIGGER es_update_user_location - AFTER UPDATE OF "location" ON public.user - FOR EACH ROW EXECUTE PROCEDURE update_user_location(); - - -CREATE OR REPLACE FUNCTION public.update_user_website() RETURNS trigger AS -$def$ - declare - BEGIN - UPDATE public.user_es SET website=NEW.website where id=NEW.id; - return new; - END -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_update_user_website on public.user; - -CREATE TRIGGER es_update_user_website - AFTER UPDATE OF "website" ON public.user - FOR EACH ROW EXECUTE PROCEDURE update_user_website(); - - -CREATE OR REPLACE FUNCTION public.update_user_email() RETURNS trigger AS -$def$ - declare BEGIN - UPDATE public.user_es SET email=NEW.email where id=NEW.id; + UPDATE public.user_es + SET description=NEW.description, + name=NEW.name, + full_name=NEW.full_name, + location=NEW.location, + website=NEW.website, + email=NEW.email + where id=NEW.id; return new; END $def$ LANGUAGE plpgsql; -DROP TRIGGER IF EXISTS es_update_user_email on public.user; +DROP TRIGGER IF EXISTS es_update_user on public.user; -CREATE TRIGGER es_update_user_email - AFTER UPDATE OF "email" ON public.user - FOR EACH ROW EXECUTE PROCEDURE update_user_email(); +CREATE TRIGGER es_update_user + AFTER UPDATE ON public.user + FOR EACH ROW EXECUTE PROCEDURE update_user(); diff --git a/models/user_business_analysis.go b/models/user_business_analysis.go index c958e47e6..a15b9db5f 100644 --- a/models/user_business_analysis.go +++ b/models/user_business_analysis.go @@ -201,15 +201,7 @@ func QueryUserStaticDataAll(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusi return nil, 0 } log.Info("query return total:" + fmt.Sprint(allCount)) - if allCount == 0 { - CommitCodeSizeMap, err := GetAllUserKPIStats() - if err != nil { - log.Info("query commit code errr.") - } else { - log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap))) - } - RefreshUserStaticAllTabel(make(map[string]int), CommitCodeSizeMap) - } + pageSize := 1000 totalPage := int(allCount) / pageSize userBusinessAnalysisReturnList := UserBusinessAnalysisAllList{} @@ -369,7 +361,7 @@ func RefreshUserStaticAllTabel(wikiCountMap map[string]int, CommitCodeSizeMap ma CodeMergeCountMap := queryPullRequest(start_unix, end_unix) CommitCountMap := queryCommitAction(start_unix, end_unix, 5) - IssueCountMap := queryAction(start_unix, end_unix, 6) + IssueCountMap := queryCreateIssue(start_unix, end_unix) CommentCountMap := queryComment(start_unix, end_unix) FocusRepoCountMap := queryWatch(start_unix, end_unix) @@ -394,7 +386,7 @@ func RefreshUserStaticAllTabel(wikiCountMap map[string]int, CommitCodeSizeMap ma var indexTotal int64 indexTotal = 0 for { - sess.Select("`user`.*").Table("user").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) userList := make([]*User, 0) sess.Find(&userList) for i, userRecord := range userList { @@ -527,7 +519,7 @@ func CounDataByDateAndReCount(wikiCountMap map[string]int, startTime time.Time, DataDate := startTime.Format("2006-01-02") CodeMergeCountMap := queryPullRequest(start_unix, end_unix) CommitCountMap := queryCommitAction(start_unix, end_unix, 5) - IssueCountMap := queryAction(start_unix, end_unix, 6) + IssueCountMap := queryCreateIssue(start_unix, end_unix) CommentCountMap := queryComment(start_unix, end_unix) FocusRepoCountMap := queryWatch(start_unix, end_unix) @@ -558,7 +550,7 @@ func CounDataByDateAndReCount(wikiCountMap map[string]int, startTime time.Time, var indexTotal int64 indexTotal = 0 for { - sess.Select("`user`.*").Table("user").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("`user`.*").Table("user").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) userList := make([]*User, 0) sess.Find(&userList) @@ -708,7 +700,7 @@ func querySolveIssue(start_unix int64, end_unix int64) map[int64]int { issueAssigneesList := make([]*IssueAssignees, 0) sess.Select("issue_assignees.*").Table("issue_assignees"). Join("inner", "issue", "issue.id=issue_assignees.issue_id"). - Where(cond).Limit(Page_SIZE, int(indexTotal)) + Where(cond).OrderBy("issue_assignees.id asc").Limit(Page_SIZE, int(indexTotal)) sess.Find(&issueAssigneesList) @@ -743,7 +735,7 @@ func queryPullRequest(start_unix int64, end_unix int64) map[int64]int { indexTotal = 0 for { issueList := make([]*Issue, 0) - sess.Select("issue.*").Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("issue.*").Table("issue").Join("inner", "pull_request", "issue.id=pull_request.issue_id").Where(cond).OrderBy("issue.id asc").Limit(Page_SIZE, int(indexTotal)) sess.Find(&issueList) log.Info("query issue(PR) size=" + fmt.Sprint(len(issueList))) for _, issueRecord := range issueList { @@ -776,7 +768,7 @@ func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[i var indexTotal int64 indexTotal = 0 for { - sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) actionList := make([]*Action, 0) sess.Find(&actionList) @@ -798,29 +790,30 @@ func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[i return resultMap } -func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]int { +func queryCreateIssue(start_unix int64, end_unix int64) map[int64]int { + sess := x.NewSession() defer sess.Close() resultMap := make(map[int64]int) - cond := "op_type=" + fmt.Sprint(actionType) + " and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix) + cond := "is_pull=false and created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix) - count, err := sess.Where(cond).Count(new(Action)) + count, err := sess.Where(cond).Count(new(Issue)) if err != nil { - log.Info("query Action error. return.") + log.Info("query Issue error. return.") return resultMap } var indexTotal int64 indexTotal = 0 for { - sess.Select("id,user_id,op_type,act_user_id").Table("action").Where(cond).Limit(Page_SIZE, int(indexTotal)) - actionList := make([]*Action, 0) - sess.Find(&actionList) - log.Info("query action size=" + fmt.Sprint(len(actionList))) - for _, actionRecord := range actionList { - if _, ok := resultMap[actionRecord.UserID]; !ok { - resultMap[actionRecord.UserID] = 1 + sess.Select("id,poster_id").Table("issue").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) + issueList := make([]*Issue, 0) + sess.Find(&issueList) + log.Info("query issue size=" + fmt.Sprint(len(issueList))) + for _, issueRecord := range issueList { + if _, ok := resultMap[issueRecord.PosterID]; !ok { + resultMap[issueRecord.PosterID] = 1 } else { - resultMap[actionRecord.UserID] += 1 + resultMap[issueRecord.PosterID] += 1 } } indexTotal += Page_SIZE @@ -829,6 +822,7 @@ func queryAction(start_unix int64, end_unix int64, actionType int64) map[int64]i } } return resultMap + } func queryComment(start_unix int64, end_unix int64) map[int64]int { @@ -845,7 +839,7 @@ func queryComment(start_unix int64, end_unix int64) map[int64]int { var indexTotal int64 indexTotal = 0 for { - sess.Select("id,type,poster_id").Table("comment").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,type,poster_id").Table("comment").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) commentList := make([]*Comment, 0) sess.Find(&commentList) log.Info("query Comment size=" + fmt.Sprint(len(commentList))) @@ -881,7 +875,7 @@ func queryWatch(start_unix int64, end_unix int64) map[int64]int { indexTotal = 0 for { watchList := make([]*Watch, 0) - sess.Select("id,user_id,repo_id").Table("watch").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,user_id,repo_id").Table("watch").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) sess.Find(&watchList) log.Info("query Watch size=" + fmt.Sprint(len(watchList))) @@ -919,7 +913,7 @@ func queryStar(start_unix int64, end_unix int64) map[int64]int { var indexTotal int64 indexTotal = 0 for { - sess.Select("id,uid,repo_id").Table("star").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,uid,repo_id").Table("star").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) starList := make([]*Star, 0) sess.Find(&starList) @@ -955,7 +949,7 @@ func queryFollow(start_unix int64, end_unix int64) map[int64]int { var indexTotal int64 indexTotal = 0 for { - sess.Select("id,user_id,follow_id").Table("follow").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,user_id,follow_id").Table("follow").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) followList := make([]*Follow, 0) sess.Find(&followList) @@ -991,7 +985,7 @@ func queryDatasetSize(start_unix int64, end_unix int64) map[int64]int { var indexTotal int64 indexTotal = 0 for { - sess.Select("id,uploader_id,size").Table("attachment").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,uploader_id,size").Table("attachment").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) attachmentList := make([]*Attachment, 0) sess.Find(&attachmentList) @@ -1027,7 +1021,7 @@ func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int { var indexTotal int64 indexTotal = 0 for { - sess.Select("id,owner_id,name").Table("repository").Where(cond).Limit(Page_SIZE, int(indexTotal)) + sess.Select("id,owner_id,name").Table("repository").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) repoList := make([]*Repository, 0) sess.Find(&repoList) log.Info("query Repository size=" + fmt.Sprint(len(repoList))) @@ -1117,7 +1111,7 @@ func queryLoginCount(start_unix int64, end_unix int64) map[int64]int { var indexTotal int64 indexTotal = 0 for { - statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).Limit(Page_SIZE, int(indexTotal)) + statictisSess.Select("id,u_id").Table("user_login_log").Where(cond).OrderBy("id asc").Limit(Page_SIZE, int(indexTotal)) userLoginLogList := make([]*UserLoginLog, 0) statictisSess.Find(&userLoginLogList) log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList))) diff --git a/routers/home.go b/routers/home.go index 7cc353ed8..3262aa823 100755 --- a/routers/home.go +++ b/routers/home.go @@ -518,19 +518,19 @@ func RecommendOrgFromPromote(ctx *context.Context) { recommendFromPromote(ctx, url) } -func recommendFromPromote(ctx *context.Context, url string) { +func recommendFromPromote(ctx *context.Context, url string) []string { resp, err := http.Get(url) if err != nil { log.Info("Get organizations url error=" + err.Error()) - ctx.ServerError("QueryTrainJobList:", err) - return + ctx.ServerError("Get organizations url error:", err) + return nil } bytes, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { log.Info("Get organizations url error=" + err.Error()) - ctx.ServerError("QueryTrainJobList:", err) - return + ctx.ServerError("Read organizations url error:", err) + return nil } allLineStr := string(bytes) @@ -546,10 +546,11 @@ func recommendFromPromote(ctx *context.Context, url string) { result[i] = strings.Trim(line[tmpIndex+1:], " ") } } - ctx.JSON(http.StatusOK, result) + return result } func RecommendRepoFromPromote(ctx *context.Context) { url := setting.RecommentRepoAddr + "projects" recommendFromPromote(ctx, url) + } From 08805d42a12ea74c2b47ebdd15fe92dc88dc7ae1 Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 23 Dec 2021 16:05:54 +0800 Subject: [PATCH 007/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dataset_foreigntable_for_es.sql | 1 + models/dbsql/repo_foreigntable_for_es.sql | 8 ++--- modules/setting/setting.go | 2 ++ routers/home.go | 51 +++++++++++++++++++++++----- routers/search.go | 30 ++++++++++++++++ 5 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 routers/search.go diff --git a/models/dbsql/dataset_foreigntable_for_es.sql b/models/dbsql/dataset_foreigntable_for_es.sql index 9e1e886b6..13bb88e2f 100644 --- a/models/dbsql/dataset_foreigntable_for_es.sql +++ b/models/dbsql/dataset_foreigntable_for_es.sql @@ -120,6 +120,7 @@ $def$ SET description=NEW.description, title=NEW.title, category=NEW.category, + download_times=NEW.download_times, file_name=(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment where dataset_id=NEW.id) where id=NEW.id; return new; diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 027a7de76..a1e5a877e 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -1,4 +1,4 @@ -drop FOREIGN table if exists public.repository_es; +DROP FOREIGN table if exists public.repository_es; CREATE FOREIGN TABLE public.repository_es ( id bigint NOT NULL, owner_id bigint, @@ -95,7 +95,7 @@ delete from public.repository_es; balance, clone_cnt, num_commit, - git_clone_cnt) + git_clone_cnt,lang) SELECT id, owner_id, @@ -137,8 +137,8 @@ delete from public.repository_es; balance, clone_cnt, num_commit, - git_clone_cnt - FROM public.repository; + git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) + FROM public.repository b; CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 2d70e47b1..ae83b9a9c 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -436,6 +436,8 @@ var ( //home page RecommentRepoAddr string + ESSearchURL string + //labelsystem config LabelTaskName string LabelDatasetDeleteQueue string diff --git a/routers/home.go b/routers/home.go index 3262aa823..b430a7af1 100755 --- a/routers/home.go +++ b/routers/home.go @@ -513,24 +513,37 @@ func NotFound(ctx *context.Context) { ctx.Data["Title"] = "Page Not Found" ctx.NotFound("home.NotFound", nil) } + func RecommendOrgFromPromote(ctx *context.Context) { url := setting.RecommentRepoAddr + "organizations" - recommendFromPromote(ctx, url) + result, err := recommendFromPromote(url) + if err != nil { + ctx.ServerError("500", err) + return + } + resultOrg := make([]*models.User, 0) + for i, userName := range result { + user, err := models.GetUserByName(userName) + if err == nil { + resultOrg[i] = user + } else { + log.Info("query user error," + err.Error()) + } + } + ctx.JSON(200, resultOrg) } -func recommendFromPromote(ctx *context.Context, url string) []string { +func recommendFromPromote(url string) ([]string, error) { resp, err := http.Get(url) if err != nil { log.Info("Get organizations url error=" + err.Error()) - ctx.ServerError("Get organizations url error:", err) - return nil + return nil, err } bytes, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { log.Info("Get organizations url error=" + err.Error()) - ctx.ServerError("Read organizations url error:", err) - return nil + return nil, err } allLineStr := string(bytes) @@ -546,11 +559,31 @@ func recommendFromPromote(ctx *context.Context, url string) []string { result[i] = strings.Trim(line[tmpIndex+1:], " ") } } - return result + return result, nil } func RecommendRepoFromPromote(ctx *context.Context) { url := setting.RecommentRepoAddr + "projects" - recommendFromPromote(ctx, url) - + result, err := recommendFromPromote(url) + if err != nil { + ctx.ServerError("500", err) + return + } + resultRepo := make([]*models.Repository, 0) + for i, repoName := range result { + tmpIndex := strings.Index(repoName, ".") + if tmpIndex == -1 { + log.Info("error repo name format.") + } else { + ownerName := repoName[0:tmpIndex] + repoName := repoName[tmpIndex+1:] + repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) + if err == nil { + resultRepo[i] = repo + } else { + log.Info("query repo error," + err.Error()) + } + } + } + ctx.JSON(200, resultRepo) } diff --git a/routers/search.go b/routers/search.go new file mode 100644 index 000000000..37665c961 --- /dev/null +++ b/routers/search.go @@ -0,0 +1,30 @@ +package routers + +import ( + "code.gitea.io/gitea/modules/context" +) + +type Table struct { + TableName string + SpecifyField string + SortBy string + Where string +} + +type SearchOptions struct { + Page int64 + PageSize int64 + Key string + SearchObj []Table +} + +func Search(ctx *context.Context) { + // TableName := ctx.Query("TableName") + // Key := ctx.Query("Key") + // SortBy := ctx.Query("SortBy") + // Page := ctx.QueryInt64("Page") + // PageSize := ctx.QueryInt64("PageSize") + + // ESSearchUrl := setting.RecommentRepoAddr + +} From 2befca7a7db89a815a55227e399ef11ef81eb45b Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 23 Dec 2021 16:14:18 +0800 Subject: [PATCH 008/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/home.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/home.go b/routers/home.go index b430a7af1..add06395b 100755 --- a/routers/home.go +++ b/routers/home.go @@ -575,8 +575,8 @@ func RecommendRepoFromPromote(ctx *context.Context) { if tmpIndex == -1 { log.Info("error repo name format.") } else { - ownerName := repoName[0:tmpIndex] - repoName := repoName[tmpIndex+1:] + ownerName := strings.Trim(repoName[0:tmpIndex], " ") + repoName := strings.Trim(repoName[tmpIndex+1:], " ") repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) if err == nil { resultRepo[i] = repo From dda7d94815c13dc715c63ffd685e25205fb5d85d Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 23 Dec 2021 16:29:03 +0800 Subject: [PATCH 009/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/home.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/home.go b/routers/home.go index add06395b..1a301c93b 100755 --- a/routers/home.go +++ b/routers/home.go @@ -522,10 +522,10 @@ func RecommendOrgFromPromote(ctx *context.Context) { return } resultOrg := make([]*models.User, 0) - for i, userName := range result { + for _, userName := range result { user, err := models.GetUserByName(userName) if err == nil { - resultOrg[i] = user + resultOrg = append(resultOrg, user) } else { log.Info("query user error," + err.Error()) } @@ -570,7 +570,7 @@ func RecommendRepoFromPromote(ctx *context.Context) { return } resultRepo := make([]*models.Repository, 0) - for i, repoName := range result { + for _, repoName := range result { tmpIndex := strings.Index(repoName, ".") if tmpIndex == -1 { log.Info("error repo name format.") @@ -579,7 +579,7 @@ func RecommendRepoFromPromote(ctx *context.Context) { repoName := strings.Trim(repoName[tmpIndex+1:], " ") repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) if err == nil { - resultRepo[i] = repo + resultRepo = append(resultRepo, repo) } else { log.Info("query repo error," + err.Error()) } From 2b538f8002558ce45d8cb63855eb050181e019ca Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 08:53:56 +0800 Subject: [PATCH 010/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- modules/setting/setting.go | 1 + routers/home.go | 31 +++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index ae83b9a9c..10302f8db 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1236,6 +1236,7 @@ func NewContext() { sec = Cfg.Section("homepage") RecommentRepoAddr = sec.Key("Address").MustString("https://git.openi.org.cn/OpenIOSSG/promote/raw/branch/master/") + ESSearchURL = sec.Key("ESSearchURL").MustString("https://git.openi.org.cn/OpenIOSSG/promote/raw/branch/master/") sec = Cfg.Section("cloudbrain") CBAuthUser = sec.Key("USER").MustString("") diff --git a/routers/home.go b/routers/home.go index 1a301c93b..356857ab8 100755 --- a/routers/home.go +++ b/routers/home.go @@ -521,21 +521,29 @@ func RecommendOrgFromPromote(ctx *context.Context) { ctx.ServerError("500", err) return } - resultOrg := make([]*models.User, 0) + resultOrg := make([]map[string]interface{}, 0) for _, userName := range result { user, err := models.GetUserByName(userName) if err == nil { - resultOrg = append(resultOrg, user) + userMap := make(map[string]interface{}) + userMap["Name"] = user.Name + userMap["ID"] = user.ID + userMap["Avatar"] = user.RelAvatarLink() + userMap["NumRepos"] = user.NumRepos + userMap["NumTeams"] = user.NumTeams + userMap["NumMembers"] = user.NumMembers + resultOrg = append(resultOrg, userMap) } else { log.Info("query user error," + err.Error()) } } + ctx.JSON(200, resultOrg) } func recommendFromPromote(url string) ([]string, error) { resp, err := http.Get(url) - if err != nil { + if err != nil || resp.StatusCode != 200 { log.Info("Get organizations url error=" + err.Error()) return nil, err } @@ -569,9 +577,10 @@ func RecommendRepoFromPromote(ctx *context.Context) { ctx.ServerError("500", err) return } - resultRepo := make([]*models.Repository, 0) + resultRepo := make([]map[string]interface{}, 0) + //resultRepo := make([]*models.Repository, 0) for _, repoName := range result { - tmpIndex := strings.Index(repoName, ".") + tmpIndex := strings.Index(repoName, "/") if tmpIndex == -1 { log.Info("error repo name format.") } else { @@ -579,7 +588,17 @@ func RecommendRepoFromPromote(ctx *context.Context) { repoName := strings.Trim(repoName[tmpIndex+1:], " ") repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) if err == nil { - resultRepo = append(resultRepo, repo) + repoMap := make(map[string]interface{}) + repoMap["ID"] = fmt.Sprint(repo.ID) + repoMap["Name"] = repo.Name + repoMap["OwnerName"] = repo.OwnerName + repoMap["NumStars"] = repo.NumStars + repoMap["NumForks"] = repo.NumForks + repoMap["Description"] = repo.Description + repoMap["NumWatchs"] = repo.NumWatches + repoMap["Topics"] = repo.Topics + repoMap["Avatar"] = repo.RelAvatarLink() + resultRepo = append(resultRepo, repoMap) } else { log.Info("query repo error," + err.Error()) } From cfd309de2e254b3008def53c389d2067bdd75701 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 10:44:39 +0800 Subject: [PATCH 011/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/routes/routes.go | 1 + routers/search.go | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 221950949..4776f9758 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -319,6 +319,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/action/notification", routers.ActionNotification) m.Get("/recommend/org", routers.RecommendOrgFromPromote) m.Get("/recommend/repo", routers.RecommendRepoFromPromote) + m.Get("/all/search/", routers.Search) m.Group("/explore", func() { m.Get("", func(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") diff --git a/routers/search.go b/routers/search.go index 37665c961..60d7be80e 100644 --- a/routers/search.go +++ b/routers/search.go @@ -2,6 +2,7 @@ package routers import ( "code.gitea.io/gitea/modules/context" + "github.com/olivere/elastic" ) type Table struct { @@ -18,13 +19,26 @@ type SearchOptions struct { SearchObj []Table } +var client *elastic.Client + +var host = "http://192.168.207.94:9200" + func Search(ctx *context.Context) { - // TableName := ctx.Query("TableName") - // Key := ctx.Query("Key") - // SortBy := ctx.Query("SortBy") - // Page := ctx.QueryInt64("Page") - // PageSize := ctx.QueryInt64("PageSize") + TableName := ctx.Query("TableName") + //Key := ctx.Query("Key") + //SortBy := ctx.Query("SortBy") + //Page := ctx.QueryInt64("Page") + //PageSize := ctx.QueryInt64("PageSize") + + //ESSearchUrl := setting.RecommentRepoAddr - // ESSearchUrl := setting.RecommentRepoAddr + var err error + //这个地方有个小坑 不加上elastic.SetSniff(false) 会连接不上 + client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) + if err != nil { + panic(err) + } + res, err := client.Search(TableName + "-es-index").Do(ctx) + ctx.JSON(200, res) } From 20ff1b39bd4ba57c80bbe0cef332d285461fe2c0 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 10:50:19 +0800 Subject: [PATCH 012/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/search.go b/routers/search.go index 60d7be80e..5e1e0a76f 100644 --- a/routers/search.go +++ b/routers/search.go @@ -2,7 +2,7 @@ package routers import ( "code.gitea.io/gitea/modules/context" - "github.com/olivere/elastic" + "github.com/olivere/elastic/v7" ) type Table struct { @@ -39,6 +39,6 @@ func Search(ctx *context.Context) { panic(err) } - res, err := client.Search(TableName + "-es-index").Do(ctx) + res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) ctx.JSON(200, res) } From be71c637cb0528df3f3568a3783159c0bbba050b Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 11:28:05 +0800 Subject: [PATCH 013/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dataset_foreigntable_for_es.sql | 2 +- models/dbsql/issue_foreigntable_for_es.sql | 2 +- models/dbsql/pr_foreigntable_for_es.sql | 2 +- models/dbsql/user_foreigntable_for_es.sql | 2 +- routers/search.go | 12 ++++++++++-- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/models/dbsql/dataset_foreigntable_for_es.sql b/models/dbsql/dataset_foreigntable_for_es.sql index 13bb88e2f..5bb71348a 100644 --- a/models/dbsql/dataset_foreigntable_for_es.sql +++ b/models/dbsql/dataset_foreigntable_for_es.sql @@ -20,7 +20,7 @@ OPTIONS ( host '192.168.207.94', port '9200', - index 'user_es-index', + index 'dataset-es-index', rowid_column 'id', default_sort '_id' ) diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index d05dae93a..14d08171c 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -29,7 +29,7 @@ OPTIONS ( host '192.168.207.94', port '9200', - index 'user_es-index', + index 'issue-es-index', rowid_column 'id', default_sort '_id' ) diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql index 1ebf4093b..96c47de79 100644 --- a/models/dbsql/pr_foreigntable_for_es.sql +++ b/models/dbsql/pr_foreigntable_for_es.sql @@ -25,7 +25,7 @@ OPTIONS ( host '192.168.207.94', port '9200', - index 'user_es-index', + index 'pr-es-index', rowid_column 'id', default_sort '_id' ) diff --git a/models/dbsql/user_foreigntable_for_es.sql b/models/dbsql/user_foreigntable_for_es.sql index 5708a5b4c..cc9e56db6 100644 --- a/models/dbsql/user_foreigntable_for_es.sql +++ b/models/dbsql/user_foreigntable_for_es.sql @@ -55,7 +55,7 @@ OPTIONS ( host '192.168.207.94', port '9200', - index 'user_es-index', + index 'user-es-index', rowid_column 'id', default_sort '_id' ) diff --git a/routers/search.go b/routers/search.go index 5e1e0a76f..c307c668a 100644 --- a/routers/search.go +++ b/routers/search.go @@ -25,7 +25,7 @@ var host = "http://192.168.207.94:9200" func Search(ctx *context.Context) { TableName := ctx.Query("TableName") - //Key := ctx.Query("Key") + Key := ctx.Query("Key") //SortBy := ctx.Query("SortBy") //Page := ctx.QueryInt64("Page") //PageSize := ctx.QueryInt64("PageSize") @@ -39,6 +39,14 @@ func Search(ctx *context.Context) { panic(err) } - res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) + boolQ := elastic.NewBoolQuery() + + nameQuery := elastic.NewTermQuery("name", Key) + descriptionQuery := elastic.NewTermQuery("description", Key) + + boolQ.Filter(nameQuery) + boolQ.Filter(descriptionQuery) + + res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) ctx.JSON(200, res) } From 143c67bb2ac2977d78ada8a50945623a531c77c7 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 11:32:16 +0800 Subject: [PATCH 014/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/search.go b/routers/search.go index c307c668a..0956ff953 100644 --- a/routers/search.go +++ b/routers/search.go @@ -47,6 +47,6 @@ func Search(ctx *context.Context) { boolQ.Filter(nameQuery) boolQ.Filter(descriptionQuery) - res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) + res, err := client.Search(TableName + "-es-index").Query(nameQuery).Do(ctx.Req.Context()) ctx.JSON(200, res) } From ed86391725faa80ff2a51aa392b9b12fb413678b Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 11:40:39 +0800 Subject: [PATCH 015/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/search.go b/routers/search.go index 0956ff953..67ea72228 100644 --- a/routers/search.go +++ b/routers/search.go @@ -44,9 +44,9 @@ func Search(ctx *context.Context) { nameQuery := elastic.NewTermQuery("name", Key) descriptionQuery := elastic.NewTermQuery("description", Key) - boolQ.Filter(nameQuery) - boolQ.Filter(descriptionQuery) + boolQ.Should(nameQuery, descriptionQuery) + //boolQ.Filter(descriptionQuery) - res, err := client.Search(TableName + "-es-index").Query(nameQuery).Do(ctx.Req.Context()) + res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) ctx.JSON(200, res) } From ed94d34092de799bb9df0700d9052d8c46aa94a1 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 11:47:05 +0800 Subject: [PATCH 016/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/routers/search.go b/routers/search.go index 67ea72228..951414ee7 100644 --- a/routers/search.go +++ b/routers/search.go @@ -41,11 +41,11 @@ func Search(ctx *context.Context) { boolQ := elastic.NewBoolQuery() - nameQuery := elastic.NewTermQuery("name", Key) - descriptionQuery := elastic.NewTermQuery("description", Key) - - boolQ.Should(nameQuery, descriptionQuery) - //boolQ.Filter(descriptionQuery) + if Key != "" { + nameQuery := elastic.NewTermQuery("name", Key) + descriptionQuery := elastic.NewTermQuery("description", Key) + boolQ.Should(nameQuery, descriptionQuery) + } res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) ctx.JSON(200, res) From c4903896f0a523aff678876c0e8b0b37f6ffafc2 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 11:55:17 +0800 Subject: [PATCH 017/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/routers/search.go b/routers/search.go index 951414ee7..bc3bde83c 100644 --- a/routers/search.go +++ b/routers/search.go @@ -38,15 +38,19 @@ func Search(ctx *context.Context) { if err != nil { panic(err) } - - boolQ := elastic.NewBoolQuery() - if Key != "" { + boolQ := elastic.NewBoolQuery() nameQuery := elastic.NewTermQuery("name", Key) descriptionQuery := elastic.NewTermQuery("description", Key) boolQ.Should(nameQuery, descriptionQuery) + res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) + if err != nil { + ctx.JSON(200, res) + } + } else { + res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) + if err != nil { + ctx.JSON(200, res) + } } - - res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) - ctx.JSON(200, res) } From ef88d92605fecc6f5aa734e6ef629dec5e5e577e Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 24 Dec 2021 11:57:36 +0800 Subject: [PATCH 018/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/routers/search.go b/routers/search.go index bc3bde83c..117c6503e 100644 --- a/routers/search.go +++ b/routers/search.go @@ -2,6 +2,7 @@ package routers import ( "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" "github.com/olivere/elastic/v7" ) @@ -44,12 +45,13 @@ func Search(ctx *context.Context) { descriptionQuery := elastic.NewTermQuery("description", Key) boolQ.Should(nameQuery, descriptionQuery) res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) - if err != nil { + if err == nil { ctx.JSON(200, res) } } else { + log.Info("query all content.") res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) - if err != nil { + if err == nil { ctx.JSON(200, res) } } From 61dc241387ca28910009188e95f21544a7ebfc66 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 09:46:22 +0800 Subject: [PATCH 019/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/search.go b/routers/search.go index 117c6503e..fbe6f701e 100644 --- a/routers/search.go +++ b/routers/search.go @@ -41,16 +41,16 @@ func Search(ctx *context.Context) { } if Key != "" { boolQ := elastic.NewBoolQuery() - nameQuery := elastic.NewTermQuery("name", Key) - descriptionQuery := elastic.NewTermQuery("description", Key) + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) + descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) boolQ.Should(nameQuery, descriptionQuery) - res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) + res, err := client.Search(TableName+"-es-index").Query(boolQ).Sort("updated_unix", false).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } } else { log.Info("query all content.") - res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) + res, err := client.Search(TableName+"-es-index").Sort("updated_unix", false).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } From 57e6714fea2d84145fb48cc8e75ebc3064bf4a60 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 09:49:12 +0800 Subject: [PATCH 020/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/search.go b/routers/search.go index fbe6f701e..4082ccf2f 100644 --- a/routers/search.go +++ b/routers/search.go @@ -50,7 +50,7 @@ func Search(ctx *context.Context) { } } else { log.Info("query all content.") - res, err := client.Search(TableName+"-es-index").Sort("updated_unix", false).Do(ctx.Req.Context()) + res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } From 31ab2415d7c23e24ed779ba1685f6ba6aedbacdc Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 09:50:05 +0800 Subject: [PATCH 021/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routers/search.go b/routers/search.go index 4082ccf2f..5f84e106b 100644 --- a/routers/search.go +++ b/routers/search.go @@ -47,12 +47,16 @@ func Search(ctx *context.Context) { res, err := client.Search(TableName+"-es-index").Query(boolQ).Sort("updated_unix", false).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) + } else { + log.Info("query es error," + err.Error()) } } else { log.Info("query all content.") res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) + } else { + log.Info("query es error," + err.Error()) } } } From 4022792c800d1cf12a1b97d7ebc7a365694634b5 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 10:09:14 +0800 Subject: [PATCH 022/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/routers/search.go b/routers/search.go index 5f84e106b..5c1535a9c 100644 --- a/routers/search.go +++ b/routers/search.go @@ -44,7 +44,7 @@ func Search(ctx *context.Context) { nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) boolQ.Should(nameQuery, descriptionQuery) - res, err := client.Search(TableName+"-es-index").Query(boolQ).Sort("updated_unix", false).Do(ctx.Req.Context()) + res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } else { @@ -52,6 +52,7 @@ func Search(ctx *context.Context) { } } else { log.Info("query all content.") + //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) From 0c09b298aa4450e207bec3f5b97956589e69e4f4 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 10:41:33 +0800 Subject: [PATCH 023/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/search.go b/routers/search.go index 5c1535a9c..6bb8e10f9 100644 --- a/routers/search.go +++ b/routers/search.go @@ -43,7 +43,9 @@ func Search(ctx *context.Context) { boolQ := elastic.NewBoolQuery() nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) + owner_idQuery := elastic.NewTermQuery("owner_id", 3) boolQ.Should(nameQuery, descriptionQuery) + boolQ.Must(owner_idQuery) res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) From a06c77798dc1d1b8cad28e00aaf11b9e8c4f4fdc Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 10:48:01 +0800 Subject: [PATCH 024/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/search.go b/routers/search.go index 6bb8e10f9..15f7d1055 100644 --- a/routers/search.go +++ b/routers/search.go @@ -43,9 +43,9 @@ func Search(ctx *context.Context) { boolQ := elastic.NewBoolQuery() nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) - owner_idQuery := elastic.NewTermQuery("owner_id", 3) + //owner_idQuery := elastic.NewTermQuery("owner_id", 3) boolQ.Should(nameQuery, descriptionQuery) - boolQ.Must(owner_idQuery) + //boolQ.Must(owner_idQuery) res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) From 092fae099015f992f154094340a22e70174cee99 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 16:01:12 +0800 Subject: [PATCH 025/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- modules/setting/setting.go | 2 +- routers/init.go | 2 + routers/repo/ai_model_manage.go | 2 +- routers/search.go | 252 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 239 insertions(+), 19 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 10302f8db..d8ce12dd9 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1236,7 +1236,7 @@ func NewContext() { sec = Cfg.Section("homepage") RecommentRepoAddr = sec.Key("Address").MustString("https://git.openi.org.cn/OpenIOSSG/promote/raw/branch/master/") - ESSearchURL = sec.Key("ESSearchURL").MustString("https://git.openi.org.cn/OpenIOSSG/promote/raw/branch/master/") + ESSearchURL = sec.Key("ESSearchURL").MustString("http://192.168.207.94:9200") sec = Cfg.Section("cloudbrain") CBAuthUser = sec.Key("USER").MustString("") diff --git a/routers/init.go b/routers/init.go index 8b93b64d8..eab513c78 100755 --- a/routers/init.go +++ b/routers/init.go @@ -71,6 +71,8 @@ func NewServices() { log.Info("decompression.NewContext() succeed.") labelmsg.Init() log.Info("labelmsg.Init() succeed.") + InitESClient() + log.Info("ES Client succeed.") } // In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology diff --git a/routers/repo/ai_model_manage.go b/routers/repo/ai_model_manage.go index 22b551206..86d8c140a 100644 --- a/routers/repo/ai_model_manage.go +++ b/routers/repo/ai_model_manage.go @@ -632,7 +632,7 @@ func QueryModelFileForPredict(ctx *context.Context) { ctx.ServerError("no such model:", err) return } - prefix := model.Path[len(setting.Bucket)+2:] + prefix := model.Path[len(setting.Bucket)+1:] fileinfos, err := storage.GetAllObjectByBucketAndPrefix(setting.Bucket, prefix) ctx.JSON(http.StatusOK, fileinfos) } diff --git a/routers/search.go b/routers/search.go index 15f7d1055..8e18b028b 100644 --- a/routers/search.go +++ b/routers/search.go @@ -3,6 +3,7 @@ package routers import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" "github.com/olivere/elastic/v7" ) @@ -22,44 +23,261 @@ type SearchOptions struct { var client *elastic.Client -var host = "http://192.168.207.94:9200" +func InitESClient() { + ESSearchUrl := setting.ESSearchURL + var err error + client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(ESSearchUrl)) + if err != nil { + panic(err) + } +} func Search(ctx *context.Context) { TableName := ctx.Query("TableName") Key := ctx.Query("Key") - //SortBy := ctx.Query("SortBy") - //Page := ctx.QueryInt64("Page") - //PageSize := ctx.QueryInt64("PageSize") + Page := ctx.QueryInt("Page") + PageSize := ctx.QueryInt("PageSize") + if Page <= 0 { + Page = 1 + } + if PageSize <= 0 { + PageSize = setting.UI.IssuePagingNum + } + if TableName == "repository" { + searchRepo(ctx, "repository-es-index", Key, Page, PageSize) + return + } else if TableName == "issue" { + searchIssue(ctx, "issue-es-index", Key, Page, PageSize) + return + } else if TableName == "user" { + searchUserOrOrg(ctx, "user-es-index", Key, Page, PageSize, true) + return + } else if TableName == "org" { + searchUserOrOrg(ctx, "user-es-index", Key, Page, PageSize, false) + return + } else if TableName == "dataset" { + searchDataSet(ctx, "dataset-es-index", Key, Page, PageSize) + return + } else if TableName == "pr" { + searchPR(ctx, "issue-es-index", Key, Page, PageSize) + return + } - //ESSearchUrl := setting.RecommentRepoAddr + // if Key != "" { + // boolQ := elastic.NewBoolQuery() + // nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) + // descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) + // //owner_idQuery := elastic.NewTermQuery("owner_id", 3) + // boolQ.Should(nameQuery, descriptionQuery) + // //boolQ.Must(owner_idQuery) + // res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) + // if err == nil { + // ctx.JSON(200, res) + // } else { + // log.Info("query es error," + err.Error()) + // } + // } else { + // log.Info("query all content.") + // //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} + // res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) + // if err == nil { + // ctx.JSON(200, res) + // } else { + // log.Info("query es error," + err.Error()) + // } + // } +} - var err error - //这个地方有个小坑 不加上elastic.SetSniff(false) 会连接不上 - client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) - if err != nil { - panic(err) +func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { + + /* + 项目, ES名称: repository-es-index + 搜索: + name character varying(255) , 项目名称 + description text, 项目描述 + topics json, 标签 + 排序: + updated_unix + num_watches, + num_stars, + num_forks, + */ + + SortBy := ctx.Query("SortBy") + if SortBy == "" { + SortBy = "updated_unix.keyword" } + ascending := ctx.QueryBool("Ascending") if Key != "" { boolQ := elastic.NewBoolQuery() - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) - descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) - //owner_idQuery := elastic.NewTermQuery("owner_id", 3) - boolQ.Should(nameQuery, descriptionQuery) - //boolQ.Must(owner_idQuery) - res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) + topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1) + boolQ.Should(topicsQuery) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) + return } else { log.Info("query es error," + err.Error()) } + } + ctx.JSON(200, "") +} + +func searchRepo(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { + /* + 项目, ES名称: repository-es-index + 搜索: + name character varying(255) , 项目名称 + description text, 项目描述 + topics json, 标签 + 排序: + updated_unix + num_watches, + num_stars, + num_forks, + */ + + SortBy := ctx.Query("SortBy") + if SortBy == "" { + SortBy = "updated_unix.keyword" + } + ascending := ctx.QueryBool("Ascending") + + if Key != "" { + boolQ := elastic.NewBoolQuery() + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") + descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("desc_second") + topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("topics_third") + boolQ.Should(nameQuery, descriptionQuery, topicsQuery) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + if err == nil { + ctx.JSON(200, res) + } else { + log.Info("query es error," + err.Error()) + ctx.JSON(200, "") + } } else { log.Info("query all content.") //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} - res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) + res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { ctx.JSON(200, res) } else { log.Info("query es error," + err.Error()) + ctx.JSON(200, "") } } } + +func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { + /* + 用户或者组织 ES名称: user-es-index + 搜索: + name , 名称 + full_name 全名 + description 描述或者简介 + 排序: + created_unix + 名称字母序 + */ + SortBy := ctx.Query("SortBy") + if SortBy == "" { + SortBy = "updated_unix.keyword" + } + ascending := ctx.QueryBool("Ascending") + boolQ := elastic.NewBoolQuery() + if Key != "" { + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") + full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("fullname_second") + descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("desc_third") + boolQ.Should(nameQuery, full_nameQuery, descriptionQuery) + } + typeValue := 1 + if IsQueryUser { + typeValue = 0 + } + UserOrOrgQuery := elastic.NewTermQuery("type", typeValue) + boolQ.Must(UserOrOrgQuery) + + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + if err == nil { + ctx.JSON(200, res) + } else { + log.Info("query es error," + err.Error()) + ctx.JSON(200, "") + } +} + +func searchDataSet(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { + /* + 数据集,ES名称:dataset-es-index + 搜索: + title , 名称 + description 描述 + category 标签 + file_name 数据集文件名称 + 排序: + download_times + + */ +} + +func searchIssue(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { + + /* + 任务,合并请求 ES名称:issue-es-index + 搜索: + name character varying(255) , 标题 + content text, 内容 + comment text, 评论 + 排序: + updated_unix + */ + + boolQ := elastic.NewBoolQuery() + if Key != "" { + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") + contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("content_second") + commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("comment_third") + boolQ.Should(nameQuery, contentQuery, commentQuery) + } + isIssueQuery := elastic.NewTermQuery("is_pull", false) + boolQ.Must(isIssueQuery) + res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + if err == nil { + ctx.JSON(200, res) + } else { + log.Info("query es error," + err.Error()) + } + +} + +func searchPR(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { + + /* + 任务,合并请求 ES名称:issue-es-index + 搜索: + name character varying(255) , 标题 + content text, 内容 + comment text, 评论 + 排序: + updated_unix + */ + + boolQ := elastic.NewBoolQuery() + if Key != "" { + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") + contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("content_second") + commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("comment_third") + boolQ.Should(nameQuery, contentQuery, commentQuery) + } + isIssueQuery := elastic.NewTermQuery("is_pull", true) + boolQ.Must(isIssueQuery) + res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + if err == nil { + ctx.JSON(200, res) + } else { + log.Info("query es error," + err.Error()) + } + +} From 7d77ea407ce28b171c2c7e3200e9b88f89b5c3b9 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 17:12:44 +0800 Subject: [PATCH 026/315] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 94 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/routers/search.go b/routers/search.go index 8e18b028b..42c5d506f 100644 --- a/routers/search.go +++ b/routers/search.go @@ -1,24 +1,18 @@ package routers import ( + "encoding/json" + "fmt" + "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "github.com/olivere/elastic/v7" ) -type Table struct { - TableName string - SpecifyField string - SortBy string - Where string -} - -type SearchOptions struct { - Page int64 - PageSize int64 - Key string - SearchObj []Table +type SearchRes struct { + Total int64 + Result []map[string]interface{} } var client *elastic.Client @@ -62,34 +56,9 @@ func Search(ctx *context.Context) { searchPR(ctx, "issue-es-index", Key, Page, PageSize) return } - - // if Key != "" { - // boolQ := elastic.NewBoolQuery() - // nameQuery := elastic.NewMatchQuery("name", Key).Boost(2) - // descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1) - // //owner_idQuery := elastic.NewTermQuery("owner_id", 3) - // boolQ.Should(nameQuery, descriptionQuery) - // //boolQ.Must(owner_idQuery) - // res, err := client.Search(TableName + "-es-index").Query(boolQ).Do(ctx.Req.Context()) - // if err == nil { - // ctx.JSON(200, res) - // } else { - // log.Info("query es error," + err.Error()) - // } - // } else { - // log.Info("query all content.") - // //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} - // res, err := client.Search(TableName + "-es-index").Do(ctx.Req.Context()) - // if err == nil { - // ctx.JSON(200, res) - // } else { - // log.Info("query es error," + err.Error()) - // } - // } } func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { - /* 项目, ES名称: repository-es-index 搜索: @@ -102,12 +71,12 @@ func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page num_stars, num_forks, */ - SortBy := ctx.Query("SortBy") if SortBy == "" { SortBy = "updated_unix.keyword" } ascending := ctx.QueryBool("Ascending") + log.Info("query searchRepoByLabel start") if Key != "" { boolQ := elastic.NewBoolQuery() topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1) @@ -142,7 +111,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa SortBy = "updated_unix.keyword" } ascending := ctx.QueryBool("Ascending") - + log.Info("query searchRepo start") if Key != "" { boolQ := elastic.NewBoolQuery() nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") @@ -151,7 +120,8 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa boolQ.Should(nameQuery, descriptionQuery, topicsQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - ctx.JSON(200, res) + result := makeRepoResult(res) + ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) ctx.JSON(200, "") @@ -161,7 +131,8 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - ctx.JSON(200, res) + result := makeRepoResult(res) + ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) ctx.JSON(200, "") @@ -169,6 +140,47 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } } +func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { + total := sRes.Hits.TotalHits.Value + result := make([]map[string]interface{}, 0) + + for i, hit := range sRes.Hits.Hits { + log.Info("this is " + fmt.Sprint(i) + " result.") + recordSource := make(map[string]interface{}) + source, err := hit.Source.MarshalJSON() + if err == nil { + err = json.Unmarshal(source, &recordSource) + if err == nil { + record := make(map[string]interface{}) + record["name"] = recordSource["name"] + record["owner_name"] = recordSource["owner_name"] + record["description"] = recordSource["description"] + record["num_watches"] = recordSource["num_watches"] + record["num_stars"] = recordSource["num_stars"] + record["num_forks"] = recordSource["num_forks"] + record["topics"] = recordSource["topics"] + record["avatar"] = recordSource["avatar"] + record["updated_unix"] = recordSource["updated_unix"] + record["lang"] = recordSource["lang"] + result = append(result, record) + } else { + log.Info("deal source error," + err.Error()) + } + } else { + log.Info("deal source error," + err.Error()) + } + + } + + returnObj := &SearchRes{ + Total: total, + Result: result, + } + + return returnObj + +} + func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { /* 用户或者组织 ES名称: user-es-index From 37838ce367c0e941f49d2ae17c36ca174f2371eb Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 27 Dec 2021 17:51:21 +0800 Subject: [PATCH 027/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/routers/search.go b/routers/search.go index 42c5d506f..fd6de2183 100644 --- a/routers/search.go +++ b/routers/search.go @@ -3,6 +3,7 @@ package routers import ( "encoding/json" "fmt" + "strings" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -120,7 +121,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa boolQ.Should(nameQuery, descriptionQuery, topicsQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - result := makeRepoResult(res) + result := makeRepoResult(res, Key) ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) @@ -131,7 +132,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - result := makeRepoResult(res) + result := makeRepoResult(res, "") ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) @@ -140,7 +141,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } } -func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { +func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { total := sRes.Hits.TotalHits.Value result := make([]map[string]interface{}, 0) @@ -148,13 +149,44 @@ func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { log.Info("this is " + fmt.Sprint(i) + " result.") recordSource := make(map[string]interface{}) source, err := hit.Source.MarshalJSON() + var isNeedToDealText bool + isNeedToDealText = false + if len(hit.MatchedQueries) > 0 && Key != "" { + if hit.MatchedQueries[0] == "desc_second" || hit.MatchedQueries[0] == "topics_third" { + isNeedToDealText = true + } + } if err == nil { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) record["name"] = recordSource["name"] record["owner_name"] = recordSource["owner_name"] - record["description"] = recordSource["description"] + desc := recordSource["description"].(string) + stringlen := len(desc) + if isNeedToDealText && stringlen > 200 { + index := strings.Index(desc, Key) + if index > 0 { + start := index - 50 + if start < 0 { + start = 0 + } + end := index + 150 + if end >= stringlen { + end = stringlen + } + record["description"] = desc[start:end] + } else { + record["description"] = desc[0:200] + } + } else { + if stringlen > 200 { + record["description"] = desc[0:200] + } else { + record["description"] = desc + } + } + record["num_watches"] = recordSource["num_watches"] record["num_stars"] = recordSource["num_stars"] record["num_forks"] = recordSource["num_forks"] @@ -169,7 +201,6 @@ func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { } else { log.Info("deal source error," + err.Error()) } - } returnObj := &SearchRes{ @@ -178,7 +209,6 @@ func makeRepoResult(sRes *elastic.SearchResult) *SearchRes { } return returnObj - } func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { From a2e154de256315feb28e433fd1c40b898b9eb092 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 28 Dec 2021 15:16:44 +0800 Subject: [PATCH 028/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dataset_foreigntable_for_es.sql | 63 ++++---- models/dbsql/issue_foreigntable_for_es.sql | 96 ++++++------ models/dbsql/pr_foreigntable_for_es.sql | 88 +++++------ models/dbsql/repo_foreigntable_for_es.sql | 17 ++- routers/search.go | 213 ++++++++++++++++++++------- 5 files changed, 307 insertions(+), 170 deletions(-) diff --git a/models/dbsql/dataset_foreigntable_for_es.sql b/models/dbsql/dataset_foreigntable_for_es.sql index 5bb71348a..1ba386b3e 100644 --- a/models/dbsql/dataset_foreigntable_for_es.sql +++ b/models/dbsql/dataset_foreigntable_for_es.sql @@ -57,36 +57,41 @@ DELETE FROM public.dataset_es; CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS $def$ + DECLARE + privateValue boolean=false; BEGIN - INSERT INTO public.dataset_es( - id, - title, - status, - category, - description, - download_times, - license, task, - release_id, - user_id, - repo_id, - created_unix, - updated_unix) - VALUES ( - NEW.id, - NEW.title, - NEW.status, - NEW.category, - NEW.description, - NEW.download_times, - NEW.license, - NEW.task, - NEW.release_id, - NEW.user_id, - NEW.repo_id, - NEW.created_unix, - NEW.updated_unix - ); - RETURN NEW; + select into privateValue is_private from public.repository where id=NEW.repo_id; + if not privateValue then + INSERT INTO public.dataset_es( + id, + title, + status, + category, + description, + download_times, + license, task, + release_id, + user_id, + repo_id, + created_unix, + updated_unix) + VALUES ( + NEW.id, + NEW.title, + NEW.status, + NEW.category, + NEW.description, + NEW.download_times, + NEW.license, + NEW.task, + NEW.release_id, + NEW.user_id, + NEW.repo_id, + NEW.created_unix, + NEW.updated_unix + ); + end if; + RETURN NEW; END; $def$ LANGUAGE plpgsql; diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index 14d08171c..012cf9701 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -86,53 +86,57 @@ INSERT INTO public.issue_es( CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS $def$ + DECLARE + privateValue boolean=false; BEGIN - INSERT INTO public.issue_es( - id, - repo_id, - index, - poster_id, - original_author, - original_author_id, - name, - content, - milestone_id, - priority, - is_closed, - is_pull, - num_comments, - ref, - deadline_unix, - created_unix, - updated_unix, - closed_unix, - is_locked, - amount, - is_transformed) - VALUES ( - NEW.id, - NEW.repo_id, - NEW.index, - NEW.poster_id, - NEW.original_author, - NEW.original_author_id, - NEW.name, - NEW.content, - NEW.milestone_id, - NEW.priority, - NEW.is_closed, - NEW.is_pull, - NEW.num_comments, - NEW.ref, - NEW.deadline_unix, - NEW.created_unix, - NEW.updated_unix, - NEW.closed_unix, - NEW.is_locked, - NEW.amount, - NEW.is_transformed - ); - + select into privateValue is_private from public.repository where id=NEW.repo_id; + if not privateValue then + INSERT INTO public.issue_es( + id, + repo_id, + index, + poster_id, + original_author, + original_author_id, + name, + content, + milestone_id, + priority, + is_closed, + is_pull, + num_comments, + ref, + deadline_unix, + created_unix, + updated_unix, + closed_unix, + is_locked, + amount, + is_transformed) + VALUES ( + NEW.id, + NEW.repo_id, + NEW.index, + NEW.poster_id, + NEW.original_author, + NEW.original_author_id, + NEW.name, + NEW.content, + NEW.milestone_id, + NEW.priority, + NEW.is_closed, + NEW.is_pull, + NEW.num_comments, + NEW.ref, + NEW.deadline_unix, + NEW.created_unix, + NEW.updated_unix, + NEW.closed_unix, + NEW.is_locked, + NEW.amount, + NEW.is_transformed + ); + end if; RETURN NEW; END; $def$ diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql index 96c47de79..6d28b7651 100644 --- a/models/dbsql/pr_foreigntable_for_es.sql +++ b/models/dbsql/pr_foreigntable_for_es.sql @@ -78,49 +78,53 @@ delete from public.pull_request_es; CREATE OR REPLACE FUNCTION public.insert_pull_request_data() RETURNS trigger AS $def$ + DECLARE + privateValue boolean=false; BEGIN - INSERT INTO public.pull_request_es( - id, - type, - status, - conflicted_files, - commits_ahead, - commits_behind, - issue_id, - index, - head_repo_id, - base_repo_id, - head_branch, - base_branch, - merge_base, - has_merged, - merged_commit_id, - merger_id, - merged_unix, - is_transformed, - amount) - VALUES ( - NEW.id, - NEW.type, - NEW.status, - NEW.conflicted_files, - NEW.commits_ahead, - NEW.commits_behind, - NEW.issue_id, - NEW.index, - NEW.head_repo_id, - NEW.base_repo_id, - NEW.head_branch, - NEW.base_branch, - NEW.merge_base, - NEW.has_merged, - NEW.merged_commit_id, - NEW.merger_id, - NEW.merged_unix, - NEW.is_transformed, - NEW.amount - ); - + select into privateValue is_private from public.repository where id=NEW.repo_id; + if not privateValue then + INSERT INTO public.pull_request_es( + id, + type, + status, + conflicted_files, + commits_ahead, + commits_behind, + issue_id, + index, + head_repo_id, + base_repo_id, + head_branch, + base_branch, + merge_base, + has_merged, + merged_commit_id, + merger_id, + merged_unix, + is_transformed, + amount) + VALUES ( + NEW.id, + NEW.type, + NEW.status, + NEW.conflicted_files, + NEW.commits_ahead, + NEW.commits_behind, + NEW.issue_id, + NEW.index, + NEW.head_repo_id, + NEW.base_repo_id, + NEW.head_branch, + NEW.base_branch, + NEW.merge_base, + NEW.has_merged, + NEW.merged_commit_id, + NEW.merger_id, + NEW.merged_unix, + NEW.is_transformed, + NEW.amount + ); + end if; RETURN NEW; END; $def$ diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index a1e5a877e..094fd4e75 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -1,3 +1,4 @@ +-- 要处理项目从私有变为公有,并且从公有变成私有的情况 DROP FOREIGN table if exists public.repository_es; CREATE FOREIGN TABLE public.repository_es ( id bigint NOT NULL, @@ -55,7 +56,7 @@ OPTIONS ) ; delete from public.repository_es; - INSERT INTO public.repository_es (id, +INSERT INTO public.repository_es (id, owner_id, owner_name, lower_name, @@ -138,13 +139,14 @@ delete from public.repository_es; clone_cnt, num_commit, git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) - FROM public.repository b; + FROM public.repository b where b.is_private=false; CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS $def$ BEGIN - INSERT INTO public.repository_es (id, + if not NEW.is_private then + INSERT INTO public.repository_es (id, owner_id, owner_name, lower_name, @@ -226,6 +228,7 @@ $def$ NEW.clone_cnt, NEW.num_commit, NEW.git_clone_cnt); + end if; RETURN NEW; END; $def$ @@ -242,6 +245,14 @@ CREATE TRIGGER es_insert_repository CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS $def$ BEGIN + if OLD.is_private != NEW.is_private then + if OLD.is_private and not NEW.is_private then + --delete + + end if; + + end if; + update public.repository_es SET description=NEW.description, name=NEW.name, lower_name=NEW.lower_name, diff --git a/routers/search.go b/routers/search.go index fd6de2183..dce250748 100644 --- a/routers/search.go +++ b/routers/search.go @@ -3,8 +3,10 @@ package routers import ( "encoding/json" "fmt" + "strconv" "strings" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -35,9 +37,10 @@ func Search(ctx *context.Context) { if Page <= 0 { Page = 1 } - if PageSize <= 0 { + if PageSize <= 0 || PageSize > 200 { PageSize = setting.UI.IssuePagingNum } + if TableName == "repository" { searchRepo(ctx, "repository-es-index", Key, Page, PageSize) return @@ -80,11 +83,12 @@ func searchRepoByLabel(ctx *context.Context, TableName string, Key string, Page log.Info("query searchRepoByLabel start") if Key != "" { boolQ := elastic.NewBoolQuery() - topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1) + topicsQuery := elastic.NewMatchQuery("topics", Key) boolQ.Should(topicsQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - ctx.JSON(200, res) + result := makeRepoResult(res, "") + ctx.JSON(200, result) return } else { log.Info("query es error," + err.Error()) @@ -115,9 +119,9 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa log.Info("query searchRepo start") if Key != "" { boolQ := elastic.NewBoolQuery() - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") - descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("desc_second") - topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("topics_third") + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("f_second") + topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, descriptionQuery, topicsQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { @@ -146,16 +150,10 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { result := make([]map[string]interface{}, 0) for i, hit := range sRes.Hits.Hits { - log.Info("this is " + fmt.Sprint(i) + " result.") + log.Info("this is repo query " + fmt.Sprint(i) + " result.") recordSource := make(map[string]interface{}) source, err := hit.Source.MarshalJSON() - var isNeedToDealText bool - isNeedToDealText = false - if len(hit.MatchedQueries) > 0 && Key != "" { - if hit.MatchedQueries[0] == "desc_second" || hit.MatchedQueries[0] == "topics_third" { - isNeedToDealText = true - } - } + if err == nil { err = json.Unmarshal(source, &recordSource) if err == nil { @@ -163,35 +161,16 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["name"] = recordSource["name"] record["owner_name"] = recordSource["owner_name"] desc := recordSource["description"].(string) - stringlen := len(desc) - if isNeedToDealText && stringlen > 200 { - index := strings.Index(desc, Key) - if index > 0 { - start := index - 50 - if start < 0 { - start = 0 - } - end := index + 150 - if end >= stringlen { - end = stringlen - } - record["description"] = desc[start:end] - } else { - record["description"] = desc[0:200] - } - } else { - if stringlen > 200 { - record["description"] = desc[0:200] - } else { - record["description"] = desc - } - } + + record["description"] = dealLongText(desc, Key, hit.MatchedQueries) record["num_watches"] = recordSource["num_watches"] record["num_stars"] = recordSource["num_stars"] record["num_forks"] = recordSource["num_forks"] record["topics"] = recordSource["topics"] - record["avatar"] = recordSource["avatar"] + if recordSource["avatar"] != nil { + record["avatar"] = setting.AppSubURL + "/repo-avatars/" + recordSource["avatar"].(string) + } record["updated_unix"] = recordSource["updated_unix"] record["lang"] = recordSource["lang"] result = append(result, record) @@ -211,6 +190,40 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { return returnObj } +func dealLongText(text string, Key string, MatchedQueries []string) string { + var isNeedToDealText bool + isNeedToDealText = false + if len(MatchedQueries) > 0 && Key != "" { + if MatchedQueries[0] == "f_second" || MatchedQueries[0] == "f_third" { + isNeedToDealText = true + } + } + stringlen := len(text) + if isNeedToDealText && stringlen > 200 { + index := strings.Index(text, Key) + if index > 0 { + start := index - 50 + if start < 0 { + start = 0 + } + end := index + 150 + if end >= stringlen { + end = stringlen + } + return text[start:end] + } else { + return text[0:200] + } + } else { + if stringlen > 200 { + return text[0:200] + } else { + return text + } + } + +} + func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page int, PageSize int, IsQueryUser bool) { /* 用户或者组织 ES名称: user-es-index @@ -229,9 +242,9 @@ func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page in ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() if Key != "" { - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") - full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("fullname_second") - descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("desc_third") + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("f_second") + descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, full_nameQuery, descriptionQuery) } typeValue := 1 @@ -243,13 +256,64 @@ func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page in res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - ctx.JSON(200, res) + result := makeUserOrOrgResult(res, Key, ctx) + ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) ctx.JSON(200, "") } } +func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Context) *SearchRes { + total := sRes.Hits.TotalHits.Value + result := make([]map[string]interface{}, 0) + + for i, hit := range sRes.Hits.Hits { + log.Info("this is user query " + fmt.Sprint(i) + " result.") + recordSource := make(map[string]interface{}) + source, err := hit.Source.MarshalJSON() + + if err == nil { + err = json.Unmarshal(source, &recordSource) + if err == nil { + record := make(map[string]interface{}) + record["name"] = recordSource["name"] + record["full_name"] = recordSource["full_name"] + desc := recordSource["description"].(string) + + record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + if ctx.User != nil { + record["email"] = recordSource["email"] + } else { + record["email"] = "" + } + + record["location"] = recordSource["location"] + record["website"] = recordSource["website"] + record["num_repos"] = recordSource["num_repos"] + record["num_teams"] = recordSource["num_teams"] + record["num_members"] = recordSource["num_members"] + + record["avatar"] = strings.TrimRight(setting.AppSubURL, "/") + "/user/avatar/" + recordSource["name"].(string) + "/" + strconv.Itoa(-1) + record["updated_unix"] = recordSource["updated_unix"] + record["created_unix"] = recordSource["created_unix"] + result = append(result, record) + } else { + log.Info("deal source error," + err.Error()) + } + } else { + log.Info("deal source error," + err.Error()) + } + } + + returnObj := &SearchRes{ + Total: total, + Result: result, + } + + return returnObj +} + func searchDataSet(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { /* 数据集,ES名称:dataset-es-index @@ -278,22 +342,70 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P boolQ := elastic.NewBoolQuery() if Key != "" { - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") - contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("content_second") - commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("comment_third") + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") + commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, contentQuery, commentQuery) } isIssueQuery := elastic.NewTermQuery("is_pull", false) boolQ.Must(isIssueQuery) res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - ctx.JSON(200, res) + result := makeIssueResult(res, Key) + ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) } } +func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { + total := sRes.Hits.TotalHits.Value + result := make([]map[string]interface{}, 0) + + for i, hit := range sRes.Hits.Hits { + log.Info("this is issue query " + fmt.Sprint(i) + " result.") + recordSource := make(map[string]interface{}) + source, err := hit.Source.MarshalJSON() + + if err == nil { + err = json.Unmarshal(source, &recordSource) + if err == nil { + record := make(map[string]interface{}) + record["id"] = recordSource["id"] + record["repo_id"] = recordSource["repo_id"] + repo, errRepo := models.GetRepositoryByID(recordSource["repo_id"].(int64)) + if errRepo == nil { + record["repoUrl"] = repo.FullName() + } + record["name"] = recordSource["name"] + desc := recordSource["content"].(string) + record["content"] = dealLongText(desc, Key, hit.MatchedQueries) + if recordSource["pr_id"] != nil { + record["pr_id"] = recordSource["pr_id"] + } + + desc = recordSource["comment"].(string) + record["comment"] = dealLongText(desc, Key, hit.MatchedQueries) + record["num_comments"] = recordSource["num_comments"] + record["updated_unix"] = recordSource["updated_unix"] + result = append(result, record) + } else { + log.Info("deal source error," + err.Error()) + } + } else { + log.Info("deal source error," + err.Error()) + } + } + + returnObj := &SearchRes{ + Total: total, + Result: result, + } + + return returnObj +} + func searchPR(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { /* @@ -308,16 +420,17 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page boolQ := elastic.NewBoolQuery() if Key != "" { - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("name_first") - contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("content_second") - commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("comment_third") + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") + commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, contentQuery, commentQuery) } isIssueQuery := elastic.NewTermQuery("is_pull", true) boolQ.Must(isIssueQuery) res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { - ctx.JSON(200, res) + result := makeIssueResult(res, Key) + ctx.JSON(200, result) } else { log.Info("query es error," + err.Error()) } From 4944980b2d29dbc01396f5520533aa5bdcb1da64 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 31 Dec 2021 10:21:23 +0800 Subject: [PATCH 029/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dataset_foreigntable_for_es.sql | 5 +- models/dbsql/issue_foreigntable_for_es.sql | 10 ++- models/dbsql/pr_foreigntable_for_es.sql | 7 +- models/dbsql/repo_foreigntable_for_es.sql | 7 +- models/dbsql/user_foreigntable_for_es.sql | 8 +- routers/search.go | 105 ++++++++++++++++++++++++--- 6 files changed, 123 insertions(+), 19 deletions(-) diff --git a/models/dbsql/dataset_foreigntable_for_es.sql b/models/dbsql/dataset_foreigntable_for_es.sql index 1ba386b3e..f9fb40c88 100644 --- a/models/dbsql/dataset_foreigntable_for_es.sql +++ b/models/dbsql/dataset_foreigntable_for_es.sql @@ -102,6 +102,7 @@ CREATE TRIGGER es_insert_dataset AFTER INSERT ON public.dataset FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data(); +ALTER TABLE public.dataset ENABLE ALWAYS TRIGGER es_insert_dataset; CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name_delete() RETURNS trigger AS $def$ @@ -117,6 +118,7 @@ CREATE TRIGGER es_udpate_dataset_file_name_delete AFTER DELETE ON public.attachment FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name_delete(); +ALTER TABLE public.attachment ENABLE ALWAYS TRIGGER es_udpate_dataset_file_name_delete; CREATE OR REPLACE FUNCTION public.update_dataset() RETURNS trigger AS $def$ @@ -139,6 +141,7 @@ CREATE TRIGGER es_update_dataset AFTER UPDATE ON public.dataset FOR EACH ROW EXECUTE PROCEDURE update_dataset(); +ALTER TABLE public.dataset ENABLE ALWAYS TRIGGER es_update_dataset; CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS $def$ @@ -155,4 +158,4 @@ CREATE TRIGGER es_delete_dataset AFTER DELETE ON public.dataset FOR EACH ROW EXECUTE PROCEDURE delete_dataset(); - +ALTER TABLE public.dataset ENABLE ALWAYS TRIGGER es_delete_dataset; diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index 012cf9701..1162c55ac 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -148,7 +148,7 @@ CREATE TRIGGER es_insert_issue AFTER INSERT ON public.issue FOR EACH ROW EXECUTE PROCEDURE insert_issue_data(); - +ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_insert_issue; CREATE OR REPLACE FUNCTION public.udpate_issue_comment() RETURNS trigger AS $def$ @@ -169,6 +169,8 @@ CREATE TRIGGER es_udpate_issue_comment AFTER DELETE OR UPDATE ON public.comment FOR EACH ROW EXECUTE PROCEDURE udpate_issue_comment(); +ALTER TABLE public.comment ENABLE ALWAYS TRIGGER es_udpate_issue_comment; + CREATE OR REPLACE FUNCTION public.update_issue() RETURNS trigger AS $def$ @@ -192,7 +194,7 @@ CREATE TRIGGER es_update_issue AFTER UPDATE ON public.issue FOR EACH ROW EXECUTE PROCEDURE update_issue(); - +ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_update_issue; CREATE OR REPLACE FUNCTION public.delete_issue() RETURNS trigger AS $def$ @@ -207,4 +209,6 @@ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS es_delete_issue on public.issue; CREATE TRIGGER es_delete_issue AFTER DELETE ON public.issue - FOR EACH ROW EXECUTE PROCEDURE delete_issue(); \ No newline at end of file + FOR EACH ROW EXECUTE PROCEDURE delete_issue(); + +ALTER TABLE public.issue ENABLE ALWAYS TRIGGER es_delete_issue; \ No newline at end of file diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql index 6d28b7651..089f62060 100644 --- a/models/dbsql/pr_foreigntable_for_es.sql +++ b/models/dbsql/pr_foreigntable_for_es.sql @@ -136,7 +136,7 @@ CREATE TRIGGER es_insert_pull_request AFTER INSERT ON public.pull_request FOR EACH ROW EXECUTE PROCEDURE insert_pull_request_data(); - +ALTER TABLE public.pull_request ENABLE ALWAYS TRIGGER es_insert_pull_request; CREATE OR REPLACE FUNCTION public.update_pull_request() RETURNS trigger AS $def$ @@ -156,6 +156,7 @@ CREATE TRIGGER es_update_pull_request AFTER UPDATE ON public.pull_request FOR EACH ROW EXECUTE PROCEDURE update_pull_request(); +ALTER TABLE public.pull_request ENABLE ALWAYS TRIGGER es_update_pull_request; CREATE OR REPLACE FUNCTION public.delete_pull_request() RETURNS trigger AS $def$ @@ -170,4 +171,6 @@ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS es_delete_pull_request on public.pull_request; CREATE TRIGGER es_delete_pull_request AFTER DELETE ON public.pull_request - FOR EACH ROW EXECUTE PROCEDURE delete_pull_request(); \ No newline at end of file + FOR EACH ROW EXECUTE PROCEDURE delete_pull_request(); + +ALTER TABLE public.pull_request ENABLE ALWAYS TRIGGER es_delete_pull_request; \ No newline at end of file diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 094fd4e75..ae6fe51b3 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -241,6 +241,7 @@ CREATE TRIGGER es_insert_repository AFTER INSERT ON public.repository FOR EACH ROW EXECUTE PROCEDURE insert_repository_data(); +ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_insert_repository; CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS $def$ @@ -274,6 +275,7 @@ CREATE TRIGGER es_update_repository AFTER UPDATE ON public.repository FOR EACH ROW EXECUTE PROCEDURE update_repository(); +ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_update_repository; CREATE OR REPLACE FUNCTION public.delete_repository() RETURNS trigger AS $def$ @@ -290,6 +292,7 @@ CREATE TRIGGER es_delete_repository AFTER DELETE ON public.repository FOR EACH ROW EXECUTE PROCEDURE delete_repository(); +ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_delete_repository; CREATE OR REPLACE FUNCTION public.udpate_repository_lang() RETURNS trigger AS $def$ @@ -310,4 +313,6 @@ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS es_udpate_repository_lang on public.language_stat; CREATE TRIGGER es_udpate_repository_lang AFTER INSERT OR UPDATE OR DELETE ON public.language_stat - FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang(); \ No newline at end of file + FOR EACH ROW EXECUTE PROCEDURE udpate_repository_lang(); + +ALTER TABLE public.language_stat ENABLE ALWAYS TRIGGER es_udpate_repository_lang; \ No newline at end of file diff --git a/models/dbsql/user_foreigntable_for_es.sql b/models/dbsql/user_foreigntable_for_es.sql index cc9e56db6..a1a42ee12 100644 --- a/models/dbsql/user_foreigntable_for_es.sql +++ b/models/dbsql/user_foreigntable_for_es.sql @@ -251,6 +251,8 @@ CREATE TRIGGER es_insert_user AFTER INSERT ON public.user FOR EACH ROW EXECUTE PROCEDURE insert_user_data(); +ALTER TABLE public.user ENABLE ALWAYS TRIGGER es_insert_user; + CREATE OR REPLACE FUNCTION public.update_user() RETURNS trigger AS $def$ @@ -274,7 +276,7 @@ CREATE TRIGGER es_update_user AFTER UPDATE ON public.user FOR EACH ROW EXECUTE PROCEDURE update_user(); - +ALTER TABLE public.user ENABLE ALWAYS TRIGGER es_update_user; CREATE OR REPLACE FUNCTION public.delete_user() RETURNS trigger AS $def$ @@ -289,4 +291,6 @@ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS es_delete_user on public.user; CREATE TRIGGER es_delete_user AFTER DELETE ON public.user - FOR EACH ROW EXECUTE PROCEDURE delete_user(); \ No newline at end of file + FOR EACH ROW EXECUTE PROCEDURE delete_user(); + +ALTER TABLE public.user ENABLE ALWAYS TRIGGER es_delete_user; \ No newline at end of file diff --git a/routers/search.go b/routers/search.go index dce250748..2dbc0f2b2 100644 --- a/routers/search.go +++ b/routers/search.go @@ -175,10 +175,10 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["lang"] = recordSource["lang"] result = append(result, record) } else { - log.Info("deal source error," + err.Error()) + log.Info("deal repo source error," + err.Error()) } } else { - log.Info("deal source error," + err.Error()) + log.Info("deal repo source error," + err.Error()) } } @@ -299,10 +299,10 @@ func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Co record["created_unix"] = recordSource["created_unix"] result = append(result, record) } else { - log.Info("deal source error," + err.Error()) + log.Info("deal user source error," + err.Error()) } } else { - log.Info("deal source error," + err.Error()) + log.Info("deal user source error," + err.Error()) } } @@ -326,6 +326,82 @@ func searchDataSet(ctx *context.Context, TableName string, Key string, Page int, download_times */ + SortBy := ctx.Query("SortBy") + if SortBy == "" { + SortBy = "download_times.keyword" + } + ascending := ctx.QueryBool("Ascending") + log.Info("query searchRepo start") + boolQ := elastic.NewBoolQuery() + if Key != "" { + nameQuery := elastic.NewMatchQuery("title", Key).Boost(2).QueryName("f_first") + descQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("f_second") + fileNameQuery := elastic.NewMatchQuery("file_name", Key).Boost(1).QueryName("f_third") + categoryQuery := elastic.NewMatchQuery("category", Key).Boost(1).QueryName("f_fourth") + boolQ.Should(nameQuery, descQuery, categoryQuery, fileNameQuery) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + if err == nil { + result := makeDatasetResult(res, Key) + ctx.JSON(200, result) + } else { + log.Info("query es error," + err.Error()) + } + } else { + log.Info("query all content.") + //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} + res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + if err == nil { + result := makeRepoResult(res, "") + ctx.JSON(200, result) + } else { + log.Info("query es error," + err.Error()) + ctx.JSON(200, "") + } + } + +} + +func makeDatasetResult(sRes *elastic.SearchResult, Key string) *SearchRes { + total := sRes.Hits.TotalHits.Value + result := make([]map[string]interface{}, 0) + + for i, hit := range sRes.Hits.Hits { + log.Info("this is dataset query " + fmt.Sprint(i) + " result.") + recordSource := make(map[string]interface{}) + source, err := hit.Source.MarshalJSON() + + if err == nil { + err = json.Unmarshal(source, &recordSource) + if err == nil { + record := make(map[string]interface{}) + record["id"] = recordSource["id"] + userId := recordSource["user_id"].(int64) + user, errUser := models.GetUserByID(userId) + if errUser == nil { + record["owerName"] = user.GetDisplayName() + record["avatar"] = user.RelAvatarLink() + } + record["title"] = recordSource["title"] + record["category"] = recordSource["category"] + desc := recordSource["description"].(string) + record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + record["download_times"] = recordSource["download_times"] + record["created_unix"] = recordSource["created_unix"] + result = append(result, record) + } else { + log.Info("deal dataset source error," + err.Error()) + } + } else { + log.Info("deal dataset source error," + err.Error()) + } + } + + returnObj := &SearchRes{ + Total: total, + Result: result, + } + + return returnObj } func searchIssue(ctx *context.Context, TableName string, Key string, Page int, PageSize int) { @@ -339,7 +415,11 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P 排序: updated_unix */ - + SortBy := ctx.Query("SortBy") + if SortBy == "" { + SortBy = "updated_unix.keyword" + } + ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() if Key != "" { nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") @@ -349,7 +429,7 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P } isIssueQuery := elastic.NewTermQuery("is_pull", false) boolQ.Must(isIssueQuery) - res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { result := makeIssueResult(res, Key) ctx.JSON(200, result) @@ -377,6 +457,7 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { repo, errRepo := models.GetRepositoryByID(recordSource["repo_id"].(int64)) if errRepo == nil { record["repoUrl"] = repo.FullName() + record["avatar"] = repo.RelAvatarLink() } record["name"] = recordSource["name"] desc := recordSource["content"].(string) @@ -391,10 +472,10 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["updated_unix"] = recordSource["updated_unix"] result = append(result, record) } else { - log.Info("deal source error," + err.Error()) + log.Info("deal issue source error," + err.Error()) } } else { - log.Info("deal source error," + err.Error()) + log.Info("deal issue source error," + err.Error()) } } @@ -417,7 +498,11 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page 排序: updated_unix */ - + SortBy := ctx.Query("SortBy") + if SortBy == "" { + SortBy = "updated_unix.keyword" + } + ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() if Key != "" { nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") @@ -427,7 +512,7 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page } isIssueQuery := elastic.NewTermQuery("is_pull", true) boolQ.Must(isIssueQuery) - res, err := client.Search(TableName).Query(boolQ).Sort("updated_unix.keyword", false).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { result := makeIssueResult(res, Key) ctx.JSON(200, result) From 8ab81f00f4b5efd1aadc2ddfa9022ed5b7681fbc Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 6 Jan 2022 15:03:08 +0800 Subject: [PATCH 030/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/dataset_foreigntable_for_es.sql | 28 +- models/dbsql/issue_foreigntable_for_es.sql | 46 ++-- models/dbsql/pr_foreigntable_for_es.sql | 45 ++-- models/dbsql/repo_foreigntable_for_es.sql | 388 +++++++++++++++++++++------ models/dbsql/user_foreigntable_for_es.sql | 2 +- routers/repo/ai_model_manage.go | 4 + 6 files changed, 366 insertions(+), 147 deletions(-) diff --git a/models/dbsql/dataset_foreigntable_for_es.sql b/models/dbsql/dataset_foreigntable_for_es.sql index f9fb40c88..fa46a5777 100644 --- a/models/dbsql/dataset_foreigntable_for_es.sql +++ b/models/dbsql/dataset_foreigntable_for_es.sql @@ -40,20 +40,20 @@ DELETE FROM public.dataset_es; created_unix, updated_unix,file_name) SELECT - id, - title, - status, - category, - description, - download_times, - license, - task, - release_id, - user_id, - repo_id, - created_unix, - updated_unix,(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment a where a.dataset_id=b.id) - FROM public.dataset b; + b.id, + b.title, + b.status, + b.category, + b.description, + b.download_times, + b.license, + b.task, + b.release_id, + b.user_id, + b.repo_id, + b.created_unix, + b.updated_unix,(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment a where a.dataset_id=b.id) + FROM public.dataset b,public.repository c where b.repo_id=c.id and c.is_private=false; CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS $def$ diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index 1162c55ac..786e7c989 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -35,7 +35,7 @@ OPTIONS ) ; -DELETE FROM public.issue_es; +delete from public.issue_es; INSERT INTO public.issue_es( id, repo_id, @@ -59,29 +59,29 @@ INSERT INTO public.issue_es( amount, is_transformed,comment) SELECT - id, - repo_id, - index, - poster_id, - original_author, - original_author_id, - name, - content, - milestone_id, - priority, - is_closed, - is_pull, - num_comments, - ref, - deadline_unix, - created_unix, - updated_unix, - closed_unix, - is_locked, - amount, - is_transformed, + b.id, + b.repo_id, + b.index, + b.poster_id, + b.original_author, + b.original_author_id, + b.name, + b.content, + b.milestone_id, + b.priority, + b.is_closed, + b.is_pull, + b.num_comments, + b.ref, + b.deadline_unix, + b.created_unix, + b.updated_unix, + b.closed_unix, + b.is_locked, + b.amount, + b.is_transformed, (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id) - FROM public.issue b; + FROM public.issue b,public.repository c where b.repo_id=c.id and c.is_private=false; CREATE OR REPLACE FUNCTION public.insert_issue_data() RETURNS trigger AS diff --git a/models/dbsql/pr_foreigntable_for_es.sql b/models/dbsql/pr_foreigntable_for_es.sql index 089f62060..14cdb2854 100644 --- a/models/dbsql/pr_foreigntable_for_es.sql +++ b/models/dbsql/pr_foreigntable_for_es.sql @@ -53,27 +53,26 @@ delete from public.pull_request_es; is_transformed, amount) SELECT - id, - type, - status, - conflicted_files, - commits_ahead, - commits_behind, - issue_id, - index, - head_repo_id, - base_repo_id, - head_branch, - base_branch, - merge_base, - has_merged, - merged_commit_id, - merger_id, - merged_unix, - is_transformed, - amount - FROM public.pull_request; - + b.id, + b.type, + b.status, + b.conflicted_files, + b.commits_ahead, + b.commits_behind, + b.issue_id, + b.index, + b.head_repo_id, + b.base_repo_id, + b.head_branch, + b.base_branch, + b.merge_base, + b.has_merged, + b.merged_commit_id, + b.merger_id, + b.merged_unix, + b.is_transformed, + b.amount + FROM public.pull_request b,public.repository c where b.base_repo_id=c.id and c.is_private=false; CREATE OR REPLACE FUNCTION public.insert_pull_request_data() RETURNS trigger AS @@ -81,7 +80,7 @@ $def$ DECLARE privateValue boolean=false; BEGIN - select into privateValue is_private from public.repository where id=NEW.repo_id; + select into privateValue is_private from public.repository where id=NEW.base_repo_id; if not privateValue then INSERT INTO public.pull_request_es( id, @@ -124,6 +123,8 @@ $def$ NEW.is_transformed, NEW.amount ); + + UPDATE public.issue_es SET pr_id=NEW.id where id=NEW.issue_id; end if; RETURN NEW; END; diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index ae6fe51b3..805c46746 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -56,7 +56,97 @@ OPTIONS ) ; delete from public.repository_es; -INSERT INTO public.repository_es (id, + INSERT INTO public.repository_es (id, + owner_id, + owner_name, + lower_name, + name, + description, + website, + original_service_type, + original_url, + default_branch, + num_watches, + num_stars, + num_forks, + num_issues, + num_closed_issues, + num_pulls, + num_closed_pulls, + num_milestones, + num_closed_milestones, + is_private, + is_empty, + is_archived, + is_mirror, + status, + is_fork, + fork_id, + is_template, + template_id, + size, + is_fsck_enabled, + close_issues_via_commit_in_any_branch, + topics, + avatar, + created_unix, + updated_unix, + contract_address, + block_chain_status, + balance, + clone_cnt, + num_commit, + git_clone_cnt,lang) + SELECT + id, + owner_id, + owner_name, + lower_name, + name, + description, + website, + original_service_type, + original_url, + default_branch, + num_watches, + num_stars, + num_forks, + num_issues, + num_closed_issues, + num_pulls, + num_closed_pulls, + num_milestones, + num_closed_milestones, + is_private, + is_empty, + is_archived, + is_mirror, + status, + is_fork, + fork_id, + is_template, + template_id, + size, + is_fsck_enabled, + close_issues_via_commit_in_any_branch, + topics, + avatar, + created_unix, + updated_unix, + contract_address, + block_chain_status, + balance, + clone_cnt, + num_commit, + git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) + FROM public.repository b where b.is_private=false; + + +CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS +$def$ + BEGIN + if not NEW.is_private then + INSERT INTO public.repository_es (id, owner_id, owner_name, lower_name, @@ -96,9 +186,70 @@ INSERT INTO public.repository_es (id, balance, clone_cnt, num_commit, - git_clone_cnt,lang) - SELECT - id, + git_clone_cnt) VALUES + (NEW.id, + NEW.owner_id, + NEW.owner_name, + NEW.lower_name, + NEW.name, + NEW.description, + NEW.website, + NEW.original_service_type, + NEW.original_url, + NEW.default_branch, + NEW.num_watches, + NEW.num_stars, + NEW.num_forks, + NEW.num_issues, + NEW.num_closed_issues, + NEW.num_pulls, + NEW.num_closed_pulls, + NEW.num_milestones, + NEW.num_closed_milestones, + NEW.is_private, + NEW.is_empty, + NEW.is_archived, + NEW.is_mirror, + NEW.status, + NEW.is_fork, + NEW.fork_id, + NEW.is_template, + NEW.template_id, + NEW.size, + NEW.is_fsck_enabled, + NEW.close_issues_via_commit_in_any_branch, + NEW.topics, + NEW.avatar, + NEW.created_unix, + NEW.updated_unix, + NEW.contract_address, + NEW.block_chain_status, + NEW.balance, + NEW.clone_cnt, + NEW.num_commit, + NEW.git_clone_cnt); + end if; + RETURN NEW; + END; +$def$ +LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS es_insert_repository on public.repository; + + +CREATE TRIGGER es_insert_repository + AFTER INSERT ON public.repository + FOR EACH ROW EXECUTE PROCEDURE insert_repository_data(); + +ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_insert_repository; + +CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS +$def$ + BEGIN + if OLD.is_private != NEW.is_private then + if OLD.is_private and not NEW.is_private then + --insert + INSERT INTO public.repository_es (id, owner_id, owner_name, lower_name, @@ -138,15 +289,9 @@ INSERT INTO public.repository_es (id, balance, clone_cnt, num_commit, - git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) - FROM public.repository b where b.is_private=false; - - -CREATE OR REPLACE FUNCTION public.insert_repository_data() RETURNS trigger AS -$def$ - BEGIN - if not NEW.is_private then - INSERT INTO public.repository_es (id, + git_clone_cnt,lang) + SELECT + id, owner_id, owner_name, lower_name, @@ -186,86 +331,152 @@ $def$ balance, clone_cnt, num_commit, - git_clone_cnt) VALUES - (NEW.id, - NEW.owner_id, - NEW.owner_name, - NEW.lower_name, - NEW.name, - NEW.description, - NEW.website, - NEW.original_service_type, - NEW.original_url, - NEW.default_branch, - NEW.num_watches, - NEW.num_stars, - NEW.num_forks, - NEW.num_issues, - NEW.num_closed_issues, - NEW.num_pulls, - NEW.num_closed_pulls, - NEW.num_milestones, - NEW.num_closed_milestones, - NEW.is_private, - NEW.is_empty, - NEW.is_archived, - NEW.is_mirror, - NEW.status, - NEW.is_fork, - NEW.fork_id, - NEW.is_template, - NEW.template_id, - NEW.size, - NEW.is_fsck_enabled, - NEW.close_issues_via_commit_in_any_branch, - NEW.topics, - NEW.avatar, - NEW.created_unix, - NEW.updated_unix, - NEW.contract_address, - NEW.block_chain_status, - NEW.balance, - NEW.clone_cnt, - NEW.num_commit, - NEW.git_clone_cnt); - end if; - RETURN NEW; - END; -$def$ -LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS es_insert_repository on public.repository; - - -CREATE TRIGGER es_insert_repository - AFTER INSERT ON public.repository - FOR EACH ROW EXECUTE PROCEDURE insert_repository_data(); - -ALTER TABLE public.repository ENABLE ALWAYS TRIGGER es_insert_repository; + git_clone_cnt,(select string_agg(language, ',') from public.language_stat a where a.repo_id=b.id) + FROM public.repository b where b.id=NEW.id; + INSERT INTO public.dataset_es( + id, + title, + status, + category, + description, + download_times, + license, task, + release_id, + user_id, + repo_id, + created_unix, + updated_unix,file_name) + SELECT + b.id, + b.title, + b.status, + b.category, + b.description, + b.download_times, + b.license, + b.task, + b.release_id, + b.user_id, + b.repo_id, + b.created_unix, + b.updated_unix,(select array_to_string(array_agg(name order by created_unix desc),',') from public.attachment a where a.dataset_id=b.id) + FROM public.dataset b where b.repo_id=NEW.id; -CREATE OR REPLACE FUNCTION public.update_repository() RETURNS trigger AS -$def$ - BEGIN - if OLD.is_private != NEW.is_private then - if OLD.is_private and not NEW.is_private then - --delete + INSERT INTO public.issue_es( + id, + repo_id, + index, + poster_id, + original_author, + original_author_id, + name, + content, + milestone_id, + priority, + is_closed, + is_pull, + num_comments, + ref, + deadline_unix, + created_unix, + updated_unix, + closed_unix, + is_locked, + amount, + is_transformed,comment,pr_id) + SELECT + b.id, + b.repo_id, + b.index, + b.poster_id, + b.original_author, + b.original_author_id, + b.name, + b.content, + b.milestone_id, + b.priority, + b.is_closed, + b.is_pull, + b.num_comments, + b.ref, + b.deadline_unix, + b.created_unix, + b.updated_unix, + b.closed_unix, + b.is_locked, + b.amount, + b.is_transformed, + (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id), + (select id from public.pull_request d where d.issue_id=b.id) + FROM public.issue b where b.repo_id=NEW.id; + + INSERT INTO public.pull_request_es( + id, + type, + status, + conflicted_files, + commits_ahead, + commits_behind, + issue_id, + index, + head_repo_id, + base_repo_id, + head_branch, + base_branch, + merge_base, + has_merged, + merged_commit_id, + merger_id, + merged_unix, + is_transformed, + amount) + SELECT + b.id, + b.type, + b.status, + b.conflicted_files, + b.commits_ahead, + b.commits_behind, + b.issue_id, + b.index, + b.head_repo_id, + b.base_repo_id, + b.head_branch, + b.base_branch, + b.merge_base, + b.has_merged, + b.merged_commit_id, + b.merger_id, + b.merged_unix, + b.is_transformed, + b.amount + FROM public.pull_request b where b.base_repo_id=NEW.id; end if; + if not OLD.is_private and NEW.is_private then + delete from public.issue_es where repo_id=NEW.id; + delete from public.dataset_es where repo_id=NEW.id; + delete from public.pull_request_es where base_repo_id=NEW.id; + delete from public.repository_es where id=NEW.id; + end if; + end if; - update public.repository_es SET description=NEW.description, - name=NEW.name, - lower_name=NEW.lower_name, - owner_name=NEW.owner_name, - website=NEW.website, - updated_unix=NEW.updated_unix, - num_watches=NEW.num_watches, - num_stars=NEW.num_stars, - num_forks=NEW.num_forks, - topics=NEW.topics - where id=NEW.id; - return new; + if not NEW.is_private then + update public.repository_es SET description=NEW.description, + name=NEW.name, + lower_name=NEW.lower_name, + owner_name=NEW.owner_name, + website=NEW.website, + updated_unix=NEW.updated_unix, + num_watches=NEW.num_watches, + num_stars=NEW.num_stars, + num_forks=NEW.num_forks, + topics=NEW.topics + where id=NEW.id; + return new; + end if; END $def$ LANGUAGE plpgsql; @@ -281,6 +492,9 @@ CREATE OR REPLACE FUNCTION public.delete_repository() RETURNS trigger AS $def$ declare BEGIN + delete from public.issue_es where repo_id=OLD.id; + delete from public.dataset_es where repo_id=OLD.id; + delete from public.pull_request_es where base_repo_id=OLD.id; DELETE FROM public.repository_es where id=OLD.id; return new; END diff --git a/models/dbsql/user_foreigntable_for_es.sql b/models/dbsql/user_foreigntable_for_es.sql index a1a42ee12..ded554c0f 100644 --- a/models/dbsql/user_foreigntable_for_es.sql +++ b/models/dbsql/user_foreigntable_for_es.sql @@ -60,7 +60,7 @@ OPTIONS default_sort '_id' ) ; -delete from public.user_es; +delete from public.user_es; INSERT INTO public.user_es( id, lower_name, diff --git a/routers/repo/ai_model_manage.go b/routers/repo/ai_model_manage.go index 86d8c140a..df9ec0a8a 100644 --- a/routers/repo/ai_model_manage.go +++ b/routers/repo/ai_model_manage.go @@ -626,6 +626,7 @@ func QueryModelListForPredict(ctx *context.Context) { func QueryModelFileForPredict(ctx *context.Context) { id := ctx.Query("ID") + parentDir := ctx.Query("parentDir") model, err := models.QueryModelById(id) if err != nil { log.Error("no such model!", err.Error()) @@ -633,6 +634,9 @@ func QueryModelFileForPredict(ctx *context.Context) { return } prefix := model.Path[len(setting.Bucket)+1:] + if parentDir != "" { + prefix = prefix + parentDir + } fileinfos, err := storage.GetAllObjectByBucketAndPrefix(setting.Bucket, prefix) ctx.JSON(http.StatusOK, fileinfos) } From 9c9a968a16fe0a7b566c712f3bc6d9ecd6d5dd9e Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 6 Jan 2022 15:26:51 +0800 Subject: [PATCH 031/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/search.go b/routers/search.go index 2dbc0f2b2..64db86c30 100644 --- a/routers/search.go +++ b/routers/search.go @@ -119,11 +119,11 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa log.Info("query searchRepo start") if Key != "" { boolQ := elastic.NewBoolQuery() - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + nameQuery := elastic.NewMatchQuery("name", Key).Boost(1024).QueryName("f_first") descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("f_second") topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, descriptionQuery, topicsQuery) - res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).SortBy(elastic.NewScoreSort(), elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { result := makeRepoResult(res, Key) ctx.JSON(200, result) @@ -134,7 +134,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } else { log.Info("query all content.") //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} - res, err := client.Search(TableName).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { result := makeRepoResult(res, "") ctx.JSON(200, result) @@ -505,7 +505,7 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() if Key != "" { - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + nameQuery := elastic.NewMatchQuery("name", Key).Boost(1024).QueryName("f_first") contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, contentQuery, commentQuery) From df8968d882d878f88c423b4d67b68b42c78c8f84 Mon Sep 17 00:00:00 2001 From: zouap Date: Thu, 6 Jan 2022 16:28:33 +0800 Subject: [PATCH 032/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/repo_foreigntable_for_es.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/dbsql/repo_foreigntable_for_es.sql b/models/dbsql/repo_foreigntable_for_es.sql index 805c46746..713aa2112 100644 --- a/models/dbsql/repo_foreigntable_for_es.sql +++ b/models/dbsql/repo_foreigntable_for_es.sql @@ -475,8 +475,9 @@ $def$ num_forks=NEW.num_forks, topics=NEW.topics where id=NEW.id; - return new; + end if; + return new; END $def$ LANGUAGE plpgsql; From 0dec5f01a6b6b3e38544f9500d94c19fb45e8d6b Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 09:53:29 +0800 Subject: [PATCH 033/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/search.go b/routers/search.go index 64db86c30..f43836cc9 100644 --- a/routers/search.go +++ b/routers/search.go @@ -427,7 +427,7 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, contentQuery, commentQuery) } - isIssueQuery := elastic.NewTermQuery("is_pull", false) + isIssueQuery := elastic.NewTermQuery("is_pull.keyword", false) boolQ.Must(isIssueQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { From 35fffdb39369391291d1678845fdddd25e166fcc Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 09:59:09 +0800 Subject: [PATCH 034/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/search.go b/routers/search.go index f43836cc9..658cd2963 100644 --- a/routers/search.go +++ b/routers/search.go @@ -427,7 +427,7 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, contentQuery, commentQuery) } - isIssueQuery := elastic.NewTermQuery("is_pull.keyword", false) + isIssueQuery := elastic.NewTermQuery("is_pull", "f") boolQ.Must(isIssueQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { @@ -510,7 +510,7 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, contentQuery, commentQuery) } - isIssueQuery := elastic.NewTermQuery("is_pull", true) + isIssueQuery := elastic.NewTermQuery("is_pull", "t") boolQ.Must(isIssueQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { From 8714538c3a256c7d8f34a8b8fe4d5cf8bf31ec47 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 10:02:27 +0800 Subject: [PATCH 035/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/search.go b/routers/search.go index 658cd2963..96b4b368a 100644 --- a/routers/search.go +++ b/routers/search.go @@ -454,6 +454,7 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { record := make(map[string]interface{}) record["id"] = recordSource["id"] record["repo_id"] = recordSource["repo_id"] + log.Info("recordSource[\"repo_id\"]=" + fmt.Sprint(recordSource["repo_id"])) repo, errRepo := models.GetRepositoryByID(recordSource["repo_id"].(int64)) if errRepo == nil { record["repoUrl"] = repo.FullName() From 0eb327a82d27a69e7949d1862561f6c23b9bb69a Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 10:34:21 +0800 Subject: [PATCH 036/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/routers/search.go b/routers/search.go index 96b4b368a..1a774eec6 100644 --- a/routers/search.go +++ b/routers/search.go @@ -455,10 +455,14 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["id"] = recordSource["id"] record["repo_id"] = recordSource["repo_id"] log.Info("recordSource[\"repo_id\"]=" + fmt.Sprint(recordSource["repo_id"])) - repo, errRepo := models.GetRepositoryByID(recordSource["repo_id"].(int64)) - if errRepo == nil { - record["repoUrl"] = repo.FullName() - record["avatar"] = repo.RelAvatarLink() + repoIdstr := recordSource["repo_id"].(string) + repoId, cerr := strconv.ParseInt(repoIdstr, 10, 64) + if cerr == nil { + repo, errRepo := models.GetRepositoryByID(repoId) + if errRepo == nil { + record["repoUrl"] = repo.FullName() + record["avatar"] = repo.RelAvatarLink() + } } record["name"] = recordSource["name"] desc := recordSource["content"].(string) From 82dedae8e4a5edfa6f95de09fd046ea80d390dbe Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 10:43:17 +0800 Subject: [PATCH 037/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/routers/search.go b/routers/search.go index 1a774eec6..f2e250345 100644 --- a/routers/search.go +++ b/routers/search.go @@ -160,9 +160,12 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { record := make(map[string]interface{}) record["name"] = recordSource["name"] record["owner_name"] = recordSource["owner_name"] - desc := recordSource["description"].(string) - - record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + if recordSource["description"] != nil { + desc := recordSource["description"].(string) + record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + } else { + record["description"] = "" + } record["num_watches"] = recordSource["num_watches"] record["num_stars"] = recordSource["num_stars"] @@ -279,9 +282,12 @@ func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Co record := make(map[string]interface{}) record["name"] = recordSource["name"] record["full_name"] = recordSource["full_name"] - desc := recordSource["description"].(string) - - record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + if recordSource["description"] != nil { + desc := recordSource["description"].(string) + record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + } else { + record["description"] = "" + } if ctx.User != nil { record["email"] = recordSource["email"] } else { @@ -383,8 +389,12 @@ func makeDatasetResult(sRes *elastic.SearchResult, Key string) *SearchRes { } record["title"] = recordSource["title"] record["category"] = recordSource["category"] - desc := recordSource["description"].(string) - record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + if recordSource["description"] != nil { + desc := recordSource["description"].(string) + record["description"] = dealLongText(desc, Key, hit.MatchedQueries) + } else { + record["description"] = "" + } record["download_times"] = recordSource["download_times"] record["created_unix"] = recordSource["created_unix"] result = append(result, record) @@ -465,14 +475,21 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { } } record["name"] = recordSource["name"] - desc := recordSource["content"].(string) - record["content"] = dealLongText(desc, Key, hit.MatchedQueries) + if recordSource["content"] != nil { + desc := recordSource["content"].(string) + record["content"] = dealLongText(desc, Key, hit.MatchedQueries) + } else { + record["content"] = "" + } if recordSource["pr_id"] != nil { record["pr_id"] = recordSource["pr_id"] } - - desc = recordSource["comment"].(string) - record["comment"] = dealLongText(desc, Key, hit.MatchedQueries) + if recordSource["comment"] != nil { + desc := recordSource["comment"].(string) + record["comment"] = dealLongText(desc, Key, hit.MatchedQueries) + } else { + record["comment"] = "" + } record["num_comments"] = recordSource["num_comments"] record["updated_unix"] = recordSource["updated_unix"] result = append(result, record) From 141802df641b8bf326e56f79d688bdedd9d745f1 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 10:55:06 +0800 Subject: [PATCH 038/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/routers/search.go b/routers/search.go index f2e250345..2edb510c6 100644 --- a/routers/search.go +++ b/routers/search.go @@ -158,6 +158,7 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) + record["id"] = recordSource["_id"] record["name"] = recordSource["name"] record["owner_name"] = recordSource["owner_name"] if recordSource["description"] != nil { @@ -280,6 +281,7 @@ func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Co err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) + record["id"] = recordSource["_id"] record["name"] = recordSource["name"] record["full_name"] = recordSource["full_name"] if recordSource["description"] != nil { @@ -380,7 +382,7 @@ func makeDatasetResult(sRes *elastic.SearchResult, Key string) *SearchRes { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) - record["id"] = recordSource["id"] + record["id"] = recordSource["_id"] userId := recordSource["user_id"].(int64) user, errUser := models.GetUserByID(userId) if errUser == nil { @@ -462,7 +464,7 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) - record["id"] = recordSource["id"] + record["id"] = recordSource["_id"] record["repo_id"] = recordSource["repo_id"] log.Info("recordSource[\"repo_id\"]=" + fmt.Sprint(recordSource["repo_id"])) repoIdstr := recordSource["repo_id"].(string) From 742b46bb27741c724a66d2284a2bffa8cd6d0cb6 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 11:00:29 +0800 Subject: [PATCH 039/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/search.go b/routers/search.go index 2edb510c6..30c35d4cb 100644 --- a/routers/search.go +++ b/routers/search.go @@ -443,6 +443,8 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P boolQ.Must(isIssueQuery) res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { + searchJson, _ := json.Marshal(res) + log.Info("searchJson=" + string(searchJson)) result := makeIssueResult(res, Key) ctx.JSON(200, result) } else { From 414d0ab401ad53fd521896fc09fd696e953c53ce Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 11:08:04 +0800 Subject: [PATCH 040/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/routers/search.go b/routers/search.go index 30c35d4cb..a0f66e649 100644 --- a/routers/search.go +++ b/routers/search.go @@ -158,7 +158,7 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) - record["id"] = recordSource["_id"] + record["id"] = hit.Id record["name"] = recordSource["name"] record["owner_name"] = recordSource["owner_name"] if recordSource["description"] != nil { @@ -281,7 +281,7 @@ func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Co err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) - record["id"] = recordSource["_id"] + record["id"] = hit.Id record["name"] = recordSource["name"] record["full_name"] = recordSource["full_name"] if recordSource["description"] != nil { @@ -382,7 +382,7 @@ func makeDatasetResult(sRes *elastic.SearchResult, Key string) *SearchRes { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) - record["id"] = recordSource["_id"] + record["id"] = hit.Id userId := recordSource["user_id"].(int64) user, errUser := models.GetUserByID(userId) if errUser == nil { @@ -434,10 +434,11 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() if Key != "" { + log.Info("issue Key=" + Key) nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") - boolQ.Should(nameQuery, contentQuery, commentQuery) + boolQ.Must(nameQuery, contentQuery, commentQuery) } isIssueQuery := elastic.NewTermQuery("is_pull", "f") boolQ.Must(isIssueQuery) @@ -466,7 +467,7 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { err = json.Unmarshal(source, &recordSource) if err == nil { record := make(map[string]interface{}) - record["id"] = recordSource["_id"] + record["id"] = hit.Id record["repo_id"] = recordSource["repo_id"] log.Info("recordSource[\"repo_id\"]=" + fmt.Sprint(recordSource["repo_id"])) repoIdstr := recordSource["repo_id"].(string) From 73b63462392c99f93f0d7d2ef35aef3bdb94de88 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 11:10:05 +0800 Subject: [PATCH 041/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/routers/search.go b/routers/search.go index a0f66e649..9e143a140 100644 --- a/routers/search.go +++ b/routers/search.go @@ -433,15 +433,17 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P } ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() + isIssueQuery := elastic.NewTermQuery("is_pull", "f") + boolQ.Must(isIssueQuery) + if Key != "" { log.Info("issue Key=" + Key) nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") - boolQ.Must(nameQuery, contentQuery, commentQuery) + boolQ.Should(nameQuery, contentQuery, commentQuery) } - isIssueQuery := elastic.NewTermQuery("is_pull", "f") - boolQ.Must(isIssueQuery) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { searchJson, _ := json.Marshal(res) From 3761c55a21ff5511af2ef334c15375369f8938ac Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 11:24:18 +0800 Subject: [PATCH 042/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/routers/search.go b/routers/search.go index 9e143a140..ad93b86d8 100644 --- a/routers/search.go +++ b/routers/search.go @@ -434,14 +434,17 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() isIssueQuery := elastic.NewTermQuery("is_pull", "f") - boolQ.Must(isIssueQuery) if Key != "" { + boolKeyQ := elastic.NewBoolQuery() log.Info("issue Key=" + Key) nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") - boolQ.Should(nameQuery, contentQuery, commentQuery) + boolKeyQ.Should(nameQuery, contentQuery, commentQuery) + boolQ.Must(isIssueQuery, boolKeyQ) + } else { + boolQ.Must(isIssueQuery) } res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) From d99c90b7719ec7adb31956b17541b7c27d3cdb23 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 14:40:12 +0800 Subject: [PATCH 043/315] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=AB=98=E4=BA=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 58 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/routers/search.go b/routers/search.go index ad93b86d8..5d7b224f8 100644 --- a/routers/search.go +++ b/routers/search.go @@ -134,8 +134,10 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } else { log.Info("query all content.") //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} - res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("name", "description", "topics")).Do(ctx.Req.Context()) if err == nil { + searchJson, _ := json.Marshal(res) + log.Info("searchJson=" + string(searchJson)) result := makeRepoResult(res, "") ctx.JSON(200, result) } else { @@ -245,21 +247,28 @@ func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page in } ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() - if Key != "" { - nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") - full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("f_second") - descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("f_third") - boolQ.Should(nameQuery, full_nameQuery, descriptionQuery) - } + typeValue := 1 if IsQueryUser { typeValue = 0 } UserOrOrgQuery := elastic.NewTermQuery("type", typeValue) - boolQ.Must(UserOrOrgQuery) + if Key != "" { + boolKeyQ := elastic.NewBoolQuery() + log.Info("issue Key=" + Key) + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") + full_nameQuery := elastic.NewMatchQuery("full_name", Key).Boost(1.5).QueryName("f_second") + descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1).QueryName("f_third") + boolKeyQ.Should(nameQuery, full_nameQuery, descriptionQuery) + boolQ.Must(UserOrOrgQuery, boolKeyQ) + } else { + boolQ.Must(UserOrOrgQuery) + } - res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("name", "full_name", "description")).Do(ctx.Req.Context()) if err == nil { + searchJson, _ := json.Marshal(res) + log.Info("searchJson=" + string(searchJson)) result := makeUserOrOrgResult(res, Key, ctx) ctx.JSON(200, result) } else { @@ -347,7 +356,7 @@ func searchDataSet(ctx *context.Context, TableName string, Key string, Page int, fileNameQuery := elastic.NewMatchQuery("file_name", Key).Boost(1).QueryName("f_third") categoryQuery := elastic.NewMatchQuery("category", Key).Boost(1).QueryName("f_fourth") boolQ.Should(nameQuery, descQuery, categoryQuery, fileNameQuery) - res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("title", "description", "file_name", "category")).Do(ctx.Req.Context()) if err == nil { result := makeDatasetResult(res, Key) ctx.JSON(200, result) @@ -447,7 +456,7 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P boolQ.Must(isIssueQuery) } - res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("name", "content", "comment")).Do(ctx.Req.Context()) if err == nil { searchJson, _ := json.Marshal(res) log.Info("searchJson=" + string(searchJson)) @@ -456,7 +465,19 @@ func searchIssue(ctx *context.Context, TableName string, Key string, Page int, P } else { log.Info("query es error," + err.Error()) } +} +func queryHighlight(names ...string) *elastic.Highlight { + re := elastic.NewHighlight() + for i := 0; i < len(names); i++ { + field := &elastic.HighlighterField{ + Name: names[i], + } + re.Fields(field) + } + re.PreTags("") + re.PostTags("") + return re } func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { @@ -536,15 +557,20 @@ func searchPR(ctx *context.Context, TableName string, Key string, Page int, Page } ascending := ctx.QueryBool("Ascending") boolQ := elastic.NewBoolQuery() + isPRQuery := elastic.NewTermQuery("is_pull", "t") + if Key != "" { - nameQuery := elastic.NewMatchQuery("name", Key).Boost(1024).QueryName("f_first") + boolKeyQ := elastic.NewBoolQuery() + log.Info("issue Key=" + Key) + nameQuery := elastic.NewMatchQuery("name", Key).Boost(2).QueryName("f_first") contentQuery := elastic.NewMatchQuery("content", Key).Boost(1.5).QueryName("f_second") commentQuery := elastic.NewMatchQuery("comment", Key).Boost(1).QueryName("f_third") - boolQ.Should(nameQuery, contentQuery, commentQuery) + boolKeyQ.Should(nameQuery, contentQuery, commentQuery) + boolQ.Must(isPRQuery, boolKeyQ) + } else { + boolQ.Must(isPRQuery) } - isIssueQuery := elastic.NewTermQuery("is_pull", "t") - boolQ.Must(isIssueQuery) - res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).Sort(SortBy, ascending).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("name", "content", "comment")).Do(ctx.Req.Context()) if err == nil { result := makeIssueResult(res, Key) ctx.JSON(200, result) From 5af94b8774e22341fae20fe96c068fa06dbd4655 Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 15:16:11 +0800 Subject: [PATCH 044/315] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=AB=98=E4=BA=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/routers/search.go b/routers/search.go index 5d7b224f8..d966f243c 100644 --- a/routers/search.go +++ b/routers/search.go @@ -161,10 +161,10 @@ func makeRepoResult(sRes *elastic.SearchResult, Key string) *SearchRes { if err == nil { record := make(map[string]interface{}) record["id"] = hit.Id - record["name"] = recordSource["name"] + record["name"] = getLabelValue("name", recordSource, hit.Highlight) record["owner_name"] = recordSource["owner_name"] if recordSource["description"] != nil { - desc := recordSource["description"].(string) + desc := getLabelValue("description", recordSource, hit.Highlight) record["description"] = dealLongText(desc, Key, hit.MatchedQueries) } else { record["description"] = "" @@ -277,6 +277,18 @@ func searchUserOrOrg(ctx *context.Context, TableName string, Key string, Page in } } +func getLabelValue(key string, recordSource map[string]interface{}, searchHighliht elastic.SearchHitHighlight) string { + if value, ok := searchHighliht[key]; !ok { + if recordSource[key] != nil { + return recordSource[key].(string) + } else { + return "" + } + } else { + return value[0] + } +} + func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Context) *SearchRes { total := sRes.Hits.TotalHits.Value result := make([]map[string]interface{}, 0) @@ -291,10 +303,10 @@ func makeUserOrOrgResult(sRes *elastic.SearchResult, Key string, ctx *context.Co if err == nil { record := make(map[string]interface{}) record["id"] = hit.Id - record["name"] = recordSource["name"] - record["full_name"] = recordSource["full_name"] + record["name"] = getLabelValue("name", recordSource, hit.Highlight) + record["full_name"] = getLabelValue("full_name", recordSource, hit.Highlight) if recordSource["description"] != nil { - desc := recordSource["description"].(string) + desc := getLabelValue("description", recordSource, hit.Highlight) record["description"] = dealLongText(desc, Key, hit.MatchedQueries) } else { record["description"] = "" @@ -398,14 +410,15 @@ func makeDatasetResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["owerName"] = user.GetDisplayName() record["avatar"] = user.RelAvatarLink() } - record["title"] = recordSource["title"] - record["category"] = recordSource["category"] + record["title"] = getLabelValue("title", recordSource, hit.Highlight) + record["category"] = getLabelValue("category", recordSource, hit.Highlight) if recordSource["description"] != nil { - desc := recordSource["description"].(string) + desc := getLabelValue("description", recordSource, hit.Highlight) record["description"] = dealLongText(desc, Key, hit.MatchedQueries) } else { record["description"] = "" } + record["file_name"] = recordSource["file_name"] record["download_times"] = recordSource["download_times"] record["created_unix"] = recordSource["created_unix"] result = append(result, record) @@ -505,9 +518,9 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["avatar"] = repo.RelAvatarLink() } } - record["name"] = recordSource["name"] + record["name"] = getLabelValue("name", recordSource, hit.Highlight) if recordSource["content"] != nil { - desc := recordSource["content"].(string) + desc := getLabelValue("content", recordSource, hit.Highlight) record["content"] = dealLongText(desc, Key, hit.MatchedQueries) } else { record["content"] = "" @@ -516,7 +529,7 @@ func makeIssueResult(sRes *elastic.SearchResult, Key string) *SearchRes { record["pr_id"] = recordSource["pr_id"] } if recordSource["comment"] != nil { - desc := recordSource["comment"].(string) + desc := getLabelValue("comment", recordSource, hit.Highlight) record["comment"] = dealLongText(desc, Key, hit.MatchedQueries) } else { record["comment"] = "" From 3360c46b79690fade94f28573a5f58320d1937af Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 16:30:10 +0800 Subject: [PATCH 045/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/dbsql/issue_foreigntable_for_es.sql | 5 +++-- routers/search.go | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/models/dbsql/issue_foreigntable_for_es.sql b/models/dbsql/issue_foreigntable_for_es.sql index 786e7c989..bb5c1634e 100644 --- a/models/dbsql/issue_foreigntable_for_es.sql +++ b/models/dbsql/issue_foreigntable_for_es.sql @@ -57,7 +57,7 @@ INSERT INTO public.issue_es( closed_unix, is_locked, amount, - is_transformed,comment) + is_transformed,comment,pr_id) SELECT b.id, b.repo_id, @@ -80,7 +80,8 @@ INSERT INTO public.issue_es( b.is_locked, b.amount, b.is_transformed, - (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id) + (select array_to_string(array_agg(content order by created_unix desc),',') from public.comment a where a.issue_id=b.id), + (select id from public.pull_request d where b.id=d.issue_id and b.is_pull=true) FROM public.issue b,public.repository c where b.repo_id=c.id and c.is_private=false; diff --git a/routers/search.go b/routers/search.go index d966f243c..d0c8013b1 100644 --- a/routers/search.go +++ b/routers/search.go @@ -123,8 +123,10 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1.5).QueryName("f_second") topicsQuery := elastic.NewMatchQuery("topics", Key).Boost(1).QueryName("f_third") boolQ.Should(nameQuery, descriptionQuery, topicsQuery) - res, err := client.Search(TableName).Query(boolQ).SortBy(elastic.NewScoreSort(), elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) + res, err := client.Search(TableName).Query(boolQ).SortBy(elastic.NewScoreSort(), elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("name", "description", "topics")).Do(ctx.Req.Context()) if err == nil { + searchJson, _ := json.Marshal(res) + log.Info("searchJson=" + string(searchJson)) result := makeRepoResult(res, Key) ctx.JSON(200, result) } else { @@ -134,7 +136,7 @@ func searchRepo(ctx *context.Context, TableName string, Key string, Page int, Pa } else { log.Info("query all content.") //搜索的属性要指定{"timestamp":{"unmapped_type":"date"}} - res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Highlight(queryHighlight("name", "description", "topics")).Do(ctx.Req.Context()) + res, err := client.Search(TableName).SortBy(elastic.NewFieldSort(SortBy).Order(ascending)).From((Page - 1) * PageSize).Size(PageSize).Do(ctx.Req.Context()) if err == nil { searchJson, _ := json.Marshal(res) log.Info("searchJson=" + string(searchJson)) From 43238e4df2528131bac4e35fd098631c624ed4cc Mon Sep 17 00:00:00 2001 From: zouap Date: Fri, 7 Jan 2022 16:46:29 +0800 Subject: [PATCH 046/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/search.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/routers/search.go b/routers/search.go index d0c8013b1..410944197 100644 --- a/routers/search.go +++ b/routers/search.go @@ -406,11 +406,14 @@ func makeDatasetResult(sRes *elastic.SearchResult, Key string) *SearchRes { if err == nil { record := make(map[string]interface{}) record["id"] = hit.Id - userId := recordSource["user_id"].(int64) - user, errUser := models.GetUserByID(userId) - if errUser == nil { - record["owerName"] = user.GetDisplayName() - record["avatar"] = user.RelAvatarLink() + userIdStr := recordSource["user_id"].(string) + userId, cerr := strconv.ParseInt(userIdStr, 10, 64) + if cerr == nil { + user, errUser := models.GetUserByID(userId) + if errUser == nil { + record["owerName"] = user.GetDisplayName() + record["avatar"] = user.RelAvatarLink() + } } record["title"] = getLabelValue("title", recordSource, hit.Highlight) record["category"] = getLabelValue("category", recordSource, hit.Highlight) From fd2d645c1a55947ea54ed68f050b014f649656a3 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 10 Jan 2022 15:57:12 +0800 Subject: [PATCH 047/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/repo.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/models/repo.go b/models/repo.go index 6863004a6..e5cc5831d 100755 --- a/models/repo.go +++ b/models/repo.go @@ -13,6 +13,7 @@ import ( "html/template" "math/rand" + "code.gitea.io/gitea/modules/git" "xorm.io/xorm" "code.gitea.io/gitea/modules/blockchain" @@ -2559,6 +2560,56 @@ func UpdateRepositoryCommitNum(repo *Repository) error { return nil } +type RepoFile struct { + CommitId string + Content []byte +} + +// ReadLatestFileInRepo read latest version of file in repository +// return a RepoFile +func ReadLatestFileInRepo(userName, repoName, refName, treePath string) (*RepoFile, error) { + var err error + repoPath := RepoPath(userName, repoName) + gitRepo, err := git.OpenRepository(repoPath) + if err != nil { + log.Error("ReadLatestFileInRepo error when OpenRepository,error=%v", err) + return nil, err + } + commitID, err := gitRepo.GetBranchCommitID(refName) + if err != nil { + log.Error("ReadLatestFileInRepo error when GetBranchCommitID,error=%v", err) + return nil, err + } + commit, err := gitRepo.GetBranchCommit(refName) + if err != nil { + log.Error("ReadLatestFileInRepo error when GetBranchCommit,error=%v", err) + return nil, err + } + + blob, err := commit.GetBlobByPath(treePath) + if err != nil { + log.Error("ReadLatestFileInRepo error when GetBlobByPath,error=%v", err) + return nil, err + } + + reader, err := blob.DataAsync() + if err != nil { + return nil, err + } + defer func() { + if err = reader.Close(); err != nil { + log.Error("ReadLatestFileInRepo: Close: %v", err) + } + }() + + buf := make([]byte, 1024) + n, _ := reader.Read(buf) + if n >= 0 { + buf = buf[:n] + } + return &RepoFile{CommitId: commitID, Content: buf}, nil +} + func GenerateDefaultRepoName(ownerName string) string { if len(ownerName) > 5 { ownerName = ownerName[:5] From 9f3353510c469ba8903168c3f09d4fbdcb06f565 Mon Sep 17 00:00:00 2001 From: zouap Date: Mon, 17 Jan 2022 10:26:44 +0800 Subject: [PATCH 048/315] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- templates/base/head_navbar.tmpl | 2 +- templates/explore/search_new.tmpl | 222 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 templates/explore/search_new.tmpl diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index d1d40d1d6..60826f0ad 100755 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -95,7 +95,7 @@ {{if .IsSigned}}