search.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package api
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/torkelo/grafana-pro/pkg/bus"
  6. "github.com/torkelo/grafana-pro/pkg/middleware"
  7. m "github.com/torkelo/grafana-pro/pkg/models"
  8. )
  9. func Search(c *middleware.Context) {
  10. queryText := c.Query("q")
  11. result := m.SearchResult{
  12. Dashboards: []*m.DashboardSearchHit{},
  13. Tags: []*m.DashboardTagCloudItem{},
  14. }
  15. if strings.HasPrefix(queryText, "tags!:") {
  16. query := m.GetDashboardTagsQuery{}
  17. err := bus.Dispatch(&query)
  18. if err != nil {
  19. c.JsonApiErr(500, "Failed to get tags from database", err)
  20. return
  21. }
  22. result.Tags = query.Result
  23. result.TagsOnly = true
  24. } else {
  25. searchQueryRegEx, _ := regexp.Compile(`(tags:(\w*)\sAND\s)?(?:title:)?(.*)?`)
  26. matches := searchQueryRegEx.FindStringSubmatch(queryText)
  27. query := m.SearchDashboardsQuery{
  28. Title: matches[3],
  29. Tag: matches[2],
  30. AccountId: c.GetAccountId(),
  31. }
  32. err := bus.Dispatch(&query)
  33. if err != nil {
  34. c.JsonApiErr(500, "Search failed", err)
  35. return
  36. }
  37. result.Dashboards = query.Result
  38. }
  39. c.JSON(200, result)
  40. }