Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions archives/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ func (a *Archive) endDate() time.Time {
const lookupActiveOrgs = `
SELECT o.id, o.name, o.created_on, o.is_anon
FROM orgs_org o
WHERE o.is_active = TRUE order by o.id
LEFT JOIN (
SELECT DISTINCT ON (org_id) org_id, record_count
FROM archives_archive
WHERE period = 'M'
AND archive_type = 'message'
ORDER BY org_id, start_date DESC
) latest_archive ON latest_archive.org_id = o.id
WHERE o.is_active = TRUE
ORDER BY COALESCE(latest_archive.record_count, 0) DESC, o.id
`

const lookupInactiveOrgs = `
Expand All @@ -102,7 +110,7 @@ FROM orgs_org o
WHERE o.is_active = FALSE order by o.id
`

// GetActiveOrgs returns the active organizations sorted by id
// GetActiveOrgs returns the active organizations sorted by most recent monthly archive record count (descending)
func GetActiveOrgs(ctx context.Context, db *sqlx.DB, conf *Config) ([]Org, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
Expand Down
45 changes: 45 additions & 0 deletions archives/archives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,51 @@ func setup(t *testing.T) *sqlx.DB {
return db
}

func TestGetActiveOrgsOrdering(t *testing.T) {
db := setup(t)
ctx := context.Background()
config := NewConfig()

// with default test data all orgs have monthly record_count=0, so tiebreaker is o.id
orgs, err := GetActiveOrgs(ctx, db, config)
assert.NoError(t, err)
assert.Equal(t, 3, len(orgs))
assert.Equal(t, 1, orgs[0].ID)
assert.Equal(t, 2, orgs[1].ID)
assert.Equal(t, 3, orgs[2].ID)

// insert monthly archives: Org 2 gets 500, Org 1 gets 100 (Org 3 already has 0)
_, err = db.Exec(`
INSERT INTO archives_archive(archive_type, created_on, start_date, period, record_count, size, hash, url, needs_deletion, build_time, org_id) VALUES
('message', '2017-12-01 00:00:00+00', '2017-12-01', 'M', 100, 0, '', '', FALSE, 0, 1),
('message', '2017-12-01 00:00:00+00', '2017-12-01', 'M', 500, 0, '', '', FALSE, 0, 2)
`)
assert.NoError(t, err)

// ordering should now be: Org 2 (500), Org 1 (100), Org 3 (0)
orgs, err = GetActiveOrgs(ctx, db, config)
assert.NoError(t, err)
assert.Equal(t, 3, len(orgs))
assert.Equal(t, 2, orgs[0].ID)
assert.Equal(t, 1, orgs[1].ID)
assert.Equal(t, 3, orgs[2].ID)

// add a more recent monthly archive for Org 3 with highest record_count
_, err = db.Exec(`
INSERT INTO archives_archive(archive_type, created_on, start_date, period, record_count, size, hash, url, needs_deletion, build_time, org_id) VALUES
('message', '2018-01-01 00:00:00+00', '2018-01-01', 'M', 1000, 0, '', '', FALSE, 0, 3)
`)
assert.NoError(t, err)

// ordering should now be: Org 3 (1000), Org 2 (500), Org 1 (100)
orgs, err = GetActiveOrgs(ctx, db, config)
assert.NoError(t, err)
assert.Equal(t, 3, len(orgs))
assert.Equal(t, 3, orgs[0].ID)
assert.Equal(t, 2, orgs[1].ID)
assert.Equal(t, 1, orgs[2].ID)
}

func TestGetMissingDayArchives(t *testing.T) {
db := setup(t)

Expand Down