Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/erda-project/erda
Browse files Browse the repository at this point in the history
  • Loading branch information
kakj-go committed Oct 29, 2021
2 parents 41b9878 + 11e5f9c commit 984c5e4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 20 deletions.
12 changes: 12 additions & 0 deletions modules/cmp/tasks/collect_daily_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (d *DailyQuotaCollector) Task() (bool, error) {
logrus.WithError(err).WithField("clusters", clusterNames).Errorln()
}

d.clearExpire()

return false, nil
}

Expand Down Expand Up @@ -253,6 +255,16 @@ func (d *DailyQuotaCollector) collectClusterDaily(clusterNames []string) error {
return nil
}

func (d *DailyQuotaCollector) clearExpire() {
lastYear := time.Now().AddDate(-1, 0, 0).Format("2006-01-02 00:00:00")
if err := d.db.Where("created_at < ?", lastYear).Delete(new(apistructs.ClusterResourceDailyModel)).Error; err != nil {
logrus.Warnln("failed to clear expire data from ClusterResourceDailyModel")
}
if err := d.db.Where("created_at < ?", lastYear).Delete(new(apistructs.ProjectResourceDailyModel)).Error; err != nil {
logrus.Warnln("failed to clear expire data from ProjectResourceDailyModel")
}
}

type DailyQuotaCollectorOption func(collector *DailyQuotaCollector)

func DailyQuotaCollectorWithDBClient(client *dbclient.DBClient) DailyQuotaCollectorOption {
Expand Down
7 changes: 7 additions & 0 deletions modules/core-services/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,9 @@ func (p *Project) fetchAvailable(ctx context.Context, dto *apistructs.ProjectDTO

// 根据已有统计值计算比率
func (p *Project) calcuRequestRate(dto *apistructs.ProjectDTO) {
if dto.ResourceConfig == nil {
return
}
for _, source := range []*apistructs.ResourceConfigInfo{
dto.ResourceConfig.PROD,
dto.ResourceConfig.STAGING,
Expand All @@ -772,6 +775,9 @@ func (p *Project) calcuRequestRate(dto *apistructs.ProjectDTO) {
}

func (p *Project) makeProjectDtoTips(dto *apistructs.ProjectDTO, langCodes i18n.LanguageCodes) {
if dto.ResourceConfig == nil {
return
}
for _, source := range []*apistructs.ResourceConfigInfo{
dto.ResourceConfig.PROD,
dto.ResourceConfig.STAGING,
Expand Down Expand Up @@ -925,6 +931,7 @@ func (p *Project) ListAllProjects(userID string, params *apistructs.ProjectListR
if v, ok := projectOwnerMap[projectDTOs[i].ID]; ok {
projectDTOs[i].Owners = v
}
p.fetchQuota(&projectDTOs[i])
}

return &apistructs.PagingProjectDTO{Total: total, List: projectDTOs}, nil
Expand Down
25 changes: 23 additions & 2 deletions modules/pipeline/services/pipelinesvc/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/mohae/deepcopy"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/xormplus/xorm"

"github.com/erda-project/erda/apistructs"
Expand Down Expand Up @@ -354,8 +355,8 @@ func (s *PipelineSvc) CreatePipelineGraph(p *spec.Pipeline) (err error) {
var stages []*spec.PipelineStage
_, err = s.dbClient.Transaction(func(session *xorm.Session) (interface{}, error) {
// create pipeline
if err := s.dbClient.CreatePipeline(p, dbclient.WithTxSession(session)); err != nil {
return nil, apierrors.ErrCreatePipeline.InternalError(err)
if err := s.createPipelineAndCheckNotEndStatus(p, session); err != nil {
return nil, err
}
// create pipeline stages
stages, err = s.createPipelineGraphStage(p, pipelineYml, dbclient.WithTxSession(session))
Expand Down Expand Up @@ -395,6 +396,26 @@ func (s *PipelineSvc) CreatePipelineGraph(p *spec.Pipeline) (err error) {
return nil
}

func (s *PipelineSvc) createPipelineAndCheckNotEndStatus(p *spec.Pipeline, session *xorm.Session) error {
// Check whether the parent pipeline has an end state
for _, parentPipelineID := range p.Extra.SnippetChain {
parentPipeline, _, err := s.dbClient.GetPipelineBase(parentPipelineID, dbclient.WithTxSession(session))
if err != nil {
logrus.Errorf("check whether the parent pipeline has an end state, error %v", err)
continue
}
if parentPipeline.Status.IsEndStatus() {
return fmt.Errorf("parent pipeline was end status")
}
}

// create pipeline
if err := s.dbClient.CreatePipeline(p, dbclient.WithTxSession(session)); err != nil {
return apierrors.ErrCreatePipeline.InternalError(err)
}
return nil
}

func getString(v interface{}) string {
if v == nil {
return ""
Expand Down
43 changes: 43 additions & 0 deletions modules/pipeline/services/pipelinesvc/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,46 @@ func TestPipelineSvc_MergePipelineYmlTasks(t *testing.T) {
})
}
}

func TestPipelineSvc_createPipelineAndCheckNotEndStatus(t *testing.T) {
type args struct {
p *spec.Pipeline
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test_end_status_error",
args: args{
p: &spec.Pipeline{
PipelineExtra: spec.PipelineExtra{
Extra: spec.PipelineExtraInfo{
SnippetChain: []uint64{1},
},
PipelineYml: "version: \"1.1\"\nstages:\n - stage:\n - git-checkout:\n alias: git-checkout\n description: 代码仓库克隆\n - stage:\n - java:\n alias: java-demo\n description: 针对 java 工程的编译打包任务,产出可运行镜像\n params:\n build_type: maven\n container_type: spring-boot\n target: ./target/docker-java-app-example.jar\n workdir: ${git-checkout}\n caches:\n - path: /root/.m2/repository\n - stage:\n - release:\n alias: release\n description: 用于打包完成时,向dicehub 提交完整可部署的dice.yml。用户若没在pipeline.yml里定义该action,CI会自动在pipeline.yml里插入该action\n params:\n dice_yml: ${git-checkout}/dice.yml\n image:\n java-demo: ${java-demo:OUTPUT:image}\n - stage:\n - dice:\n alias: dice\n description: 用于 dice 平台部署应用服务\n params:\n release_id: ${release:OUTPUT:releaseID}\n - stage:\n - snippet:\n alias: snippet\n",
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &PipelineSvc{}

var db = &dbclient.Client{}
patch2 := monkey.PatchInstanceMethod(reflect.TypeOf(db), "GetPipelineBase", func(db *dbclient.Client, id uint64, ops ...dbclient.SessionOption) (spec.PipelineBase, bool, error) {
return spec.PipelineBase{
Status: apistructs.PipelineStatusSuccess,
}, true, nil
})
defer patch2.Unpatch()

if err := s.createPipelineAndCheckNotEndStatus(tt.args.p, nil); (err != nil) != tt.wantErr {
t.Errorf("createPipelineAndCheckNotEndStatus() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
36 changes: 18 additions & 18 deletions pkg/resourcecalculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,22 @@ func (q *ResourceCalculator) totalForWorkspace(workspace Workspace) uint64 {
return sum
}

func (q *ResourceCalculator) deductionQuota(workspace Workspace, quota uint64) {
q.deduction += quota
func (q *ResourceCalculator) deductionQuota(workspace Workspace, value uint64) {
q.deduction += value
// 按优先级减扣
p := priority(workspace)
for _, workspaces := range p {
if q.WorkspacesValues[workspaces] >= quota {
q.WorkspacesValues[workspaces] -= quota
q.takeUp(workspaces, quota)
if q.WorkspacesValues[workspaces] >= value {
q.WorkspacesValues[workspaces] -= value
q.takeUp(workspaces, value)
return
}
quota -= q.WorkspacesValues[workspaces]
value -= q.WorkspacesValues[workspaces]
q.takeUp(workspaces, q.WorkspacesValues[workspaces])
q.WorkspacesValues[workspaces] = 0
}

q.takeUp(WorkspaceString(workspace), quota)
q.takeUp(WorkspaceString(workspace), value)
}

func (q *ResourceCalculator) takeUp(workspaces string, value uint64) {
Expand Down Expand Up @@ -226,19 +226,19 @@ func WorkspacesString(workspaces []Workspace) []string {
return result
}

func CoreToMillcore(v float64) uint64 {
return uint64(v * 1000)
func CoreToMillcore(value float64) uint64 {
return uint64(value * 1000)
}

func MillcoreToCore(v uint64, accuracy int32) float64 {
return Accuracy(float64(v)/1000, accuracy)
func MillcoreToCore(value uint64, accuracy int32) float64 {
return Accuracy(float64(value)/1000, accuracy)
}

func GibibyteToByte(v float64) uint64 {
return uint64(v * 1024 * 1024 * 1024)
func GibibyteToByte(value float64) uint64 {
return uint64(value * 1024 * 1024 * 1024)
}
func ByteToGibibyte(v uint64, accuracy int32) float64 {
return Accuracy(float64(v)/(1024*1024*1024), accuracy)
func ByteToGibibyte(value uint64, accuracy int32) float64 {
return Accuracy(float64(value)/(1024*1024*1024), accuracy)
}

func priority(workspace Workspace) []string {
Expand Down Expand Up @@ -292,9 +292,9 @@ func ResourceToString(res float64, typ string) string {
}
}

func Accuracy(value float64, accuracy int32) float64 {
value, _ = decimal.NewFromFloat(value).Round(accuracy).Float64()
return value
func Accuracy(v float64, accuracy int32) float64 {
v, _ = decimal.NewFromFloat(v).Round(accuracy).Float64()
return v
}

func setPrec(f float64, prec int) float64 {
Expand Down

0 comments on commit 984c5e4

Please sign in to comment.