Added filesize and timestamps to debug JSON output

This commit is contained in:
Seednode 2023-01-20 18:14:07 -06:00
parent 2f81e540e2
commit 6c888ba20a
3 changed files with 38 additions and 23 deletions

View File

@ -52,41 +52,52 @@ type ScanStats struct {
} }
type TimesServed struct { type TimesServed struct {
File string File string
Count uint64 Size string
Served uint64
Timestamps []string
} }
type ServeStats struct { type ServeStats struct {
ImagesServed uint64 Served uint64
ImageList []string List []string
ImageCount map[string]uint64 Count map[string]uint64
FileSize map[string]string
Timestamps map[string][]string
} }
func (s *ServeStats) GetFilesTotal() uint64 { func (s *ServeStats) GetFilesTotal() uint64 {
return atomic.LoadUint64(&s.ImagesServed) return atomic.LoadUint64(&s.Served)
} }
func (s *ServeStats) IncrementCounter(image string) { func (s *ServeStats) IncrementCounter(image string, timestamp time.Time, filesize string) {
s.ImagesServed += 1 s.Served += 1
s.ImageCount[image] += 1 s.Count[image] += 1
if !contains(s.ImageList, image) { s.Timestamps[image] = append(s.Timestamps[image], timestamp.Format(LogDate))
s.ImageList = append(s.ImageList, image)
_, exists := s.FileSize[image]
if !exists {
s.FileSize[image] = filesize
}
if !contains(s.List, image) {
s.List = append(s.List, image)
} }
} }
func (s *ServeStats) ListImages() ([]byte, error) { func (s *ServeStats) ListImages() ([]byte, error) {
a := []TimesServed{} a := []TimesServed{}
sortedList := s.ImageList sortedList := s.List
sort.SliceStable(sortedList, func(p, q int) bool { sort.SliceStable(sortedList, func(p, q int) bool {
return sortedList[p] < sortedList[q] return sortedList[p] < sortedList[q]
}) })
for _, image := range s.ImageList { for _, image := range s.List {
a = append(a, TimesServed{image, s.ImageCount[image]}) a = append(a, TimesServed{image, s.FileSize[image], s.Count[image], s.Timestamps[image]})
} }
r, err := json.MarshalIndent(a, "", " ") r, err := json.MarshalIndent(a, "", " ")

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.31.2" var Version = "0.31.3"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -307,10 +307,6 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string, sta
startTime := time.Now() startTime := time.Now()
if Debug {
stats.IncrementCounter(filePath)
}
buf, err := os.ReadFile(filePath) buf, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return err return err
@ -318,16 +314,22 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string, sta
w.Write(buf) w.Write(buf)
fileSize := humanReadableSize(len(buf))
if Verbose { if Verbose {
fmt.Printf("%v | Served %v (%v) to %v in %v\n", fmt.Printf("%v | Served %v (%v) to %v in %v\n",
startTime.Format(LogDate), startTime.Format(LogDate),
filePath, filePath,
humanReadableSize(len(buf)), fileSize,
getRealIp(r), getRealIp(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
} }
if Debug {
stats.IncrementCounter(filePath, startTime, fileSize)
}
return nil return nil
} }
@ -497,9 +499,11 @@ func ServePage(args []string) error {
fileCache := &[]string{} fileCache := &[]string{}
stats := &ServeStats{ stats := &ServeStats{
ImagesServed: 0, Served: 0,
ImageList: []string{}, List: []string{},
ImageCount: make(map[string]uint64), Count: make(map[string]uint64),
FileSize: make(map[string]string),
Timestamps: make(map[string][]string),
} }
http.Handle("/", serveHtmlHandler(paths, regexes, fileCache)) http.Handle("/", serveHtmlHandler(paths, regexes, fileCache))