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