2023-09-11 17:09:08 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
|
|
)
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
type serveStats struct {
|
2023-09-11 17:09:08 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
list []string
|
|
|
|
count map[string]uint32
|
|
|
|
size map[string]string
|
|
|
|
times map[string][]string
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
type publicServeStats struct {
|
2023-09-11 17:09:08 +00:00
|
|
|
List []string
|
|
|
|
Count map[string]uint32
|
|
|
|
Size map[string]string
|
|
|
|
Times map[string][]string
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
type timesServed struct {
|
|
|
|
File string
|
|
|
|
Served uint32
|
|
|
|
Size string
|
|
|
|
Times []string
|
|
|
|
}
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
func (stats *serveStats) incrementCounter(file string, timestamp time.Time, filesize string) {
|
|
|
|
stats.mutex.Lock()
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.count[file]++
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.times[file] = append(stats.times[file], timestamp.Format(logDate))
|
|
|
|
|
|
|
|
_, exists := stats.size[file]
|
2023-09-11 17:09:08 +00:00
|
|
|
if !exists {
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.size[file] = filesize
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
if !contains(stats.list, file) {
|
|
|
|
stats.list = append(stats.list, file)
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.mutex.Unlock()
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
func (stats *serveStats) Import(source *publicServeStats) {
|
|
|
|
stats.mutex.Lock()
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
copy(stats.list, source.List)
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
for k, v := range source.Count {
|
2023-09-13 17:30:24 +00:00
|
|
|
fmt.Printf("Setting count[%s] to %d\n", k, v)
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.count[k] = v
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
for k, v := range source.Size {
|
2023-09-13 17:30:24 +00:00
|
|
|
fmt.Printf("Setting size[%s] to %v\n", k, v)
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.size[k] = v
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
for k, v := range source.Times {
|
2023-09-13 17:30:24 +00:00
|
|
|
fmt.Printf("Setting times[%s] to %v\n", k, v)
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.times[k] = v
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.mutex.Unlock()
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
func (source *serveStats) Export() *publicServeStats {
|
2023-09-13 17:30:24 +00:00
|
|
|
source.mutex.RLock()
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats := &publicServeStats{
|
|
|
|
List: make([]string, len(source.list)),
|
|
|
|
Count: make(map[string]uint32, len(source.count)),
|
|
|
|
Size: make(map[string]string, len(source.size)),
|
|
|
|
Times: make(map[string][]string, len(source.times)),
|
|
|
|
}
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
copy(stats.List, source.list)
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
for k, v := range source.count {
|
|
|
|
stats.Count[k] = v
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
for k, v := range source.size {
|
|
|
|
stats.Size[k] = v
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
for k, v := range source.times {
|
|
|
|
stats.Times[k] = v
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
source.mutex.RUnlock()
|
|
|
|
|
|
|
|
return stats
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:30:24 +00:00
|
|
|
func (stats *serveStats) exportFile(path string) error {
|
2023-09-11 17:09:08 +00:00
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
z, err := zstd.NewWriter(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer z.Close()
|
|
|
|
|
|
|
|
enc := gob.NewEncoder(z)
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
err = enc.Encode(stats.Export())
|
2023-09-11 17:09:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:30:24 +00:00
|
|
|
func (stats *serveStats) importFile(path string) error {
|
2023-09-11 17:09:08 +00:00
|
|
|
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
z, err := zstd.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer z.Close()
|
|
|
|
|
|
|
|
dec := gob.NewDecoder(z)
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
source := &publicServeStats{
|
2023-09-11 17:09:08 +00:00
|
|
|
List: []string{},
|
|
|
|
Count: make(map[string]uint32),
|
|
|
|
Size: make(map[string]string),
|
|
|
|
Times: make(map[string][]string),
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
err = dec.Decode(source)
|
2023-09-11 17:09:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
stats.Import(source)
|
2023-09-11 17:09:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:30:24 +00:00
|
|
|
func (source *serveStats) listFiles(page int) ([]byte, error) {
|
|
|
|
stats := source.Export()
|
|
|
|
|
|
|
|
sort.SliceStable(stats.List, func(p, q int) bool {
|
|
|
|
return strings.ToLower(stats.List[p]) < strings.ToLower(stats.List[q])
|
|
|
|
})
|
|
|
|
|
|
|
|
var startIndex, stopIndex int
|
|
|
|
|
|
|
|
if page == -1 {
|
|
|
|
startIndex = 0
|
|
|
|
stopIndex = len(stats.List)
|
|
|
|
} else {
|
|
|
|
startIndex = ((page - 1) * int(PageLength))
|
|
|
|
stopIndex = (startIndex + int(PageLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
if startIndex > len(stats.List)-1 {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if stopIndex > len(stats.List) {
|
|
|
|
stopIndex = len(stats.List)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := make([]timesServed, (stopIndex - startIndex))
|
|
|
|
|
|
|
|
for k, v := range stats.List[startIndex:stopIndex] {
|
|
|
|
a[k] = timesServed{v, stats.Count[v], stats.Size[v], stats.Times[v]}
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := json.MarshalIndent(a, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
func serveStatsPage(args []string, stats *serveStats) httprouter.Handle {
|
2023-09-11 17:09:08 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
page, err := strconv.Atoi(p.ByName("page"))
|
|
|
|
if err != nil || page == 0 {
|
|
|
|
page = -1
|
|
|
|
}
|
|
|
|
|
2023-09-13 17:00:24 +00:00
|
|
|
response, err := stats.listFiles(page)
|
2023-09-11 17:09:08 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-11 17:09:08 +00:00
|
|
|
fmt.Printf("%s | Served statistics page (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-11 17:09:08 +00:00
|
|
|
humanReadableSize(len(response)),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if StatisticsFile != "" {
|
2023-09-13 17:30:24 +00:00
|
|
|
stats.exportFile(StatisticsFile)
|
2023-09-13 17:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerStatsHandlers(mux *httprouter.Router, args []string, stats *serveStats) {
|
2023-09-13 17:30:24 +00:00
|
|
|
mux.GET("/stats", serveStatsPage(args, stats))
|
|
|
|
if PageLength != 0 {
|
|
|
|
mux.GET("/stats/:page", serveStatsPage(args, stats))
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
}
|