Skip to content

Commit 10d20cf

Browse files
committed
ML: Example job using a power transformation
Add an example of using a power transformation on an ML job. Signed-off-by: Chris Marchbanks <[email protected]>
1 parent e93a250 commit 10d20cf

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

docs/resources/machine_learning_job.md

+38
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,44 @@ resource "grafana_machine_learning_job" "test_job" {
9696
}
9797
```
9898

99+
### Rescaled Forecast
100+
101+
This forecast has had the data transformed using a power transformation in order to avoid negative lower predictions.
102+
103+
```terraform
104+
resource "grafana_data_source" "foo" {
105+
type = "prometheus"
106+
name = "prometheus-ds-test"
107+
uid = "prometheus-ds-test-uid"
108+
url = "https://my-instance.com"
109+
basic_auth_enabled = true
110+
basic_auth_username = "username"
111+
112+
json_data_encoded = jsonencode({
113+
httpMethod = "POST"
114+
prometheusType = "Mimir"
115+
prometheusVersion = "2.4.0"
116+
})
117+
118+
secure_json_data_encoded = jsonencode({
119+
basicAuthPassword = "password"
120+
})
121+
}
122+
123+
resource "grafana_machine_learning_job" "test_job" {
124+
name = "Test Job"
125+
metric = "tf_test_job"
126+
datasource_type = "prometheus"
127+
datasource_uid = grafana_data_source.foo.uid
128+
query_params = {
129+
expr = "grafanacloud_grafana_instance_active_user_count"
130+
}
131+
hyper_params = {
132+
transformation_id = "power"
133+
}
134+
}
135+
```
136+
99137
### Forecast with Holidays
100138

101139
This forecast has holidays which will be taken into account when training the model.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
resource "grafana_data_source" "foo" {
2+
type = "prometheus"
3+
name = "prometheus-ds-test"
4+
uid = "prometheus-ds-test-uid"
5+
url = "https://my-instance.com"
6+
basic_auth_enabled = true
7+
basic_auth_username = "username"
8+
9+
json_data_encoded = jsonencode({
10+
httpMethod = "POST"
11+
prometheusType = "Mimir"
12+
prometheusVersion = "2.4.0"
13+
})
14+
15+
secure_json_data_encoded = jsonencode({
16+
basicAuthPassword = "password"
17+
})
18+
}
19+
20+
resource "grafana_machine_learning_job" "test_job" {
21+
name = "Test Job"
22+
metric = "tf_test_job"
23+
datasource_type = "prometheus"
24+
datasource_uid = grafana_data_source.foo.uid
25+
query_params = {
26+
expr = "grafanacloud_grafana_instance_active_user_count"
27+
}
28+
hyper_params = {
29+
transformation_id = "power"
30+
}
31+
}

internal/resources/machinelearning/resource_job_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ func TestAccResourceJob(t *testing.T) {
8181
resource.TestCheckResourceAttrSet("grafana_machine_learning_job.test_job", "holidays.0"),
8282
),
8383
},
84+
{
85+
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_machine_learning_job/transformed_job.tf", map[string]string{
86+
"Test Job": randomName,
87+
}),
88+
Check: resource.ComposeTestCheckFunc(
89+
resource.TestCheckResourceAttrSet("grafana_machine_learning_job.test_job", "id"),
90+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "name", randomName),
91+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "metric", "tf_test_job"),
92+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "datasource_type", "prometheus"),
93+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "datasource_uid", "prometheus-ds-test-uid"),
94+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "query_params.expr", "grafanacloud_grafana_instance_active_user_count"),
95+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "interval", "300"),
96+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "training_window", "7776000"),
97+
resource.TestCheckResourceAttr("grafana_machine_learning_job.test_job", "hyper_params.transformation_id", "power"),
98+
),
99+
},
84100
},
85101
})
86102
}

templates/resources/machine_learning_job.md.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ This forecast has tuned hyperparameters to improve the accuracy of the model.
2626

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

29+
### Rescaled Forecast
30+
31+
This forecast has had the data transformed using a power transformation in order to avoid negative lower predictions.
32+
33+
{{ tffile "examples/resources/grafana_machine_learning_job/transformed_job.tf" }}
34+
2935
### Forecast with Holidays
3036

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

0 commit comments

Comments
 (0)