github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/stats/stats.go (about) 1 package stats 2 3 import ( 4 "github.com/v2pro/plz/countlog/spi" 5 ) 6 7 type EventAggregator struct { 8 executor Executor 9 collector Collector 10 } 11 12 type EventAggregatorConfig struct { 13 Executor Executor 14 Collector Collector 15 } 16 17 func NewEventAggregator(cfg EventAggregatorConfig) *EventAggregator { 18 executor := cfg.Executor 19 if executor == nil { 20 executor = DefaultExecutor 21 } 22 return &EventAggregator{ 23 executor: executor, 24 collector: cfg.Collector, 25 } 26 } 27 28 func (aggregator *EventAggregator) HandlerOf(site *spi.LogSite) spi.EventHandler { 29 if site.Agg != "" { 30 return aggregator.createHandler(site.Agg, site) 31 } 32 sample := site.Sample 33 for i := 0; i < len(sample); i += 2 { 34 if sample[i].(string) == "agg" { 35 return aggregator.createHandler(sample[i+1].(string), site) 36 } 37 } 38 return nil 39 } 40 41 func (aggregator *EventAggregator) createHandler(agg string, site *spi.LogSite) spi.EventHandler { 42 if aggregator.collector == nil { 43 // disable aggregation if collector not set 44 return &spi.DummyEventHandler{Site: site} 45 } 46 extractor, dimensionElemCount := newDimensionExtractor(site) 47 window := newWindow(aggregator.executor, aggregator.collector, dimensionElemCount) 48 switch agg { 49 case "counter": 50 return &countEvent{ 51 Window: window, 52 extractor: extractor, 53 site: site, 54 } 55 default: 56 // TODO: log unknown agg 57 } 58 return nil 59 }