Skip to content

Commit

Permalink
Merge pull request #40 from nyaruka/retention-period
Browse files Browse the repository at this point in the history
rename ArchiveLength to RetentionPeriod
  • Loading branch information
nicpottier authored Mar 2, 2020
2 parents df17765 + 54d7905 commit 469c5f3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Usage of archiver:
whether we should keep local archive files after upload (default false)
-log-level string
the log level, one of error, warn, info, debug (default "info")
-retention-period int
the number of days to keep before archiving (default 90)
-s3-bucket string
the S3 bucket we will write archives to (default "dl-archiver-test")
-s3-disable-ssl
Expand All @@ -122,6 +124,7 @@ Environment variables:
ARCHIVER_DELETE - bool
ARCHIVER_KEEP_FILES - bool
ARCHIVER_LOG_LEVEL - string
ARCHIVER_RETENTION_PERIOD - int
ARCHIVER_S3_BUCKET - string
ARCHIVER_S3_DISABLE_SSL - bool
ARCHIVER_S3_ENDPOINT - string
Expand Down
20 changes: 10 additions & 10 deletions archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ const (

// Org represents the model for an org
type Org struct {
ID int `db:"id"`
Name string `db:"name"`
CreatedOn time.Time `db:"created_on"`
IsAnon bool `db:"is_anon"`
Language *string `db:"language"`
ActiveDays int
ID int `db:"id"`
Name string `db:"name"`
CreatedOn time.Time `db:"created_on"`
IsAnon bool `db:"is_anon"`
Language *string `db:"language"`
RetentionPeriod int
}

// Archive represents the model for an archive
Expand Down Expand Up @@ -117,7 +117,7 @@ func GetActiveOrgs(ctx context.Context, db *sqlx.DB, conf *Config) ([]Org, error

orgs := make([]Org, 0, 10)
for rows.Next() {
org := Org{ActiveDays: conf.ArchiveLength}
org := Org{RetentionPeriod: conf.RetentionPeriod}
err = rows.StructScan(&org)
if err != nil {
return nil, errors.Wrapf(err, "error scanning active org")
Expand Down Expand Up @@ -218,7 +218,7 @@ func GetMissingDailyArchives(ctx context.Context, db *sqlx.DB, now time.Time, or
defer cancel()

// our first archive would be active days from today
endDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, -org.ActiveDays)
endDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, -org.RetentionPeriod)
orgUTC := org.CreatedOn.In(time.UTC)
startDate := time.Date(orgUTC.Year(), orgUTC.Month(), orgUTC.Day(), 0, 0, 0, 0, time.UTC)

Expand Down Expand Up @@ -290,7 +290,7 @@ func GetMissingMonthlyArchives(ctx context.Context, db *sqlx.DB, now time.Time,
ctx, cancel := context.WithTimeout(ctx, time.Minute*5)
defer cancel()

lastActive := now.AddDate(0, 0, -org.ActiveDays)
lastActive := now.AddDate(0, 0, -org.RetentionPeriod)
endDate := time.Date(lastActive.Year(), lastActive.Month(), 1, 0, 0, 0, 0, time.UTC)

orgUTC := org.CreatedOn.In(time.UTC)
Expand Down Expand Up @@ -1236,7 +1236,7 @@ func DeleteArchivedMessages(ctx context.Context, config *Config, db *sqlx.DB, s3
// DeleteBroadcasts deletes all broadcasts older than 90 days for the passed in org which have no active messages on them
func DeleteBroadcasts(ctx context.Context, now time.Time, config *Config, db *sqlx.DB, org Org) error {
start := time.Now()
threshhold := now.AddDate(0, 0, -org.ActiveDays)
threshhold := now.AddDate(0, 0, -org.RetentionPeriod)

rows, err := db.QueryxContext(ctx, selectOldOrgBroadcasts, org.ID, threshhold)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func TestGetMissingDayArchives(t *testing.T) {
assert.Equal(t, time.Date(2017, 10, 10, 0, 0, 0, 0, time.UTC), tasks[30].StartDate)

// org 3 again, but changing the archive period so we have no tasks
orgs[2].ActiveDays = 200
orgs[2].RetentionPeriod = 200
tasks, err = GetMissingDailyArchives(ctx, db, now, orgs[2], MessageType)
assert.NoError(t, err)
assert.Equal(t, 0, len(tasks))

// org 1 again, but lowering the archive period so we have tasks
orgs[0].ActiveDays = 2
orgs[0].RetentionPeriod = 2
tasks, err = GetMissingDailyArchives(ctx, db, now, orgs[0], MessageType)
assert.NoError(t, err)
assert.Equal(t, 58, len(tasks))
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Config struct {

ArchiveMessages bool `help:"whether we should archive messages"`
ArchiveRuns bool `help:"whether we should archive runs"`
ArchiveLength int `help:"how many days back to archive"`
RetentionPeriod int `help:"the number of days to keep before archiving"`
Delete bool `help:"whether to delete messages and runs from the db after archival (default false)"`
}

Expand All @@ -46,7 +46,7 @@ func NewConfig() *Config {

ArchiveMessages: true,
ArchiveRuns: true,
ArchiveLength: 90,
RetentionPeriod: 90,
Delete: false,
}

Expand Down

0 comments on commit 469c5f3

Please sign in to comment.