diff --git a/cmd/files.go b/cmd/files.go index 62aa526..3df6b0b 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -52,41 +52,52 @@ type ScanStats struct { } type TimesServed struct { - File string - Count uint64 + File string + 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, "", " ") diff --git a/cmd/version.go b/cmd/version.go index 11bbd45..61a71c2 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.31.2" +var Version = "0.31.3" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 64dfb95..c999a21 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -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))