JSON output for stats is now pretty-printed and sorted alphabetically
This commit is contained in:
parent
6bb779d1bc
commit
0f4c4845b2
15
cmd/files.go
15
cmd/files.go
|
@ -12,6 +12,7 @@ import (
|
|||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"sort"
|
||||
|
||||
"math/rand"
|
||||
"os"
|
||||
|
@ -51,8 +52,8 @@ type ScanStats struct {
|
|||
}
|
||||
|
||||
type TimesServed struct {
|
||||
file string
|
||||
count uint64
|
||||
File string
|
||||
Count uint64
|
||||
}
|
||||
|
||||
type ServeStats struct {
|
||||
|
@ -66,6 +67,8 @@ func (s *ServeStats) GetFilesTotal() uint64 {
|
|||
}
|
||||
|
||||
func (s *ServeStats) IncrementCounter(image string) {
|
||||
s.ImagesServed += 1
|
||||
|
||||
s.ImageCount[image] += 1
|
||||
|
||||
if !contains(s.ImageList, image) {
|
||||
|
@ -76,11 +79,17 @@ func (s *ServeStats) IncrementCounter(image string) {
|
|||
func (s *ServeStats) ListImages() ([]byte, error) {
|
||||
a := []TimesServed{}
|
||||
|
||||
sortedList := s.ImageList
|
||||
|
||||
sort.SliceStable(sortedList, func(p, q int) bool {
|
||||
return sortedList[p] < sortedList[q]
|
||||
})
|
||||
|
||||
for _, image := range s.ImageList {
|
||||
a = append(a, TimesServed{image, s.ImageCount[image]})
|
||||
}
|
||||
|
||||
r, err := json.Marshal(a)
|
||||
r, err := json.MarshalIndent(a, "", " ")
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version = "0.31.1"
|
||||
var Version = "0.31.2"
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
|
|
28
cmd/web.go
28
cmd/web.go
|
@ -307,7 +307,9 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string, sta
|
|||
|
||||
startTime := time.Now()
|
||||
|
||||
if Debug {
|
||||
stats.IncrementCounter(filePath)
|
||||
}
|
||||
|
||||
buf, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
|
@ -342,7 +344,6 @@ func generateCache(args []string, fileCache *[]string) error {
|
|||
|
||||
func serveCacheClearHandler(args []string, fileCache *[]string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if Cache {
|
||||
err := generateCache(args, fileCache)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
@ -352,7 +353,6 @@ func serveCacheClearHandler(args []string, fileCache *[]string) http.HandlerFunc
|
|||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte("Ok"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serveStats(args []string, fileCache *[]string) error {
|
||||
|
@ -496,13 +496,6 @@ func ServePage(args []string) error {
|
|||
|
||||
fileCache := &[]string{}
|
||||
|
||||
if Cache {
|
||||
err := generateCache(args, fileCache)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
stats := &ServeStats{
|
||||
ImagesServed: 0,
|
||||
ImageList: []string{},
|
||||
|
@ -510,13 +503,22 @@ func ServePage(args []string) error {
|
|||
}
|
||||
|
||||
http.Handle("/", serveHtmlHandler(paths, regexes, fileCache))
|
||||
http.Handle("/clear_cache", serveCacheClearHandler(args, fileCache))
|
||||
|
||||
http.Handle("/stats", serveStatsHandler(args, stats))
|
||||
|
||||
http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths, stats)))
|
||||
http.HandleFunc("/favicon.ico", doNothing)
|
||||
|
||||
if Cache {
|
||||
err := generateCache(args, fileCache)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
http.Handle("/clear_cache", serveCacheClearHandler(args, fileCache))
|
||||
}
|
||||
|
||||
if Debug {
|
||||
http.Handle("/stats", serveStatsHandler(args, stats))
|
||||
}
|
||||
|
||||
err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue