statping/types/failures/samples.go

50 lines
935 B
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package failures
import (
"fmt"
"github.com/hunterlong/statping/types"
2020-03-06 22:18:06 +00:00
"github.com/hunterlong/statping/utils"
2020-03-04 10:29:00 +00:00
"github.com/prometheus/common/log"
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-09 15:15:15 +00:00
func Samples() error {
2020-03-04 14:20:47 +00:00
tx := DB().Begin()
sg := new(sync.WaitGroup)
2020-03-04 10:29:00 +00:00
2020-03-06 22:18:06 +00:00
createdAt := utils.Now().Add(-3 * types.Day)
2020-03-04 10:29:00 +00:00
2020-03-04 14:20:47 +00:00
for i := int64(1); i <= 4; i++ {
sg.Add(1)
2020-03-04 10:29:00 +00:00
2020-03-09 15:15:15 +00:00
log.Infoln(fmt.Sprintf("Adding %v Failure records to service", 400))
2020-03-04 10:29:00 +00:00
2020-03-04 14:20:47 +00:00
go func() {
defer sg.Done()
2020-03-06 22:18:06 +00:00
for fi := 0.; fi <= float64(400); fi++ {
createdAt = createdAt.Add(35 * time.Minute)
2020-03-04 14:20:47 +00:00
failure := &Failure{
Service: i,
Issue: "testing right here",
CreatedAt: createdAt.UTC(),
}
tx = tx.Create(&failure)
2020-03-04 10:29:00 +00:00
}
2020-03-04 14:20:47 +00:00
}()
}
sg.Wait()
2020-03-04 10:29:00 +00:00
2020-03-04 14:20:47 +00:00
if err := tx.Commit().Error(); err != nil {
log.Error(err)
2020-03-09 15:15:15 +00:00
return err
2020-03-04 10:29:00 +00:00
}
2020-03-04 14:20:47 +00:00
2020-03-09 15:15:15 +00:00
return nil
2020-03-04 10:29:00 +00:00
}