| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package http |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/etix/mirrorbits/database" |
| 15 |
"github.com/etix/mirrorbits/filesystem" |
| 16 |
"github.com/etix/mirrorbits/mirrors" |
| 17 |
) |
| 18 |
|
| 19 |
/* |
| 20 |
Total (all files, all mirrors): |
| 21 |
STATS_TOTAL |
| 22 |
|
| 23 |
List of hashes for a file: |
| 24 |
STATS_FILE = path -> value All time |
| 25 |
STATS_FILE_[year] = path -> value By year |
| 26 |
STATS_FILE_[year]_[month] = path -> value By month |
| 27 |
STATS_FILE_[year]_[month]_[day] = path -> value By day |
| 28 |
|
| 29 |
List of hashes for a mirror: |
| 30 |
STATS_MIRROR = mirror -> value All time |
| 31 |
STATS_MIRROR_[year] = mirror -> value By year |
| 32 |
STATS_MIRROR_[year]_[month] = mirror -> value By month |
| 33 |
STATS_MIRROR_[year]_[month]_[day] = mirror -> value By day |
| 34 |
*/ |
| 35 |
|
| 36 |
var ( |
| 37 |
errEmptyFileError = errors.New("stats: file parameter is empty") |
| 38 |
errUnknownMirror = errors.New("stats: unknown mirror") |
| 39 |
) |
| 40 |
|
| 41 |
// Stats is the internal structure for the download stats |
| 42 |
type Stats struct { |
| 43 |
r *database.Redis |
| 44 |
countChan chan countItem |
| 45 |
mapStats map[string]int64 |
| 46 |
stop chan bool |
| 47 |
wg sync.WaitGroup |
| 48 |
downgraded bool |
| 49 |
} |
| 50 |
|
| 51 |
type countItem struct { |
| 52 |
mirrorID int |
| 53 |
filepath string |
| 54 |
size int64 |
| 55 |
time time.Time |
| 56 |
} |
| 57 |
|
| 58 |
// NewStats returns an instance of the stats counter |
| 59 |
func NewStats(redis *database.Redis) *Stats { |
| 60 |
s := &Stats{ |
| 61 |
r: redis, |
| 62 |
countChan: make(chan countItem, 1000), |
| 63 |
mapStats: make(map[string]int64), |
| 64 |
stop: make(chan bool), |
| 65 |
} |
| 66 |
go s.processCountDownload() |
| 67 |
return s |
| 68 |
} |
| 69 |
|
| 70 |
// Terminate stops the stats handler and commit results to the database |
| 71 |
func (s *Stats) Terminate() { |
| 72 |
close(s.stop) |
| 73 |
log.Notice("Saving stats") |
| 74 |
s.wg.Wait() |
| 75 |
} |
| 76 |
|
| 77 |
// CountDownload is a lightweight method used to count a new download for a specific file and mirror |
| 78 |
func (s *Stats) CountDownload(m mirrors.Mirror, fileinfo filesystem.FileInfo) error { |
| 79 |
if m.Name == "" { |
| 80 |
return errUnknownMirror |
| 81 |
} |
| 82 |
if fileinfo.Path == "" { |
| 83 |
return errEmptyFileError |
| 84 |
} |
| 85 |
|
| 86 |
s.countChan <- countItem{m.ID, fileinfo.Path, fileinfo.Size, time.Now().UTC()} |
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
// Process all stacked download messages |
| 91 |
func (s *Stats) processCountDownload() { |
| 92 |
s.wg.Add(1) |
| 93 |
pushTicker := time.NewTicker(500 * time.Millisecond) |
| 94 |
|
| 95 |
for { |
| 96 |
select { |
| 97 |
case <-s.stop: |
| 98 |
s.pushStats() |
| 99 |
s.wg.Done() |
| 100 |
return |
| 101 |
case c := <-s.countChan: |
| 102 |
date := c.time.Format("2006_01_02|") // Includes separator |
| 103 |
s.mapStats["f"+date+c.filepath]++ |
| 104 |
s.mapStats["m"+date+strconv.Itoa(c.mirrorID)]++ |
| 105 |
s.mapStats["s"+date+strconv.Itoa(c.mirrorID)] += c.size |
| 106 |
case <-pushTicker.C: |
| 107 |
s.pushStats() |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// Push the resulting stats on redis |
| 113 |
func (s *Stats) pushStats() { |
| 114 |
if len(s.mapStats) <= 0 { |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
rconn := s.r.Get() |
| 119 |
defer rconn.Close() |
| 120 |
|
| 121 |
if rconn.Err() != nil { |
| 122 |
if s.downgraded == false { |
| 123 |
log.Warningf("Uncommited stats kept in-memory: %v", rconn.Err()) |
| 124 |
} |
| 125 |
|
| 126 |
s.downgraded = true |
| 127 |
return |
| 128 |
} |
| 129 |
|
| 130 |
rconn.Send("MULTI") |
| 131 |
|
| 132 |
for k, v := range s.mapStats { |
| 133 |
if v == 0 { |
| 134 |
continue |
| 135 |
} |
| 136 |
|
| 137 |
separator := strings.Index(k, "|") |
| 138 |
if separator <= 0 { |
| 139 |
log.Critical("Stats: separator not found") |
| 140 |
continue |
| 141 |
} |
| 142 |
typ := k[:1] |
| 143 |
date := k[1:separator] |
| 144 |
object := k[separator+1:] |
| 145 |
|
| 146 |
if typ == "f" { |
| 147 |
// File |
| 148 |
|
| 149 |
fkey := fmt.Sprintf("STATS_FILE_%s", date) |
| 150 |
|
| 151 |
for i := 0; i < 4; i++ { |
| 152 |
rconn.Send("HINCRBY", fkey, object, v) |
| 153 |
fkey = fkey[:strings.LastIndex(fkey, "_")] |
| 154 |
} |
| 155 |
|
| 156 |
// Increase the total too |
| 157 |
rconn.Send("INCRBY", "STATS_TOTAL", v) |
| 158 |
} else if typ == "m" { |
| 159 |
// Mirror |
| 160 |
|
| 161 |
mkey := fmt.Sprintf("STATS_MIRROR_%s", date) |
| 162 |
|
| 163 |
for i := 0; i < 4; i++ { |
| 164 |
rconn.Send("HINCRBY", mkey, object, v) |
| 165 |
mkey = mkey[:strings.LastIndex(mkey, "_")] |
| 166 |
} |
| 167 |
} else if typ == "s" { |
| 168 |
// Bytes |
| 169 |
|
| 170 |
mkey := fmt.Sprintf("STATS_MIRROR_BYTES_%s", date) |
| 171 |
|
| 172 |
for i := 0; i < 4; i++ { |
| 173 |
rconn.Send("HINCRBY", mkey, object, v) |
| 174 |
mkey = mkey[:strings.LastIndex(mkey, "_")] |
| 175 |
} |
| 176 |
} else { |
| 177 |
log.Warning("Stats: unknown type", typ) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
_, err := rconn.Do("EXEC") |
| 182 |
|
| 183 |
if err != nil { |
| 184 |
log.Errorf("Stats: could not save stats to redis: %s", err.Error()) |
| 185 |
return |
| 186 |
} |
| 187 |
|
| 188 |
s.downgraded = false |
| 189 |
|
| 190 |
// Clear the map |
| 191 |
s.mapStats = make(map[string]int64) |
| 192 |
} |