Skip to content

Commit

Permalink
feat: add stats page
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 14, 2022
1 parent 7e45bc2 commit 687caa5
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 11 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/soulteary/apt-proxy

go 1.18

require github.com/stretchr/testify v1.7.2
require (
github.com/stretchr/testify v1.7.2
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d h1:Zu/JngovGLVi6t2J3nmAf3AoTDwuzw85YZ3b9o4yU7s=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
69 changes: 69 additions & 0 deletions pkgs/system/filesize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package system

import (
"math"
"os"
"path/filepath"
"strconv"

"golang.org/x/sys/unix"
)

func DiskAvailable() (int64, error) {
var stat unix.Statfs_t
wd, err := os.Getwd()

if err != nil {
return 0, err
}

unix.Statfs(wd, &stat)
return int64(stat.Bavail * uint64(stat.Bsize)), nil
}

// https://stackoverflow.com/questions/32482673/how-to-get-directory-total-size
func DirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}

// https://hakk.dev/docs/golang-convert-file-size-human-readable/
func fileSizeRound(val float64, roundOn float64, places int) float64 {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}

func HumanFileSize(input int64) string {
size := float64(input)

var units = []string{
0: "B",
1: "KB",
2: "MB",
3: "GB",
4: "TB",
}

level := math.Log(size) / math.Log(1024)
fileSize := fileSizeRound(math.Pow(1024, level-math.Floor(level)), .5, 2)
unit := units[int(math.Floor(level))]

return strconv.FormatFloat(fileSize, 'f', -1, 64) + " " + string(unit)
}
50 changes: 50 additions & 0 deletions pkgs/system/gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package system

// https://github.com/soulteary/hosts-blackhole/blob/main/pkg/system/gc.go

import (
"fmt"
"runtime"
"runtime/debug"
"strconv"
"strings"
)

func GetMemoryUsageAndGoroutine() (int64, string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return int64(m.Alloc), strconv.Itoa(runtime.NumGoroutine())
}

func Stats(logPrint bool) string {
var m runtime.MemStats
runtime.ReadMemStats(&m)

stats := []string{
"Runtime Information:",
fmt.Sprintf(" MEM Alloc = %10v MB", toMB(m.Alloc)),
fmt.Sprintf(" MEM HeapAlloc = %10v MB", toMB(m.HeapAlloc)),
fmt.Sprintf(" MEM Sys = %10v MB", toMB(m.Sys)),
fmt.Sprintf(" MEM NumGC = %10v", m.NumGC),
fmt.Sprintf(" RUN NumCPU = %10d", runtime.NumCPU()),
fmt.Sprintf(" RUN NumGoroutine = %10d", runtime.NumGoroutine()),
}

if logPrint {
for _, info := range stats {
fmt.Println(info)
}
}
return strings.Join(stats, "\n")
}

func ManualGC() {
runtime.GC()
debug.FreeOSMemory()
Stats(true)
}

func toMB(b uint64) uint64 {
const bytesInKB = 1024
return b / bytesInKB / bytesInKB
}
40 changes: 30 additions & 10 deletions server/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,59 @@ package server

import (
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/soulteary/apt-proxy/pkgs/system"
)

const (
INTERNAL_PAGE_HOME string = "/"
INTERNAL_PAGE_HELP = "/_/help"
)

const (
TYPE_NOT_FOUND int = 0
TYPE_HOME = 1
TYPE_HELP = 2
)

func isInternalUrls(url string) bool {
return url == INTERNAL_PAGE_HOME ||
url == INTERNAL_PAGE_HELP
if strings.Contains(url, "ubuntu") || strings.Contains(url, "debian") {
return false
}
return url == INTERNAL_PAGE_HOME
}

func getInternalResType(url string) int {
if url == INTERNAL_PAGE_HOME {
return TYPE_HOME
}

if url == INTERNAL_PAGE_HELP {
return TYPE_HELP
}
return TYPE_NOT_FOUND
}

// TODO Home, Help, Stats
func renderInternalUrls(url string, rw *http.ResponseWriter) {
if getInternalResType(url) != 0 {
io.WriteString(*rw, "Hello, APT Proxy!\n")
types := getInternalResType(url)
if types == TYPE_NOT_FOUND {
return
}

if types == TYPE_HOME {
cacheSize, _ := system.DirSize("./.aptcache")
files, _ := ioutil.ReadDir("./.aptcache/header/v1")
available, _ := system.DiskAvailable()

memoryUsage, goroutine := system.GetMemoryUsageAndGoroutine()

io.WriteString(*rw, getBaseTemplate(
system.HumanFileSize(cacheSize),
strconv.Itoa(len(files)),
system.HumanFileSize(available),
system.HumanFileSize(memoryUsage),
goroutine,
))
return
}

}
Loading

0 comments on commit 687caa5

Please sign in to comment.