cmd_graph.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package console
  11. import (
  12. "fmt"
  13. "net/url"
  14. "sort"
  15. "strings"
  16. "devt.de/krotik/common/stringutil"
  17. "devt.de/krotik/eliasdb/api/v1"
  18. )
  19. // Command: info
  20. // =============
  21. /*
  22. CommandInfo is a command name.
  23. */
  24. const CommandInfo = "info"
  25. /*
  26. CmdInfo returns general database information.
  27. */
  28. type CmdInfo struct {
  29. }
  30. /*
  31. Name returns the command name (as it should be typed)
  32. */
  33. func (c *CmdInfo) Name() string {
  34. return CommandInfo
  35. }
  36. /*
  37. ShortDescription returns a short description of the command (single line)
  38. */
  39. func (c *CmdInfo) ShortDescription() string {
  40. return "Returns general database information."
  41. }
  42. /*
  43. LongDescription returns an extensive description of the command (can be multiple lines)
  44. */
  45. func (c *CmdInfo) LongDescription() string {
  46. return "Returns general database information such as known node kinds, known attributes, etc ..."
  47. }
  48. /*
  49. Run executes the command.
  50. */
  51. func (c *CmdInfo) Run(args []string, capi CommandConsoleAPI) error {
  52. res, err := capi.Req(v1.EndpointInfoQuery, "GET", nil)
  53. if err == nil {
  54. var data = res.(map[string]interface{})
  55. var keys, tab []string
  56. tab = append(tab, "Kind")
  57. tab = append(tab, "Count")
  58. counts := data["node_counts"].(map[string]interface{})
  59. for k := range counts {
  60. keys = append(keys, k)
  61. }
  62. sort.Strings(keys)
  63. for _, k := range keys {
  64. c := counts[k]
  65. tab = append(tab, k)
  66. tab = append(tab, fmt.Sprintf("%10v", c))
  67. }
  68. capi.ExportBuffer().WriteString(stringutil.PrintCSVTable(tab, 2))
  69. fmt.Fprint(capi.Out(), stringutil.PrintGraphicStringTable(tab, 2, 1,
  70. stringutil.SingleLineTable))
  71. }
  72. return err
  73. }
  74. // Command: part
  75. // =============
  76. /*
  77. CommandPart is a command name.
  78. */
  79. const CommandPart = "part"
  80. /*
  81. CmdPart displays or sets the current partition.
  82. */
  83. type CmdPart struct {
  84. }
  85. /*
  86. Name returns the command name (as it should be typed)
  87. */
  88. func (c *CmdPart) Name() string {
  89. return CommandPart
  90. }
  91. /*
  92. ShortDescription returns a short description of the command (single line)
  93. */
  94. func (c *CmdPart) ShortDescription() string {
  95. return "Displays or sets the current partition."
  96. }
  97. /*
  98. LongDescription returns an extensive description of the command (can be multiple lines)
  99. */
  100. func (c *CmdPart) LongDescription() string {
  101. return "Displays or sets the current partition."
  102. }
  103. /*
  104. Run executes the command.
  105. */
  106. func (c *CmdPart) Run(args []string, capi CommandConsoleAPI) error {
  107. if len(args) == 0 {
  108. fmt.Fprintln(capi.Out(), capi.Partition())
  109. } else {
  110. capi.SetPartition(args[0])
  111. fmt.Fprintln(capi.Out(),
  112. fmt.Sprintf("Current partition is: %s", args[0]))
  113. }
  114. return nil
  115. }
  116. // Command: find
  117. // =============
  118. /*
  119. CommandFind is a command name.
  120. */
  121. const CommandFind = "find"
  122. /*
  123. CmdFind does a full-text search of the database.
  124. */
  125. type CmdFind struct {
  126. }
  127. /*
  128. Name returns the command name (as it should be typed)
  129. */
  130. func (c *CmdFind) Name() string {
  131. return CommandFind
  132. }
  133. /*
  134. ShortDescription returns a short description of the command (single line)
  135. */
  136. func (c *CmdFind) ShortDescription() string {
  137. return "Do a full-text search of the database."
  138. }
  139. /*
  140. LongDescription returns an extensive description of the command (can be multiple lines)
  141. */
  142. func (c *CmdFind) LongDescription() string {
  143. return "Do a full-text search of the database."
  144. }
  145. /*
  146. Run executes the command.
  147. */
  148. func (c *CmdFind) Run(args []string, capi CommandConsoleAPI) error {
  149. if len(args) < 1 {
  150. return fmt.Errorf("Please specify a search phrase")
  151. }
  152. phrase := url.QueryEscape(strings.Join(args, " "))
  153. res, err := capi.Req(fmt.Sprintf("%s?lookup=1&text=%s", v1.EndpointFindQuery, phrase), "GET", nil)
  154. if err == nil {
  155. partitions := res.(map[string]interface{})
  156. for _, p := range stringutil.MapKeys(partitions) {
  157. kinds := partitions[p].(map[string]interface{})
  158. for _, k := range stringutil.MapKeys(kinds) {
  159. nodes := kinds[k].([]interface{})
  160. // Construct table header
  161. header := []string{"Partition", p, "Kind", k}
  162. capi.ExportBuffer().WriteString(stringutil.PrintCSVTable(header, 2))
  163. fmt.Fprint(capi.Out(), stringutil.PrintStringTable(header, 2))
  164. // Construct table
  165. node := nodes[0].(map[string]interface{})
  166. attrs := stringutil.MapKeys(node)
  167. var tab []string
  168. tab = append(tab, attrs...)
  169. for _, n := range nodes {
  170. node := n.(map[string]interface{})
  171. for _, attr := range attrs {
  172. tab = append(tab, fmt.Sprint(node[attr]))
  173. }
  174. }
  175. capi.ExportBuffer().WriteString(stringutil.PrintCSVTable(tab, len(attrs)))
  176. fmt.Fprint(capi.Out(), stringutil.PrintGraphicStringTable(tab, len(attrs), 1,
  177. stringutil.SingleLineTable))
  178. fmt.Fprintln(capi.Out(), "")
  179. }
  180. }
  181. }
  182. return nil
  183. }