Skip to content

Commit

Permalink
Fix: opg by partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jul 10, 2023
1 parent 64754b2 commit f2925c6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
38 changes: 38 additions & 0 deletions internal/helpers/quarters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package helpers

import (
"errors"
"time"
)

// QuarterOf -
func QuarterOf(month time.Month) int {
return (int(month) + 2) / 3
}

// QuarterBoundaries -
func QuarterBoundaries(current time.Time) (time.Time, time.Time, error) {
year := current.Year()
quarter := QuarterOf(current.Month())

switch quarter {
case 1:
start := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
case 2:
start := time.Date(year, time.April, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
case 3:
start := time.Date(year, time.July, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
case 4:
start := time.Date(year, time.October, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
}

return time.Now(), time.Now(), errors.New("invalid quarter")
}
37 changes: 32 additions & 5 deletions internal/postgres/operation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"fmt"
"time"

"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/bcd/consts"
"github.com/baking-bad/bcdhub/internal/helpers"
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/account"
"github.com/baking-bad/bcdhub/internal/models/contract"
"github.com/baking-bad/bcdhub/internal/models/operation"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/baking-bad/bcdhub/internal/postgres/core"
Expand Down Expand Up @@ -251,15 +255,35 @@ func (storage *Storage) OPG(address string, size, lastID int64) ([]operation.OPG
result = make([]operation.OPG, 0)
currentOpg operation.OPG
isAdded bool
lastAction = time.Now().UTC()
limit = 1000
)

if bcd.IsContractLazy(address) {
if err := storage.DB.Model((*contract.Contract)(nil)).
Column("last_action").
Where("account_id = ?", accountID).
Select(&lastAction); err != nil {
return nil, err
}
}

for !end {
startTime, endTime, err := helpers.QuarterBoundaries(lastAction)
if err != nil {
return nil, err
}

subQuery := storage.DB.Model((*operation.Operation)(nil)).
Column("id", "hash", "counter", "entrypoint", "amount", "fee", "burned", "level", "content_index", "timestamp", "kind", "status", "internal", "destination_id", "source_id").
WhereGroup(
func(q *orm.Query) (*orm.Query, error) {
return q.Where("destination_id = ?", accountID).WhereOr("source_id = ?", accountID), nil
},
).
Limit(1000).
Where("timestamp < ?", endTime).
Where("timestamp >= ?", startTime).
Limit(limit).
Order("id desc")

if lastID > 0 {
Expand All @@ -271,10 +295,6 @@ func (storage *Storage) OPG(address string, size, lastID int64) ([]operation.OPG
return nil, err
}

if len(ops) == 0 {
break
}

for i := range ops {
if !bytes.Equal(currentOpg.Hash, ops[i].Hash) && currentOpg.Counter != ops[i].Counter {
if len(currentOpg.Hash) > 0 && currentOpg.Counter > 0 {
Expand Down Expand Up @@ -322,6 +342,13 @@ func (storage *Storage) OPG(address string, size, lastID int64) ([]operation.OPG
}

lastID = currentOpg.LastID

if len(ops) < limit {
lastAction = lastAction.AddDate(0, -3, 0)
if lastAction.Before(consts.BeginningOfTime) {
break
}
}
}

if len(currentOpg.Hash) > 0 && currentOpg.Counter > 0 && !isAdded {
Expand Down
36 changes: 3 additions & 33 deletions internal/postgres/partition_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package postgres

import (
"context"
"errors"
"fmt"
"time"

"github.com/baking-bad/bcdhub/internal/helpers"
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/bigmapdiff"
"github.com/baking-bad/bcdhub/internal/models/operation"
Expand All @@ -29,38 +29,8 @@ func NewPartitionManager(conn *core.Postgres) *PartitionManager {

const createPartitionTemplate = `CREATE TABLE IF NOT EXISTS ? PARTITION OF ? FOR VALUES FROM (?) TO (?);`

func quarterOf(month time.Month) int {
return (int(month) + 2) / 3
}

func quarterBoundaries(current time.Time) (time.Time, time.Time, error) {
year := current.Year()
quarter := quarterOf(current.Month())

switch quarter {
case 1:
start := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
case 2:
start := time.Date(year, time.April, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
case 3:
start := time.Date(year, time.July, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
case 4:
start := time.Date(year, time.October, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(0, 3, 0)
return start, end, nil
}

return time.Now(), time.Now(), errors.New("invalid quarter")
}

func (pm *PartitionManager) partitionId(currentTime time.Time) string {
return fmt.Sprintf("%dQ%d", currentTime.Year(), quarterOf(currentTime.Month()))
return fmt.Sprintf("%dQ%d", currentTime.Year(), helpers.QuarterOf(currentTime.Month()))
}

// CreatePartitions -
Expand All @@ -70,7 +40,7 @@ func (pm *PartitionManager) CreatePartitions(ctx context.Context, currentTime ti
return nil
}

start, end, err := quarterBoundaries(currentTime)
start, end, err := helpers.QuarterBoundaries(currentTime)
if err != nil {
return err
}
Expand Down

0 comments on commit f2925c6

Please sign in to comment.