search.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. "github.com/torkelo/grafana-pro/pkg/setting"
  9. )
  10. func Search(c *middleware.Context) {
  11. queryText := c.Query("q")
  12. starred := c.Query("starred")
  13. result := m.SearchResult{
  14. Dashboards: []*m.DashboardSearchHit{},
  15. Tags: []*m.DashboardTagCloudItem{},
  16. }
  17. if strings.HasPrefix(queryText, "tags!:") {
  18. query := m.GetDashboardTagsQuery{AccountId: c.AccountId}
  19. err := bus.Dispatch(&query)
  20. if err != nil {
  21. c.JsonApiErr(500, "Failed to get tags from database", err)
  22. return
  23. }
  24. result.Tags = query.Result
  25. result.TagsOnly = true
  26. } else {
  27. searchQueryRegEx, _ := regexp.Compile(`(tags:(\w*)\sAND\s)?(?:title:)?(.*)?`)
  28. matches := searchQueryRegEx.FindStringSubmatch(queryText)
  29. query := m.SearchDashboardsQuery{
  30. Title: matches[3],
  31. Tag: matches[2],
  32. UserId: c.UserId,
  33. IsStarred: starred == "1",
  34. AccountId: c.AccountId,
  35. }
  36. err := bus.Dispatch(&query)
  37. if err != nil {
  38. c.JsonApiErr(500, "Search failed", err)
  39. return
  40. }
  41. result.Dashboards = query.Result
  42. for _, dash := range result.Dashboards {
  43. dash.Url = setting.AbsUrlTo("dashboard/db/" + dash.Slug)
  44. }
  45. }
  46. c.JSON(200, result)
  47. }