Skip to content

Commit

Permalink
feat: add logs after some sync actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rboixaderg committed Nov 27, 2023
1 parent 8c16558 commit 2a691dd
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 21 deletions.
3 changes: 3 additions & 0 deletions electron-app/src/events/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export enum EVENTS {
START_LISTENING = 'start-listening',
STOP_LISTENING = 'stop-listening',
SYNC_CREATED = 'sync-created',
SYNC_UPDATED = 'sync-updated',
SYNC_DELETED = 'sync-deleted',
}

export type EVENTS_TYPE = (typeof EVENTS)[keyof typeof EVENTS];
Expand Down
3 changes: 3 additions & 0 deletions electron-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import os from 'os';
import path from 'path';
import { EVENTS } from './events/events';
import { beforeStartServer } from './fileSystemServerFn';

import { AppFileSystemRoutes } from './presentation/routes';
import { Server, eventEmitter } from './server';
import { initFileSystemSubscribers } from './subscribers';

let contextMenu: Electron.Menu;
let tray = null;
const basePath = `${os.homedir()}/.nuclia`;
const appRoutes = new AppFileSystemRoutes(basePath);
const server = new Server({ port: 8000, routes: appRoutes.getRoutes() });
initFileSystemSubscribers(basePath);

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
Expand Down
2 changes: 1 addition & 1 deletion electron-app/src/logic/logs/domain/log.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface LogEntityOptions {
}

export class LogEntity {
public level: LogSeverityLevel; // Enum
public level: LogSeverityLevel;
public message: string;
public createdAt: Date;
public origin: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class FileSystemLogDatasource implements ILogDatasource {
const content = await readFile(path);
if (content === '') return [];

const logs = content.split('\n').map(LogEntity.fromJson);
const logs = content.split('\n').filter(Boolean).map(LogEntity.fromJson);
return logs;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EVENTS } from '../../../../events/events';
import { eventEmitter } from '../../../../server';
import { CreateSyncDto } from '../dto/create-sync.dto';
import { ISyncRepository } from '../sync.repository';

Expand All @@ -10,5 +12,6 @@ export class CreateSync implements CreateSyncUseCase {

async execute(dto: CreateSyncDto): Promise<void> {
await this.repository.createSync(dto);
eventEmitter.emit(EVENTS.SYNC_CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CustomError } from '../../../errors';
import { EVENTS } from '../../../../events/events';
import { eventEmitter } from '../../../../server';
import { ISyncRepository } from '../sync.repository';

export interface DeleteSyncUseCase {
Expand All @@ -9,10 +10,7 @@ export class DeleteSync implements DeleteSyncUseCase {
constructor(private readonly repository: ISyncRepository) {}

async execute(id: string) {
const data = await this.repository.getSync(id);
if (data === null) {
throw new CustomError(`Sync with id ${id} not found`, 404);
}
await this.repository.deleteSync(id);
eventEmitter.emit(EVENTS.SYNC_DELETED);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EVENTS } from '../../../../events/events';
import { eventEmitter } from '../../../../server';
import { UpdateSyncDto } from '../dto/update-sync.dto';
import { ISyncRepository } from '../sync.repository';

Expand All @@ -10,5 +12,6 @@ export class UpdateSync implements UpdateSyncUseCase {

async execute(dto: UpdateSyncDto) {
await this.repository.updateSync(dto);
eventEmitter.emit(EVENTS.SYNC_UPDATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class FileSystemSyncDatasource implements ISyncDatasource {
}

async deleteSync(id: string): Promise<void> {
const data = await this.getSync(id);
if (data === null) {
throw new CustomError(`Sync with id ${id} not found`, 404);
}
await this.loadSyncData();
delete this.allSyncData[id];
await writeFile(this.basePath, JSON.stringify(this.allSyncData, null, 2));
Expand Down
41 changes: 41 additions & 0 deletions electron-app/src/subscribers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { EVENTS } from './events/events';
import { LogEntity, LogSeverityLevel } from './logic/logs/domain/log.entity';
import { SaveLogs } from './logic/logs/domain/use-cases/save-logs.use-case';
import { FileSystemLogDatasource } from './logic/logs/infrastructure/file-system.log.datasource';
import { LogRepository } from './logic/logs/infrastructure/log.repository';
import { eventEmitter } from './server';

export function initFileSystemSubscribers(basePath: string) {
eventEmitter.subscribe(EVENTS.SYNC_CREATED, () => {
const saveLog = new SaveLogs(new LogRepository(new FileSystemLogDatasource(basePath)));
saveLog.execute(
new LogEntity({
message: 'Sync created',
level: LogSeverityLevel.low,
origin: 'electron-app',
}),
);
});

eventEmitter.subscribe(EVENTS.SYNC_UPDATED, () => {
const saveLog = new SaveLogs(new LogRepository(new FileSystemLogDatasource(basePath)));
saveLog.execute(
new LogEntity({
message: 'Sync updated',
level: LogSeverityLevel.low,
origin: 'electron-app',
}),
);
});

eventEmitter.subscribe(EVENTS.SYNC_DELETED, () => {
const saveLog = new SaveLogs(new LogRepository(new FileSystemLogDatasource(basePath)));
saveLog.execute(
new LogEntity({
message: 'Sync deleted',
level: LogSeverityLevel.low,
origin: 'electron-app',
}),
);
});
}
36 changes: 22 additions & 14 deletions electron-app/tests/sever.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { deleteDirectory } from '../src/fileSystemFn';
import { beforeStartServer } from '../src/fileSystemServerFn';
import { AppFileSystemRoutes } from '../src/presentation/routes';
import { Server } from '../src/server';
import { initFileSystemSubscribers } from '../src/subscribers';

const appRoutes = new AppFileSystemRoutes('.nuclia');
const testServer = new Server({ port: 8000, routes: appRoutes.getRoutes() });
initFileSystemSubscribers('.nuclia');

describe('Server without folder', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -53,6 +55,12 @@ describe('Server width folder', () => {
expect(Object.keys(response.body).length).toEqual(0);
});

test('Get empty logs', async () => {
const response = await request(testServer.app).get('/logs');
expect(response.status).toBe(200);
expect(Object.keys(response.body).length).toEqual(0);
});

test('Add new sync', async () => {
const response = await request(testServer.app).post('/sync').send({});
expect(response.status).toBe(201);
Expand All @@ -64,6 +72,12 @@ describe('Server width folder', () => {
expect(Object.keys(response.body).length).toEqual(1);
});

test('Get logs with sync created', async () => {
const response = await request(testServer.app).get('/logs');
expect(response.status).toBe(200);
expect(Object.keys(response.body).length).toEqual(1);
});

test('Update a sync', async () => {
const response = await request(testServer.app).get('/sync');
expect(response.status).toBe(200);
Expand Down Expand Up @@ -106,25 +120,19 @@ describe('Server width folder', () => {
expect(response.status).toBe(200);
expect(Object.keys(response.body).length).toEqual(0);
});
});

describe('Server Logs', () => {
beforeAll(async () => {
await beforeStartServer('.nuclia');
await testServer.start();
});
afterAll(async () => {
await deleteDirectory('.nuclia');
await testServer.close();
test('Get all logs', async () => {
const response = await request(testServer.app).get('/logs');
expect(response.status).toBe(200);
expect(Object.keys(response.body).length).toEqual(3);
});

test('Init server', async () => {
const response = await request(testServer.app).get('/');
test('Delete logs', async () => {
let response = await request(testServer.app).delete('/logs');
expect(response.status).toBe(200);
});

test('Get logs', async () => {
const response = await request(testServer.app).get('/logs');
response = await request(testServer.app).get('/logs');
expect(response.status).toBe(200);
expect(Object.keys(response.body).length).toEqual(0);
});
});

0 comments on commit 2a691dd

Please sign in to comment.