Skip to content

Commit

Permalink
feat: move deletion of expired adoptions to the dashboard BE (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh authored Oct 29, 2024
1 parent f474aaa commit ed51f2e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
19 changes: 0 additions & 19 deletions src/lib/override/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,6 @@ export class AdoptedProbes {

if (probeIsConnected) { // date is old, but probe is connected, therefore updating the sync date
await this.updateLastSyncDate(ip);
return;
}

if (this.isMoreThan30DaysAgo(lastSyncDate)) { // probe wasn't connected for >30 days, removing adoption
await this.deleteAdoptedProbe(ip);
}
}

Expand Down Expand Up @@ -380,11 +375,6 @@ export class AdoptedProbes {
}
}

private async deleteAdoptedProbe (ip: string) {
await this.sql(ADOPTED_PROBES_TABLE).where({ ip }).delete();
this.adoptedIpToProbe.delete(ip);
}

private async sendNotification (recipient: string, subject: string, message: string) {
await this.sql.raw(`
INSERT INTO ${NOTIFICATIONS_TABLE} (recipient, subject, message) SELECT :recipient, :subject, :message
Expand Down Expand Up @@ -418,13 +408,4 @@ export class AdoptedProbes {
const currentDate = new Date();
return date.toDateString() === currentDate.toDateString();
}

private isMoreThan30DaysAgo (date: Date) {
const currentDate = new Date();

const timeDifference = currentDate.getTime() - date.getTime();
const daysDifference = timeDifference / (24 * 3600 * 1000);

return daysDifference > 30;
}
}
35 changes: 4 additions & 31 deletions test/tests/unit/override/adopted-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('AdoptedProbes', () => {
expect(updateStub.args[0]).to.deep.equal([{ ip: '1.1.1.1', altIps: JSON.stringify([ '2.2.2.2' ]), uuid: '1-1-1-1-1' }]);
});

it('class should update status to "offline" if adopted probe was not found and lastSyncDate < 30 days away', async () => {
it('class should update status to "offline" if adopted probe was not found', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchProbesWithAdminData);
selectStub.resolves([{ ...defaultAdoptedProbe, lastSyncDate: relativeDayUtc(-15) }]);
fetchProbesWithAdminData.resolves([]);
Expand All @@ -161,7 +161,7 @@ describe('AdoptedProbes', () => {
expect(deleteStub.callCount).to.equal(0);
});

it('class should do nothing if adopted probe was not found and lastSyncDate < 30 days away but it is already "offline"', async () => {
it('class should do nothing if adopted probe was not found but it is already "offline"', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchProbesWithAdminData);
selectStub.resolves([{ ...defaultAdoptedProbe, lastSyncDate: relativeDayUtc(-15), status: 'offline' }]);
fetchProbesWithAdminData.resolves([]);
Expand All @@ -173,21 +173,7 @@ describe('AdoptedProbes', () => {
expect(deleteStub.callCount).to.equal(0);
});

it('class should delete adoption if adopted probe was not found and lastSyncDate > 30 days away', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchProbesWithAdminData);
selectStub.resolves([{ ...defaultAdoptedProbe, lastSyncDate: relativeDayUtc(-45) }]);
fetchProbesWithAdminData.resolves([]);

await adoptedProbes.syncDashboardData();

expect(whereStub.callCount).to.equal(2);
expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]);
expect(updateStub.callCount).to.equal(1);
expect(updateStub.args[0]).to.deep.equal([{ status: 'offline' }]);
expect(deleteStub.callCount).to.equal(1);
});

it('class should update lastSyncDate if probe is connected and lastSyncDate < 30 days away', async () => {
it('class should update lastSyncDate if probe is connected', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchProbesWithAdminData);
selectStub.resolves([{ ...defaultAdoptedProbe, lastSyncDate: relativeDayUtc(-15) }]);

Expand All @@ -200,20 +186,7 @@ describe('AdoptedProbes', () => {
expect(deleteStub.callCount).to.equal(0);
});

it('class should update lastSyncDate if probe is connected and lastSyncDate > 30 days away', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchProbesWithAdminData);
selectStub.resolves([{ ...defaultAdoptedProbe, lastSyncDate: relativeDayUtc(-45) }]);

await adoptedProbes.syncDashboardData();

expect(whereStub.callCount).to.equal(1);
expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]);
expect(updateStub.callCount).to.equal(1);
expect(updateStub.firstCall.args[0].lastSyncDate).to.be.greaterThanOrEqual(relativeDayUtc());
expect(deleteStub.callCount).to.equal(0);
});

it('class should update lastSyncDate should not update anything if lastSyncDate is today', async () => {
it('class should not update anything if lastSyncDate is today', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchProbesWithAdminData);

await adoptedProbes.syncDashboardData();
Expand Down

0 comments on commit ed51f2e

Please sign in to comment.