statping/types/hits/samples.go

57 lines
1.1 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package hits
import (
2020-03-06 09:33:46 +00:00
"fmt"
"github.com/hunterlong/statping/database"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2020-03-04 14:20:47 +00:00
"sync"
2020-03-04 10:29:00 +00:00
"time"
2020-03-06 09:33:46 +00:00
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2020-03-04 10:29:00 +00:00
)
2020-03-04 14:20:47 +00:00
var SampleHits = 9900.
func Samples() {
tx := DB().Begin()
sg := new(sync.WaitGroup)
for i := int64(1); i <= 4; i++ {
2020-03-06 09:33:46 +00:00
err := createHitsAt(tx, i, sg)
if err != nil {
log.Error(err)
}
tx = DB().Begin()
2020-03-04 10:29:00 +00:00
}
2020-03-06 09:33:46 +00:00
}
func createHitsAt(db database.Database, serviceID int64, sg *sync.WaitGroup) error {
createdAt := utils.Now().Add(-30 * types.Day)
p := utils.NewPerlin(2, 2, 5, utils.Now().UnixNano())
i := 0
for hi := 0.; hi <= SampleHits; hi++ {
latency := p.Noise1D(hi / 500)
createdAt = createdAt.Add(10 * time.Minute)
hit := &Hit{
Service: serviceID,
Latency: latency,
PingTime: latency * 0.15,
CreatedAt: createdAt,
}
db = db.Create(&hit)
fmt.Printf("Creating hit %d hit %d: %.2f %v\n", serviceID, hit.Id, latency, createdAt.String())
i++
2020-03-04 14:20:47 +00:00
}
2020-03-06 09:33:46 +00:00
return db.Commit().Error()
2020-03-04 10:29:00 +00:00
}