From cfdb69987dece0d00f88520a9be82c242940987b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 29 May 2018 16:48:45 +0200 Subject: [PATCH] Implement ProcessLogService and hook it up to existing log features --- src/app/app.module.ts | 4 ++- src/app/config.ts | 3 ++- src/app/console/console.component.ts | 17 +++++++------ src/app/dashboard/dashboard.component.ts | 14 ++++++----- src/app/services/processlog-command.ts | 5 ++++ src/app/services/processlog.service.ts | 31 ++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 src/app/services/processlog-command.ts create mode 100644 src/app/services/processlog.service.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 98fc89a..ce6ccef 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -27,6 +27,7 @@ import { TestmessengerComponent } from './testmessenger/testmessenger.component' import { DeploymentNotificationService } from './services/deployment-notification.service'; import { SafePipe } from './pipes/safe.pipe'; import { ConsoleComponent } from './console/console.component'; +import { ProcessLogService } from './services/processlog.service'; @NgModule({ @@ -57,7 +58,8 @@ import { ConsoleComponent } from './console/console.component'; TerminadoService, FSMUpdateService, ProcessManagerService, - DeploymentNotificationService + DeploymentNotificationService, + ProcessLogService ], bootstrap: [ AppComponent diff --git a/src/app/config.ts b/src/app/config.ts index 56c8b1d..0efc28b 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -40,7 +40,8 @@ export const config = { console: { route: 'console', defaultContent: '', - rewriteContentWithNewLogs: 'stdout' + rewriteContentWithProcessLogs: 'stdout', + showLiveLogs: true }, testmessenger: { route: 'testmessenger' diff --git a/src/app/console/console.component.ts b/src/app/console/console.component.ts index 60dc3e3..04c6155 100644 --- a/src/app/console/console.component.ts +++ b/src/app/console/console.component.ts @@ -5,6 +5,7 @@ import { Component, OnInit } from '@angular/core'; import { WebSocketService } from '../services/websocket.service'; import { ConsoleCommand } from './console-command'; import { config } from '../config'; +import { ProcessLogService } from '../services/processlog.service'; @Component({ selector: 'app-console', @@ -13,20 +14,22 @@ import { config } from '../config'; }) export class ConsoleComponent implements OnInit { console_content: string = config.console.defaultContent; - rewriteContentWithNewLogs: string = config.console.rewriteContentWithNewLogs; + rewriteContentWithProcessLogs: string = config.console.rewriteContentWithProcessLogs; command_handlers = { 'write': this.writeHandler.bind(this), 'read': this.readHandler.bind(this) }; - constructor(private webSocketService: WebSocketService) {} + constructor(private webSocketService: WebSocketService, + private processLogService: ProcessLogService) {} ngOnInit() { this.webSocketService.connect(); - this.webSocketService.observeKey('console').subscribe((event) => { - this.command_handlers[event.data.command](event.data); - }); + this.webSocketService.observeKey('console').subscribe( + (event) => this.command_handlers[event.data.command](event.data) + ); + this.processLogService.newLog.subscribe((data) => this.newLogHandler(data)); } writeHandler(data: ConsoleCommand) { @@ -38,8 +41,8 @@ export class ConsoleComponent implements OnInit { } newLogHandler(logs: any) { - if (this.rewriteContentWithNewLogs !== '') { - const log = logs[this.rewriteContentWithNewLogs]; + if (this.rewriteContentWithProcessLogs !== '') { + const log = logs[this.rewriteContentWithProcessLogs]; if (log) { this.setContent(log); } diff --git a/src/app/dashboard/dashboard.component.ts b/src/app/dashboard/dashboard.component.ts index 106c6bb..f8cd3dc 100644 --- a/src/app/dashboard/dashboard.component.ts +++ b/src/app/dashboard/dashboard.component.ts @@ -7,7 +7,7 @@ import { Subscription } from 'rxjs'; import { WebSocketService } from '../services/websocket.service'; import { LayoutCommand } from './layout-command'; import { config } from '../config'; -import { ConsoleComponent } from '../console/console.component'; +import { ProcessLogService } from '../services/processlog.service'; @Component({ selector: 'app-dashboard', @@ -21,7 +21,6 @@ export class DashboardComponent implements OnInit, OnDestroy { hide_messages: boolean = config.dashboard.hide_messages; iframeUrl: string = config.dashboard.iframeUrl; @ViewChild('webiframe') webiframe: ElementRef; - @ViewChild(ConsoleComponent) childConsole: ConsoleComponent; selectedTerminalMenuItem = config.dashboard.terminalOrConsole; command_handlers = {'layout': this.layoutHandler.bind(this), @@ -31,7 +30,8 @@ export class DashboardComponent implements OnInit, OnDestroy { constructor(private deploymentNotificationService: DeploymentNotificationService, private webSocketService: WebSocketService, - private changeDetectorRef: ChangeDetectorRef) {} + private changeDetectorRef: ChangeDetectorRef, + private processLogService: ProcessLogService) {} ngOnInit() { this.webSocketService.connect(); @@ -103,9 +103,11 @@ export class DashboardComponent implements OnInit, OnDestroy { } setConsoleContent(logs: any) { - this.childConsole.newLogHandler(logs); - if (config.ide.showConsoleOnDeploy) { - this.selectTerminalMenuItem('console'); + if (!config.console.showLiveLogs) { + this.processLogService.newLog.next(logs); + if (config.ide.showConsoleOnDeploy) { + this.selectTerminalMenuItem('console'); + } } } } diff --git a/src/app/services/processlog-command.ts b/src/app/services/processlog-command.ts new file mode 100644 index 0000000..2a589b0 --- /dev/null +++ b/src/app/services/processlog-command.ts @@ -0,0 +1,5 @@ +export class ProcessLogCommand { + command: string; + stdout: string; + stderr: string; +} diff --git a/src/app/services/processlog.service.ts b/src/app/services/processlog.service.ts new file mode 100644 index 0000000..a46fff5 --- /dev/null +++ b/src/app/services/processlog.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { WebSocketService } from './websocket.service'; +import { Subject } from 'rxjs/Subject'; +import { ProcessLogCommand } from './processlog-command'; +import { config } from '../config'; + +@Injectable() +export class ProcessLogService { + newLog = new Subject(); + showLiveLogs = config.console.showLiveLogs; + + command_handlers = { + 'new_log': this.newLogHandler.bind(this) + }; + + constructor(private webSocketService: WebSocketService) { + this.webSocketService.connect(); + this.webSocketService.observeKey('processlog').subscribe( + (event) => this.command_handlers[event.data.command](event.data) + ); + } + + newLogHandler(data: ProcessLogCommand) { + if (this.showLiveLogs) { + this.newLog.next({ + stdout: data.stdout, + stderr: data.stderr + }); + } + } +}