/
metrics.go
105 lines (93 loc) · 2.68 KB
/
metrics.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
package dao
import (
"fmt"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
type IntrospectionCount struct {
Introspected int64
Missed int64
}
type metricsDaoImpl struct {
db *gorm.DB
}
func GetMetricsDao(db *gorm.DB) MetricsDao {
if db == nil {
return nil
}
return metricsDaoImpl{
db: db,
}
}
func (d metricsDaoImpl) RepositoriesCount() int {
// select COUNT(*) from repositories ;
var output int64 = -1
d.db.
Model(&models.Repository{}).
Count(&output)
return int(output)
}
func (d metricsDaoImpl) RepositoryConfigsCount() int {
// select COUNT(*) from repository_configurations ;
var output int64 = -1
d.db.
Model(&models.RepositoryConfiguration{}).
Count(&output)
return int(output)
}
func (d metricsDaoImpl) OrganizationTotal() int64 {
var output int64 = -1
tx := d.db.
Model(&models.RepositoryConfiguration{}).Group("org_id").
Count(&output)
if tx.Error != nil {
log.Error().Err(tx.Error).Msg("Cannot calculate OrganizationTotal")
}
return output
}
func (d metricsDaoImpl) RepositoriesIntrospectionCount(hours int, public bool) IntrospectionCount {
// select COUNT(*)
// from repositories
// where public
// and failed_introspections_count <= FailedIntrospectionsLimit
// and (last_introspection_time < NOW() - INTERVAL '24 hours' or last_introspection_time is NULL);
output := IntrospectionCount{
Introspected: -1,
Missed: -1,
}
interval := fmt.Sprintf("%v hours", hours)
publicClause := "not public"
if public {
publicClause = "public"
}
tx := d.db.Model(&models.Repository{}).
Where(publicClause).Where(d.db.Where("last_introspection_time is NULL and last_introspection_status != ?", config.StatusPending).
Where("failed_introspections_count <= ?", config.FailedIntrospectionsLimit).
Or("last_introspection_time < NOW() - cast(? as INTERVAL)", interval)).
Count(&output.Missed)
if tx.Error != nil {
log.Logger.Err(tx.Error).Msg("error")
}
tx = d.db.Model(&models.Repository{}).
Where(publicClause).Where("last_introspection_time >= NOW() - cast(? as INTERVAL)", interval).
Count(&output.Introspected)
if tx.Error != nil {
log.Logger.Err(tx.Error).Msg("Error")
}
return output
}
func (d metricsDaoImpl) PublicRepositoriesFailedIntrospectionCount() int {
// select COUNT(*)
// from repositories
// where public
// and last_introspection_status in ('Invalid','Unavailable');
var output int64 = -1
d.db.
Model(&models.Repository{}).
Where("public").
Where("last_introspection_status in (?, ?)", config.StatusInvalid, config.StatusUnavailable).
Count(&output)
return int(output)
}