docstore/pkg/server/helper.go

37 lines
799 B
Go

// Copyright (C) 2021 Marius Schellenberger
package server
import (
"runtime"
"strconv"
"git.giftfish.de/ston1th/docstore/pkg/core"
bleve "git.giftfish.de/ston1th/docstore/pkg/index"
)
const div = 1048576
func fmtMem(i uint64) string {
return strconv.FormatFloat(float64(i)/div, 'f', 2, 64)
}
func getStats(i *bleve.Index) *core.Stats {
mem := new(runtime.MemStats)
runtime.ReadMemStats(mem)
return &core.Stats{
Goroutines: runtime.NumGoroutine(),
Alloc: fmtMem(mem.Alloc),
Sys: fmtMem(mem.Sys),
Docs: i.DocCount(),
GoVersion: runtime.Version(),
}
}
// Parts taken from strings.TrimPrefix
func hasTrimPrefix(s, prefix string) (string, bool) {
if len(s) >= len(prefix) && s[0:len(prefix)] == prefix {
return s[len(prefix):], true
}
return s, false
}