Skip to content

Commit

Permalink
fix: fix dashboard integration issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Oct 5, 2023
1 parent 31ec3ec commit 00ca77f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion config/default.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
},
},
sql: {
url: 'mysql://root:@localhost:3306/directus',
url: 'mysql://directus:password@localhost:3306/directus',
},
admin: {
key: '',
Expand Down
6 changes: 4 additions & 2 deletions config/init.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CREATE DATABASE IF NOT EXISTS directus;

USE directus;

CREATE TABLE IF NOT EXISTS adopted_probes (
Expand All @@ -12,5 +11,8 @@ CREATE TABLE IF NOT EXISTS adopted_probes (
ip VARCHAR(255) NOT NULL,
uuid VARCHAR(255)
);

INSERT IGNORE INTO adopted_probes (id, userId, ip) VALUES ('1', '6191378', '79.205.97.254');

CREATE USER IF NOT EXISTS 'directus';
GRANT ALL PRIVILEGES ON `directus`.* TO 'directus';
FLUSH PRIVILEGES;
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ services:
mariadb:
image: mariadb:latest
environment:
- MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1
- MARIADB_RANDOM_ROOT_PASSWORD=1
- MARIADB_USER=directus
- MARIADB_PASSWORD=password
ports:
- "3306:3306"
volumes:
Expand Down
2 changes: 1 addition & 1 deletion src/adoption-code/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CodeSender {

this.sendToSocket(socket, request.code);

return 'Code was sent to the probe';
return 'Code was sent to the probe.';
}

private async findSocketByIp (ip: string) {
Expand Down
17 changes: 6 additions & 11 deletions src/lib/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export class AdoptedProbes {

constructor (private readonly sql: Knex) {}

async startSync () {
await this.syncDashboardData();
this.scheduleSync();
scheduleSync () {
setTimeout(() => {
this.syncDashboardData()
.finally(() => this.scheduleSync())
.catch(error => logger.error(error));
}, 5000);
}

async syncProbeIds (probeIp: string, probeUuid: string) {
Expand Down Expand Up @@ -54,14 +57,6 @@ export class AdoptedProbes {
this.adoptedProbesByIp = new Map(probes.map(probe => [ probe.ip, { uuid: probe.uuid, city: '' }]));
this.adoptedProbesByUuid = new Map(probes.map(probe => [ probe.uuid, { ip: probe.ip, city: '' }]));
}

scheduleSync () {
setTimeout(() => {
this.syncDashboardData()
.finally(() => this.scheduleSync())
.catch(error => logger.error(error));
}, 5000);
}
}

export const adoptedProbes = new AdoptedProbes(client);
5 changes: 2 additions & 3 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ export const createServer = async (): Promise<Server> => {
await populateIpWhiteList();
// Populate cities info
await populateCitiesList();
// Populate adopted probes info and start regular sync
await adoptedProbes.startSync();


await initWsServer();

adoptedProbes.scheduleSync();

const { getWsServer } = await import('./ws/server.js');
const { getHttpServer } = await import('./http/server.js');

Expand Down
3 changes: 0 additions & 3 deletions src/probe/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { getRegion } from '../lib/ip-ranges.js';
import type { Probe, ProbeLocation, Tag } from './types.js';
import { verifyIpLimit } from '../lib/ws/helper/probe-ip-limit.js';
import { fakeLookup } from '../lib/geoip/fake-client.js';
import { adoptedProbes } from '../lib/adopted-probes.js';

const geoipClient = createGeoipClient();

Expand Down Expand Up @@ -55,8 +54,6 @@ export const buildProbe = async (socket: Socket): Promise<Probe> => {

await verifyIpLimit(ip, socket.id);

await adoptedProbes.syncProbeIds(ip, uuid);

const location = getLocation(ipInfo);

const tags = getTags(ip);
Expand Down
2 changes: 1 addition & 1 deletion test/tests/integration/adoption-code/adoption-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Adoption code', () => {
})
.expect(200).expect((response) => {
expect(response.body).to.deep.equal({
result: 'Code was sent to the probe',
result: 'Code was sent to the probe.',
});
});

Expand Down
34 changes: 19 additions & 15 deletions test/tests/unit/adopted-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ const sqlStub = sinon.stub().returns({
select: selectStub,
where: whereStub,
});
let clock: sinon.SinonSandbox['clock'];
let sandbox: sinon.SinonSandbox;

describe('AdoptedProbes', () => {
before(() => {
clock = sinon.useFakeTimers();
});

beforeEach(() => {
sandbox = sinon.createSandbox({ useFakeTimers: true });
sinon.resetHistory();
});

afterEach(() => {
clock.restore();
sandbox.restore();
});

it('startSync method should sync the data and start regular syncs', async () => {
Expand All @@ -34,16 +31,15 @@ describe('AdoptedProbes', () => {
uuid: '1-1-1-1-1',
}]);

await adoptedProbes.startSync();
adoptedProbes.scheduleSync();

expect(sqlStub.callCount).to.equal(0);
expect(selectStub.callCount).to.equal(0);
await sandbox.clock.tickAsync(5500);
expect(sqlStub.callCount).to.equal(1);
expect(sqlStub.args[0]).deep.equal([ 'adopted_probes' ]);
expect(selectStub.callCount).to.equal(1);
expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid' ]);

await clock.tickAsync(5500);
expect(sqlStub.callCount).to.equal(2);
expect(selectStub.callCount).to.equal(2);
});

it('syncProbeIds method should do nothing if probe was not found either by ip or uuid', async () => {
Expand All @@ -53,7 +49,9 @@ describe('AdoptedProbes', () => {
uuid: '1-1-1-1-1',
}]);

await adoptedProbes.startSync();
adoptedProbes.scheduleSync();
await sandbox.clock.tickAsync(5500);

await adoptedProbes.syncProbeIds('2.2.2.2', '2-2-2-2-2');

expect(whereStub.callCount).to.equal(0);
Expand All @@ -67,7 +65,9 @@ describe('AdoptedProbes', () => {
uuid: '1-1-1-1-1',
}]);

await adoptedProbes.startSync();
adoptedProbes.scheduleSync();
await sandbox.clock.tickAsync(5500);

await adoptedProbes.syncProbeIds('1.1.1.1', '1-1-1-1-1');

expect(whereStub.callCount).to.equal(0);
Expand All @@ -81,7 +81,9 @@ describe('AdoptedProbes', () => {
uuid: '1-1-1-1-1',
}]);

await adoptedProbes.startSync();
adoptedProbes.scheduleSync();
await sandbox.clock.tickAsync(5500);

await adoptedProbes.syncProbeIds('1.1.1.1', '2-2-2-2-2');

expect(whereStub.callCount).to.equal(1);
Expand All @@ -97,7 +99,9 @@ describe('AdoptedProbes', () => {
uuid: '1-1-1-1-1',
}]);

await adoptedProbes.startSync();
adoptedProbes.scheduleSync();
await sandbox.clock.tickAsync(5500);

await adoptedProbes.syncProbeIds('2.2.2.2', '1-1-1-1-1');

expect(whereStub.callCount).to.equal(1);
Expand Down

0 comments on commit 00ca77f

Please sign in to comment.