-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.go
More file actions
73 lines (63 loc) · 1.7 KB
/
group.go
File metadata and controls
73 lines (63 loc) · 1.7 KB
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
package workflow
// Group represents a group of tasks in a workflow. It will give informations
// about the execution state like completion percentage, any error that occurred
// and the last message received from the tasks.
// Groups can be skipped if the command in skip_cmd from the yaml definition
// returns a zero status code.
type Group struct {
Id string `json:"id"`
Tasks []*Task `json:"tasks"`
skip_cmd string
Skip bool `json:"skip"`
Percent float64 `json:"percent"`
Started bool `json:"started"`
Finished bool `json:"finished"`
LastMessage string `json:"lastMessage"`
Error string `json:"error"`
}
func newGroup(y map[string]any) (*Group, error) {
id, ok := y["id"].(string)
if !ok {
return nil, WorkflowErrorGroupMissingId
}
skip_cmd, _ := y["skip_cmd"].(string)
tasks, ok := y["tasks"].([]any)
if !ok {
return nil, WorkflowErrorGroupMissingTasks
}
result := &Group{
Id: id,
Tasks: []*Task{},
skip_cmd: skip_cmd,
}
for i := range tasks {
task, err := newTask(tasks[i].(map[string]any))
if err != nil {
return nil, err
}
result.Tasks = append(result.Tasks, task)
}
return result, nil
}
func (w *Group) progress() (float64, float64) {
// Calculate total weight
total := 0.0
current := 0.0
finished := true
for i := range w.Tasks {
task := w.Tasks[i]
if task.Finished {
current += float64(task.Weight)
} else {
finished = false
current += float64(task.Weight) * task.Percent
}
total += float64(task.Weight)
}
w.Finished = finished
if total > 0 {
w.Percent = float64(current) / float64(total) * 100
}
// Make sure we don't return more than total
return min(current, total), total
}