/
render.go
140 lines (123 loc) · 4.16 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed 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 assigneeHorizontalBarChart
import (
"context"
"encoding/json"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
"github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/dop/component-protocol/components/issue-dashboard/common/chartbuilders"
"github.com/erda-project/erda/modules/dop/component-protocol/components/issue-dashboard/common/gshelper"
"github.com/erda-project/erda/modules/dop/component-protocol/components/issue-dashboard/common/stackhandlers"
"github.com/erda-project/erda/modules/dop/dao"
"github.com/erda-project/erda/modules/openapi/component-protocol/components/base"
)
func init() {
base.InitProviderWithCreator("issue-dashboard", "assigneeHorizontalBarChart",
func() servicehub.Provider { return &ComponentAction{} })
}
func (f *ComponentAction) getState(c *cptype.Component) error {
d, err := json.Marshal(c.State)
if err != nil {
return err
}
return json.Unmarshal(d, &f.State)
}
func (f *ComponentAction) Render(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData) error {
if err := f.getState(c); err != nil {
return err
}
helper := gshelper.NewGSHelper(gs)
members := helper.GetMembers()
memberMap := make(map[string]*apistructs.Member)
for i := range members {
m := &members[i]
memberMap[m.UserID] = m
}
issueList := helper.GetIssueList()
var bugList []interface{}
for i := range issueList {
if issueList[i].Belong == string(apistructs.IssueStateBelongClosed) {
continue
}
bugList = append(bugList, &issueList[i])
}
handler := stackhandlers.NewStackRetriever(
stackhandlers.WithIssueStateList(helper.GetIssueStateList()),
stackhandlers.WithIssueStageList(helper.GetIssueStageList()),
).GetRetriever(f.State.Values.Type)
builder := &chartbuilders.BarBuilder{
Items: bugList,
StackHandler: handler,
FixedXAxisOrTop: chartbuilders.FixedXAxisOrTop{
Top: 500,
XIndexer: getXIndexer(),
XDisplayConverter: func(opt *chartbuilders.FixedXAxisOrTop) opts.XAxis {
return opts.XAxis{
Type: "value",
Max: opt.MaxValue,
}
},
},
YAxisOpt: chartbuilders.YAxisOpt{
YDisplayConverter: func(opt *chartbuilders.YAxisOpt) opts.YAxis {
var names []string
for _, userID := range opt.YAxis {
var name string
m, ok := memberMap[userID]
if ok && m != nil {
name = m.Nick
//name = m.Nick + " (" + userID + ")"
} else {
name = cputil.I18n(ctx, "user-not-exist")
}
names = append(names, name)
}
return opts.YAxis{
Type: "category",
Data: names,
}
},
},
StackOpt: chartbuilders.StackOpt{
SkipEmpty: true,
},
DataHandleOpt: chartbuilders.DataHandleOpt{
SeriesConverter: chartbuilders.GetStackBarSingleSeriesConverter(),
DataWhiteList: f.State.Values.Value,
},
Result: chartbuilders.Result{
PostProcessor: chartbuilders.GetHorizontalPostProcessor(),
},
}
if err := builder.Generate(); err != nil {
return err
}
props := make(map[string]interface{})
props["title"] = "缺陷 - 按未关闭缺陷的处理人分布(Top 500)"
props["chartType"] = "bar"
props["option"] = builder.Result.Bb
props["style"] = map[string]interface{}{"height": 400}
c.Props = props
c.State = nil
return nil
}
func getXIndexer() func(interface{}) string {
return func(item interface{}) string {
return item.(*dao.IssueItem).Assignee
}
}