add project size to arthur JSON output

This commit is contained in:
Max Erenberg 2023-04-29 23:40:55 -04:00 committed by Mirror
parent 586520aa55
commit 79ca1950f5
2 changed files with 47 additions and 5 deletions

View File

@ -11,6 +11,8 @@ import (
"log"
"net"
"os"
"os/exec"
"regexp"
"strings"
"text/tabwriter"
"time"
@ -33,6 +35,14 @@ FLAGS:
//go:embed layout.html
var layoutHtmlTemplate string
type EnrichedRepoStatusInfo struct {
serverArthurPkg.RepoStatusInfo
Size string `json:"size,omitempty"`
}
type EnrichedStatusInfo struct {
Repos []*EnrichedRepoStatusInfo `json:"repos"`
}
func main() {
log.SetPrefix("[ERROR]: ")
log.SetFlags(0)
@ -80,19 +90,22 @@ func main() {
fmt.Println(string(response))
return
}
statusInfo := serverArthurPkg.StatusInfo{}
statusInfo := EnrichedStatusInfo{}
err = json.Unmarshal(response, &statusInfo)
if err != nil {
log.Fatal(err)
}
if shouldOutputJson {
filesystemSizes := getFilesystemSizes()
for _, repoInfo := range statusInfo.Repos {
repoInfo.Size = filesystemSizes[repoInfo.Name]
}
jsonOutput, jsonErr := json.MarshalIndent(&statusInfo, "", " ")
if jsonErr == nil {
fmt.Println(string(jsonOutput))
} else {
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Println(string(jsonOutput))
} else if shouldOutputHtml {
funcs := template.FuncMap{
"unixTimeToStr": unixTimeToStr,
@ -145,3 +158,32 @@ func unixTimeToStr(unixTime int64) string {
// for other ways to format the time see: https://pkg.go.dev/time#pkg-constants
return time.Unix(unixTime, 0).In(location).Format(time.RFC1123)
}
// getFilesystemSizes parses the output of the `zfs list` command and returns
// a map which looks like {"debian":"1.62T","gnu":"148G",...}.
func getFilesystemSizes() map[string]string {
pattern := regexp.MustCompile(`^cscmirror\d/([^ ]+) (.*)$`)
result := make(map[string]string)
// Use the absolute path to the zfs command because /sbin isn't in the
// mirror user's PATH variable when running from cron
cmd := exec.Command("sh", "-c", `/sbin/zfs list -t filesystem -H | awk '/^cscmirror[[:digit:]]\// {print $1 " " $2}'`)
output, err := cmd.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "zfs list failed: %+v\n", err)
return result
}
lines := bytes.Split(output, []byte("\n"))
for _, line := range lines {
if len(line) == 0 {
continue
}
matches := pattern.FindSubmatch(line)
if matches == nil {
continue
}
projectName := string(matches[1])
diskUsage := string(matches[2])
result[projectName] = diskUsage
}
return result
}

View File

@ -84,7 +84,7 @@
{{if .IsRunning}} Is currently syncing {{else}} Is not syncing {{end}}
</div>
<div class="repo-data-item">
Last sync time: {{unixTimeToStr .LastAttemptStartTime}}
{{if .IsRunning}}Sync started at{{else}}Last sync time{{end}}: {{unixTimeToStr .LastAttemptStartTime}}
</div>
<div class="repo-data-item">
Last sync result: {{.LastAttemptExit}}