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

@ -53,40 +53,51 @@ type ScanStats struct {
type TimesServed struct {
File string
Count uint64
Size string
Served uint64
Timestamps []string
}
type ServeStats struct {
ImagesServed uint64
ImageList []string
ImageCount map[string]uint64
Served uint64
List []string
Count map[string]uint64
FileSize map[string]string
Timestamps map[string][]string
}
func (s *ServeStats) GetFilesTotal() uint64 {
return atomic.LoadUint64(&s.ImagesServed)
return atomic.LoadUint64(&s.Served)
}
func (s *ServeStats) IncrementCounter(image string) {
s.ImagesServed += 1
func (s *ServeStats) IncrementCounter(image string, timestamp time.Time, filesize string) {
s.Served += 1
s.ImageCount[image] += 1
s.Count[image] += 1
if !contains(s.ImageList, image) {
s.ImageList = append(s.ImageList, image)
s.Timestamps[image] = append(s.Timestamps[image], timestamp.Format(LogDate))
_, 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) {
a := []TimesServed{}
sortedList := s.ImageList
sortedList := s.List
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]})
for _, image := range s.List {
a = append(a, TimesServed{image, s.FileSize[image], s.Count[image], s.Timestamps[image]})
}
r, err := json.MarshalIndent(a, "", " ")

View File

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

View File

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