@@ -3,6 +3,9 @@ package http
33import (
44 "fmt"
55 "net/http"
6+ "regexp"
7+ "strconv"
8+ "strings"
69 "time"
710
811 "github.com/gomodule/redigo/redis"
@@ -15,17 +18,18 @@ type metrics struct {
1518 Total int
1619}
1720
18- func statsToPrometheusFormat (metrics metrics , labelName string , labelValue string ) string {
21+ func statsToPrometheusFormat (metrics metrics , label_name string ,
22+ label_value string ) string {
1923 var output string
2024
2125 output += fmt .Sprintf ("%s_total{%s=\" %s\" } %d\n " ,
22- labelName , labelName , labelValue , metrics .Total )
26+ label_name , label_name , label_value , metrics .Total )
2327 output += fmt .Sprintf ("%s_day{%s=\" %s\" } %d\n " ,
24- labelName , labelName , labelValue , metrics .Day )
28+ label_name , label_name , label_value , metrics .Day )
2529 output += fmt .Sprintf ("%s_month{%s=\" %s\" } %d\n " ,
26- labelName , labelName , labelValue , metrics .Month )
30+ label_name , label_name , label_value , metrics .Month )
2731 output += fmt .Sprintf ("%s_year{%s=\" %s\" } %d\n \n " ,
28- labelName , labelName , labelValue , metrics .Year )
32+ label_name , label_name , label_value , metrics .Year )
2933
3034 return output
3135}
@@ -141,5 +145,83 @@ func (h *HTTP) metricsHandler(w http.ResponseWriter, r *http.Request) {
141145 index += 4
142146 }
143147
148+ // Get all tracked files
149+ var trackedFilesList []string
150+ mkeyLenMap := make (map [string ]int )
151+ mkeyFileMap := make (map [string ]string )
152+ trackedFilesList , err = h .redis .GetListOfTrackedFiles ()
153+ if err != nil {
154+ log .Error ("Cannot fetch list of files: " + err .Error ())
155+ return
156+ }
157+
158+ // Get tracked files downloads per country and mirror
159+ // rconn.Send("MULTI")
160+ for _ , file := range trackedFilesList {
161+ log .Info ("Tracked files: " , file )
162+ mkey := "STATS_TRACKED_" + file + "_" + today .Format ("2006_01_02" )
163+ for i := 0 ; i < 4 ; i ++ {
164+ exists , _ := redis .Bool (rconn .Do ("EXISTS" , mkey ))
165+ if exists {
166+ mkeyLenMap [mkey ], _ = redis .Int (rconn .Do ("HLEN" , mkey ))
167+ log .Info ("mKey: " , mkey )
168+ log .Info (file , " len: " , mkeyLenMap [mkey ])
169+ mkeyFileMap [mkey ] = file
170+ // rconn.Send("HGETALL", mkey)
171+ mkey = mkey [:strings .LastIndex (mkey , "_" )]
172+ }
173+ }
174+ }
175+
176+ rconn .Send ("MULTI" )
177+ for mkey := range mkeyLenMap {
178+ rconn .Send ("HGETALL" , mkey )
179+ }
180+ stats , err = redis .Values (rconn .Do ("EXEC" ))
181+ if err != nil {
182+ log .Error ("Cannot fetch file per country stats: " + err .Error ())
183+ return
184+ }
185+
186+ pattern := regexp .MustCompile ("_" )
187+ index = 0
188+ for mkey , hashLen := range mkeyLenMap {
189+ for i := 0 ; i < 2 * hashLen ; i += 2 {
190+ hashSlice , _ := redis .ByteSlices (stats [index ], err )
191+ key , _ := redis .String (hashSlice [i ], err )
192+ value , _ := redis .Int (hashSlice [i + 1 ], err )
193+ log .Info ("mkey: " , mkey )
194+ log .Info ("File: " , mkeyFileMap [mkey ])
195+ log .Info ("Key: " , key )
196+ log .Info ("Value: " , value )
197+ sep := strings .Index (key , "_" )
198+ country := key [:sep ]
199+ mirrorID , err := strconv .Atoi (key [sep + 1 :])
200+ if err != nil {
201+ log .Error ("Failed to convert mirror ID: " , err )
202+ return
203+ }
204+ mirror := mirrorsMap [mirrorID ]
205+ duration := len (pattern .FindAllStringIndex (mkey , - 1 ))
206+ var durationStr string
207+ switch duration {
208+ case 2 :
209+ durationStr = "total"
210+ case 3 :
211+ durationStr = "year"
212+ case 4 :
213+ durationStr = "month"
214+ case 5 :
215+ durationStr = "day"
216+ }
217+ output += fmt .Sprintf ("stats_tracked_" +
218+ "%s{file=\" %s\" ,country=\" %s\" ,mirror=\" %s\" } %d\n " ,
219+ durationStr , mkeyFileMap [mkey ], country , mirror , value ,
220+ )
221+ }
222+ output += "\n "
223+ index ++
224+ }
225+
144226 w .Write ([]byte (output ))
145227}
0 commit comments