cmd_report.go 1.6 KB

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