Added first served/last served/most served stats to /_/stats

This commit is contained in:
Seednode 2023-05-12 09:05:14 -05:00
parent 7184542cb6
commit c31518d932
2 changed files with 75 additions and 1 deletions

View File

@ -76,5 +76,5 @@ func init() {
})
rootCmd.SetVersionTemplate("roulette v{{.Version}}\n")
rootCmd.Version = "0.46.0"
rootCmd.Version = "0.46.1"
}

View File

@ -37,6 +37,8 @@ const (
Timeout time.Duration = 10 * time.Second
)
type Stat int
type Regexes struct {
alphanumeric *regexp.Regexp
filename *regexp.Regexp
@ -265,6 +267,67 @@ func (s *ServeStats) ListImages() ([]byte, error) {
return r, nil
}
func (s *ServeStats) GetHistory() (string, string, error) {
stats := s.toExported()
type History struct {
name string
time time.Time
}
firstServed := History{}
lastServed := History{}
for _, name := range stats.List {
for _, accessTime := range stats.Times[name] {
t, err := time.Parse(LogDate, accessTime)
if err != nil {
return "", "", err
}
if (firstServed.time == time.Time{}) {
firstServed.name = name
firstServed.time = t
}
if (lastServed.time == time.Time{}) {
lastServed.name = name
lastServed.time = t
}
switch {
case t.Before(firstServed.time):
firstServed.name = name
firstServed.time = t
case t.After(lastServed.time):
lastServed.name = name
lastServed.time = t
}
}
}
return firstServed.name, lastServed.name, nil
}
func (s *ServeStats) GetMostServed() string {
stats := s.toExported()
retVal := ""
for _, v := range stats.List {
if retVal == "" {
retVal = v
continue
}
if stats.Count[v] > stats.Count[retVal] {
retVal = v
}
}
return retVal
}
func (s *ServeStats) Export(path string) error {
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
@ -519,6 +582,17 @@ func serveStats(args []string, stats *ServeStats) http.HandlerFunc {
startTime := time.Now()
first, last, err := stats.GetHistory()
if err != nil {
log.Fatal(err)
}
most := stats.GetMostServed()
fmt.Printf("First served: %s\n", first)
fmt.Printf("Last served: %s\n", last)
fmt.Printf("Most served: %s\n", most)
response, err := stats.ListImages()
if err != nil {
fmt.Println(err)