Skip to content

Commit

Permalink
feat: setting data for adopted probes (#442)
Browse files Browse the repository at this point in the history
* feat: ignore syncing of custom fields

* refactor: move fetch sockets from ws server

* test: fix tests

* refactor: rename fetchsockets

* feat: update probe info with custom data

* feat: add index values

* feat: get username from dashboard

* test: fix tests

* feat: return probe info after send code to the probe

* fix: fix filtering by user tags

* fix: fix test ts build

* feat: log and send to the probe updated location

* fix: tests

* feat: do not apply adoption city data if country doesn`t match

* fix: test db

* fix: update init sql script

* test: add GET int test

* fix: nock geo ip

* test: add adoption POST test

* test: add adopted-probes tests

* test: add more adopted sockets tests

* feat: update state of the adopted probes in US cities

* feat: update gihtub ci

* feat: use github field instead of last_name field

* test: update userId value

* fix: treat null and undefined values as equal

* refactor: update sql query

* fix: remove user tags from index

* feat: rename normalize function

* feat: update country field of the adopted probe

* feat: send notification to the user

* test: add tests for edit of country field

* feat: get rid of duplicate notification messages

* feat: update dev mariadb password

* test: fix tests

* feat: add new cities altnames

---------

Co-authored-by: Martin Kolárik <martin@kolarik.sk>
  • Loading branch information
alexey-yarmosh and MartinKolarik authored Nov 23, 2023
1 parent 113fd52 commit cc03c96
Show file tree
Hide file tree
Showing 33 changed files with 1,116 additions and 419 deletions.
28 changes: 15 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ jobs:
--health-timeout 5s
--health-retries 5
mariadb:
image: mariadb:10.11.5
ports:
- 3306:3306
env:
MARIADB_DATABASE: directus
MARIADB_USER: directus
MARIADB_PASSWORD: password
MARIADB_RANDOM_ROOT_PASSWORD: 1
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3

- name: Shutdown Ubuntu MySQL
run: sudo service mysql stop

- name: Setup MariaDB
uses: getong/mariadb-action@v1.1
with:
mariadb version: '10.11.5'
mysql user: 'directus'
mysql password: 'password'
mysql database: 'directus'

- name: Init MariaDB
run: |
sleep 5
mysql -u directus -ppassword --database=directus --protocol=tcp < config/init.sql
- uses: actions/setup-node@v3
Expand Down
75 changes: 66 additions & 9 deletions config/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,72 @@ CREATE TABLE IF NOT EXISTS adopted_probes (
userId VARCHAR(255) NOT NULL,
ip VARCHAR(255) NOT NULL,
uuid VARCHAR(255),
lastSyncDate DATE,
status VARCHAR(255),
version VARCHAR(255),
country VARCHAR(255),
lastSyncDate DATE NOT NULL,
isCustomCity TINYINT DEFAULT 0,
tags LONGTEXT,
status VARCHAR(255) NOT NULL,
version VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
city VARCHAR(255),
latitude FLOAT,
longitude FLOAT,
asn INTEGER,
network VARCHAR(255)
state VARCHAR(255),
latitude FLOAT(10, 5),
longitude FLOAT(10, 5),
asn INTEGER NOT NULL,
network VARCHAR(255) NOT NULL,
countryOfCustomCity VARCHAR(255)
);

INSERT IGNORE INTO adopted_probes (id, userId, ip) VALUES ('1', '6191378', '79.205.97.254');
CREATE TABLE IF NOT EXISTS directus_users (
id CHAR(36),
github VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS directus_notifications (
id CHAR(10),
recipient CHAR(36),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
subject VARCHAR(255),
message TEXT
);

INSERT IGNORE INTO adopted_probes (
userId,
lastSyncDate,
ip,
uuid,
isCustomCity,
tags,
status,
version,
country,
city,
latitude,
longitude,
network,
asn,
countryOfCustomCity
) VALUES (
'89da69bd-a236-4ab7-9c5d-b5f52ce09959',
CURRENT_DATE,
'51.158.22.211',
'c77f021d-23ff-440a-aa96-35e82c73e731',
1,
'["mytag1"]',
'ready',
'0.26.0',
'FR',
'Marseille',
'43.29695',
'5.38107',
'SCALEWAY S.A.S.',
12876,
'FR'
);

INSERT IGNORE INTO directus_users (
id,
github
) VALUES (
'89da69bd-a236-4ab7-9c5d-b5f52ce09959',
'jimaek'
);
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- MARIADB_DATABASE=directus
- MARIADB_USER=directus
- MARIADB_PASSWORD=password
- MARIADB_RANDOM_ROOT_PASSWORD=1
- MARIADB_ROOT_PASSWORD=root
ports:
- "3306:3306"
volumes:
Expand Down
2 changes: 1 addition & 1 deletion public/demo/measurements.vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const app = () => ({
const locations = this.query.locations.map(({ limit, fields }) => ({
fields: fields.map(field => ({
...field,
value: field.type === 'tags' ? field.value.split(',') : field.value,
value: field.type === 'tags' ? field.value.split(',').map(tag => tag.trim()) : field.value,
})),
...(limit ? { limit } : {}),
}));
Expand Down
13 changes: 11 additions & 2 deletions src/adoption-code/route/adoption-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ import { codeSender } from '../sender.js';

const handle = async (ctx: Context): Promise<void> => {
const request = ctx.request.body as AdoptionCodeRequest;
const result = await codeSender.sendCode(request);
const socket = await codeSender.sendCode(request);

ctx.body = {
result,
uuid: socket.data.probe.uuid,
version: socket.data.probe.version,
status: socket.data.probe.status,
city: socket.data.probe.location.city,
state: socket.data.probe.location.state,
country: socket.data.probe.location.country,
latitude: socket.data.probe.location.latitude,
longitude: socket.data.probe.location.longitude,
asn: socket.data.probe.location.asn,
network: socket.data.probe.location.network,
};
};

Expand Down
11 changes: 6 additions & 5 deletions src/adoption-code/sender.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import createHttpError from 'http-errors';
import { fetchSockets, RemoteProbeSocket } from '../lib/ws/server.js';
import type { RemoteProbeSocket } from '../lib/ws/server.js';
import { fetchSockets } from '../lib/ws/fetch-sockets.js';
import type { AdoptionCodeRequest } from './types.js';

export class CodeSender {
constructor (private readonly fetchWsSockets: typeof fetchSockets) {}

async sendCode (request: AdoptionCodeRequest): Promise<string> {
async sendCode (request: AdoptionCodeRequest): Promise<RemoteProbeSocket> {
const socket = await this.findSocketByIp(request.ip);

if (!socket) {
Expand All @@ -15,16 +16,16 @@ export class CodeSender {

this.sendToSocket(socket, request.code);

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

private async findSocketByIp (ip: string) {
const sockets = await this.fetchWsSockets();
return sockets.find(socket => socket.data.probe.ipAddress === ip);
}

private sendToSocket (sockets: RemoteProbeSocket, code: string) {
sockets.emit('probe:adoption:code', {
private sendToSocket (socket: RemoteProbeSocket, code: string) {
socket.emit('probe:adoption:code', {
code,
});
}
Expand Down
Loading

0 comments on commit cc03c96

Please sign in to comment.