Skip to content

Commit

Permalink
feat(github): add index to tool layer table _tool_github_jobs (#7932) (
Browse files Browse the repository at this point in the history
…#7933)

Co-authored-by: Lynwee <[email protected]>
  • Loading branch information
github-actions[bot] and d4x1 authored Aug 21, 2024
1 parent cc0201d commit 21df929
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions backend/plugins/github/models/migrationscripts/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ func All() []plugin.MigrationScript {
new(restructReviewer),
new(addIsDraftToPr),
new(changeIssueComponentType),
new(addIndexToGithubJobs),
}
}
65 changes: 65 additions & 0 deletions backend/plugins/github/tasks/cicd_job_convertor_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 21df929

Please sign in to comment.