Skip to content

Commit

Permalink
Merge pull request nyaruka#73 from nyaruka/update_daily_fix
Browse files Browse the repository at this point in the history
Change query used to update `rollup_id` on dailies and remove temporary logging
  • Loading branch information
rowanseymour authored Jul 7, 2022
2 parents fb51066 + 5655759 commit 45c9e40
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
18 changes: 2 additions & 16 deletions archives/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,6 @@ VALUES(:archive_type, :org_id, :created_on, :start_date, :period, :record_count,
RETURNING id
`

const updateRollups = `
UPDATE archives_archive
SET rollup_id = $1
WHERE ARRAY[id] <@ $2
`

// WriteArchiveToDB write an archive to the Database
func WriteArchiveToDB(ctx context.Context, db *sqlx.DB, archive *Archive) error {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
Expand Down Expand Up @@ -641,7 +635,7 @@ func WriteArchiveToDB(ctx context.Context, db *sqlx.DB, archive *Archive) error
childIDs = append(childIDs, c.ID)
}

result, err := tx.ExecContext(ctx, updateRollups, archive.ID, pq.Array(childIDs))
result, err := tx.ExecContext(ctx, `UPDATE archives_archive SET rollup_id = $1 WHERE id = ANY($2)`, archive.ID, pq.Array(childIDs))
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error updating rollup ids")
Expand Down Expand Up @@ -800,8 +794,6 @@ func RollupOrgArchives(ctx context.Context, now time.Time, config *Config, db *s
continue
}

t1 := dates.Since(start)

if config.UploadToS3 {
err = UploadArchive(ctx, s3Client, config.S3Bucket, archive)
if err != nil {
Expand All @@ -810,16 +802,12 @@ func RollupOrgArchives(ctx context.Context, now time.Time, config *Config, db *s
}
}

t2 := dates.Since(start)

err = WriteArchiveToDB(ctx, db, archive)
if err != nil {
log.WithError(err).Error("error writing record to db")
continue
}

t3 := dates.Since(start)

if !config.KeepFiles {
err := DeleteArchiveFile(archive)
if err != nil {
Expand All @@ -828,9 +816,7 @@ func RollupOrgArchives(ctx context.Context, now time.Time, config *Config, db *s
}
}

t4 := dates.Since(start)

log.WithFields(logrus.Fields{"id": archive.ID, "record_count": archive.RecordCount, "elapsed": dates.Since(start)}).WithFields(logrus.Fields{"t1": t1, "t2": t2, "t3": t3, "t4": t4}).Info("rollup created")
log.WithFields(logrus.Fields{"id": archive.ID, "record_count": archive.RecordCount, "elapsed": dates.Since(start)}).Info("rollup created")
created = append(created, archive)
}

Expand Down
2 changes: 1 addition & 1 deletion archives/archives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func TestArchiveActiveOrgs(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, map[string][]float64{
"archiver.archive_elapsed": {860.0},
"archiver.archive_elapsed": {848.0},
"archiver.orgs_archived": {3},
"archiver.msgs_records_archived": {5},
"archiver.msgs_archives_created": {92},
Expand Down

0 comments on commit 45c9e40

Please sign in to comment.