improve performance, bump github.com/tribalwarshelp/shared

This commit is contained in:
Dawid Wysokiński 2020-07-07 09:57:41 +02:00 committed by Kichiyaki
parent 9c78f11b51
commit c2afb2144a
4 changed files with 16 additions and 31 deletions

View File

@ -202,40 +202,26 @@ func (h *handler) updateServersData() {
return
}
var wg sync.WaitGroup
max := runtime.NumCPU()
count := 0
for _, server := range servers {
url, ok := urls[server.Key]
if !ok {
log.Printf("No one URL associated with key: %s, skipping...", server.Key)
continue
}
if count >= max {
wg.Wait()
count = 0
}
sh := &updateServerDataHandler{
db: h.db.WithParam("SERVER", pg.Safe(server.Key)),
baseURL: url,
server: server,
}
count++
wg.Add(1)
go func(server *models.Server, sh *updateServerDataHandler) {
defer wg.Done()
log.Printf("%s: updating data", server.Key)
if err := sh.update(); err != nil {
log.Println(errors.Wrap(err, server.Key))
return
} else {
log.Printf("%s: data updated", server.Key)
}
}(server, sh)
log.Printf("%s: updating data", server.Key)
if err := sh.update(); err != nil {
log.Println(errors.Wrap(err, server.Key))
return
} else {
log.Printf("%s: data updated", server.Key)
}
}
wg.Wait()
}
func (h *handler) updateServersHistory() {

View File

@ -405,7 +405,7 @@ func (h *updateServerDataHandler) isDateTheSameAsServerHistoryUpdatedAt(t time.T
t.Day() == h.server.HistoryUpdatedAt.Day()
}
func (h *updateServerDataHandler) calculateODDifference(od1 models.OpponentsDefeated, od2 models.OpponentsDefeated) models.OpponentsDefeated {
func (h *updateServerDataHandler) calculateODifference(od1 models.OpponentsDefeated, od2 models.OpponentsDefeated) models.OpponentsDefeated {
return models.OpponentsDefeated{
RankAtt: (od1.RankAtt - od2.RankAtt) * -1,
ScoreAtt: od1.ScoreAtt - od2.ScoreAtt,
@ -437,7 +437,7 @@ func (h *updateServerDataHandler) calculateDailyTribeStats(tribes []*models.Trib
Rank: (tribe.Rank - historyRecord.Rank) * -1,
Dominance: tribe.Dominance - historyRecord.Dominance,
CreateDate: historyRecord.CreateDate,
OpponentsDefeated: h.calculateODDifference(tribe.OpponentsDefeated, historyRecord.OpponentsDefeated),
OpponentsDefeated: h.calculateODifference(tribe.OpponentsDefeated, historyRecord.OpponentsDefeated),
})
}
}
@ -461,7 +461,7 @@ func (h *updateServerDataHandler) calculateDailyPlayerStats(players []*models.Pl
Points: player.Points - historyRecord.Points,
Rank: (player.Rank - historyRecord.Rank) * -1,
CreateDate: historyRecord.CreateDate,
OpponentsDefeated: h.calculateODDifference(player.OpponentsDefeated, historyRecord.OpponentsDefeated),
OpponentsDefeated: h.calculateODifference(player.OpponentsDefeated, historyRecord.OpponentsDefeated),
})
}
}
@ -542,8 +542,8 @@ func (h *updateServerDataHandler) update() error {
for _, tribe := range tribes {
ids = append(ids, tribe.ID)
}
if _, err := tx.Model(&models.Tribe{}).
Where("id NOT IN (?)", pg.In(ids)).
if _, err := tx.Model(&tribes).
Where("tribe.id NOT IN (?)", pg.In(ids)).
Set("exists = false").
Update(); err != nil && err != pg.ErrNoRows {
return errors.Wrap(err, "cannot update not exist tribes")

3
go.mod
View File

@ -9,8 +9,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/segmentio/encoding v0.1.14 // indirect
github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87
github.com/tribalwarshelp/twcron v0.0.0-20200629130116-b320263c4502 // indirect
github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
google.golang.org/grpc v1.30.0 // indirect

4
go.sum
View File

@ -94,8 +94,8 @@ github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYm
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87 h1:TMuZUk0wW+8dXSGVJVLRFFhLONKKYJRnLBFvbb9XTrE=
github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
github.com/tribalwarshelp/twcron v0.0.0-20200629130116-b320263c4502 h1:6sIsm8dw+9qY3mdVJUSaJOfXWcrWxJrlLA8u3uTcADs=
github.com/tribalwarshelp/twcron v0.0.0-20200629130116-b320263c4502/go.mod h1:dLYqb08YNQQUu4KNVm69nf7GOjwM96DoXNmI5CLuBLE=
github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c h1:wenSReGHPux51q5s+9Ji0TOHCt8Zx6whxsjx8Iv7aZg=
github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
github.com/vmihailenco/bufpool v0.1.5/go.mod h1:fL9i/PRTuS7AELqAHwSU1Zf1c70xhkhGe/cD5ud9pJk=
github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94=
github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=