/
api_show_graph.go
127 lines (106 loc) · 2.6 KB
/
api_show_graph.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
package api
import (
"encoding/json"
"fmt"
"github.com/aleksaan/statusek/database"
"github.com/aleksaan/statusek/models"
_ "github.com/lib/pq"
)
type status struct {
id uint
object_id uint
status_name string
status_type string
}
type event struct {
id uint
status_id uint
}
type node struct {
Id uint `json:"id"`
Label string `json:"label"`
Color string `json:"color"`
}
type edge struct {
From uint `json:"from"`
To uint `json:"to"`
}
type graph struct {
Nodes []node `json:"nodes"`
Edges []edge `json:"edges"`
}
type state struct {
Counter int `json:"counter"`
Graph graph `json:"graph"`
}
var color_map = map[string]string{
"DEFAULT": "#98ff98",
"MANDATORY": "#606060",
"OPTIONAL": "#0066CC",
"FAILED": "#FF3333",
}
// var ApiGetGraph = func(w http.ResponseWriter, r *http.Request) {
// var resp = &tResponseBody{result: make(map[string]interface{})}
// apiCommonStart(r)
// _, params := apiCommonDecodeParams(r)
// var res = get_graph(params.InstanceToken)
// resp.result["res"] = res
// sendResponse(w, resp, rc.SUCCESS)
// }
func get_graph(instanceToken string) string {
db := database.DB
//select instance
instance := &models.Instance{}
instance.GetInstance(db, instanceToken, false)
//select statuses = nodes
var istatuses []models.Status
db.Where("object_id=?", instance.ObjectID).Find(&istatuses)
//events of instance
var ievents []models.Event
db.Where("instance_id=?", instance.ID).Find(&ievents)
g := graph{}
st := state{}
//fill statuses
statuses := []status{}
for _, row := range istatuses {
statuses = append(statuses, status{id: row.ID, object_id: row.ObjectID, status_name: row.StatusName, status_type: row.StatusType})
}
//fill events
events := []event{}
for _, ev := range ievents {
events = append(events, event{id: ev.ID, status_id: ev.StatusID})
}
//fill edges
g.Edges = []edge{}
iworkflows := []models.Workflow{}
for _, stat := range istatuses {
result := db.Where("status_prev_id=?", stat.ID).Find(&iworkflows)
if result.RowsAffected > 0 {
for _, wf := range iworkflows {
g.Edges = append(g.Edges, edge{wf.StatusPrevID, wf.StatusNextID})
}
}
}
//graph
st.Counter = len(statuses)
for i := range statuses {
s := statuses[i]
var node_color = color_map["DEFAULT"]
for _, el := range events {
if el.status_id == s.id {
node_color = color_map[s.status_type]
break
}
}
g.Nodes = append(g.Nodes, node{s.id, s.status_name, node_color})
}
st.Graph = g
// To json
s, err := json.Marshal(st)
if err != nil {
fmt.Println(err)
return err.Error()
}
res := string(s)
return res
}