@@ -45,6 +45,14 @@ func TestNewAggregator_Invalid(t *testing.T) {
4545 Input : "Input" ,
4646 })
4747 require .NotNil (t , err )
48+
49+ // invalid first agg
50+ _ , err = newAggregator (api.OutputField {
51+ Operation : "first" ,
52+ SplitAB : true ,
53+ Input : "Input" ,
54+ })
55+ require .NotNil (t , err )
4856}
4957
5058func TestNewAggregator_Valid (t * testing.T ) {
@@ -56,27 +64,27 @@ func TestNewAggregator_Valid(t *testing.T) {
5664 {
5765 name : "Default SplitAB" ,
5866 outputField : api.OutputField {Name : "MyAgg" , Operation : "sum" },
59- expected : & aSum {aggregateBase {"MyAgg" , "MyAgg" , false , 0 }},
67+ expected : & aSum {aggregateBase {"MyAgg" , "MyAgg" , false , float64 ( 0 ) }},
6068 },
6169 {
6270 name : "Default input" ,
6371 outputField : api.OutputField {Name : "MyAgg" , Operation : "sum" , SplitAB : true },
64- expected : & aSum {aggregateBase {"MyAgg" , "MyAgg" , true , 0 }},
72+ expected : & aSum {aggregateBase {"MyAgg" , "MyAgg" , true , float64 ( 0 ) }},
6573 },
6674 {
6775 name : "Custom input" ,
6876 outputField : api.OutputField {Name : "MyAgg" , Operation : "sum" , Input : "MyInput" },
69- expected : & aSum {aggregateBase {"MyInput" , "MyAgg" , false , 0 }},
77+ expected : & aSum {aggregateBase {"MyInput" , "MyAgg" , false , float64 ( 0 ) }},
7078 },
7179 {
7280 name : "OperationType sum" ,
7381 outputField : api.OutputField {Name : "MyAgg" , Operation : "sum" },
74- expected : & aSum {aggregateBase {"MyAgg" , "MyAgg" , false , 0 }},
82+ expected : & aSum {aggregateBase {"MyAgg" , "MyAgg" , false , float64 ( 0 ) }},
7583 },
7684 {
7785 name : "OperationType count" ,
7886 outputField : api.OutputField {Name : "MyAgg" , Operation : "count" },
79- expected : & aCount {aggregateBase {"MyAgg" , "MyAgg" , false , 0 }},
87+ expected : & aCount {aggregateBase {"MyAgg" , "MyAgg" , false , float64 ( 0 ) }},
8088 },
8189 {
8290 name : "OperationType max" ,
@@ -88,6 +96,21 @@ func TestNewAggregator_Valid(t *testing.T) {
8896 outputField : api.OutputField {Name : "MyAgg" , Operation : "min" },
8997 expected : & aMin {aggregateBase {"MyAgg" , "MyAgg" , false , math .MaxFloat64 }},
9098 },
99+ {
100+ name : "Default first" ,
101+ outputField : api.OutputField {Name : "MyCp" , Operation : "first" },
102+ expected : & aFirst {aggregateBase {"MyCp" , "MyCp" , false , nil }},
103+ },
104+ {
105+ name : "Custom input first" ,
106+ outputField : api.OutputField {Name : "MyCp" , Operation : "first" , Input : "MyInput" },
107+ expected : & aFirst {aggregateBase {"MyInput" , "MyCp" , false , nil }},
108+ },
109+ {
110+ name : "Default last" ,
111+ outputField : api.OutputField {Name : "MyCp" , Operation : "last" },
112+ expected : & aLast {aggregateBase {"MyCp" , "MyCp" , false , nil }},
113+ },
91114 }
92115
93116 for _ , test := range table {
@@ -106,6 +129,8 @@ func TestAddField_and_Update(t *testing.T) {
106129 {Name : "numFlowLogs" , Operation : "count" },
107130 {Name : "minFlowLogBytes" , Operation : "min" , Input : "Bytes" },
108131 {Name : "maxFlowLogBytes" , Operation : "max" , Input : "Bytes" },
132+ {Name : "FirstFlowDirection" , Operation : "first" , Input : "FlowDirection" },
133+ {Name : "LastFlowDirection" , Operation : "last" , Input : "FlowDirection" },
109134 }
110135 var aggs []aggregator
111136 for _ , of := range ofs {
@@ -119,38 +144,40 @@ func TestAddField_and_Update(t *testing.T) {
119144 portA := 1
120145 portB := 9002
121146 protocolA := 6
147+ flowDirA := 0
148+ flowDirB := 1
122149
123150 table := []struct {
124151 name string
125152 flowLog config.GenericMap
126153 direction direction
127- expected map [string ]float64
154+ expected map [string ]interface {}
128155 }{
129156 {
130157 name : "flowLog 1" ,
131- flowLog : newMockFlowLog (ipA , portA , ipB , portB , protocolA , 100 , 10 , false ),
158+ flowLog : newMockFlowLog (ipA , portA , ipB , portB , protocolA , flowDirA , 100 , 10 , false ),
132159 direction : dirAB ,
133- expected : map [string ]float64 { "Bytes_AB" : 100 , "Bytes_BA" : 0 , "Packets" : 10 , "maxFlowLogBytes" : 100 , "minFlowLogBytes" : 100 , "numFlowLogs" : 1 },
160+ expected : map [string ]interface {}{ "Bytes_AB" : float64 ( 100 ) , "Bytes_BA" : float64 ( 0 ) , "Packets" : float64 ( 10 ) , "maxFlowLogBytes" : float64 ( 100 ) , "minFlowLogBytes" : float64 ( 100 ) , "numFlowLogs" : float64 ( 1 ), "FirstFlowDirection" : 0 , "LastFlowDirection" : 0 },
134161 },
135162 {
136163 name : "flowLog 2" ,
137- flowLog : newMockFlowLog (ipA , portA , ipB , portB , protocolA , 200 , 20 , false ),
164+ flowLog : newMockFlowLog (ipA , portA , ipB , portB , protocolA , flowDirB , 200 , 20 , false ),
138165 direction : dirBA ,
139- expected : map [string ]float64 { "Bytes_AB" : 100 , "Bytes_BA" : 200 , "Packets" : 30 , "maxFlowLogBytes" : 200 , "minFlowLogBytes" : 100 , "numFlowLogs" : 2 },
166+ expected : map [string ]interface {}{ "Bytes_AB" : float64 ( 100 ) , "Bytes_BA" : float64 ( 200 ) , "Packets" : float64 ( 30 ) , "maxFlowLogBytes" : float64 ( 200 ) , "minFlowLogBytes" : float64 ( 100 ) , "numFlowLogs" : float64 ( 2 ), "FirstFlowDirection" : 0 , "LastFlowDirection" : 1 },
140167 },
141168 }
142169
143170 conn := NewConnBuilder (nil ).Build ()
144171 for _ , agg := range aggs {
145172 agg .addField (conn )
146173 }
147- expectedInits := map [string ]float64 { "Bytes_AB" : 0 , "Bytes_BA" : 0 , "Packets" : 0 , "maxFlowLogBytes" : - math .MaxFloat64 , "minFlowLogBytes" : math .MaxFloat64 , "numFlowLogs" : 0 }
174+ expectedInits := map [string ]interface {}{ "Bytes_AB" : float64 ( 0 ) , "Bytes_BA" : float64 ( 0 ) , "Packets" : float64 ( 0 ) , "maxFlowLogBytes" : float64 ( - math .MaxFloat64 ) , "minFlowLogBytes" : float64 ( math .MaxFloat64 ) , "numFlowLogs" : float64 ( 0 ), "FirstFlowDirection" : nil , "LastFlowDirection" : nil }
148175 require .Equal (t , expectedInits , conn .(* connType ).aggFields )
149176
150- for _ , test := range table {
177+ for i , test := range table {
151178 t .Run (test .name , func (t * testing.T ) {
152179 for _ , agg := range aggs {
153- agg .update (conn , test .flowLog , test .direction )
180+ agg .update (conn , test .flowLog , test .direction , i == 0 )
154181 }
155182 require .Equal (t , test .expected , conn .(* connType ).aggFields )
156183 })
0 commit comments