add project size to arthur JSON output
This commit is contained in:
parent
586520aa55
commit
79ca1950f5
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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}}
|
||||
|
|
Loading…
Reference in New Issue