Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Mizu
Commits
86f9035d
Commit
86f9035d
authored
2 years ago
by
Roee Gadot
Browse files
Options
Download
Email Patches
Plain Diff
no message
parent
a0faf859
fix/add-tests-for-buckets-statistics
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
agent/pkg/providers/stats_provider.go
+5
-5
agent/pkg/providers/stats_provider.go
agent/pkg/providers/stats_provider_internal_test.go
+331
-0
agent/pkg/providers/stats_provider_internal_test.go
agent/pkg/providers/stats_provider_test.go
+0
-52
agent/pkg/providers/stats_provider_test.go
with
336 additions
and
57 deletions
+336
-57
agent/pkg/providers/stats_provider.go
+
5
-
5
View file @
86f9035d
...
...
@@ -86,7 +86,7 @@ func GetAccumulativeStatsTiming(intervalSeconds int, numberOfBars int) []*Accumu
return
make
([]
*
AccumulativeStatsProtocolTime
,
0
)
}
firstBucketTime
:=
G
etBucketBorders
(
time
.
Now
()
.
UTC
(),
intervalSeconds
,
numberOfBars
)
firstBucketTime
:=
g
etBucketBorders
(
time
.
Now
()
.
UTC
(),
intervalSeconds
,
numberOfBars
)
methodsPerProtocolPerTimeAggregated
,
protocolToColor
:=
getAggregatedResultTimingFromSpecificTime
(
intervalSeconds
,
bucketStatsCopy
,
firstBucketTime
)
...
...
@@ -109,7 +109,7 @@ func EntryAdded(size int, summery *api.BaseEntry) {
}
func
addToBucketStats
(
size
int
,
summery
*
api
.
BaseEntry
)
{
entryTimeBucketRounded
:=
G
etBucketOfTimeStamp
(
summery
.
Timestamp
)
entryTimeBucketRounded
:=
g
etBucketOfTimeStamp
(
summery
.
Timestamp
)
if
len
(
bucketsStats
)
==
0
{
bucketsStats
=
append
(
bucketsStats
,
&
TimeFrameStatsValue
{
...
...
@@ -142,13 +142,13 @@ func addToBucketStats(size int, summery *api.BaseEntry) {
bucketOfEntry
.
ProtocolStats
[
summery
.
Protocol
.
Abbreviation
]
.
MethodsStats
[
summery
.
Method
]
.
VolumeInBytes
+=
size
}
//
G
etBucketOfTimeStamp Round the entry to the nearest threshold (one minute) floored (e.g: 15:31:45 -> 15:31:00)
func
G
etBucketOfTimeStamp
(
timestamp
int64
)
time
.
Time
{
//
g
etBucketOfTimeStamp Round the entry to the nearest threshold (one minute) floored (e.g: 15:31:45 -> 15:31:00)
func
g
etBucketOfTimeStamp
(
timestamp
int64
)
time
.
Time
{
entryTimeStampAsTime
:=
time
.
UnixMilli
(
timestamp
)
return
entryTimeStampAsTime
.
Add
(
-
1
*
InternalBucketThreshold
/
2
)
.
Round
(
InternalBucketThreshold
)
}
func
G
etBucketBorders
(
endTime
time
.
Time
,
intervalSeconds
int
,
numberOfBars
int
)
time
.
Time
{
func
g
etBucketBorders
(
endTime
time
.
Time
,
intervalSeconds
int
,
numberOfBars
int
)
time
.
Time
{
lastBucketTime
:=
endTime
.
Add
(
-
1
*
time
.
Second
*
time
.
Duration
(
intervalSeconds
)
/
2
)
.
Round
(
time
.
Second
*
time
.
Duration
(
intervalSeconds
))
firstBucketTime
:=
lastBucketTime
.
Add
(
-
1
*
time
.
Second
*
time
.
Duration
(
intervalSeconds
*
(
numberOfBars
-
1
)))
return
firstBucketTime
...
...
This diff is collapsed.
Click to expand it.
agent/pkg/providers/stats_provider_internal_test.go
0 → 100644
+
331
-
0
View file @
86f9035d
package
providers
import
(
"fmt"
"reflect"
"testing"
"time"
)
func
TestGetBucketOfTimeStamp
(
t
*
testing
.
T
)
{
tests
:=
map
[
int64
]
time
.
Time
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
45
,
0
,
time
.
Local
)
.
UnixMilli
()
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
00
,
0
,
time
.
Local
),
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
00
,
0
,
time
.
Local
)
.
UnixMilli
()
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
00
,
0
,
time
.
Local
),
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
59
,
01
,
0
,
time
.
Local
)
.
UnixMilli
()
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
59
,
00
,
0
,
time
.
Local
),
}
for
key
,
value
:=
range
tests
{
t
.
Run
(
fmt
.
Sprintf
(
"%v"
,
key
),
func
(
t
*
testing
.
T
)
{
actual
:=
getBucketOfTimeStamp
(
key
)
if
actual
!=
value
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
value
,
actual
)
}
})
}
}
type
DataForBucketBorderFunction
struct
{
EndTime
time
.
Time
IntervalInSeconds
int
NumberOfBars
int
}
func
TestGetBucketBorders
(
t
*
testing
.
T
)
{
tests
:=
map
[
DataForBucketBorderFunction
]
time
.
Time
{
DataForBucketBorderFunction
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
45
,
0
,
time
.
UTC
),
300
,
10
,
}
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
9
,
45
,
0
,
0
,
time
.
UTC
),
DataForBucketBorderFunction
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
35
,
45
,
0
,
time
.
UTC
),
60
,
5
,
}
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
31
,
00
,
0
,
time
.
UTC
),
}
for
key
,
value
:=
range
tests
{
t
.
Run
(
fmt
.
Sprintf
(
"%v"
,
key
),
func
(
t
*
testing
.
T
)
{
actual
:=
getBucketBorders
(
key
.
EndTime
,
key
.
IntervalInSeconds
,
key
.
NumberOfBars
)
if
actual
!=
value
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
value
,
actual
)
}
})
}
}
func
TestGetAggregatedStatsAllTime
(
t
*
testing
.
T
)
{
bucketStatsForTest
:=
BucketStats
{
&
TimeFrameStatsValue
{
BucketTime
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
),
ProtocolStats
:
map
[
string
]
ProtocolStats
{
"http"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"get"
:
{
EntriesCount
:
1
,
VolumeInBytes
:
2
,
},
"post"
:
{
EntriesCount
:
2
,
VolumeInBytes
:
3
,
},
},
},
"kafka"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"listTopics"
:
{
EntriesCount
:
5
,
VolumeInBytes
:
6
,
},
},
},
},
},
&
TimeFrameStatsValue
{
BucketTime
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
01
,
00
,
0
,
time
.
UTC
),
ProtocolStats
:
map
[
string
]
ProtocolStats
{
"http"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"get"
:
{
EntriesCount
:
1
,
VolumeInBytes
:
2
,
},
"post"
:
{
EntriesCount
:
2
,
VolumeInBytes
:
3
,
},
},
},
"redis"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"set"
:
{
EntriesCount
:
5
,
VolumeInBytes
:
6
,
},
},
},
},
},
}
expected
:=
map
[
string
]
map
[
string
]
*
AccumulativeStatsCounter
{
"http"
:
{
"post"
:
{
Name
:
"post"
,
EntriesCount
:
4
,
VolumeSizeBytes
:
6
,
},
"get"
:
{
Name
:
"get"
,
EntriesCount
:
2
,
VolumeSizeBytes
:
4
,
},
},
"kafka"
:
{
"listTopics"
:
{
Name
:
"listTopics"
,
EntriesCount
:
5
,
VolumeSizeBytes
:
6
,
},
},
"redis"
:
{
"set"
:
{
Name
:
"set"
,
EntriesCount
:
5
,
VolumeSizeBytes
:
6
,
},
},
}
actual
,
_
:=
getAggregatedStatsAllTime
(
bucketStatsForTest
)
if
!
reflect
.
DeepEqual
(
actual
,
expected
)
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
3
,
len
(
actual
))
}
}
func
TestGetAggregatedStatsFromSpecificTime
(
t
*
testing
.
T
)
{
bucketStatsForTest
:=
BucketStats
{
&
TimeFrameStatsValue
{
BucketTime
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
),
ProtocolStats
:
map
[
string
]
ProtocolStats
{
"http"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"get"
:
{
EntriesCount
:
1
,
VolumeInBytes
:
2
,
},
},
},
"kafka"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"listTopics"
:
{
EntriesCount
:
5
,
VolumeInBytes
:
6
,
},
},
},
},
},
&
TimeFrameStatsValue
{
BucketTime
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
01
,
00
,
0
,
time
.
UTC
),
ProtocolStats
:
map
[
string
]
ProtocolStats
{
"http"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"get"
:
{
EntriesCount
:
1
,
VolumeInBytes
:
2
,
},
"post"
:
{
EntriesCount
:
2
,
VolumeInBytes
:
3
,
},
},
},
"redis"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"set"
:
{
EntriesCount
:
5
,
VolumeInBytes
:
6
,
},
},
},
},
},
}
expected
:=
map
[
time
.
Time
]
map
[
string
]
map
[
string
]
*
AccumulativeStatsCounter
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
)
:
{
"http"
:
{
"post"
:
{
Name
:
"post"
,
EntriesCount
:
2
,
VolumeSizeBytes
:
3
,
},
"get"
:
{
Name
:
"get"
,
EntriesCount
:
2
,
VolumeSizeBytes
:
4
,
},
},
"kafka"
:
{
"listTopics"
:
{
Name
:
"listTopics"
,
EntriesCount
:
5
,
VolumeSizeBytes
:
6
,
},
},
"redis"
:
{
"set"
:
{
Name
:
"set"
,
EntriesCount
:
5
,
VolumeSizeBytes
:
6
,
},
},
},
}
actual
,
_
:=
getAggregatedResultTimingFromSpecificTime
(
300
,
bucketStatsForTest
,
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
))
if
!
reflect
.
DeepEqual
(
actual
,
expected
)
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
3
,
len
(
actual
))
}
}
func
TestGetAggregatedStatsFromSpecificTimeMultipleBuckets
(
t
*
testing
.
T
)
{
bucketStatsForTest
:=
BucketStats
{
&
TimeFrameStatsValue
{
BucketTime
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
),
ProtocolStats
:
map
[
string
]
ProtocolStats
{
"http"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"get"
:
{
EntriesCount
:
1
,
VolumeInBytes
:
2
,
},
},
},
"kafka"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"listTopics"
:
{
EntriesCount
:
5
,
VolumeInBytes
:
6
,
},
},
},
},
},
&
TimeFrameStatsValue
{
BucketTime
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
01
,
00
,
0
,
time
.
UTC
),
ProtocolStats
:
map
[
string
]
ProtocolStats
{
"http"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"get"
:
{
EntriesCount
:
1
,
VolumeInBytes
:
2
,
},
"post"
:
{
EntriesCount
:
2
,
VolumeInBytes
:
3
,
},
},
},
"redis"
:
{
MethodsStats
:
map
[
string
]
*
SizeAndEntriesCount
{
"set"
:
{
EntriesCount
:
5
,
VolumeInBytes
:
6
,
},
},
},
},
},
}
expected
:=
map
[
time
.
Time
]
map
[
string
]
map
[
string
]
*
AccumulativeStatsCounter
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
)
:
{
"http"
:
{
"get"
:
{
Name
:
"get"
,
EntriesCount
:
1
,
VolumeSizeBytes
:
2
,
},
},
"kafka"
:
{
"listTopics"
:
{
Name
:
"listTopics"
,
EntriesCount
:
5
,
VolumeSizeBytes
:
6
,
},
},
},
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
01
,
00
,
0
,
time
.
UTC
)
:
{
"http"
:
{
"post"
:
{
Name
:
"post"
,
EntriesCount
:
2
,
VolumeSizeBytes
:
3
,
},
"get"
:
{
Name
:
"get"
,
EntriesCount
:
1
,
VolumeSizeBytes
:
2
,
},
},
"redis"
:
{
"set"
:
{
Name
:
"set"
,
EntriesCount
:
5
,
VolumeSizeBytes
:
6
,
},
},
},
}
actual
,
_
:=
getAggregatedResultTimingFromSpecificTime
(
60
,
bucketStatsForTest
,
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
00
,
00
,
0
,
time
.
UTC
))
if
!
reflect
.
DeepEqual
(
actual
,
expected
)
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
3
,
len
(
actual
))
}
}
This diff is collapsed.
Click to expand it.
agent/pkg/providers/stats_provider_test.go
+
0
-
52
View file @
86f9035d
...
...
@@ -81,56 +81,4 @@ func TestEntryAddedVolume(t *testing.T) {
}
})
}
}
func
TestGetBucketOfTimeStamp
(
t
*
testing
.
T
)
{
tests
:=
map
[
int64
]
time
.
Time
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
45
,
0
,
time
.
Local
)
.
UnixMilli
()
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
00
,
0
,
time
.
Local
),
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
00
,
0
,
time
.
Local
)
.
UnixMilli
()
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
00
,
0
,
time
.
Local
),
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
59
,
01
,
0
,
time
.
Local
)
.
UnixMilli
()
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
59
,
00
,
0
,
time
.
Local
),
}
for
key
,
value
:=
range
tests
{
t
.
Run
(
fmt
.
Sprintf
(
"%v"
,
key
),
func
(
t
*
testing
.
T
)
{
actual
:=
providers
.
GetBucketOfTimeStamp
(
key
)
if
actual
!=
value
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
value
,
actual
)
}
})
}
}
type
DataForBucketBorderFunction
struct
{
EndTime
time
.
Time
IntervalInSeconds
int
NumberOfBars
int
}
func
TestGetBucketBorders
(
t
*
testing
.
T
)
{
tests
:=
map
[
DataForBucketBorderFunction
]
time
.
Time
{
DataForBucketBorderFunction
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
34
,
45
,
0
,
time
.
UTC
),
300
,
10
,
}
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
9
,
45
,
0
,
0
,
time
.
UTC
),
DataForBucketBorderFunction
{
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
35
,
45
,
0
,
time
.
UTC
),
60
,
5
,
}
:
time
.
Date
(
2022
,
time
.
Month
(
1
),
1
,
10
,
31
,
00
,
0
,
time
.
UTC
),
}
for
key
,
value
:=
range
tests
{
t
.
Run
(
fmt
.
Sprintf
(
"%v"
,
key
),
func
(
t
*
testing
.
T
)
{
actual
:=
providers
.
GetBucketBorders
(
key
.
EndTime
,
key
.
IntervalInSeconds
,
key
.
NumberOfBars
)
if
actual
!=
value
{
t
.
Errorf
(
"unexpected result - expected: %v, actual: %v"
,
value
,
actual
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help