multidimensional.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2019 The Vitess Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package stats
  14. import (
  15. "fmt"
  16. "strings"
  17. )
  18. // MultiTracker is a CountTracker that tracks counts grouping them by
  19. // more than one dimension.
  20. type MultiTracker interface {
  21. CountTracker
  22. Labels() []string
  23. }
  24. // CounterForDimension returns a CountTracker for the provided
  25. // dimension. It will panic if the dimension isn't a legal label for
  26. // mt.
  27. func CounterForDimension(mt MultiTracker, dimension string) CountTracker {
  28. for i, lab := range mt.Labels() {
  29. if lab == dimension {
  30. return wrappedCountTracker{
  31. f: func() map[string]int64 {
  32. result := make(map[string]int64)
  33. for k, v := range mt.Counts() {
  34. if k == "All" {
  35. result[k] = v
  36. continue
  37. }
  38. result[strings.Split(k, ".")[i]] += v
  39. }
  40. return result
  41. },
  42. }
  43. }
  44. }
  45. panic(fmt.Sprintf("label %v is not one of %v", dimension, mt.Labels()))
  46. }