mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: updating last seen now will create instance if does not exist (#6328)
This commit is contained in:
		
							parent
							
								
									68abe28257
								
							
						
					
					
						commit
						1633722877
					
				@ -78,10 +78,18 @@ export default class ClientInstanceStore implements IClientInstanceStore {
 | 
			
		||||
        clientIp,
 | 
			
		||||
    }: INewClientInstance): Promise<void> {
 | 
			
		||||
        await this.db(TABLE)
 | 
			
		||||
            .update({ last_seen: new Date(), client_ip: clientIp })
 | 
			
		||||
            .where({ app_name: appName, instance_id: instanceId, environment })
 | 
			
		||||
            .insert({
 | 
			
		||||
                app_name: appName,
 | 
			
		||||
                instance_id: instanceId,
 | 
			
		||||
                environment,
 | 
			
		||||
                last_seen: new Date(),
 | 
			
		||||
                client_ip: clientIp,
 | 
			
		||||
            })
 | 
			
		||||
            .onConflict(['app_name', 'instance_id', 'environment'])
 | 
			
		||||
            .ignore();
 | 
			
		||||
            .merge({
 | 
			
		||||
                last_seen: new Date(),
 | 
			
		||||
                client_ip: clientIp,
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async bulkUpsert(instances: INewClientInstance[]): Promise<void> {
 | 
			
		||||
@ -97,7 +105,10 @@ export default class ClientInstanceStore implements IClientInstanceStore {
 | 
			
		||||
        instanceId,
 | 
			
		||||
    }: Pick<INewClientInstance, 'appName' | 'instanceId'>): Promise<void> {
 | 
			
		||||
        await this.db(TABLE)
 | 
			
		||||
            .where({ app_name: appName, instance_id: instanceId })
 | 
			
		||||
            .where({
 | 
			
		||||
                app_name: appName,
 | 
			
		||||
                instance_id: instanceId,
 | 
			
		||||
            })
 | 
			
		||||
            .del();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -113,7 +124,10 @@ export default class ClientInstanceStore implements IClientInstanceStore {
 | 
			
		||||
        'appName' | 'instanceId'
 | 
			
		||||
    >): Promise<IClientInstance> {
 | 
			
		||||
        const row = await this.db(TABLE)
 | 
			
		||||
            .where({ app_name: appName, instance_id: instanceId })
 | 
			
		||||
            .where({
 | 
			
		||||
                app_name: appName,
 | 
			
		||||
                instance_id: instanceId,
 | 
			
		||||
            })
 | 
			
		||||
            .first();
 | 
			
		||||
        return mapRow(row);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,13 @@ beforeAll(async () => {
 | 
			
		||||
    app = await setupApp(db.stores);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
afterEach(async () => {
 | 
			
		||||
    await Promise.all([
 | 
			
		||||
        db.stores.clientMetricsStoreV2.deleteAll(),
 | 
			
		||||
        db.stores.clientInstanceStore.deleteAll(),
 | 
			
		||||
    ]);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
afterAll(async () => {
 | 
			
		||||
    await app.destroy();
 | 
			
		||||
    await db.destroy();
 | 
			
		||||
@ -46,3 +53,14 @@ test('should accept empty client metrics', async () => {
 | 
			
		||||
        })
 | 
			
		||||
        .expect(202);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
test('should create instance if does not exist', async () => {
 | 
			
		||||
    const instances = await db.stores.clientInstanceStore.getAll();
 | 
			
		||||
    expect(instances.length).toBe(0);
 | 
			
		||||
    await app.request
 | 
			
		||||
        .post('/api/client/metrics')
 | 
			
		||||
        .send(metricsExample)
 | 
			
		||||
        .expect(202);
 | 
			
		||||
    const finalInstances = await db.stores.clientInstanceStore.getAll();
 | 
			
		||||
    expect(finalInstances.length).toBe(1);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user