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