спеціалізована СУБД для зберігання та обробки показань датчиків та лічильників
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.
diploma/examples/requests/generate.go

82 lines
1.7 KiB

3 months ago
package main
import (
"math/rand"
"time"
3 months ago
"gordenko.dev/dima/diploma/proto"
3 months ago
)
3 months ago
func GenerateCumulativeMeasures(days int) []proto.Measure {
3 months ago
var (
3 months ago
measures []proto.Measure
3 months ago
minutes = []int{14, 29, 44, 59}
hoursPerDay = 24
totalHours = days * hoursPerDay
since = time.Now().AddDate(0, 0, -days)
totalValue float64
)
for i := range totalHours {
hourTime := since.Add(time.Duration(i) * time.Hour)
for _, m := range minutes {
measureTime := time.Date(
hourTime.Year(),
hourTime.Month(),
hourTime.Day(),
hourTime.Hour(),
m, // minutes
0, // seconds
0, // nanoseconds
time.Local,
)
3 months ago
measure := proto.Measure{
3 months ago
Timestamp: uint32(measureTime.Unix()),
Value: totalValue,
}
measures = append(measures, measure)
totalValue += rand.Float64()
}
}
return measures
}
3 months ago
func GenerateInstantMeasures(days int, baseValue float64) []proto.Measure {
3 months ago
var (
3 months ago
measures []proto.Measure
3 months ago
minutes = []int{14, 29, 44, 59}
hoursPerDay = 24
totalHours = days * hoursPerDay
since = time.Now().AddDate(0, 0, -days)
)
for i := range totalHours {
hourTime := since.Add(time.Duration(i) * time.Hour)
for _, m := range minutes {
measureTime := time.Date(
hourTime.Year(),
hourTime.Month(),
hourTime.Day(),
hourTime.Hour(),
m, // minutes
0, // seconds
0, // nanoseconds
time.Local,
)
// value = +-10% from base value
fluctuation := baseValue * 0.1
value := baseValue + (rand.Float64()*2-1)*fluctuation
3 months ago
measure := proto.Measure{
3 months ago
Timestamp: uint32(measureTime.Unix()),
Value: value,
}
measures = append(measures, measure)
}
}
return measures
}