ops_query_mode.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package service
  2. import (
  3. "errors"
  4. "strings"
  5. )
  6. type OpsQueryMode string
  7. const (
  8. OpsQueryModeAuto OpsQueryMode = "auto"
  9. OpsQueryModeRaw OpsQueryMode = "raw"
  10. OpsQueryModePreagg OpsQueryMode = "preagg"
  11. )
  12. // ErrOpsPreaggregatedNotPopulated indicates that raw logs exist for a window, but the
  13. // pre-aggregation tables are not populated yet. This is primarily used to implement
  14. // the forced `preagg` mode UX.
  15. var ErrOpsPreaggregatedNotPopulated = errors.New("ops pre-aggregated tables not populated")
  16. func ParseOpsQueryMode(raw string) OpsQueryMode {
  17. v := strings.ToLower(strings.TrimSpace(raw))
  18. switch v {
  19. case string(OpsQueryModeRaw):
  20. return OpsQueryModeRaw
  21. case string(OpsQueryModePreagg):
  22. return OpsQueryModePreagg
  23. default:
  24. return OpsQueryModeAuto
  25. }
  26. }
  27. func (m OpsQueryMode) IsValid() bool {
  28. switch m {
  29. case OpsQueryModeAuto, OpsQueryModeRaw, OpsQueryModePreagg:
  30. return true
  31. default:
  32. return false
  33. }
  34. }
  35. func shouldFallbackOpsPreagg(filter *OpsDashboardFilter, err error) bool {
  36. return filter != nil &&
  37. filter.QueryMode == OpsQueryModeAuto &&
  38. errors.Is(err, ErrOpsPreaggregatedNotPopulated)
  39. }
  40. func cloneOpsFilterWithMode(filter *OpsDashboardFilter, mode OpsQueryMode) *OpsDashboardFilter {
  41. if filter == nil {
  42. return nil
  43. }
  44. cloned := *filter
  45. cloned.QueryMode = mode
  46. return &cloned
  47. }