File: cmd_report.go

package info (click to toggle)
syncthing 0.14.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,388 kB
  • ctags: 4,608
  • sloc: xml: 781; sh: 271; makefile: 45
file content (72 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (C) 2014 Audrius Butkevičius

package main

import (
	"encoding/json"
	"fmt"

	"github.com/AudriusButkevicius/cli"
)

func init() {
	cliCommands = append(cliCommands, cli.Command{
		Name:     "report",
		HideHelp: true,
		Usage:    "Reporting command group",
		Subcommands: []cli.Command{
			{
				Name:     "system",
				Usage:    "Report system state",
				Requires: &cli.Requires{},
				Action:   reportSystem,
			},
			{
				Name:     "connections",
				Usage:    "Report about connections to other devices",
				Requires: &cli.Requires{},
				Action:   reportConnections,
			},
			{
				Name:     "usage",
				Usage:    "Usage report",
				Requires: &cli.Requires{},
				Action:   reportUsage,
			},
		},
	})
}

func reportSystem(c *cli.Context) {
	response := httpGet(c, "system/status")
	data := make(map[string]interface{})
	json.Unmarshal(responseToBArray(response), &data)
	prettyPrintJSON(data)
}

func reportConnections(c *cli.Context) {
	response := httpGet(c, "system/connections")
	data := make(map[string]map[string]interface{})
	json.Unmarshal(responseToBArray(response), &data)
	var overall map[string]interface{}
	for key, value := range data {
		if key == "total" {
			overall = value
			continue
		}
		value["Device ID"] = key
		prettyPrintJSON(value)
		fmt.Println()
	}
	if overall != nil {
		fmt.Println("=== Overall statistics ===")
		prettyPrintJSON(overall)
	}
}

func reportUsage(c *cli.Context) {
	response := httpGet(c, "svc/report")
	report := make(map[string]interface{})
	json.Unmarshal(responseToBArray(response), &report)
	prettyPrintJSON(report)
}