@@ -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,82 @@ 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+ mkey := "STATS_TRACKED_" + file + "_" + today .Format ("2006_01_02" )
162+ for i := 0 ; i < 4 ; i ++ {
163+ exists , _ := redis .Bool (rconn .Do ("EXISTS" , mkey ))
164+ if exists {
165+ mkeyLenMap [mkey ], _ = redis .Int (rconn .Do ("HLEN" , mkey ))
166+ log .Info ("mKey: " , mkey )
167+ log .Info (file , " len: " , mkeyLenMap [mkey ])
168+ mkeyFileMap [mkey ] = file
169+ // rconn.Send("HGETALL", mkey)
170+ mkey = mkey [:strings .LastIndex (mkey , "_" )]
171+ }
172+ }
173+ }
174+
175+ rconn .Send ("MULTI" )
176+ for mkey := range mkeyLenMap {
177+ rconn .Send ("HGETALL" , mkey )
178+ }
179+ stats , err = redis .Values (rconn .Do ("EXEC" ))
180+ if err != nil {
181+ log .Error ("Cannot fetch file per country stats: " + err .Error ())
182+ return
183+ }
184+
185+ pattern := regexp .MustCompile ("_" )
186+ index = 0
187+ for mkey , hashLen := range mkeyLenMap {
188+ for i := 0 ; i < 2 * hashLen ; i += 2 {
189+ hashSlice , _ := redis .ByteSlices (stats [index ], err )
190+ key , _ := redis .String (hashSlice [i ], err )
191+ value , _ := redis .Int (hashSlice [i + 1 ], err )
192+ log .Info ("mkey: " , mkey )
193+ log .Info ("File: " , mkeyFileMap [mkey ])
194+ log .Info ("Key: " , key )
195+ log .Info ("Value: " , value )
196+ sep := strings .Index (key , "_" )
197+ country := key [:sep ]
198+ mirrorID , err := strconv .Atoi (key [sep + 1 :])
199+ if err != nil {
200+ log .Error ("Failed to convert mirror ID: " , err )
201+ return
202+ }
203+ mirror := mirrorsMap [mirrorID ]
204+ duration := len (pattern .FindAllStringIndex (mkey , - 1 ))
205+ var durationStr string
206+ switch duration {
207+ case 2 :
208+ durationStr = "total"
209+ case 3 :
210+ durationStr = "year"
211+ case 4 :
212+ durationStr = "month"
213+ case 5 :
214+ durationStr = "day"
215+ }
216+ output += fmt .Sprintf ("stats_tracked_" +
217+ "%s{file=\" %s\" ,country=\" %s\" ,mirror=\" %s\" } %d\n " ,
218+ durationStr , mkeyFileMap [mkey ], country , mirror , value ,
219+ )
220+ }
221+ output += "\n "
222+ index ++
223+ }
224+
144225 w .Write ([]byte (output ))
145226}
0 commit comments