You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.5 KiB
70 lines
1.5 KiB
6 months ago
|
package service
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/emirpasic/gods/v2/sets/hashset"
|
||
|
"os"
|
||
|
"rition-testsuite/client"
|
||
|
"sort"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func (s *Service) Run() {
|
||
|
data, err := os.ReadFile(s.config.DataFileLocation)
|
||
|
if err != nil {
|
||
|
fmt.Println(err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
fileDataItemList := make([]client.DataItem, 0)
|
||
|
err = json.Unmarshal(data, &fileDataItemList)
|
||
|
if err != nil {
|
||
|
fmt.Println(err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
sort.Slice(fileDataItemList, func(i, j int) bool {
|
||
|
return fileDataItemList[i].Timestamp < fileDataItemList[j].Timestamp
|
||
|
})
|
||
|
|
||
|
times := hashset.New[int64]()
|
||
|
fileDataMap := map[int64][]client.DataItem{}
|
||
|
for _, dataItem := range fileDataItemList {
|
||
|
if _, has := fileDataMap[dataItem.Timestamp]; !has {
|
||
|
fileDataMap[dataItem.Timestamp] = make([]client.DataItem, 0, 22)
|
||
|
}
|
||
|
|
||
|
fileDataMap[dataItem.Timestamp] = append(fileDataMap[dataItem.Timestamp], dataItem)
|
||
|
times.Add(dataItem.Timestamp)
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
timesValue := times.Values()
|
||
|
sort.Slice(timesValue, func(i, j int) bool {
|
||
|
return timesValue[i] < timesValue[j]
|
||
|
})
|
||
|
|
||
|
fileDataItemList = nil
|
||
|
times = nil
|
||
|
|
||
|
ticker := time.NewTicker(s.config.ReportInterval)
|
||
|
defer ticker.Stop()
|
||
|
for range ticker.C {
|
||
|
if i < len(timesValue) {
|
||
|
fmt.Printf("reporting: %d\n", timesValue[i])
|
||
|
s.Report(fileDataMap[timesValue[i]])
|
||
|
i++
|
||
|
} else {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Service) Report(dataList []client.DataItem) {
|
||
|
_, err := s.client.Report(dataList)
|
||
|
if err != nil {
|
||
|
fmt.Println("err: ", err.Error())
|
||
|
}
|
||
|
}
|