-
Notifications
You must be signed in to change notification settings - Fork 9
/
services.go
118 lines (89 loc) · 4.61 KB
/
services.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
package db
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/redhatinsights/platform-changelog-go/internal/config"
l "github.com/redhatinsights/platform-changelog-go/internal/logging"
"github.com/redhatinsights/platform-changelog-go/internal/metrics"
"github.com/redhatinsights/platform-changelog-go/internal/models"
"github.com/redhatinsights/platform-changelog-go/internal/structs"
)
func (conn *DBConnectorImpl) CreateServiceTableEntry(s *models.Services) error {
results := conn.db.Create(s)
return evaluateError(results.Error)
}
func (conn *DBConnectorImpl) UpdateServiceTableEntry(name string, s config.Service) (service models.Services, err error) {
newService := models.Services{Name: name, DisplayName: s.DisplayName, Tenant: s.Tenant}
results := conn.db.Model(models.Services{}).Where("name = ?", name).Updates(&newService)
return newService, evaluateError(results.Error)
}
func (conn *DBConnectorImpl) DeleteServiceTableEntry(name string) (models.Services, error) {
// save the service to delete the timelines
service, _, _ := conn.GetServiceByName(name)
results := conn.db.Model(models.Services{}).Where("name = ?", name).Delete(&models.Services{})
if results.Error != nil {
return models.Services{}, evaluateError(results.Error)
}
// delete the timelines for the service
err := conn.DeleteTimelinesByService(service)
if err != nil {
return models.Services{}, evaluateError(err)
}
return service, nil
}
func (conn *DBConnectorImpl) GetServicesAll(offset int, limit int, q structs.Query) ([]structs.ExpandedServicesData, int64, error) {
callDurationTimer := prometheus.NewTimer(metrics.SqlGetServicesAll)
defer callDurationTimer.ObserveDuration()
var count int64
var services []structs.ExpandedServicesData
db := conn.db.Model(models.Services{})
if len(q.Name) > 0 {
db = db.Where("services.name IN ?", q.Name)
}
if len(q.DisplayName) > 0 {
db = db.Where("services.display_name IN ?", q.DisplayName)
}
if len(q.Tenant) > 0 {
db = db.Where("services.tenant IN ?", q.Tenant)
}
// Uses the Services model here to reflect the proper db relation
db.Model(models.Services{}).Count(&count)
// TODO: add a sort_by field to the query struct
result := db.Order("ID desc").Preload("Projects").Limit(limit).Offset(offset).Scan(&services)
var servicesWithTimelines []structs.ExpandedServicesData
for i := 0; i < len(services); i++ {
s, _, _ := conn.GetLatest(services[i])
servicesWithTimelines = append(servicesWithTimelines, s)
}
return servicesWithTimelines, count, evaluateError(result.Error)
}
func (conn *DBConnectorImpl) GetLatest(service structs.ExpandedServicesData) (structs.ExpandedServicesData, error, error) {
l.Log.Debugf("Query name: %s", service.Name)
// TODO: Make one query to get the latest commit and deploy for each service
comResult := conn.db.Model(models.Timelines{}).Select("*").Joins("JOIN services ON timelines.service_id = services.id").Where("services.name = ?", service.Name).Where("timelines.type = ?", "commit").Order("Timestamp desc").Limit(1).Find(&service.Commit)
depResult := conn.db.Model(models.Timelines{}).Select("*").Joins("JOIN services ON timelines.service_id = services.id").Where("services.name = ?", service.Name).Where("timelines.type = ?", "deploy").Order("Timestamp desc").Limit(1).Find(&service.Deploy)
return service, evaluateError(comResult.Error), evaluateError(depResult.Error)
}
func (conn *DBConnectorImpl) GetServiceNames() ([]string, error) {
var names []string
result := conn.db.Model(models.Services{}).Pluck("name", &names)
return names, evaluateError(result.Error)
}
func (conn *DBConnectorImpl) GetServiceByID(id int) (models.Services, int64, error) {
callDurationTimer := prometheus.NewTimer(metrics.SqlGetServiceByID)
defer callDurationTimer.ObserveDuration()
var service models.Services
result := conn.db.Model(models.Services{}).Preload("Projects").Where("services.id = ?", id).First(&service)
return service, result.RowsAffected, evaluateError(result.Error)
}
func (conn *DBConnectorImpl) GetServiceByName(name string) (models.Services, int64, error) {
callDurationTimer := prometheus.NewTimer(metrics.SqlGetServiceByName)
defer callDurationTimer.ObserveDuration()
var service models.Services
result := conn.db.Model(models.Services{}).Preload("Projects").Where("services.name = ?", name).First(&service)
return service, result.RowsAffected, evaluateError(result.Error)
}
func (conn *DBConnectorImpl) GetServiceByRepo(repo string) (models.Services, error) {
var service models.Services
result := conn.db.Model(models.Services{}).Preload("Projects").Where("repo = ?", repo).First(&service)
return service, evaluateError(result.Error)
}