Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
primitives
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xx network
primitives
Merge requests
!34
Release
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Release
release
into
master
Overview
0
Commits
52
Pipelines
0
Changes
56
Merged
Jake Taylor
requested to merge
release
into
master
2 years ago
Overview
0
Commits
52
Pipelines
0
Changes
56
Expand
0
0
Merge request reports
Compare
master
version 17
888a035f
2 years ago
version 16
86ea5c08
2 years ago
version 15
940cdd68
2 years ago
version 14
c440e68e
2 years ago
version 13
97747936
2 years ago
version 12
81c2cb07
2 years ago
version 11
4b5550a9
2 years ago
version 10
40740914
2 years ago
version 9
52d260a8
2 years ago
version 8
37673546
2 years ago
version 7
fd6ea305
2 years ago
version 6
f0d440be
2 years ago
version 5
cbec15a7
2 years ago
version 4
492f78b2
2 years ago
version 3
cab1210c
2 years ago
version 2
0a942267
2 years ago
version 1
42169a3e
2 years ago
master (base)
and
latest version
latest version
bc6fc6e5
52 commits,
1 year ago
version 17
888a035f
50 commits,
2 years ago
version 16
86ea5c08
48 commits,
2 years ago
version 15
940cdd68
46 commits,
2 years ago
version 14
c440e68e
44 commits,
2 years ago
version 13
97747936
39 commits,
2 years ago
version 12
81c2cb07
36 commits,
2 years ago
version 11
4b5550a9
34 commits,
2 years ago
version 10
40740914
32 commits,
2 years ago
version 9
52d260a8
30 commits,
2 years ago
version 8
37673546
29 commits,
2 years ago
version 7
fd6ea305
27 commits,
2 years ago
version 6
f0d440be
25 commits,
2 years ago
version 5
cbec15a7
23 commits,
2 years ago
version 4
492f78b2
22 commits,
2 years ago
version 3
cab1210c
21 commits,
2 years ago
version 2
0a942267
19 commits,
2 years ago
version 1
42169a3e
18 commits,
2 years ago
56 files
+
2770
−
1825
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
56
Search (e.g. *.vue) (Ctrl+P)
exponential/movingAvg.go
0 → 100644
+
96
−
0
Options
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
package
exponential
import
(
"sync"
"github.com/pkg/errors"
jww
"github.com/spf13/jwalterweatherman"
)
// MovingAvg tracks the exponential moving average across a number of events and
// reports when it has surpassed the set cutoff.
type
MovingAvg
struct
{
// The maximum limit for the moving average aN allowed.
cutoff
float32
// A(n), the current exponential moving average (initialize to A(0)).
aN
float32
// Exponential smoothing factor; gives the most recent events more weight.
// The greater the smoothing factor, the greater the influence of more
// recent events.
s
float32
// The number of events to average over.
e
uint32
sync
.
Mutex
}
// NewMovingAvg creates a new MovingAvg with the given cutoff, initial average,
// smoothing factor, and number of events to average.
func
NewMovingAvg
(
p
MovingAvgParams
)
*
MovingAvg
{
jww
.
TRACE
.
Printf
(
"[MAVG] Tracking new exponential moving average: %+v"
,
p
)
return
&
MovingAvg
{
cutoff
:
p
.
Cutoff
,
aN
:
p
.
InitialAverage
,
s
:
p
.
SmoothingFactor
,
e
:
p
.
NumberOfEvents
,
}
}
// Intake takes in the current average and calculates the exponential average
// returning true if it is over the cutoff and false otherwise.
//
// The moving average is calculated by:
//
// A(n) = a × (S/E) + A(n-1) × (1 − S/E)
//
// Where:
//
// A(n) is the current exponential moving average
// A(n-1) is the previous exponential moving average
// a is the intake value
// S is the smoothing factor
// E is the number of events the average is over
func
(
m
*
MovingAvg
)
Intake
(
a
float32
)
error
{
m
.
Mutex
.
Lock
()
defer
m
.
Mutex
.
Unlock
()
// Calculate exponential moving average
k
:=
m
.
s
/
(
1
+
float32
(
m
.
e
))
m
.
aN
=
(
a
*
k
)
+
(
m
.
aN
*
(
1
-
k
))
jww
.
TRACE
.
Printf
(
"[MAVG] Intake %.4f: new moving average %.2f%% over %d events"
,
a
,
m
.
aN
*
100
,
m
.
e
)
if
m
.
aN
>
m
.
cutoff
{
return
errors
.
Errorf
(
"exponential average for the last %d events of "
+
"%.2f%% went over cutoff %.2f%%"
,
m
.
e
,
m
.
aN
*
100
,
m
.
cutoff
*
100
)
}
return
nil
}
// IsOverCutoff returns true if the average has reached the cutoff and false if
// it has not
func
(
m
*
MovingAvg
)
IsOverCutoff
()
bool
{
m
.
Mutex
.
Lock
()
defer
m
.
Mutex
.
Unlock
()
return
m
.
aN
>
m
.
cutoff
}
// BoolToFloat returns 1 if true and 0 if false.
func
BoolToFloat
(
b
bool
)
float32
{
if
b
{
return
1
}
return
0
}
Loading