Skip to content

Commit

Permalink
ML: Example job using a power transformation (#2021)
Browse files Browse the repository at this point in the history
Add an example of using a power transformation on an ML job.

Signed-off-by: Chris Marchbanks <[email protected]>
  • Loading branch information
csmarchbanks authored Feb 4, 2025
1 parent e93a250 commit 484ce41
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/resources/machine_learning_job.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ resource "grafana_machine_learning_job" "test_job" {
}
```

### Rescaled Forecast

This forecast has had the data transformed using a power transformation in order to avoid negative lower predictions.

```terraform
resource "grafana_data_source" "foo" {
type = "prometheus"
name = "prometheus-ds-test"
uid = "prometheus-ds-test-uid"
url = "https://my-instance.com"
basic_auth_enabled = true
basic_auth_username = "username"
json_data_encoded = jsonencode({
httpMethod = "POST"
prometheusType = "Mimir"
prometheusVersion = "2.4.0"
})
secure_json_data_encoded = jsonencode({
basicAuthPassword = "password"
})
}
resource "grafana_machine_learning_job" "test_job" {
name = "Test Job"
metric = "tf_test_job"
datasource_type = "prometheus"
datasource_uid = grafana_data_source.foo.uid
query_params = {
expr = "grafanacloud_grafana_instance_active_user_count"
}
hyper_params = {
transformation_id = "power"
}
}
```

### Forecast with Holidays

This forecast has holidays which will be taken into account when training the model.
Expand Down
31 changes: 31 additions & 0 deletions examples/resources/grafana_machine_learning_job/transformed_job.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "grafana_data_source" "foo" {
type = "prometheus"
name = "prometheus-ds-test"
uid = "prometheus-ds-test-uid"
url = "https://my-instance.com"
basic_auth_enabled = true
basic_auth_username = "username"

json_data_encoded = jsonencode({
httpMethod = "POST"
prometheusType = "Mimir"
prometheusVersion = "2.4.0"
})

secure_json_data_encoded = jsonencode({
basicAuthPassword = "password"
})
}

resource "grafana_machine_learning_job" "test_job" {
name = "Test Job"
metric = "tf_test_job"
datasource_type = "prometheus"
datasource_uid = grafana_data_source.foo.uid
query_params = {
expr = "grafanacloud_grafana_instance_active_user_count"
}
hyper_params = {
transformation_id = "power"
}
}
16 changes: 16 additions & 0 deletions internal/resources/machinelearning/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ func TestAccResourceJob(t *testing.T) {
resource.TestCheckResourceAttrSet("grafana_machine_learning_job.test_job", "holidays.0"),
),
},
{
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_machine_learning_job/transformed_job.tf", map[string]string{
"Test Job": randomName,
}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("grafana_machine_learning_job.test_job", "id"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "name", randomName),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "metric", "tf_test_job"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "datasource_type", "prometheus"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "datasource_uid", "prometheus-ds-test-uid"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "query_params.expr", "grafanacloud_grafana_instance_active_user_count"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "interval", "300"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "training_window", "7776000"),
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "hyper_params.transformation_id", "power"),
),
},
},
})
}
Expand Down
6 changes: 6 additions & 0 deletions templates/resources/machine_learning_job.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ This forecast has tuned hyperparameters to improve the accuracy of the model.

{{ tffile "examples/resources/grafana_machine_learning_job/tuned_job.tf" }}

### Rescaled Forecast

This forecast has had the data transformed using a power transformation in order to avoid negative lower predictions.

{{ tffile "examples/resources/grafana_machine_learning_job/transformed_job.tf" }}

### Forecast with Holidays

This forecast has holidays which will be taken into account when training the model.
Expand Down

0 comments on commit 484ce41

Please sign in to comment.