/
model.go
413 lines (378 loc) · 12.7 KB
/
model.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package issueGantt
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"github.com/erda-project/erda/modules/openapi/component-protocol/components/gantt"
"github.com/erda-project/erda/pkg/strutil"
)
type Gantt struct {
CtxBdl protocol.ContextBundle `json:"-"`
// 从全局状态返回给框架
Uids []string
gantt.CommonGantt
}
type State struct {
// common state
gantt.State
IssuePagingRequest apistructs.IssuePagingRequest `json:"issuePagingRequest,omitempty"`
}
func getEdgeTime(issues []apistructs.Issue) (eMin, eMax *time.Time) {
var edgeMinTime, edgeMaxTime *time.Time
var minIssue, maxIssue apistructs.Issue
for _, v := range issues {
if edgeMinTime == nil && v.PlanStartedAt != nil {
edgeMinTime = v.PlanStartedAt
minIssue = v
}
if edgeMaxTime == nil && v.PlanFinishedAt != nil {
edgeMaxTime = v.PlanFinishedAt
maxIssue = v
}
if edgeMinTime != nil && v.PlanStartedAt != nil && v.PlanStartedAt.Before(*edgeMinTime) {
edgeMinTime = v.PlanStartedAt
minIssue = v
}
if edgeMaxTime != nil && v.PlanFinishedAt != nil && v.PlanFinishedAt.After(*edgeMaxTime) {
edgeMaxTime = v.PlanFinishedAt
maxIssue = v
}
}
logrus.Infof("raw edge issues, min:%+v, max:%+v", minIssue, maxIssue)
now := time.Now()
nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
//if edgeMinTime == nil && edgeMaxTime == nil {
tmp1 := nowDate.AddDate(0, 0, -15)
edgeMinTime = &tmp1
tmp2 := nowDate.AddDate(0, 0, 10)
edgeMaxTime = &tmp2
//} else if edgeMinTime == nil {
// tmp := edgeMaxTime.AddDate(0, 0, -24)
// edgeMinTime = &tmp
//} else if edgeMaxTime == nil {
// tmp := edgeMinTime.AddDate(0, 0, 24)
// edgeMaxTime = &tmp
//} else {
// if nowDate.After(*edgeMaxTime) {
// edgeMaxTime = &now
// }
// tmp := edgeMinTime.AddDate(0, 0, 24)
// if edgeMaxTime.Before(tmp) {
// edgeMaxTime = &tmp
// }
//}
logrus.Infof("edge issues times, min:%+v, max:%+v", *edgeMinTime, *edgeMaxTime)
return edgeMinTime, edgeMaxTime
}
func (g *Gantt) genProps(edgeMinTime, edgeMaxTime *time.Time) {
props := gantt.Props{
Visible: true,
RowKey: "id",
// ClassName: "task-gantt-table",
Columns: []gantt.PropColumn{
{Title: "处理人", DataIndex: "user", Width: 160},
{Title: "标题", DataIndex: "issues",
TitleTip: []string{
"事项的甘特图只有确保正确输入截止日期、预计时间才能正常显示",
"#gray#灰色#>gray#:代表事项截止日期的剩余时间段",
"#blue#蓝色#>blue#:代表从事项开始时间到当前/事项完成日期的时间段",
"#red#红色#>red#:代表截止日期到当前/事项完成日期的超时时间段",
},
},
}}
ganColumn := gantt.PropColumn{Title: "甘特图", DataIndex: "dateRange", TitleRenderType: "gantt", Width: 800}
data := g.genGanPropColumn(edgeMinTime, edgeMaxTime)
ganColumn.Data = data
props.Columns = append(props.Columns, ganColumn)
g.setProps(props)
}
func (g Gantt) getZhTaskName() string {
switch g.State.IssueType {
case apistructs.IssueTypeTask.String():
return "任务"
case apistructs.IssueTypeBug.String():
return "缺陷"
case apistructs.IssueTypeRequirement.String():
return "需求"
default:
return "任务"
}
}
// TODO: generate gantt prop column data based on issues
// 长度为25天
func (g *Gantt) genGanPropColumn(edgeMinTime, edgeMaxTime *time.Time) []gantt.PropColumnData {
start := *edgeMinTime
end := *edgeMaxTime
mDays := make(map[uint64][]uint64)
var months []uint64
// range: [start, end]
for !start.Equal(end) {
m := uint64(start.Month())
d := uint64(start.Day())
if months == nil || m != months[len(months)-1] {
months = append(months, m)
}
mDays[m] = append(mDays[m], d)
start = start.AddDate(0, 0, 1)
}
var data []gantt.PropColumnData
for _, m := range months {
data = append(data, gantt.PropColumnData{
Month: m,
Date: mDays[m],
})
}
return data
}
func (g *Gantt) setProps(p gantt.Props) {
g.Props = p
}
func (g *Gantt) genData(issues []apistructs.Issue, edgeMinTime, edgeMaxTime *time.Time) error {
// group by assignee
// test
//now := time.Now()
//nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
//edgeMinTime = &nowDatedice-openapi-56979f5cb8-vm6pc
// ===
aIssues := make(map[string][]apistructs.Issue)
var uids []string
for _, v := range issues {
aIssues[v.Assignee] = append(aIssues[v.Assignee], v)
uids = append(uids, v.Assignee)
}
uInfos := genUserInfo(uids)
g.setUids(uids)
// range by assignee
var data []gantt.DataItem
autoID := uint64(10000000000000)
for k, v := range aIssues {
uInfo := uInfos[k]
for _, i := range v {
t := gantt.DataTask{RenderType: "string-list"}
r := gantt.DateRange{RenderType: "gantt"}
tInfo := genTaskInfo(i)
rInfo := genRangeInfo(edgeMinTime, edgeMaxTime, i)
t.Value = append(t.Value, tInfo)
r.Value = append(r.Value, rInfo)
item := gantt.DataItem{ID: autoID + uint64(i.ID), User: *uInfo, Tasks: t, DateRange: r}
data = append(data, item)
}
}
g.setData(data)
return nil
}
// TODO: 如果总返回response透出了userInfos, 此处只需要userID即可, 不需要再查一次
func genUserInfo(uids []string) map[string]*gantt.User {
gUserInfo := make(map[string]*gantt.User)
for _, v := range uids {
uid, _ := strconv.ParseUint(v, 10, 64)
gUserInfo[v] = &gantt.User{
Value: uid,
RenderType: gantt.RenderTypeMemberAvatar,
}
}
//uInfo, err := posthandle.GetUsers(uids, false)
//if err != nil {
// logrus.Warnf("get user info failed, err:%v", err)
// return gUserInfo
//}
//for k, v := range uInfo {
// gUserInfo[k].Name = v.Name
// gUserInfo[k].Nick = v.Nick
//}
return gUserInfo
}
func genTaskInfo(i apistructs.Issue) gantt.DataTaskValue {
return gantt.DataTaskValue{
Text: i.Title,
ID: i.ID,
Type: i.Type,
IterationID: i.IterationID,
}
}
func fix(x *time.Time, l time.Time, r time.Time) {
if x.Before(l) {
*x = l
} else if x.After(r) {
*x = r
}
}
// TODO:
func genRangeInfo(edgeMinTime, edgeMaxTime *time.Time, i apistructs.Issue) gantt.DateRangeValue {
// 时间(天)
// offset 偏移时间(天),开始时间相对边界时间
var trueFinishDate time.Time
data := gantt.DateRangeValue{Tooltip: i.Title}
now := time.Now()
var startTime time.Time
if i.PlanFinishedAt != nil {
*i.PlanFinishedAt = (*i.PlanFinishedAt).Add(time.Hour * time.Duration(24))
}
// PlanStartedAt为空,向上取整;不为空,向下取整 (单位: 天)
if i.PlanFinishedAt != nil && i.ManHour.EstimateTime > 0 {
startTime = (*i.PlanFinishedAt).Add(-time.Minute * time.Duration(i.ManHour.EstimateTime*3))
}
if i.ManHour.EstimateTime == 0 || i.PlanFinishedAt == nil {
return data
}
e := edgeMinTime
s := &startTime
f := i.PlanFinishedAt
ts := i.PlanStartedAt
tf := i.FinishTime
// 非法数据不显示
if ts != nil && tf != nil && tf.Before(*ts) {
return data
}
if ts != nil && now.Before(*ts) {
return data
}
if tf != nil && now.Before(*tf) {
return data
}
edgeDate := time.Date(e.Year(), e.Month(), e.Day(), 0, 0, 0, 0, edgeMinTime.Location())
fedgeData := time.Date(edgeMaxTime.Year(), edgeMaxTime.Month(), edgeMaxTime.Day(), 0, 0, 0, 0, edgeMinTime.Location())
startDate := time.Date(s.Year(), s.Month(), s.Day(), 0, 0, 0, 0, edgeMinTime.Location())
finishDate := time.Date(f.Year(), f.Month(), f.Day(), 0, 0, 0, 0, edgeMinTime.Location())
nowData := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, edgeMinTime.Location())
if tf != nil {
trueFinishDate = time.Date(tf.Year(), tf.Month(), tf.Day(), tf.Hour(), 0, 0, 0, edgeMinTime.Location())
}
fix(&startDate, edgeDate, fedgeData)
fix(&finishDate, edgeDate, fedgeData)
if tf != nil {
fix(&trueFinishDate, edgeDate, fedgeData)
}
if ts == nil {
// 无实际开始时间
if tf == nil {
// 无实际结束时间
if now.Before(*s) || now.Equal(*s) {
// 当前时间 <= 预计开始时间
data.RestTime = finishDate.Sub(startDate).Hours() / 24.0
data.Offset = startDate.Sub(edgeDate).Hours() / 24.0
} else if now.Before(*f) || now.Equal(*f) {
// 预计开始时间 < 当前时间 <= 预计结束时间
data.RestTime = finishDate.Sub(nowData).Hours() / 24.0
data.Offset = nowData.Sub(edgeDate).Hours() / 24.0
} else {
// 预计结束时间 < 当前时间
data.Delay = now.Sub(finishDate).Hours() / 24.0
data.Offset = finishDate.Sub(edgeDate).Hours() / 24.0
}
} else {
// 有实际结束时间
if tf.Before(*f) || tf.Equal(*f) {
// 实际结束时间 <= 预计结束时间
data.RestTime = finishDate.Sub(trueFinishDate).Hours() / 24.0
data.Offset = trueFinishDate.Sub(edgeDate).Hours() / 24.0
} else {
// 预计结束时间 < 实际结束时间
data.RestTime = finishDate.Sub(startDate).Hours() / 24.0
data.Delay = trueFinishDate.Sub(finishDate).Hours() / 24.0
data.Offset = startDate.Sub(edgeDate).Hours() / 24.0
}
}
} else {
// 有实际开始时间
trueStartDate := time.Date(ts.Year(), ts.Month(), ts.Day(), ts.Hour(), 0, 0, 0, edgeMinTime.Location())
fix(&trueStartDate, edgeDate, fedgeData)
if tf == nil {
// 无实际结束时间
if now.Before(*f) || now.Equal(*f) {
// 实际开始时间 <= 当前时间 <= 预计结束时间
data.ActualTime = nowData.Sub(trueStartDate).Hours() / 24.0
data.RestTime = finishDate.Sub(nowData).Hours() / 24.0
data.Offset = trueStartDate.Sub(edgeDate).Hours() / 24.0
} else {
if ts.Before(*f) || ts.Equal(*f) {
// 实际开始时间 < 预计结束时间 <= 当前时间
data.ActualTime = finishDate.Sub(trueStartDate).Hours() / 24.0
data.Delay = nowData.Sub(finishDate).Hours() / 24.0
data.Offset = trueStartDate.Sub(edgeDate).Hours() / 24.0
}
}
} else {
// 有实际结束时间
if tf.Before(*f) || tf.Equal(*f) {
// 实际开始时间 <= 实际结束时间 <= 计划结束时间
data.ActualTime = trueFinishDate.Sub(trueStartDate).Hours() / 24.0
data.RestTime = finishDate.Sub(trueFinishDate).Hours() / 24.0
data.Offset = trueStartDate.Sub(edgeDate).Hours() / 24.0
} else {
if ts.Before(*f) || ts.Equal(*f) {
// 实际开始时间 < 预计结束时间 <= 实际结束时间
data.ActualTime = finishDate.Sub(trueStartDate).Hours() / 24.0
data.Offset = trueStartDate.Sub(edgeDate).Hours() / 24.0
} else {
// 预计结束时间 <= 实际开始时间 < 实际结束时间
data.Offset = finishDate.Sub(edgeDate).Hours() / 24.0
}
data.Delay = trueFinishDate.Sub(finishDate).Hours() / 24.0
}
}
}
ElapsedHour := float64(i.ManHour.ElapsedTime) / (60.0 * 8.0)
data.Tooltip = fmt.Sprintf("%s (实际用时: %.1f天)", data.Tooltip, ElapsedHour)
return data
}
func (g *Gantt) setData(data []gantt.DataItem) {
g.Data = gantt.Data{List: data}
}
func (g *Gantt) setOperations() {
ops := make(map[apistructs.OperationKey]apistructs.Operation)
ops[gantt.OpChangePageNo] = apistructs.Operation{Key: gantt.OpChangePageNo.String(), Reload: true}
g.Operations = ops
}
func (g *Gantt) setDefaultState(operation apistructs.OperationKey) {
// default state
defaultState := gantt.State{
PageNo: gantt.DefaultPageNo,
PageSize: gantt.DefaultPageSize,
}
switch operation {
// global default state form url
case apistructs.InitializeOperation:
if urlQueryI, ok := g.CtxBdl.InParams[getStateUrlQueryKey()]; ok {
if urlQueryStr, ok := urlQueryI.(string); ok && urlQueryStr != "" {
var urlState gantt.State
b, err := base64.StdEncoding.DecodeString(urlQueryStr)
if err != nil {
logrus.Warnf("decode url query failed, request:%v, err:%v", urlQueryStr, err)
}
if err := json.Unmarshal(b, &urlState); err != nil {
logrus.Warnf("get url state failed, request:%v, err:%v", urlState, err)
}
if err == nil {
defaultState = urlState
}
}
}
}
g.State.PageSize = defaultState.PageSize
g.State.PageNo = defaultState.PageNo
}
func (g *Gantt) setStateTotal(total uint64) {
g.State.Total = total
}
func (g *Gantt) setUids(uids []string) {
uids = strutil.DedupSlice(uids, true)
g.Uids = uids
}