cmd_report.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/AudriusButkevicius/cli"
  6. )
  7. func init() {
  8. cliCommands = append(cliCommands, cli.Command{
  9. Name: "report",
  10. HideHelp: true,
  11. Usage: "Reporting command group",
  12. Subcommands: []cli.Command{
  13. {
  14. Name: "system",
  15. Usage: "Report system state",
  16. Requires: &cli.Requires{},
  17. Action: reportSystem,
  18. },
  19. {
  20. Name: "connections",
  21. Usage: "Report about connections to other devices",
  22. Requires: &cli.Requires{},
  23. Action: reportConnections,
  24. },
  25. {
  26. Name: "usage",
  27. Usage: "Usage report",
  28. Requires: &cli.Requires{},
  29. Action: reportUsage,
  30. },
  31. },
  32. })
  33. }
  34. func reportSystem(c *cli.Context) {
  35. response := httpGet(c, "system/status")
  36. data := make(map[string]interface{})
  37. json.Unmarshal(responseToBArray(response), &data)
  38. prettyPrintJSON(data)
  39. }
  40. func reportConnections(c *cli.Context) {
  41. response := httpGet(c, "system/connections")
  42. data := make(map[string]map[string]interface{})
  43. json.Unmarshal(responseToBArray(response), &data)
  44. var overall map[string]interface{}
  45. for key, value := range data {
  46. if key == "total" {
  47. overall = value
  48. continue
  49. }
  50. value["Device ID"] = key
  51. prettyPrintJSON(value)
  52. fmt.Println()
  53. }
  54. if overall != nil {
  55. fmt.Println("=== Overall statistics ===")
  56. prettyPrintJSON(overall)
  57. }
  58. }
  59. func reportUsage(c *cli.Context) {
  60. response := httpGet(c, "svc/report")
  61. report := make(map[string]interface{})
  62. json.Unmarshal(responseToBArray(response), &report)
  63. prettyPrintJSON(report)
  64. }