-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache_utils.go
102 lines (81 loc) · 2.08 KB
/
cache_utils.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/fbessez/octo-org/models"
"github.com/fbessez/octo-org/rediscli"
)
const (
repo_stats_file = "./tmp/repo_name_to_stats.json"
user_stats_file = "./tmp/user_name_to_stats.json"
)
func writeRepoStats(orgStats *models.OrgStats) (err error) {
f, err := os.Create(repo_stats_file)
defer f.Close()
check(err)
bytes, err := json.Marshal(orgStats)
n2, err := f.Write(bytes)
fmt.Printf("wrote %d bytes", n2)
return
}
func readRepoStats() (orgStats *models.OrgStats, err error) {
jsonFile, err := os.Open(repo_stats_file)
defer jsonFile.Close()
check(err)
bytes, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(bytes, &orgStats)
return orgStats, nil
}
func writeUserStats(userStats *models.OrgStatsByUser) (err error) {
f, err := os.Create(user_stats_file)
defer f.Close()
check(err)
bytes, err := json.Marshal(userStats)
n2, err := f.Write(bytes)
fmt.Printf("wrote %d bytes", n2)
return
}
func readUserStats() (userStats *models.OrgStatsByUser, err error) {
jsonFile, err := os.Open(user_stats_file)
defer jsonFile.Close()
check(err)
bytes, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(bytes, &userStats)
return userStats, nil
}
func writeRepoNames(repoNames []string) {
for _, repoName := range repoNames {
err := rediscli.SetAdd(redisKeyRepoNames, repoName)
if err != nil {
fmt.Println("Setting "+repoName+"to "+redisKeyRepoNames+"failed miserably", err)
continue
}
}
}
func readRepoNames() (repoNames []string, err error) {
repoNames, err = rediscli.GetSetMembers(redisKeyRepoNames)
check(err)
return
}
func getForceRefreshValue(req *http.Request) bool {
forceRefresh, err := strconv.ParseBool(req.URL.Query().Get("forceRefresh"))
if forceRefresh != true || err != nil {
if !fileExists(repo_stats_file) || !fileExists(user_stats_file) {
forceRefresh = true
} else {
forceRefresh = false
}
}
return forceRefresh
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}