Skip to content

Commit

Permalink
Fix first run of linear regression erroring out (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomperoo committed Aug 15, 2021
1 parent a02f9c2 commit b966131
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Updated to Custom Pod Autoscaler `v2.2.0`.
- Updated to Horizontal Pod Autoscaler CPA `v0.7.0`.
### Fixed
- Linear regression no longer fails on first run.

## [v0.7.0] - 2021-04-05
### Added
Expand Down
10 changes: 10 additions & 0 deletions internal/prediction/linear/linear.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ func (p *Predict) GetPrediction(model *config.Model, evaluations []*stored.Evalu
return 0, errors.New("No Linear configuration provided for model")
}

if len(evaluations) == 0 {
return 0, errors.New("No evaluations provided for Linear regression model")
}

if len(evaluations) == 1 {
// If only 1 evaluation is provided do not try and calculate using the linear regression model, just return
// the target replicas from the only evaluation
return evaluations[0].Evaluation.TargetReplicas, nil
}

parameters, err := json.Marshal(linearRegressionParameters{
LookAhead: model.Linear.LookAhead,
Evaluations: evaluations,
Expand Down
62 changes: 59 additions & 3 deletions internal/prediction/linear/linear_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,41 @@ func TestPredict_GetPrediction(t *testing.T) {
&config.Model{},
[]*stored.Evaluation{},
},
{
"Fail no evaluations",
0,
errors.New("No evaluations provided for Linear regression model"),
&linear.Predict{},
&config.Model{
Type: linear.Type,
Linear: &config.Linear{
StoredValues: 5,
LookAhead: 0,
},
},
[]*stored.Evaluation{},
},
{
"Success, only one evaluation, return without the prediction",
32,
nil,
&linear.Predict{},
&config.Model{
Type: linear.Type,
Linear: &config.Linear{
StoredValues: 5,
LookAhead: 0,
},
},
[]*stored.Evaluation{
{
ID: 0,
Evaluation: stored.DBEvaluation{
TargetReplicas: 32,
},
},
},
},
{
"Fail execution of algorithm fails",
0,
Expand All @@ -70,7 +105,14 @@ func TestPredict_GetPrediction(t *testing.T) {
LookAhead: 0,
},
},
[]*stored.Evaluation{},
[]*stored.Evaluation{
{
ID: 0,
},
{
ID: 1,
},
},
},
{
"Fail algorithm returns non-integer castable value",
Expand All @@ -90,7 +132,14 @@ func TestPredict_GetPrediction(t *testing.T) {
LookAhead: 0,
},
},
[]*stored.Evaluation{},
[]*stored.Evaluation{
{
ID: 0,
},
{
ID: 1,
},
},
},
{
"Success",
Expand All @@ -110,7 +159,14 @@ func TestPredict_GetPrediction(t *testing.T) {
LookAhead: 0,
},
},
[]*stored.Evaluation{},
[]*stored.Evaluation{
{
ID: 0,
},
{
ID: 1,
},
},
},
}
for _, test := range tests {
Expand Down

0 comments on commit b966131

Please sign in to comment.