Skip to content

Commit d2616e7

Browse files
authored
Merge pull request #507 from penguin-statistics/dev
Release v3.14.3
2 parents 2363af1 + cf06f99 commit d2616e7

File tree

13 files changed

+134
-9
lines changed

13 files changed

+134
-9
lines changed

.github/workflows/pr-auto-create.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
push:
66
branches:
77
- dev
8+
# trigger on manual workflow_dispatch
9+
workflow_dispatch:
810

911
concurrency:
1012
group: pr-creator-${{ github.ref }}

cmd/app/cli/runscript/scripts/archive_drop_reports/command.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ func Command(depsFn func() CommandDeps) *cli.Command {
2424
Usage: "date to archive in GMT+8, in format of YYYY-MM-DD",
2525
Required: true,
2626
},
27+
&cli.BoolFlag{
28+
Name: "delete-after-archive",
29+
Aliases: []string{"D"},
30+
Usage: "delete the archived drop reports and extras after archiving",
31+
Required: true,
32+
},
2733
},
2834
Action: func(ctx *cli.Context) error {
2935
date := ctx.String("date")
30-
return run(ctx, depsFn(), date)
36+
deleteAfterArchive := ctx.Bool("delete-after-archive")
37+
return run(ctx, depsFn(), date, deleteAfterArchive)
3138
},
3239
}
3340
}

cmd/app/cli/runscript/scripts/archive_drop_reports/script.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/urfave/cli/v2"
1010
)
1111

12-
func run(ctx *cli.Context, deps CommandDeps, dateStr string) error {
12+
func run(ctx *cli.Context, deps CommandDeps, dateStr string, deleteAfterArchive bool) error {
1313
log.Info().Str("date", dateStr).Msg("running script")
1414

1515
var err error
@@ -19,7 +19,7 @@ func run(ctx *cli.Context, deps CommandDeps, dateStr string) error {
1919
return errors.Wrap(err, "failed to parse date")
2020
}
2121

22-
if err = deps.DropReportArchiveService.ArchiveByDate(ctx.Context, date); err != nil {
22+
if err = deps.DropReportArchiveService.ArchiveByDate(ctx.Context, date, deleteAfterArchive); err != nil {
2323
return errors.Wrap(err, "failed to run archiveDropReports")
2424
}
2525

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/avast/retry-go/v4 v4.3.4
1111
github.com/aws/aws-sdk-go-v2 v1.23.1
1212
github.com/aws/aws-sdk-go-v2/config v1.25.4
13-
github.com/aws/aws-sdk-go-v2/credentials v1.16.3
13+
github.com/aws/aws-sdk-go-v2/credentials v1.16.4
1414
github.com/aws/aws-sdk-go-v2/service/s3 v1.43.1
1515
github.com/aws/smithy-go v1.17.0
1616
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ github.com/aws/aws-sdk-go-v2/config v1.25.4 h1:r+X1x8QI6FEPdJDWCNBDZHyAcyFwSjHN8
7171
github.com/aws/aws-sdk-go-v2/config v1.25.4/go.mod h1:8GTjImECskr7D88P/Nn9uM4M4rLY9i77hLJZgkZEWV8=
7272
github.com/aws/aws-sdk-go-v2/credentials v1.16.3 h1:8PeI2krzzjDJ5etmgaMiD1JswsrLrWvKKu/uBUtNy1g=
7373
github.com/aws/aws-sdk-go-v2/credentials v1.16.3/go.mod h1:Kdh/okh+//vQ/AjEt81CjvkTo64+/zIE4OewP7RpfXk=
74+
github.com/aws/aws-sdk-go-v2/credentials v1.16.4 h1:i7UQYYDSJrtc30RSwJwfBKwLFNnBTiICqAJ0pPdum8E=
75+
github.com/aws/aws-sdk-go-v2/credentials v1.16.4/go.mod h1:Kdh/okh+//vQ/AjEt81CjvkTo64+/zIE4OewP7RpfXk=
7476
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.5 h1:KehRNiVzIfAcj6gw98zotVbb/K67taJE0fkfgM6vzqU=
7577
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.5/go.mod h1:VhnExhw6uXy9QzetvpXDolo1/hjhx4u9qukBGkuUwjs=
7678
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.4 h1:LAm3Ycm9HJfbSCd5I+wqC2S9Ej7FPrgr5CQoOljJZcE=

internal/app/appconfig/spec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ type ConfigSpec struct {
139139
AWSSecretKey string `required:"true" split_words:"true"`
140140

141141
NoArchiveDays int `split_words:"true" default:"60"`
142+
143+
DeleteDropReportAfterArchive bool `split_words:"true" default:"false"`
142144
}
143145

144146
type Config struct {

internal/controller/meta/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ func (c *AdminController) ArchiveDropReports(ctx *fiber.Ctx) error {
720720
return ctx.Status(fiber.StatusBadRequest).SendString("invalid date")
721721
}
722722

723-
err = c.DropReportArchiveService.ArchiveByDate(ctx.UserContext(), date)
723+
err = c.DropReportArchiveService.ArchiveByDate(ctx.UserContext(), date, request.DeleteAfterArchive)
724724
if err != nil {
725725
flog.ErrorFrom(ctx, "archive.drop_report").
726726
Err(err).

internal/model/types/admin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ type CloneFromCNRequest struct {
5656
}
5757

5858
type ArchiveDropReportRequest struct {
59-
Date string `json:"date" validate:"required" required:"true"`
59+
Date string `json:"date" validate:"required" required:"true"`
60+
DeleteAfterArchive bool `json:"deleteAfterArchive" validate:"required" required:"true"`
6061
}
6162

6263
type ForeignTimeRange struct {

internal/repo/drop_report.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ func (s *DropReport) GetDropReportsForArchive(ctx context.Context, cursor *model
329329
return results, newCursor(results), nil
330330
}
331331

332+
// DeleteDropReportsForArchive deletes drop reports for archive.
333+
// returns number of rows affected and error
334+
func (s *DropReport) DeleteDropReportsForArchive(ctx context.Context, tx bun.Tx, date time.Time) (int64, error) {
335+
start := time.UnixMilli(util.GetDayStartTime(&date, "CN")) // we use CN server's day start time across all servers for archive
336+
end := start.Add(time.Hour * 24)
337+
r, err := tx.NewDelete().
338+
Model((*model.DropReport)(nil)).
339+
Where("created_at >= to_timestamp(?)", start.Unix()).
340+
Where("created_at < to_timestamp(?)", end.Unix()).
341+
Exec(ctx)
342+
if err != nil {
343+
return -1, err
344+
}
345+
return r.RowsAffected()
346+
}
347+
332348
func (s *DropReport) handleStagesAndItems(query *bun.SelectQuery, stageIdItemIdMap map[int][]int) {
333349
stageConditions := make([]string, 0)
334350
for stageId, itemIds := range stageIdItemIdMap {

internal/repo/drop_report_extra.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ func (c *DropReportExtra) GetDropReportExtraForArchive(ctx context.Context, curs
6767
return dropReportExtras, newCursor, nil
6868
}
6969

70+
// DeleteDropReportExtrasForArchive deletes all drop report extras with report_id between idInclusiveStart and idInclusiveEnd.
71+
// Returns the number of rows affected and an error if any.
72+
func (c *DropReportExtra) DeleteDropReportExtrasForArchive(ctx context.Context, tx bun.Tx, idInclusiveStart int, idInclusiveEnd int) (int64, error) {
73+
r, err := tx.NewDelete().
74+
Model((*model.DropReportExtra)(nil)).
75+
Where("report_id >= ?", idInclusiveStart).
76+
Where("report_id <= ?", idInclusiveEnd).
77+
Exec(ctx)
78+
if err != nil {
79+
return -1, err
80+
}
81+
82+
return r.RowsAffected()
83+
}
84+
7085
func (c *DropReportExtra) IsDropReportExtraMD5Exist(ctx context.Context, md5 string) bool {
7186
var dropReportExtra model.DropReportExtra
7287

0 commit comments

Comments
 (0)