-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmeshery_pattern_persister.go
93 lines (71 loc) · 2.34 KB
/
meshery_pattern_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
package models
import (
"encoding/json"
"fmt"
"strings"
"github.com/gofrs/uuid"
"github.com/layer5io/meshkit/database"
)
// MesheryPatternPersister is the persister for persisting
// performance profiles on the database
type MesheryPatternPersister struct {
DB *database.Handler
}
// MesheryPatternPage represents a page of performance profiles
type MesheryPatternPage struct {
Page uint64 `json:"page"`
PageSize uint64 `json:"page_size"`
TotalCount int `json:"total_count"`
Patterns []*MesheryPattern `json:"patterns"`
}
// GetMesheryPatterns returns all of the performance profiles
func (mpp *MesheryPatternPersister) GetMesheryPatterns(search, order string, page, pageSize uint64) ([]byte, error) {
if order == "" {
order = "updated_at desc"
}
count := int64(0)
patterns := []*MesheryPattern{}
query := mpp.DB.Order(order)
if search != "" {
like := "%" + strings.ToLower(search) + "%"
query = query.Where("(lower(meshery_patterns.name) like ?)", like)
}
query.Table("meshery_patterns").Count(&count)
Paginate(uint(page), uint(pageSize))(query).Find(&patterns)
mesheryPatternPage := &MesheryPatternPage{
Page: page,
PageSize: pageSize,
TotalCount: int(count),
Patterns: patterns,
}
return marshalMesheryPatternPage(mesheryPatternPage), nil
}
// DeleteMesheryPattern takes in a profile id and delete it if it already exists
func (mpp *MesheryPatternPersister) DeleteMesheryPattern(id uuid.UUID) ([]byte, error) {
pattern := MesheryPattern{ID: &id}
mpp.DB.Delete(&pattern)
return marshalMesheryPattern(&pattern), nil
}
func (mpp *MesheryPatternPersister) SaveMesheryPattern(pattern *MesheryPattern) ([]byte, error) {
if pattern.ID == nil {
id, err := uuid.NewV4()
if err != nil {
return nil, fmt.Errorf("failed to create ID for the pattern: %s", err)
}
pattern.ID = &id
}
return marshalMesheryPattern(pattern), mpp.DB.Save(pattern).Error
}
func (mpp *MesheryPatternPersister) GetMesheryPattern(id uuid.UUID) ([]byte, error) {
var mesheryPattern MesheryPattern
err := mpp.DB.First(&mesheryPattern, id).Error
return marshalMesheryPattern(&mesheryPattern), err
}
func marshalMesheryPatternPage(mpp *MesheryPatternPage) []byte {
res, _ := json.Marshal(mpp)
return res
}
func marshalMesheryPattern(mp *MesheryPattern) []byte {
res, _ := json.Marshal(mp)
return res
}