forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
162 lines (126 loc) · 3.78 KB
/
cache.go
File metadata and controls
162 lines (126 loc) · 3.78 KB
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
152
153
154
155
156
157
158
159
160
161
162
package adminanalytics
import (
"context"
"encoding/json"
"math/rand"
"time"
"github.com/gomodule/redigo/redis"
"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/featureflag"
"github.com/sourcegraph/sourcegraph/internal/redispool"
)
var (
pool = redispool.Store
scopeKey = "adminanalytics:"
cacheDisabledInTest = false
)
func getArrayFromCache[K interface{}](cacheKey string) ([]*K, error) {
rdb := pool.Get()
defer rdb.Close()
data, err := redis.String(rdb.Do("GET", scopeKey+cacheKey))
if err != nil {
return nil, err
}
nodes := make([]*K, 0)
if err = json.Unmarshal([]byte(data), &nodes); err != nil {
return nodes, err
}
return nodes, nil
}
func getItemFromCache[T interface{}](cacheKey string) (*T, error) {
rdb := pool.Get()
defer rdb.Close()
data, err := redis.String(rdb.Do("GET", scopeKey+cacheKey))
if err != nil {
return nil, err
}
var summary T
if err = json.Unmarshal([]byte(data), &summary); err != nil {
return &summary, err
}
return &summary, nil
}
func setDataToCache(key string, data string) (bool, error) {
if cacheDisabledInTest {
return true, nil
}
rdb := pool.Get()
defer rdb.Close()
if _, err := rdb.Do("SET", key, data); err != nil {
return false, err
}
if _, err := rdb.Do("EXPIRE", key, int64(24*time.Hour/time.Second)); err != nil {
return false, err
}
return true, nil
}
func setArrayToCache[T interface{}](cacheKey string, nodes []*T) (bool, error) {
data, err := json.Marshal(nodes)
if err != nil {
return false, err
}
return setDataToCache(scopeKey+cacheKey, string(data))
}
func setItemToCache[T interface{}](cacheKey string, summary *T) (bool, error) {
data, err := json.Marshal(summary)
if err != nil {
return false, err
}
return setDataToCache(scopeKey+cacheKey, string(data))
}
var dateRanges = []string{LastThreeMonths, LastMonth, LastWeek}
var groupBys = []string{Weekly, Daily}
type CacheAll interface {
CacheAll(ctx context.Context) error
}
func refreshAnalyticsCache(ctx context.Context, db database.DB) error {
for _, dateRange := range dateRanges {
for _, groupBy := range groupBys {
stores := []CacheAll{
&Search{DateRange: dateRange, Grouping: groupBy, DB: db, Cache: true},
&Users{DateRange: dateRange, Grouping: groupBy, DB: db, Cache: true},
&Notebooks{DateRange: dateRange, Grouping: groupBy, DB: db, Cache: true},
&CodeIntel{DateRange: dateRange, Grouping: groupBy, DB: db, Cache: true},
&Repos{DB: db, Cache: true},
&BatchChanges{Grouping: groupBy, DateRange: dateRange, DB: db, Cache: true},
&Extensions{Grouping: groupBy, DateRange: dateRange, DB: db, Cache: true},
&CodeInsights{Grouping: groupBy, DateRange: dateRange, DB: db, Cache: true},
}
for _, store := range stores {
if err := store.CacheAll(ctx); err != nil {
return err
}
}
}
_, err := GetCodeIntelByLanguage(ctx, db, true, dateRange)
if err != nil {
return err
}
_, err = GetCodeIntelTopRepositories(ctx, db, true, dateRange)
if err != nil {
return err
}
}
return nil
}
var started bool
func StartAnalyticsCacheRefresh(ctx context.Context, db database.DB) {
logger := log.Scoped("adminanalytics:cache-refresh", "admin analytics cache refresh")
if started {
panic("already started")
}
started = true
ctx = featureflag.WithFlags(ctx, db.FeatureFlags())
const delay = 24 * time.Hour
for {
if !featureflag.FromContext(ctx).GetBoolOr("admin-analytics-disabled", false) {
if err := refreshAnalyticsCache(ctx, db); err != nil {
logger.Error("Error refreshing admin analytics cache", log.Error(err))
}
}
// Randomize sleep to prevent thundering herds.
randomDelay := time.Duration(rand.Intn(600)) * time.Second
time.Sleep(delay + randomDelay)
}
}