dashboard_importer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package plugins
  2. import (
  3. "fmt"
  4. "reflect"
  5. "regexp"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. "github.com/grafana/grafana/pkg/log"
  9. m "github.com/grafana/grafana/pkg/models"
  10. )
  11. type ImportDashboardCommand struct {
  12. Path string `json:"string"`
  13. Inputs []ImportDashboardInput `json:"inputs"`
  14. OrgId int64 `json:"-"`
  15. UserId int64 `json:"-"`
  16. PluginId string `json:"-"`
  17. Result *PluginDashboardInfoDTO
  18. }
  19. type ImportDashboardInput struct {
  20. Type string `json:"type"`
  21. PluginId string `json:"pluginId"`
  22. Name string `json:"name"`
  23. Value string `json:"value"`
  24. }
  25. type DashboardInputMissingError struct {
  26. VariableName string
  27. }
  28. func (e DashboardInputMissingError) Error() string {
  29. return fmt.Sprintf("Dashbord input variable: %v missing from import command", e.VariableName)
  30. }
  31. func init() {
  32. bus.AddHandler("plugins", ImportDashboard)
  33. }
  34. func ImportDashboard(cmd *ImportDashboardCommand) error {
  35. plugin, exists := Plugins[cmd.PluginId]
  36. if !exists {
  37. return PluginNotFoundError{cmd.PluginId}
  38. }
  39. var dashboard *m.Dashboard
  40. var err error
  41. if dashboard, err = loadPluginDashboard(plugin, cmd.Path); err != nil {
  42. return err
  43. }
  44. evaluator := &DashTemplateEvaluator{
  45. template: dashboard.Data,
  46. inputs: cmd.Inputs,
  47. }
  48. generatedDash, err := evaluator.Eval()
  49. if err != nil {
  50. return err
  51. }
  52. saveCmd := m.SaveDashboardCommand{
  53. Dashboard: generatedDash,
  54. OrgId: cmd.OrgId,
  55. UserId: cmd.UserId,
  56. }
  57. if err := bus.Dispatch(&saveCmd); err != nil {
  58. return err
  59. }
  60. cmd.Result = &PluginDashboardInfoDTO{
  61. PluginId: cmd.PluginId,
  62. Title: dashboard.Title,
  63. Path: cmd.Path,
  64. Revision: dashboard.GetString("revision", "1.0"),
  65. InstalledUri: "db/" + saveCmd.Result.Slug,
  66. InstalledRevision: dashboard.GetString("revision", "1.0"),
  67. Installed: true,
  68. }
  69. return nil
  70. }
  71. type DashTemplateEvaluator struct {
  72. template *simplejson.Json
  73. inputs []ImportDashboardInput
  74. variables map[string]string
  75. result *simplejson.Json
  76. varRegex *regexp.Regexp
  77. }
  78. func (this *DashTemplateEvaluator) findInput(varName string, varDef *simplejson.Json) *ImportDashboardInput {
  79. inputType := varDef.Get("type").MustString()
  80. for _, input := range this.inputs {
  81. if inputType == input.Type && (input.Name == varName || input.Name == "*") {
  82. return &input
  83. }
  84. }
  85. return nil
  86. }
  87. func (this *DashTemplateEvaluator) Eval() (*simplejson.Json, error) {
  88. this.result = simplejson.New()
  89. this.variables = make(map[string]string)
  90. this.varRegex, _ = regexp.Compile("\\$__(\\w+)")
  91. // check that we have all inputs we need
  92. if inputDefs := this.template.Get("__inputs"); inputDefs != nil {
  93. for varName, value := range inputDefs.MustMap() {
  94. input := this.findInput(varName, simplejson.NewFromAny(value))
  95. if input == nil {
  96. return nil, &DashboardInputMissingError{VariableName: varName}
  97. }
  98. this.variables["$__"+varName] = input.Value
  99. }
  100. } else {
  101. log.Info("Import: dashboard has no __import section")
  102. }
  103. this.EvalObject(this.template, this.result)
  104. return this.result, nil
  105. }
  106. func (this *DashTemplateEvaluator) EvalObject(source *simplejson.Json, writer *simplejson.Json) {
  107. for key, value := range source.MustMap() {
  108. if key == "__inputs" {
  109. continue
  110. }
  111. switch v := value.(type) {
  112. case string:
  113. interpolated := this.varRegex.ReplaceAllStringFunc(v, func(match string) string {
  114. return this.variables[match]
  115. })
  116. writer.Set(key, interpolated)
  117. case map[string]interface{}:
  118. childSource := simplejson.NewFromAny(value)
  119. childWriter := simplejson.New()
  120. writer.Set(key, childWriter.Interface())
  121. this.EvalObject(childSource, childWriter)
  122. case []interface{}:
  123. default:
  124. log.Info("type: %v", reflect.TypeOf(value))
  125. log.Error(3, "Unknown json type key: %v , type: %v", key, value)
  126. }
  127. }
  128. }