Skip to content

Commit

Permalink
fix: Update trigger to use previous value, rather than the new value …
Browse files Browse the repository at this point in the history
…coming in
  • Loading branch information
chriswk committed Oct 6, 2023
1 parent f73e25d commit 6f072a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
42 changes: 39 additions & 3 deletions src/lib/features/instance-stats/getProductionChanges.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,33 @@ const mockRawEventDaysAgo = (
days: number,
environment: string = 'production',
) => {
const date = subDays(new Date(), days);
return {
type: 'FEATURE_UPDATED',
created_at: date,
created_by: 'testrunner',
environment,
feature_name: 'test.feature',
announced: true,
};
};

const noEnvironmentEvent = (days: number) => {
return {
type: 'FEATURE_UPDATED',
created_by: 'testrunner',
feature_name: 'test.feature',
announced: true,
};
};

beforeAll(async () => {
db = await dbInit('product_changes_serial', getLogger);
getProductionChanges = createGetProductionChanges(db.rawDatabase);
});

afterEach(async () => {
await db.rawDatabase('stat_environment_updates').delete();
await db.rawDatabase('stat_environment_updates').truncate();
});

afterAll(async () => {
Expand Down Expand Up @@ -85,11 +96,36 @@ test('should handle intervals of activity', async () => {
});

test('an event being saved should add a count to the table', async () => {
await db.rawDatabase('events').insert(mockRawEventDaysAgo(70));
await db.rawDatabase
.table('events')
.insert(mockRawEventDaysAgo(70))
.returning('id');

expect(getProductionChanges()).resolves.toEqual({
await expect(getProductionChanges()).resolves.toEqual({
last30: 0,
last60: 0,
last90: 1,
});
});

test('an event with no environment should not be counted', async () => {
await db.rawDatabase('events').insert(noEnvironmentEvent(30));
await expect(getProductionChanges()).resolves.toEqual({
last30: 0,
last60: 0,
last90: 0,
});
});

test('five events per day should be counted correctly', async () => {
for (let i = 0; i < 100; i++) {
for (let j; j < 5; j++) {
await db.rawDatabase.table('events').insert(mockRawEventDaysAgo(i));
}
}
await expect(getProductionChanges()).resolves.toEqual({
last30: 150,
last60: 300,
last90: 450,
});
});
13 changes: 8 additions & 5 deletions src/lib/features/instance-stats/getProductionChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ export const createGetProductionChanges =
const productionChanges = await db
.select({
last_month: db.raw(
"SUM(DISTINCT CASE WHEN day > NOW() - INTERVAL '1 month' THEN updates END)",
"SUM(CASE WHEN day > NOW() - INTERVAL '1 month' THEN updates END)",
),
last_two_months: db.raw(
"SUM(DISTINCT CASE WHEN day > NOW() - INTERVAL '2 months' THEN updates END)",
"SUM(CASE WHEN day > NOW() - INTERVAL '60 days' THEN updates END)",
),
last_quarter: db.raw(
"SUM(DISTINCT CASE WHEN day > NOW() - INTERVAL '3 months' THEN updates END)",
"SUM(CASE WHEN day > NOW() - INTERVAL '90 days' THEN updates END)",
),
})
.from('stat_environment_updates');
return {
last30: parseInt(productionChanges?.[0]?.last_month || '0', 10),
last60: parseInt(productionChanges?.[0]?.last_month || '0', 10),
last90: parseInt(productionChanges?.[0]?.last_month || '0', 10),
last60: parseInt(
productionChanges?.[0]?.last_two_months || '0',
10,
),
last90: parseInt(productionChanges?.[0]?.last_quarter || '0', 10),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.up = function(db, cb) {
CREATE FUNCTION unleash_update_stat_environment_changes_counter() RETURNS trigger AS $unleash_update_changes_counter$
BEGIN
IF NEW.environment IS NOT NULL THEN
INSERT INTO stat_environment_updates(day, environment, updates) SELECT DATE_TRUNC('Day', NEW.created_at), NEW.environment, 1 ON CONFLICT (day, environment) DO UPDATE SET updates = EXCLUDED.updates + 1;
INSERT INTO stat_environment_updates(day, environment, updates) SELECT DATE_TRUNC('Day', NEW.created_at), NEW.environment, 1 ON CONFLICT (day, environment) DO UPDATE SET updates = stat_environment_updates.updates + 1;
END IF;
return null;
Expand Down

0 comments on commit 6f072a7

Please sign in to comment.