/
render.go
224 lines (204 loc) · 6.4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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 issueGantt
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"github.com/sirupsen/logrus"
"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/bundle"
"github.com/erda-project/erda/modules/dop/component-protocol/components/issue-manage/issueViewGroup"
"github.com/erda-project/erda/modules/dop/component-protocol/types"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/components/gantt"
)
func (g *Gantt) Render(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData) error {
// import component data
if err := g.Import(c); err != nil {
logrus.Errorf("import component failed, err:%v", err)
return err
}
g.sdk = cputil.SDK(ctx)
g.bdl = ctx.Value(types.GlobalCtxKeyBundle).(*bundle.Bundle)
// check visible
visible := false
if v, ok := c.State["issueViewGroupValue"]; ok {
if viewType, ok := v.(string); ok {
if viewType != issueViewGroup.ViewTypeGantt {
visible = false
c.Props = map[string]interface{}{}
c.Props.(map[string]interface{})["visible"] = visible
return nil
}
}
g.Props.Visible = true
visible = true
}
if !visible {
c.Props = map[string]interface{}{}
c.Props.(map[string]interface{})["visible"] = visible
return nil
}
issueType, _ := g.sdk.InParams["fixedIssueType"].(string)
g.State.IssueType = issueType
// listen on operation
switch event.Operation.String() {
case apistructs.InitializeOperation.String(), apistructs.RenderingOperation.String():
g.setDefaultState(event.Operation)
g.setOperations()
err := g.RenderOnFilter(ctx, c)
if err != nil {
logrus.Errorf("render on filter failed, err:%v", err)
return err
}
case gantt.OpChangePageNo.String():
err := g.RenderOnFilter(ctx, c)
if err != nil {
logrus.Errorf("render on filter failed, err:%v", err)
return err
}
default:
logrus.Warnf("operation [%s] not support, scenario:%v, event:%v", event.Operation, scenario, event)
}
// export rendered component data
if err := g.Export(c, gs); err != nil {
logrus.Errorf("export component failed, err:%v", err)
return err
}
return nil
}
func (g *Gantt) RenderOnFilter(ctx context.Context, c *cptype.Component) error {
// get filter request
req, err := g.getFilterReq(c)
if err != nil {
logrus.Errorf("get filter request failed, err:%v", err)
return err
}
g.State.PageSize = 10
// query issues
req.PageSize = 10
rsp, err := g.bdl.PageIssues(*req)
if err != nil {
logrus.Errorf("page issue failed, request: %v, err: %v", req, err)
return err
}
// get issues edge time
edgeMinTime, edgeMaxTime := getEdgeTime(rsp.Data.List)
// generate gantt props
g.genProps(ctx, edgeMinTime, edgeMaxTime)
// generate gantt data
err = g.genData(rsp.Data.List, edgeMinTime, edgeMaxTime)
if err != nil {
logrus.Errorf("generate data failed, request:%v, err:%v", req, err)
return err
}
g.setStateTotal(rsp.Data.Total)
return nil
}
func (g Gantt) getFilterReq(c *cptype.Component) (req *apistructs.IssuePagingRequest, err error) {
projectid, err := strconv.ParseUint(g.sdk.InParams["projectId"].(string), 10, 64)
orgid, err := strconv.ParseUint(g.sdk.Identity.OrgID, 10, 64)
cond := apistructs.IssuePagingRequest{}
filterCond, ok := c.State["filterConditions"]
if ok {
filterCondS, err := json.Marshal(filterCond)
if err != nil {
return nil, err
}
if err := json.Unmarshal(filterCondS, &cond); err != nil {
return nil, err
}
} else {
cond.OrgID = int64(orgid)
cond.ProjectID = projectid
cond.IssueListRequest.IdentityInfo.UserID = g.sdk.Identity.UserID
}
if g.State.PageSize == 0 {
cond.PageSize = gantt.DefaultPageSize
cond.PageNo = gantt.DefaultPageNo
g.State.PageSize = cond.PageSize
g.State.PageNo = cond.PageNo
} else {
cond.PageNo = g.State.PageNo
cond.PageSize = g.State.PageSize
}
// TODO: test remove
// cond.IDs = []int64{777, 663}
// cond.PageSize = 1
cond.OrderBy = "assignee"
cond.Type = getIssueTypes(g.State.IssueType)
return &cond, nil
}
func getIssueTypes(t string) []apistructs.IssueType {
switch t {
case "ALL":
return []apistructs.IssueType{apistructs.IssueTypeTask, apistructs.IssueTypeRequirement, apistructs.IssueTypeBug}
case apistructs.IssueTypeTask.String():
return []apistructs.IssueType{apistructs.IssueTypeTask}
case apistructs.IssueTypeRequirement.String():
return []apistructs.IssueType{apistructs.IssueTypeRequirement}
case apistructs.IssueTypeBug.String():
return []apistructs.IssueType{apistructs.IssueTypeBug}
default:
logrus.Warnf("wrong issue type: %v, assign all", t)
return []apistructs.IssueType{apistructs.IssueTypeTask, apistructs.IssueTypeRequirement, apistructs.IssueTypeBug}
}
}
func (g *Gantt) Import(c *cptype.Component) error {
b, err := json.Marshal(c)
if err != nil {
return err
}
if err := json.Unmarshal(b, g); err != nil {
return err
}
return nil
}
func (g *Gantt) Export(c *cptype.Component, gs *cptype.GlobalStateData) error {
// set component data
b, err := json.Marshal(g)
if err != nil {
return err
}
c.Data = nil
if err := json.Unmarshal(b, c); err != nil {
return err
}
// set url query state
err = g.SetStateToUrlQuery(c)
if err != nil {
return err
}
// set global state: uids
(*gs)[protocol.GlobalInnerKeyUserIDs.String()] = g.Uids
return nil
}
func getStateUrlQueryKey() string {
return fmt.Sprintf("%s__urlQuery", CompName)
}
func (g Gantt) SetStateToUrlQuery(c *cptype.Component) error {
g.State.Total = 0
b, err := json.Marshal(g.State)
if err != nil {
return err
}
urlQueryStr := base64.StdEncoding.EncodeToString(b)
c.State[getStateUrlQueryKey()] = urlQueryStr
return nil
}