From 21df929126791a33145ebf63c6d20a0409969daf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 21 Aug 2024 18:17:22 +0800 Subject: [PATCH] feat(github): add index to tool layer table _tool_github_jobs (#7932) (#7933) Co-authored-by: Lynwee <1507509064@qq.com> --- .../20240821_add_index_to_tool_github_jobs.go | 56 ++++++++++++++++ .../models/migrationscripts/register.go | 1 + .../github/tasks/cicd_job_convertor_test.go | 65 +++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 backend/plugins/github/models/migrationscripts/20240821_add_index_to_tool_github_jobs.go create mode 100644 backend/plugins/github/tasks/cicd_job_convertor_test.go diff --git a/backend/plugins/github/models/migrationscripts/20240821_add_index_to_tool_github_jobs.go b/backend/plugins/github/models/migrationscripts/20240821_add_index_to_tool_github_jobs.go new file mode 100644 index 00000000000..4933e557ffb --- /dev/null +++ b/backend/plugins/github/models/migrationscripts/20240821_add_index_to_tool_github_jobs.go @@ -0,0 +1,56 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package migrationscripts + +import ( + "github.com/apache/incubator-devlake/core/context" + "github.com/apache/incubator-devlake/core/errors" + "github.com/apache/incubator-devlake/core/plugin" + "net/url" +) + +var _ plugin.MigrationScript = (*addIndexToGithubJobs)(nil) + +type addIndexToGithubJobs struct{} + +func (script *addIndexToGithubJobs) Up(basicRes context.BasicRes) errors.Error { + db := basicRes.GetDal() + dbUrl := basicRes.GetConfig("DB_URL") + if dbUrl == "" { + return errors.BadInput.New("DB_URL is required") + } + u, errParse := url.Parse(dbUrl) + if errParse != nil { + return errors.Convert(errParse) + } + if u.Scheme == "mysql" { + sql := "ALTER TABLE `_tool_github_jobs` ADD INDEX `idx_repo_id_connection_id` (`repo_id`, `connection_id`)" + if err := db.Exec(sql); err != nil { + return err + } + } + return nil +} + +func (*addIndexToGithubJobs) Version() uint64 { + return 20240821160000 +} + +func (*addIndexToGithubJobs) Name() string { + return "add index to _tool_github_jobs" +} diff --git a/backend/plugins/github/models/migrationscripts/register.go b/backend/plugins/github/models/migrationscripts/register.go index 346654d99fa..b8a0722eb02 100644 --- a/backend/plugins/github/models/migrationscripts/register.go +++ b/backend/plugins/github/models/migrationscripts/register.go @@ -54,5 +54,6 @@ func All() []plugin.MigrationScript { new(restructReviewer), new(addIsDraftToPr), new(changeIssueComponentType), + new(addIndexToGithubJobs), } } diff --git a/backend/plugins/github/tasks/cicd_job_convertor_test.go b/backend/plugins/github/tasks/cicd_job_convertor_test.go new file mode 100644 index 00000000000..9dd3045f18a --- /dev/null +++ b/backend/plugins/github/tasks/cicd_job_convertor_test.go @@ -0,0 +1,65 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package tasks + +import ( + "fmt" + "github.com/apache/incubator-devlake/core/models/domainlayer/didgen" + "github.com/apache/incubator-devlake/core/plugin" + mockplugin "github.com/apache/incubator-devlake/mocks/core/plugin" + "github.com/apache/incubator-devlake/plugins/github/models" + "github.com/stretchr/testify/assert" + "testing" +) + +func GenJobIDWithReflect(jobIdGen *didgen.DomainIdGenerator) { + connectionId := uint64(1) + runId := 1 + lineId := 1 + jobIdGen.Generate(connectionId, runId, lineId) +} + +func GenJobID() { + connectionId := uint64(1) + runId := 1 + lineId := 1 + fmt.Sprintf("GithubJob:%d:%d:%d", connectionId, runId, lineId) +} + +func BenchmarkGenJobIDWithReflect(b *testing.B) { + mockMeta := mockplugin.NewPluginMeta(b) + mockMeta.On("RootPkgPath").Return("github.com/apache/incubator-devlake/plugins/github") + mockMeta.On("Name").Return("github").Maybe() + err := plugin.RegisterPlugin("github", mockMeta) + assert.NoError(b, err) + + jobIdGen := didgen.NewDomainIdGenerator(&models.GithubJob{}) + b.StartTimer() + for i := 0; i < b.N; i++ { + GenJobIDWithReflect(jobIdGen) + } + b.StopTimer() + //BenchmarkGenJobIDWithReflect-8 5611773 208.9 ns/op +} + +func BenchmarkGenJobID(b *testing.B) { + for i := 0; i < b.N; i++ { + GenJobID() + } + //BenchmarkGenJobID-8 11078593 99.43 ns/op +}