Skip to content

Commit 9ae9221

Browse files
authored
feat: now instances are only shown for last 24 hours (#9372)
1 parent e0f0108 commit 9ae9221

File tree

9 files changed

+60
-11
lines changed

9 files changed

+60
-11
lines changed

frontend/src/component/application/Application.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const Application = () => {
129129
name: 'overview',
130130
},
131131
{
132-
title: 'Connected instances',
132+
title: 'Last Seen Instances (24h)',
133133
path: `${basePath}/instances`,
134134
name: 'instances',
135135
},

frontend/src/component/application/ApplicationChart.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export const ApplicationChart = ({ data }: IApplicationChartProps) => {
287287
theme.fontSizes
288288
.smallBody
289289
}
290-
tooltip='Active instances in the last 2 days'
290+
tooltip='Active instances in the last 24 hours'
291291
/>
292292
</StyledCell>
293293
<StyledCell>

src/lib/db/client-applications-store.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ export default class ClientApplicationsStore
331331
])
332332
.from('client_instances as ci')
333333
.where('ci.app_name', appName)
334+
.whereRaw("ci.last_seen >= NOW() - INTERVAL '24 hours'")
334335
.groupBy('ci.app_name', 'ci.environment');
335336
})
336337
.select([
@@ -378,7 +379,7 @@ export default class ClientApplicationsStore
378379

379380
if (!environment) return acc;
380381

381-
strategies.forEach((strategy) => {
382+
strategies?.forEach((strategy) => {
382383
if (
383384
!DEPRECATED_STRATEGIES.includes(strategy) &&
384385
!existingStrategies.includes(strategy)

src/lib/db/client-instance-store.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export default class ClientInstanceStore implements IClientInstanceStore {
180180
return rows.map(mapRow);
181181
}
182182

183-
async getByAppNameAndEnvironment(
183+
async getRecentByAppNameAndEnvironment(
184184
appName: string,
185185
environment: string,
186186
): Promise<IClientInstance[]> {
@@ -189,6 +189,7 @@ export default class ClientInstanceStore implements IClientInstanceStore {
189189
.from(TABLE)
190190
.where('app_name', appName)
191191
.where('environment', environment)
192+
.whereRaw("last_seen >= NOW() - INTERVAL '24 hours'")
192193
.orderBy('last_seen', 'desc')
193194
.limit(1000);
194195

src/lib/features/metrics/instance/instance-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ export default class ClientInstanceService {
262262
return result;
263263
}
264264

265-
async getApplicationEnvironmentInstances(
265+
async getRecentApplicationEnvironmentInstances(
266266
appName: string,
267267
environment: string,
268268
) {
269269
const instances =
270-
await this.clientInstanceStore.getByAppNameAndEnvironment(
270+
await this.clientInstanceStore.getRecentByAppNameAndEnvironment(
271271
appName,
272272
environment,
273273
);

src/lib/routes/admin-api/metrics.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ class MetricsController extends Controller {
168168
openApiService.validPath({
169169
tags: ['Metrics'],
170170
operationId: 'getApplicationEnvironmentInstances',
171-
summary: 'Get application environment instances',
171+
summary: 'Get application environment instances (Last 24h)',
172172
description:
173-
'Returns an overview of the instances for the given `appName` and `environment` that receive traffic.',
173+
'Returns an overview of the instances for the given `appName` and `environment` that have received traffic in the last 24 hours.',
174174
responses: {
175175
200: createResponseSchema(
176176
'applicationEnvironmentInstancesSchema',
@@ -315,7 +315,7 @@ class MetricsController extends Controller {
315315
): Promise<void> {
316316
const { appName, environment } = req.params;
317317
const instances =
318-
await this.clientInstanceService.getApplicationEnvironmentInstances(
318+
await this.clientInstanceService.getRecentApplicationEnvironmentInstances(
319319
appName,
320320
environment,
321321
);

src/lib/types/stores/client-instance-store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface IClientInstanceStore
2121
setLastSeen(INewClientInstance): Promise<void>;
2222
insert(details: INewClientInstance): Promise<void>;
2323
getByAppName(appName: string): Promise<IClientInstance[]>;
24-
getByAppNameAndEnvironment(
24+
getRecentByAppNameAndEnvironment(
2525
appName: string,
2626
environment: string,
2727
): Promise<IClientInstance[]>;

src/test/e2e/api/admin/applications.e2e.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,50 @@ test('should show missing features and strategies', async () => {
221221

222222
expect(body).toMatchObject(expected);
223223
});
224+
225+
test('should not return instances older than 24h', async () => {
226+
await app.request
227+
.post('/api/client/metrics')
228+
.set('Authorization', defaultToken.secret)
229+
.send(metrics)
230+
.expect(202);
231+
232+
await app.services.clientMetricsServiceV2.bulkAdd();
233+
234+
await db.stores.clientApplicationsStore.upsert({
235+
appName: metrics.appName,
236+
});
237+
await db.stores.clientInstanceStore.insert({
238+
appName: metrics.appName,
239+
clientIp: '127.0.0.1',
240+
instanceId: 'old-instance',
241+
lastSeen: new Date(Date.now() - 26 * 60 * 60 * 1000), // 26 hours ago
242+
});
243+
244+
const { body } = await app.request
245+
.get(`/api/admin/metrics/applications/${metrics.appName}/overview`)
246+
.expect(200);
247+
248+
const expected = {
249+
environments: [
250+
{
251+
instanceCount: 1,
252+
},
253+
],
254+
};
255+
256+
expect(body).toMatchObject(expected);
257+
258+
const { body: instancesBody } = await app.request
259+
.get(
260+
`/api/admin/metrics/instances/${metrics.appName}/environment/default`,
261+
)
262+
.expect(200);
263+
264+
expect(instancesBody.instances).toHaveLength(1);
265+
expect(instancesBody.instances).toMatchObject([
266+
{
267+
instanceId: metrics.instanceId,
268+
},
269+
]);
270+
});

src/test/fixtures/fake-client-instance-store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default class FakeClientInstanceStore implements IClientInstanceStore {
9393
return this.instances.filter((i) => i.appName === appName);
9494
}
9595

96-
async getByAppNameAndEnvironment(
96+
async getRecentByAppNameAndEnvironment(
9797
appName: string,
9898
environment: string,
9999
): Promise<IClientInstance[]> {

0 commit comments

Comments
 (0)