Skip to content

Commit b660106

Browse files
committed
feat(UtmAgentConnect): refactor agent handling and improve data binding
1 parent 89892a0 commit b660106

File tree

5 files changed

+64
-60
lines changed

5 files changed

+64
-60
lines changed

frontend/src/app/assets-discover/assets-view/assets-view.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ <h5 class="card-title mb-0 text-uppercase label-header">
120120
</thead>
121121
<tbody *ngIf="assets$ | async as assets">
122122
<ng-container *ngFor="let asset of assets; trackBy: trackByFn">
123-
<tr (click)="viwAgentDetail($event, asset)" class="cursor-pointer">
123+
<tr>
124124
<td class="text-center">
125125
<div class="d-flex justify-content-between align-items-center h-100 w-100 small-md-icon">
126126
<div class="list-icons span-small-icon">

frontend/src/app/assets-discover/assets-view/assets-view.component.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {HttpResponse} from '@angular/common/http';
2-
import {AfterViewInit, ChangeDetectionStrategy, Component, OnDestroy, OnInit} from '@angular/core';
2+
import {ChangeDetectionStrategy, Component, OnDestroy, OnInit} from '@angular/core';
33
import {Router} from '@angular/router';
44
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
55
import {ResizeEvent} from 'angular-resizable-element';
@@ -42,7 +42,7 @@ import {SourceDataTypeConfigComponent} from '../source-data-type-config/source-d
4242
styleUrls: ['./assets-view.component.scss'],
4343
changeDetection: ChangeDetectionStrategy.OnPush
4444
})
45-
export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
45+
export class AssetsViewComponent implements OnInit, OnDestroy {
4646
assets$: Observable<NetScanType[]>;
4747
assets: NetScanType[];
4848
// defaultTime: ElasticFilterDefaultTime = new ElasticFilterDefaultTime('now-30d', 'now');
@@ -98,7 +98,7 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
9898

9999
ngOnInit() {
100100
this.setInitialWidth();
101-
this.starInterval();
101+
102102
this.accountService.identity().then(account => {
103103
this.reasonRun = {
104104
command: '',
@@ -130,10 +130,9 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
130130
});
131131
})
132132
);
133-
}
134133

135-
ngAfterViewInit() {
136-
this.getAssets();
134+
this.utmNetScanService.notifyRefresh(true);
135+
this.starInterval();
137136
}
138137

139138
setInitialWidth() {
@@ -145,10 +144,6 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
145144
loadPage(page: number) {
146145
this.page = page - 1;
147146
this.requestParam.page = page;
148-
this.getAssets();
149-
}
150-
151-
getAssets() {
152147
this.utmNetScanService.notifyRefresh(true);
153148
}
154149

@@ -163,13 +158,13 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
163158
onItemsPerPageChange($event: number) {
164159
this.itemsPerPage = $event;
165160
this.requestParam.size = $event;
166-
this.getAssets();
161+
this.utmNetScanService.notifyRefresh(true);
167162
}
168163

169164
onTimeFilterChange($event: TimeFilterType) {
170165
this.requestParam.discoveredInitDate = $event.timeFrom;
171166
this.requestParam.discoveredEndDate = $event.timeTo;
172-
this.getAssets();
167+
this.utmNetScanService.notifyRefresh(true);
173168
}
174169

175170
onResize($event: ResizeEvent) {
@@ -186,7 +181,7 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
186181

187182
onSortBy($event: SortEvent) {
188183
this.requestParam.sort = $event.column + ',' + $event.direction;
189-
this.getAssets();
184+
this.utmNetScanService.notifyRefresh(true);
190185
}
191186

192187
toggleCheck() {
@@ -230,7 +225,7 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
230225
}
231226
}
232227
this.assetFiltersBehavior.$assetFilter.next(this.requestParam);
233-
this.getAssets();
228+
this.utmNetScanService.notifyRefresh(true);
234229
}
235230

236231
onFilterChange($event: { prop: AssetFieldFilterEnum, values: any }) {
@@ -268,13 +263,13 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
268263
}
269264
this.assetFiltersBehavior.$assetAppliedFilter.next(this.requestParam);
270265
this.assetFiltersBehavior.$assetFilter.next(this.requestParam);
271-
this.getAssets();
266+
this.utmNetScanService.notifyRefresh(true);
272267
}
273268

274269
onSearch($event: string) {
275270
this.requestParam.assetIpMacName = $event;
276271
this.requestParam.page = 0;
277-
this.getAssets();
272+
this.utmNetScanService.notifyRefresh(true);
278273
}
279274

280275
deleteAsset(event: Event, asset: NetScanType) {
@@ -293,7 +288,7 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
293288
delete(asset: NetScanType) {
294289
this.utmNetScanService.deleteCustomAsset(asset.id).subscribe(() => {
295290
this.utmToastService.showSuccessBottom('Asset deleted successfully');
296-
this.getAssets();
291+
this.utmNetScanService.notifyRefresh(true);
297292
}, () => {
298293
this.utmToastService.showError('Error deleting asset',
299294
'Error while trying to delete asset, please try again');
@@ -305,7 +300,7 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
305300
event.stopPropagation();
306301
this.deleting.push(dat.id);
307302
this.dataSourceInputService.delete(dat.id).subscribe(() => {
308-
this.getAssets();
303+
this.utmNetScanService.notifyRefresh(true);
309304
const indexDelete = this.deleting.indexOf(dat.id);
310305
if (indexDelete !== -1) {
311306
this.deleting.splice(indexDelete, 1);
@@ -382,7 +377,7 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
382377
showDataTypeModal() {
383378
const modalSource = this.modalService.open(SourceDataTypeConfigComponent, {centered: true, size: 'lg'});
384379
modalSource.componentInstance.refreshDataInput.subscribe(() => {
385-
this.getAssets();
380+
this.utmNetScanService.notifyRefresh(true);
386381
});
387382
}
388383

@@ -407,8 +402,8 @@ export class AssetsViewComponent implements OnInit, AfterViewInit, OnDestroy {
407402
starInterval() {
408403
if (!this.interval) {
409404
this.interval = setInterval(() => {
410-
this.getAssets();
411-
}, 10000);
405+
this.utmNetScanService.notifyRefresh(true);
406+
}, 30000);
412407
}
413408
}
414409

frontend/src/app/shared/components/utm/util/utm-agent-connect/utm-agent-connect.component.html

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,36 @@
66
</div>
77
</div>
88
</div>
9+
<ng-container *ngIf="agent$ | async as agent">
10+
<div class="w-100" [ngClass]="{'mt-3': !hostname}">
11+
<app-utm-agent-detail
12+
[agent]="agent"
13+
[asset]="asset">
14+
</app-utm-agent-detail>
15+
<div class="form-group mt-3" *ngIf="hasNoReason && agent.status === 'ONLINE'">
16+
<label for="reportDesc">Provide the reason to run a command in the agent</label>
17+
<textarea class="form-control" [(ngModel)]="websocketCommand.reason" id="reportDesc"
18+
rows="3"></textarea>
19+
</div>
920

10-
<div class="w-100" [ngClass]="{'mt-3': !hostname}" *ngIf="agent">
11-
<app-utm-agent-detail
12-
[agent]="agent"
13-
[asset]="asset">
14-
</app-utm-agent-detail>
15-
<div class="form-group mt-3" *ngIf="hasNoReason && agent.status === 'ONLINE'">
16-
<label for="reportDesc">Provide the reason to run a command in the agent</label>
17-
<textarea class="form-control" [(ngModel)]="websocketCommand.reason" id="reportDesc"
18-
rows="3"></textarea>
21+
<div *ngIf="agent && agent.status === 'ONLINE'" class="d-flex justify-content-end">
22+
<button class="btn utm-button utm-button-primary" (click)="connectToAgent = true"
23+
[disabled]="!websocketCommand.reason && websocketCommand.reason === ''">
24+
<i class="icon-terminal mr-1"></i>
25+
Connect to console
26+
</button>
27+
</div>
1928
</div>
20-
21-
<div *ngIf="agent && agent.status === 'ONLINE'" class="d-flex justify-content-end">
22-
<button class="btn utm-button utm-button-primary" (click)="connectToAgent = true"
23-
[disabled]="!websocketCommand.reason && websocketCommand.reason === ''">
24-
<i class="icon-terminal mr-1"></i>
25-
Connect to console
26-
</button>
29+
<div *ngIf="agent && agent.status === 'ONLINE'" class="alert alert-warning alert-styled-right mt-3 alert-dismissible">
30+
<span class="font-weight-semibold">Warning! </span>
31+
<span>You can cause damage to the infrastructure and services of your organization</span>
2732
</div>
28-
</div>
29-
<div *ngIf="agent && agent.status === 'ONLINE'" class="alert alert-warning alert-styled-right mt-3 alert-dismissible">
30-
<span class="font-weight-semibold">Warning! </span>
31-
<span>You can cause damage to the infrastructure and services of your organization</span>
32-
</div>
33-
<div *ngIf="agent && agent.status === 'OFFLINE'" class="alert alert-info alert-styled-right mt-3 info-dismissible">
34-
<span class="font-weight-semibold">Info! </span>
35-
<span>The agent is offline. Data shown is from the last successful sync.</span>
36-
</div>
37-
<app-utm-agent-console *ngIf="agent && connectToAgent" [websocketCommand]="websocketCommand"
38-
(close)="connectToAgent = false"
39-
[hostname]="agent.hostname"></app-utm-agent-console>
33+
<div *ngIf="agent && agent.status === 'OFFLINE'" class="alert alert-info alert-styled-right mt-3 info-dismissible">
34+
<span class="font-weight-semibold">Info! </span>
35+
<span>The agent is offline. Data shown is from the last successful sync.</span>
36+
</div>
37+
<app-utm-agent-console *ngIf="agent && connectToAgent" [websocketCommand]="websocketCommand"
38+
(close)="connectToAgent = false"
39+
[hostname]="agent.hostname"></app-utm-agent-console>
40+
</ng-container>
41+
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import {Component, Input, OnInit} from '@angular/core';
1+
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
22
import {NetScanType} from '../../../../../assets-discover/shared/types/net-scan.type';
33
import {UtmAgentManagerService} from '../../../../services/agent/utm-agent-manager.service';
44
import {AgentStatusEnum, AgentType} from '../../../../types/agent/agent.type';
55
import {IncidentCommandType} from '../../../../types/incident/incident-command.type';
6+
import {Observable, of} from "rxjs";
7+
import {catchError, map} from "rxjs/operators";
68

79
@Component({
810
selector: 'app-utm-agent-connect',
911
templateUrl: './utm-agent-connect.component.html',
1012
styleUrls: ['./utm-agent-connect.component.css']
1113
})
12-
export class UtmAgentConnectComponent implements OnInit {
14+
export class UtmAgentConnectComponent implements OnInit, OnChanges {
1315
@Input() hostname: string;
1416
@Input() asset: NetScanType;
1517
@Input() websocketCommand: IncidentCommandType;
16-
agent: AgentType;
18+
agent$: Observable<AgentType>;
1719
connectToAgent = false;
1820
hasNoReason = false;
1921

@@ -22,22 +24,26 @@ export class UtmAgentConnectComponent implements OnInit {
2224

2325
ngOnInit() {
2426
this.hasNoReason = this.websocketCommand.reason === '' || !this.websocketCommand.reason;
27+
}
28+
29+
ngOnChanges(changes: SimpleChanges): void {
2530
if (this.hostname) {
26-
this.agentManagerService.getAgent(this.hostname).subscribe(response => {
27-
this.agent = response.body;
28-
},
29-
error => this.assetTypeToAgentType());
31+
this.agent$ = this.agentManagerService.getAgent(this.hostname)
32+
.pipe(
33+
map(response => response.body),
34+
catchError(() => this.assetTypeToAgentType() )
35+
);
3036
}
3137
}
3238

3339
onAgentSelect($event: AgentType) {
3440
this.websocketCommand.reason = '';
3541
this.hasNoReason = true;
36-
this.agent = $event;
42+
this.agent$ = of($event);
3743
}
3844

3945
assetTypeToAgentType() {
40-
this.agent = {
46+
return of({
4147
ip: this.asset.assetIp,
4248
hostname: this.asset.assetName,
4349
os: this.asset.assetOs,
@@ -52,6 +58,6 @@ export class UtmAgentConnectComponent implements OnInit {
5258
osMinorVersion: this.asset.assetOs,
5359
aliases: '',
5460
addresses: ''
55-
};
61+
});
5662
}
5763
}

frontend/src/app/shared/components/utm/util/utm-agent-detail/utm-agent-detail.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class UtmAgentDetailComponent implements OnInit {
2525
}
2626

2727
ngOnInit() {
28+
console.log(this.agent);
2829
this.agentIp = this.agent.ip;
2930
this.ips = this.agent.addresses !== '' ? this.agent.addresses.split(',') : [];
3031
this.macs = this.agent.mac !== '' ? this.agent.mac.split(',') : [];

0 commit comments

Comments
 (0)