@@ -13,8 +13,8 @@ type EventRecorder struct {
1313 auth string
1414 eventsUrl string
1515 flushInterval time.Duration
16- incomingEvents []AccessEvent
17- packedData [] PackedData
16+ incomingEvents []interface {}
17+ access Access
1818 httpClient http.Client
1919 mu sync.Mutex
2020 wg sync.WaitGroup
@@ -25,16 +25,27 @@ type EventRecorder struct {
2525}
2626
2727type AccessEvent struct {
28- Time int64 `json:"time"`
29- Key string `json:"key"`
30- Value interface {} `json:"value"`
31- Index * int `json:"index"`
32- Version * uint64 `json:"version"`
33- Reason string `json:"reason"`
28+ Kind string `json:"kind"`
29+ Time int64 `json:"time"`
30+ User string `json:"user"`
31+ Key string `json:"key"`
32+ Value interface {} `json:"value"`
33+ VariationIndex * int `json:"variationIndex"`
34+ RuleIndex * int `json:"ruleIndex"`
35+ Version * uint64 `json:"version"`
36+ Reason string `json:"reason"`
37+ }
38+
39+ type CustomEvent struct {
40+ Kind string `json:"kind"`
41+ Time int64 `json:"time"`
42+ User string `json:"user"`
43+ Name string `json:"name"`
44+ Value * float64 `json:"value"`
3445}
3546
3647type PackedData struct {
37- Events []AccessEvent `json:"events"`
48+ Events []interface {} `json:"events"`
3849 Access Access `json:"access"`
3950}
4051
@@ -67,17 +78,32 @@ func NewEventRecorder(eventsUrl string, flushInterval time.Duration, auth string
6778 auth : auth ,
6879 eventsUrl : eventsUrl ,
6980 flushInterval : flushInterval ,
70- incomingEvents : []AccessEvent {},
71- packedData : [] PackedData {} ,
81+ incomingEvents : []interface {} {},
82+ access : newAccess () ,
7283 httpClient : newHttpClient (flushInterval ),
7384 stopChan : make (chan struct {}),
7485 }
7586}
7687
88+ func newAccess () Access {
89+ return Access {
90+ Counters : make (map [string ][]ToggleCounter ),
91+ }
92+ }
93+
94+ func nowToggleCounter (value interface {}, version * uint64 , index * int ) ToggleCounter {
95+ return ToggleCounter {
96+ value ,
97+ version ,
98+ index ,
99+ 1 ,
100+ }
101+ }
102+
77103func (e * EventRecorder ) Start () {
78104 e .wg .Add (1 )
79105 e .startOnce .Do (func () {
80- e .ticker = time .NewTicker (e .flushInterval * time . Millisecond )
106+ e .ticker = time .NewTicker (e .flushInterval )
81107 go func () {
82108 for {
83109 select {
@@ -94,14 +120,15 @@ func (e *EventRecorder) Start() {
94120}
95121
96122func (e * EventRecorder ) doFlush () {
97- events := make ([]AccessEvent , 0 )
123+ events := make ([]interface {} , 0 )
98124 e .mu .Lock ()
99125 events , e .incomingEvents = e .incomingEvents , events
126+ packedData := e .buildPackedData (events )
127+ e .access = newAccess ()
100128 e .mu .Unlock ()
101- if len (events ) == 0 {
129+ if len (events ) == 0 && len ( packedData [ 0 ]. Access . Counters ) == 0 {
102130 return
103131 }
104- packedData := e .buildPackedData (events )
105132 body , _ := json .Marshal (packedData )
106133 req , err := http .NewRequest (http .MethodPost , e .eventsUrl , bytes .NewBuffer (body ))
107134 if err != nil {
@@ -117,62 +144,42 @@ func (e *EventRecorder) doFlush() {
117144 }
118145}
119146
120- func (e * EventRecorder ) buildPackedData (events []AccessEvent ) []PackedData {
121- access := e . buildAccess ( events )
122- p := PackedData {Access : access , Events : events }
147+ func (e * EventRecorder ) buildPackedData (events []interface {} ) []PackedData {
148+ e . access . EndTime = time . Now (). UnixNano () / 1e6
149+ p := PackedData {Access : e . access , Events : events }
123150 return []PackedData {p }
124151}
125152
126- func (e * EventRecorder ) buildAccess (events []AccessEvent ) Access {
127- counters , startTime , endTime := e .buildCounters (events )
128- access := Access {
129- StartTime : startTime ,
130- EndTime : endTime ,
131- Counters : map [string ][]ToggleCounter {},
153+ func (e * EventRecorder ) addAccess (event AccessEvent ) {
154+ if len (e .access .Counters ) == 0 {
155+ e .access .StartTime = time .Now ().UnixNano () / 1e6
132156 }
133-
134- for k , v := range counters {
135- counter := ToggleCounter {
136- Index : k .Index ,
137- Version : k .Version ,
138- Count : v .Count ,
139- Value : v .Value ,
140- }
141- c , ok := access .Counters [k .Key ]
142- if ! ok {
143- access .Counters [k .Key ] = []ToggleCounter {counter }
144- } else {
145- access .Counters [k .Key ] = append (c , counter )
157+ counters , ok := e .access .Counters [event .Key ]
158+ if ok {
159+ for index , counter := range counters {
160+ if * counter .Version == * event .Version && * counter .Index == * event .VariationIndex {
161+ counters [index ].Count = counter .Count + 1
162+ return
163+ }
146164 }
165+ e .access .Counters [event .Key ] = append (counters ,
166+ nowToggleCounter (event .Value , event .Version , event .VariationIndex ))
167+ } else {
168+ e .access .Counters [event .Key ] = []ToggleCounter {
169+ nowToggleCounter (event .Value , event .Version , event .VariationIndex )}
147170 }
148- return access
149171}
150172
151- func (e * EventRecorder ) buildCounters (events []AccessEvent ) (map [Variation ]CountValue , int64 , int64 ) {
152- var startTime * int64 = nil
153- var endTime * int64 = nil
154- counters := map [Variation ]CountValue {}
155-
156- for _ , event := range events {
157- if startTime == nil || * startTime < event .Time {
158- startTime = & event .Time
159- }
160- if endTime == nil || * endTime > event .Time {
161- endTime = & event .Time
162- }
163-
164- v := Variation {Key : event .Key , Version : event .Version , Index : event .Index }
165- c , ok := counters [v ]
166- if ! ok {
167- counters [v ] = CountValue {Count : 1 , Value : event .Value }
168- } else {
169- c .Count += 1
170- }
173+ func (e * EventRecorder ) RecordAccess (event AccessEvent , trackAccessEvents bool ) {
174+ e .mu .Lock ()
175+ if trackAccessEvents {
176+ e .incomingEvents = append (e .incomingEvents , event )
171177 }
172- return counters , * startTime , * endTime
178+ e .addAccess (event )
179+ e .mu .Unlock ()
173180}
174181
175- func (e * EventRecorder ) RecordAccess (event AccessEvent ) {
182+ func (e * EventRecorder ) RecordCustom (event CustomEvent ) {
176183 e .mu .Lock ()
177184 e .incomingEvents = append (e .incomingEvents , event )
178185 e .mu .Unlock ()
0 commit comments