Skip to content

Commit

Permalink
feat: add rowsAffected & fix json struct tag
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Nov 23, 2023
1 parent 318c204 commit 308a9b0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/model/types/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type CloneFromCNRequest struct {

type ArchiveDropReportRequest struct {
Date string `json:"date" validate:"required" required:"true"`
DeleteAfterArchive bool `json:"deleteAfterArchive validate:"required" required:"true"`
DeleteAfterArchive bool `json:"deleteAfterArchive" validate:"required" required:"true"`
}

type ForeignTimeRange struct {
Expand Down
11 changes: 8 additions & 3 deletions internal/repo/drop_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,20 @@ func (s *DropReport) GetDropReportsForArchive(ctx context.Context, cursor *model
return results, newCursor(results), nil
}

func (s *DropReport) DeleteDropReportsForArchive(ctx context.Context, tx bun.Tx, date time.Time) error {
// DeleteDropReportsForArchive deletes drop reports for archive.
// returns number of rows affected and error
func (s *DropReport) DeleteDropReportsForArchive(ctx context.Context, tx bun.Tx, date time.Time) (int64, error) {
start := time.UnixMilli(util.GetDayStartTime(&date, "CN")) // we use CN server's day start time across all servers for archive
end := start.Add(time.Hour * 24)
_, err := tx.NewDelete().
r, err := tx.NewDelete().
Model((*model.DropReport)(nil)).
Where("created_at >= to_timestamp(?)", start.Unix()).
Where("created_at < to_timestamp(?)", end.Unix()).
Exec(ctx)
return err
if err != nil {
return -1, err
}
return r.RowsAffected()
}

func (s *DropReport) handleStagesAndItems(query *bun.SelectQuery, stageIdItemIdMap map[int][]int) {
Expand Down
11 changes: 8 additions & 3 deletions internal/repo/drop_report_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ func (c *DropReportExtra) GetDropReportExtraForArchive(ctx context.Context, curs
return dropReportExtras, newCursor, nil
}

func (c *DropReportExtra) DeleteDropReportExtrasForArchive(ctx context.Context, tx bun.Tx, idInclusiveStart int, idInclusiveEnd int) error {
_, err := tx.NewDelete().
// DeleteDropReportExtrasForArchive deletes all drop report extras with report_id between idInclusiveStart and idInclusiveEnd.
// Returns the number of rows affected and an error if any.
func (c *DropReportExtra) DeleteDropReportExtrasForArchive(ctx context.Context, tx bun.Tx, idInclusiveStart int, idInclusiveEnd int) (int64, error) {
r, err := tx.NewDelete().
Model((*model.DropReportExtra)(nil)).
Where("report_id >= ?", idInclusiveStart).
Where("report_id <= ?", idInclusiveEnd).
Exec(ctx)
if err != nil {
return -1, err
}

return err
return r.RowsAffected()
}

func (c *DropReportExtra) IsDropReportExtraMD5Exist(ctx context.Context, md5 string) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/drop_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ func (s *DropReport) GetDropReportsForArchive(ctx context.Context, cursor *model
return s.DropReportRepo.GetDropReportsForArchive(ctx, cursor, date, limit)
}

func (s *DropReport) DeleteDropReportsForArchive(ctx context.Context, tx bun.Tx, date time.Time) error {
func (s *DropReport) DeleteDropReportsForArchive(ctx context.Context, tx bun.Tx, date time.Time) (int64, error) {
return s.DropReportRepo.DeleteDropReportsForArchive(ctx, tx, date)
}
7 changes: 5 additions & 2 deletions internal/service/drop_report_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,19 @@ func (s *Archive) DeleteReportsAndExtras(ctx context.Context, date time.Time, id
Int("last_id", idInclusiveEnd).
Msg("start deleting drop reports and extras")

err = s.DropReportService.DeleteDropReportsForArchive(ctx, tx, date)
var rowsAffected int64
rowsAffected, err = s.DropReportService.DeleteDropReportsForArchive(ctx, tx, date)
if err != nil {
return errors.Wrap(err, "failed to delete drop reports")
}

log.Info().
Int64("rows_affected", rowsAffected).
Str("evt.name", "archive.deletion.drop_report").
Str("date", date.Format("2006-01-02")).
Msg("finished deleting drop reports")

err = s.DropReportExtraService.DeleteDropReportExtrasForArchive(ctx, tx, idInclusiveStart, idInclusiveEnd)
rowsAffected, err = s.DropReportExtraService.DeleteDropReportExtrasForArchive(ctx, tx, idInclusiveStart, idInclusiveEnd)
if err != nil {
return errors.Wrap(err, "failed to delete drop report extras")
}
Expand All @@ -248,6 +250,7 @@ func (s *Archive) DeleteReportsAndExtras(ctx context.Context, date time.Time, id
Str("evt.name", "archive.deletion.drop_report_extra").
Int("first_id", idInclusiveStart).
Int("last_id", idInclusiveEnd).
Int64("rows_affected", rowsAffected).
Msg("finished deleting drop report extras")

if err := tx.Commit(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/drop_report_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func (s *DropReportExtra) GetDropReportExtraForArchive(ctx context.Context, curs
return s.DropReportExtraRepo.GetDropReportExtraForArchive(ctx, cursor, idInclusiveStart, idInclusiveEnd, limit)
}

func (c *DropReportExtra) DeleteDropReportExtrasForArchive(ctx context.Context, tx bun.Tx, idInclusiveStart int, idInclusiveEnd int) error {
func (c *DropReportExtra) DeleteDropReportExtrasForArchive(ctx context.Context, tx bun.Tx, idInclusiveStart int, idInclusiveEnd int) (int64, error) {
return c.DropReportExtraRepo.DeleteDropReportExtrasForArchive(ctx, tx, idInclusiveStart, idInclusiveEnd)
}

0 comments on commit 308a9b0

Please sign in to comment.