Skip to content

Commit

Permalink
fix: project last seen at metrics (#7988)
Browse files Browse the repository at this point in the history
Read `last_seen_at` from correct table `last_seen_at_metrics`, instead
of deprecated `feature.last_seen_at`
  • Loading branch information
Tymek authored Aug 27, 2024
1 parent fc86f5b commit 427c43e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/lib/features/project/project-read-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { IFeatureToggleStore } from '../feature-toggle/types/feature-toggle
import type {
IEventStore,
IFlagResolver,
ILastSeenStore,
IProjectReadModel,
IProjectStore,
} from '../../types';
Expand All @@ -15,6 +16,7 @@ let flagStore: IFeatureToggleStore;
let projectStore: IProjectStore;
let eventStore: IEventStore;
let projectReadModel: IProjectReadModel;
let lastSeenStore: ILastSeenStore;

const alwaysOnFlagResolver = {
isEnabled() {
Expand All @@ -32,6 +34,7 @@ beforeAll(async () => {
projectStore = db.stores.projectStore;
eventStore = db.stores.eventStore;
flagStore = db.stores.featureToggleStore;
lastSeenStore = db.stores.lastSeenStore;
});

afterAll(async () => {
Expand Down Expand Up @@ -148,12 +151,10 @@ test('it uses the last flag metrics received for lastReportedFlagUsage', async (

await flagStore.create(projectId, { name: flagName, createdByUserId: 1 });

await flagStore.setLastSeen([
await lastSeenStore.setLastSeen([
{ featureName: flagName, environment: 'development' },
]);

const flag = await flagStore.get(flagName);

const result = await projectReadModel.getProjectsForAdminUi();
expect(result[0].lastReportedFlagUsage).toEqual(flag.lastSeenAt);
expect(result[0].lastReportedFlagUsage).not.toBeNull();
});
7 changes: 6 additions & 1 deletion src/lib/features/project/project-read-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export class ProjectReadModel implements IProjectReadModel {
const projectTimer = this.timer('getProjectsForAdminUi');
let projects = this.db(TABLE)
.leftJoin('features', 'features.project', 'projects.id')
.leftJoin(
'last_seen_at_metrics',
'features.name',
'last_seen_at_metrics.feature_name',
)
.leftJoin(
'project_settings',
'project_settings.project',
Expand Down Expand Up @@ -99,7 +104,7 @@ export class ProjectReadModel implements IProjectReadModel {
this.db.raw(
'projects.id, projects.name, projects.description, projects.health, projects.created_at, ' +
'count(DISTINCT features.name) FILTER (WHERE features.archived_at is null) AS number_of_features, ' +
'MAX(features.last_seen_at) AS last_usage,' +
'MAX(last_seen_at_metrics.last_seen_at) AS last_usage, ' +
'MAX(events.created_at) AS last_updated',
),
'project_settings.project_mode',
Expand Down

0 comments on commit 427c43e

Please sign in to comment.