types.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package diagnostic
  2. import "fmt"
  3. // StringInterface interface that has to be implemented by messages
  4. type StringInterface interface {
  5. String() string
  6. }
  7. // CommandSucceed creates a success message
  8. func CommandSucceed(result StringInterface) *HTTPResult {
  9. return &HTTPResult{
  10. Message: "OK",
  11. Details: result,
  12. }
  13. }
  14. // FailCommand creates a failure message with error
  15. func FailCommand(err error) *HTTPResult {
  16. return &HTTPResult{
  17. Message: "FAIL",
  18. Details: &ErrorCmd{Error: err.Error()},
  19. }
  20. }
  21. // WrongCommand creates a wrong command response
  22. func WrongCommand(message, usage string) *HTTPResult {
  23. return &HTTPResult{
  24. Message: message,
  25. Details: &UsageCmd{Usage: usage},
  26. }
  27. }
  28. // HTTPResult Diagnostic Server HTTP result operation
  29. type HTTPResult struct {
  30. Message string `json:"message"`
  31. Details StringInterface `json:"details"`
  32. }
  33. func (h *HTTPResult) String() string {
  34. rsp := h.Message
  35. if h.Details != nil {
  36. rsp += "\n" + h.Details.String()
  37. }
  38. return rsp
  39. }
  40. // UsageCmd command with usage field
  41. type UsageCmd struct {
  42. Usage string `json:"usage"`
  43. }
  44. func (u *UsageCmd) String() string {
  45. return "Usage: " + u.Usage
  46. }
  47. // StringCmd command with info string
  48. type StringCmd struct {
  49. Info string `json:"info"`
  50. }
  51. func (s *StringCmd) String() string {
  52. return s.Info
  53. }
  54. // ErrorCmd command with error
  55. type ErrorCmd struct {
  56. Error string `json:"error"`
  57. }
  58. func (e *ErrorCmd) String() string {
  59. return "Error: " + e.Error
  60. }
  61. // TableObj network db table object
  62. type TableObj struct {
  63. Length int `json:"size"`
  64. Elements []StringInterface `json:"entries"`
  65. }
  66. func (t *TableObj) String() string {
  67. output := fmt.Sprintf("total entries: %d\n", t.Length)
  68. for _, e := range t.Elements {
  69. output += e.String()
  70. }
  71. return output
  72. }
  73. // PeerEntryObj entry in the networkdb peer table
  74. type PeerEntryObj struct {
  75. Index int `json:"-"`
  76. Name string `json:"-=name"`
  77. IP string `json:"ip"`
  78. }
  79. func (p *PeerEntryObj) String() string {
  80. return fmt.Sprintf("%d) %s -> %s\n", p.Index, p.Name, p.IP)
  81. }
  82. // TableEntryObj network db table entry object
  83. type TableEntryObj struct {
  84. Index int `json:"-"`
  85. Key string `json:"key"`
  86. Value string `json:"value"`
  87. Owner string `json:"owner"`
  88. }
  89. func (t *TableEntryObj) String() string {
  90. return fmt.Sprintf("%d) k:`%s` -> v:`%s` owner:`%s`\n", t.Index, t.Key, t.Value, t.Owner)
  91. }
  92. // TableEndpointsResult fully typed message for proper unmarshaling on the client side
  93. type TableEndpointsResult struct {
  94. TableObj
  95. Elements []TableEntryObj `json:"entries"`
  96. }
  97. // TablePeersResult fully typed message for proper unmarshaling on the client side
  98. type TablePeersResult struct {
  99. TableObj
  100. Elements []PeerEntryObj `json:"entries"`
  101. }
  102. // NetworkStatsResult network db stats related to entries and queue len for a network
  103. type NetworkStatsResult struct {
  104. Entries int `json:"entries"`
  105. QueueLen int `jsoin:"qlen"`
  106. }
  107. func (n *NetworkStatsResult) String() string {
  108. return fmt.Sprintf("entries: %d, qlen: %d\n", n.Entries, n.QueueLen)
  109. }