forked from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_profiles_persister.go
110 lines (97 loc) · 3.12 KB
/
test_profiles_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
package models
import (
"encoding/json"
"time"
"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/database"
SMP "github.com/layer5io/service-mesh-performance/spec"
)
// TestProfilesPersister assists with persisting session in store
type TestProfilesPersister struct {
DB *database.Handler
}
type PerformanceTestConfig struct {
ID uuid.UUID
PerformanceTestConfigBytes []byte
UpdatedAt time.Time
}
// UserTestProfiles - represents a page of user test configs
type UserTestProfiles struct {
Page uint64 `json:"page"`
PageSize uint64 `json:"page_size"`
TotalCount int `json:"total_count"`
TestConfigs []*SMP.PerformanceTestConfig `json:"test_configs"`
}
// GetTestConfigs - gets result for the page and pageSize
func (s *TestProfilesPersister) GetTestConfigs(page, pageSize uint64) ([]byte, error) {
if s.DB == nil {
return nil, ErrDBConnection
}
order := "updated_at desc"
query := s.DB.Order(order)
total := int64(0)
s.DB.Model(&PerformanceTestConfig{}).Count(&total)
testConfigs := []*SMP.PerformanceTestConfig{}
var p []*PerformanceTestConfig
Paginate(uint(page), uint(pageSize))(query).Find(&p)
for _, config := range p {
var testConfig SMP.PerformanceTestConfig
err := json.Unmarshal(config.PerformanceTestConfigBytes, &testConfig)
if err == nil {
testConfigs = append(testConfigs, &testConfig)
}
}
bd, err := json.Marshal(&UserTestProfiles{
Page: page,
PageSize: pageSize,
TotalCount: int(total),
TestConfigs: testConfigs,
})
if err != nil {
return nil, ErrMarshal(err, "result data")
}
return bd, nil
}
// GetTestConfig - gets result for a specific key
func (s *TestProfilesPersister) GetTestConfig(key uuid.UUID) (*SMP.PerformanceTestConfig, error) {
if s.DB == nil {
return nil, ErrDBConnection
}
testConfig := &SMP.PerformanceTestConfig{}
var u PerformanceTestConfig
err := s.DB.Model(&PerformanceTestConfig{}).Where("id = ?", key).First(&u).Error
if err != nil {
return nil, err
}
err = json.Unmarshal(u.PerformanceTestConfigBytes, &testConfig)
if err != nil {
return nil, err
}
return testConfig, nil
}
// DeleteTestConfig - delete result for a specific key
func (s *TestProfilesPersister) DeleteTestConfig(key uuid.UUID) error {
if s.DB == nil {
return ErrDBConnection
}
return s.DB.Model(&PerformanceTestConfig{}).Where("id = ?", key).Delete(&PerformanceTestConfig{}).Error
}
// WriteTestConfig persists the result
func (s *TestProfilesPersister) WriteTestConfig(key uuid.UUID, result []byte) error {
if s.DB == nil {
return ErrDBConnection
}
if result == nil {
return ErrResultData()
}
var p PerformanceTestConfig
if err := s.DB.Model(&PerformanceTestConfig{}).Where("id = ?", key).First(&p).Error; err == nil {
return s.UpdateTestConfig(key, p)
}
p.ID = key
p.PerformanceTestConfigBytes = result
return s.DB.Model(&PerformanceTestConfig{}).Create(&p).Error
}
func (s *TestProfilesPersister) UpdateTestConfig(key uuid.UUID, p PerformanceTestConfig) error {
return s.DB.Model(&PerformanceTestConfig{}).Where("id = ?", key).UpdateColumns(p).Error
}