tweaking retention policy code so we can test downsampling scripts.

This commit is contained in:
Jason Kulatunga
2021-11-16 18:32:29 -08:00
parent 8fb58591a6
commit b776fb8886
19 changed files with 730 additions and 346 deletions
+97
View File
@@ -0,0 +1,97 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func main() {
//webapp/backend/pkg/web/testdata/register-devices-req.json
devices := "webapp/backend/pkg/web/testdata/register-devices-req.json"
smartData := map[string][]string{
"0x5000cca264eb01d7": {"webapp/backend/pkg/models/testdata/smart-ata.json", "webapp/backend/pkg/models/testdata/smart-ata-date.json", "webapp/backend/pkg/models/testdata/smart-ata-date2.json"},
"0x5000cca264ec3183": {"webapp/backend/pkg/models/testdata/smart-fail2.json"},
"0x5002538e40a22954": {"webapp/backend/pkg/models/testdata/smart-nvme.json"},
"0x5000cca252c859cc": {"webapp/backend/pkg/models/testdata/smart-scsi.json"},
"0x5000cca264ebc248": {"webapp/backend/pkg/models/testdata/smart-scsi2.json"},
}
// send a post request to register devices
file, err := os.Open(devices)
if err != nil {
log.Fatalf("ERROR %v", err)
}
defer file.Close()
_, err = SendPostRequest("http://localhost:8080/api/devices/register", file)
if err != nil {
log.Fatalf("ERROR %v", err)
}
//
for diskId, smartDataFileNames := range smartData {
for _, smartDataFileName := range smartDataFileNames {
for daysToSubtract := 0; daysToSubtract <= 30; daysToSubtract++ { //add 4 weeks worth of data
smartDataReader, err := readSmartDataFileFixTimestamp(daysToSubtract, smartDataFileName)
if err != nil {
log.Fatalf("ERROR %v", err)
}
_, err = SendPostRequest(fmt.Sprintf("http://localhost:8080/api/device/%s/smart", diskId), smartDataReader)
if err != nil {
log.Fatalf("ERROR %v", err)
}
}
}
}
}
func SendPostRequest(url string, file io.Reader) ([]byte, error) {
response, err := http.Post(url, "application/json", file)
if err != nil {
return nil, err
}
defer response.Body.Close()
log.Printf("%v\n", response.Status)
return ioutil.ReadAll(response.Body)
}
// InfluxDB will throw an error/ignore any submitted data with a timestamp older than the
// retention period. Lets fix this by opening test files, modifying the timestamp and returning an io.Reader
func readSmartDataFileFixTimestamp(daysToSubtract int, smartDataFilepath string) (io.Reader, error) {
metricsfile, err := os.Open(smartDataFilepath)
if err != nil {
return nil, err
}
metricsFileData, err := ioutil.ReadAll(metricsfile)
if err != nil {
return nil, err
}
//unmarshal because we need to change the timestamp
var smartData collector.SmartInfo
err = json.Unmarshal(metricsFileData, &smartData)
if err != nil {
return nil, err
}
daysToSubtractInHours := time.Duration(-1 * 24 * daysToSubtract)
smartData.LocalTime.TimeT = time.Now().Add(daysToSubtractInHours * time.Hour).Unix()
updatedSmartDataBytes, err := json.Marshal(smartData)
return bytes.NewReader(updatedSmartDataBytes), nil
}
+1 -1
View File
@@ -69,7 +69,7 @@
}
},
"local_time": {
"time_t": 1635107644,
"time_t": 1637039918,
"asctime": "Sun Jun 30 00:03:30 2021 UTC"
},
"smart_status": {
+1 -1
View File
@@ -69,7 +69,7 @@
}
},
"local_time": {
"time_t": 1635127644,
"time_t": 1637039918,
"asctime": "Tue Feb 23 00:03:30 2021 UTC"
},
"smart_status": {
+1 -1
View File
@@ -70,7 +70,7 @@
}
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Sun Sep 13 16:29:23 2020 UTC"
},
"read_lookahead": {
+1 -1
View File
@@ -69,7 +69,7 @@
}
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Sun Jun 21 00:03:30 2020 UTC"
},
"smart_status": {
+1 -1
View File
@@ -66,7 +66,7 @@
}
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Thu Aug 01 15:05:13 2019 WEDT"
},
"smart_status": {
+1 -1
View File
@@ -70,7 +70,7 @@
}
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Wed Jul 8 15:48:23 2020 CEST"
},
"smart_status": {
+1 -1
View File
@@ -79,7 +79,7 @@
}
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Mon Aug 24 21:38:38 2020 CEST"
},
"smart_status": {
+1 -1
View File
@@ -79,7 +79,7 @@
}
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Mon Aug 24 21:38:42 2020 CEST"
},
"smart_status": {
+1 -1
View File
@@ -59,7 +59,7 @@
},
"logical_block_size": 512,
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Wed Jun 10 14:01:02 2020 CEST"
},
"smart_status": {
+1 -1
View File
@@ -67,7 +67,7 @@
},
"logical_block_size": 512,
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Sun Sep 20 16:24:50 2020 Europe"
},
"smart_status": {
+1 -1
View File
@@ -43,7 +43,7 @@
"name": "disk"
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Wed Oct 09 10:31:07 2019 RDT"
},
"temperature": {
+1 -1
View File
@@ -26,7 +26,7 @@
"name": "disk"
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Fri Aug 21 22:27:02 2020 UTC"
},
"smart_status": {
+1 -1
View File
@@ -44,7 +44,7 @@
"name": "disk"
},
"local_time": {
"time_t": 1635117644,
"time_t": 1637039918,
"asctime": "Sun Dec 16 17:09:15 2018 CST"
},
"smart_status": {