forked from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshery_results_persister.go
151 lines (121 loc) · 4.4 KB
/
meshery_results_persister.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
package models
import (
"encoding/json"
"time"
"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/database"
"github.com/sirupsen/logrus"
)
type MesheryResultsPersister struct {
DB *database.Handler
}
// MesheryResultPage - represents a page of meshery results
type MesheryResultPage struct {
Page uint64 `json:"page"`
PageSize uint64 `json:"page_size"`
TotalCount int `json:"total_count"`
Results []*MesheryResult `json:"results"`
}
type localMesheryResultDBRepresentation struct {
ID uuid.UUID `json:"meshery_id,omitempty"`
Name string `json:"name,omitempty"`
Mesh string `json:"mesh,omitempty"`
PerformanceProfile *uuid.UUID `json:"performance_profile,omitempty"`
Result []byte `json:"runner_results,omitempty" gorm:"type:JSONB"`
ServerMetrics interface{} `json:"server_metrics,omitempty" gorm:"type:JSONB"`
ServerBoardConfig interface{} `json:"server_board_config,omitempty" gorm:"type:JSONB"`
TestStartTime *time.Time `json:"test_start_time,omitempty"`
PerformanceProfileInfo PerformanceProfile `json:"-" gorm:"constraint:OnDelete:SET NULL;foreignKey:PerformanceProfile"`
}
func (mrp *MesheryResultsPersister) GetResults(page, pageSize uint64, profileID string) ([]byte, error) {
var res []*localMesheryResultDBRepresentation
var count int64
query := mrp.DB.Where("performance_profile = ?", profileID)
err := query.Table("meshery_results").Count(&count).Error
if err != nil {
return nil, err
}
err = Paginate(uint(page), uint(pageSize))(query).Find(&res).Error
resultPage := &MesheryResultPage{
Page: page,
PageSize: pageSize,
TotalCount: int(count),
Results: convertLocalRepresentationSliceToMesheryResultSlice(res),
}
return marshalMesheryResultsPage(resultPage), err
}
func (mrp *MesheryResultsPersister) GetAllResults(page, pageSize uint64) ([]byte, error) {
var res []*localMesheryResultDBRepresentation
var count int64
query := mrp.DB.Table("meshery_results")
err := query.Count(&count).Error
if err != nil {
return nil, err
}
err = Paginate(uint(page), uint(pageSize))(query).Find(&res).Error
resultPage := &MesheryResultPage{
Page: page,
PageSize: pageSize,
TotalCount: int(count),
Results: convertLocalRepresentationSliceToMesheryResultSlice(res),
}
return marshalMesheryResultsPage(resultPage), err
}
func (mrp *MesheryResultsPersister) GetResult(key uuid.UUID) (*MesheryResult, error) {
var lres localMesheryResultDBRepresentation
err := mrp.DB.Table("meshery_results").Find(&lres).Where("id = ?", key).Error
res := convertLocalRepresentationToMesheryResult(&lres)
return res, err
}
func (mrp *MesheryResultsPersister) WriteResult(key uuid.UUID, result []byte) error {
var data MesheryResult
if err := json.Unmarshal(result, &data); err != nil {
return err
}
data.ID = key
t := time.Now()
data.TestStartTime = &t
return mrp.DB.Table("meshery_results").Save(convertMesheryResultToLocalRepresentation(&data)).Error
}
func marshalMesheryResultsPage(mrp *MesheryResultPage) []byte {
res, _ := json.Marshal(mrp)
return res
}
func convertLocalRepresentationSliceToMesheryResultSlice(local []*localMesheryResultDBRepresentation) (res []*MesheryResult) {
for _, val := range local {
res = append(res, convertLocalRepresentationToMesheryResult(val))
}
return
}
func convertLocalRepresentationToMesheryResult(local *localMesheryResultDBRepresentation) *MesheryResult {
var jsonmap map[string]interface{}
if err := json.Unmarshal(local.Result, &jsonmap); err != nil {
logrus.Error(err)
return nil
}
res := &MesheryResult{
ID: local.ID,
Name: local.Name,
Mesh: local.Mesh,
PerformanceProfile: local.PerformanceProfile,
Result: jsonmap,
ServerMetrics: local.ServerMetrics,
ServerBoardConfig: local.ServerMetrics,
TestStartTime: local.TestStartTime,
}
return res
}
func convertMesheryResultToLocalRepresentation(mr *MesheryResult) *localMesheryResultDBRepresentation {
byt, _ := json.Marshal(mr.Result)
res := &localMesheryResultDBRepresentation{
ID: mr.ID,
Name: mr.Name,
Mesh: mr.Mesh,
PerformanceProfile: mr.PerformanceProfile,
Result: byt,
ServerMetrics: mr.ServerMetrics,
ServerBoardConfig: mr.ServerMetrics,
TestStartTime: mr.TestStartTime,
}
return res
}