2020-03-04 10:29:00 +00:00
|
|
|
package hits
|
|
|
|
|
|
|
|
import (
|
2020-03-09 15:15:15 +00:00
|
|
|
"fmt"
|
2020-04-16 09:57:00 +00:00
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/database"
|
|
|
|
"github.com/statping/statping/types"
|
|
|
|
"github.com/statping/statping/utils"
|
2020-03-04 10:29:00 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-03-06 22:18:06 +00:00
|
|
|
var SampleHits = 99900.
|
2020-03-04 14:20:47 +00:00
|
|
|
|
2020-03-09 15:15:15 +00:00
|
|
|
func Samples() error {
|
2020-03-06 22:18:06 +00:00
|
|
|
for i := int64(1); i <= 5; i++ {
|
2020-04-16 09:57:00 +00:00
|
|
|
tx := db.Begin()
|
|
|
|
tx = createHitsAt(tx, i)
|
|
|
|
|
|
|
|
if err := tx.Commit().Error(); err != nil {
|
2020-03-06 09:33:46 +00:00
|
|
|
log.Error(err)
|
2020-04-16 09:57:00 +00:00
|
|
|
return err
|
2020-03-06 09:33:46 +00:00
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 09:57:00 +00:00
|
|
|
return nil
|
2020-03-06 09:33:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 09:57:00 +00:00
|
|
|
func createHitsAt(db database.Database, serviceID int64) database.Database {
|
2020-04-24 02:26:09 +00:00
|
|
|
log.Infoln(fmt.Sprintf("Adding Sample records to service #%d...", serviceID))
|
2020-03-09 15:15:15 +00:00
|
|
|
|
2020-03-06 22:18:06 +00:00
|
|
|
createdAt := utils.Now().Add(-3 * types.Day)
|
2020-03-06 09:33:46 +00:00
|
|
|
p := utils.NewPerlin(2, 2, 5, utils.Now().UnixNano())
|
|
|
|
|
|
|
|
for hi := 0.; hi <= SampleHits; hi++ {
|
|
|
|
latency := p.Noise1D(hi / 500)
|
|
|
|
|
|
|
|
hit := &Hit{
|
|
|
|
Service: serviceID,
|
2020-03-10 05:24:35 +00:00
|
|
|
Latency: int64(latency * 10000000),
|
|
|
|
PingTime: int64(latency * 5000000),
|
2020-03-06 09:33:46 +00:00
|
|
|
CreatedAt: createdAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
db = db.Create(&hit)
|
2020-03-09 15:15:15 +00:00
|
|
|
|
2020-03-06 22:18:06 +00:00
|
|
|
if createdAt.After(utils.Now()) {
|
|
|
|
break
|
|
|
|
}
|
2020-04-16 09:57:00 +00:00
|
|
|
createdAt = createdAt.Add(30 * time.Second)
|
2020-03-04 14:20:47 +00:00
|
|
|
}
|
2020-03-06 09:33:46 +00:00
|
|
|
|
2020-04-16 09:57:00 +00:00
|
|
|
return db
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|