Skip to content

Commit 8153c67

Browse files
authored
- Fix unassign agent in edit-agent page (#217)
- Simplify input select transformations - Improve typing and fix some issues related to it
1 parent d363a90 commit 8153c67

File tree

29 files changed

+528
-614
lines changed

29 files changed

+528
-614
lines changed

src/app/agents/edit-agent/edit-agent.component.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<grid-main>
22
<app-page-title [title]="'Agent Details'" [subbutton]="false"></app-page-title>
33
<form [formGroup]="updateForm" (ngSubmit)="onSubmit()">
4-
<div fxLayout="row wrap" fxLayoutAlign="start center" fxLayoutGap="10px">
4+
<div *ngIf="showagent" fxLayout="row wrap" fxLayoutAlign="start center" fxLayoutGap="10px">
55
<simulate-form-field label="Agent ID" message="{{ showagent['id'] }}"></simulate-form-field>
66
<input-text title="Machine Name" formControlName="agentName" icon="computer"></input-text>
77
<input-select title="Owner" formControlName="userId" [items]="selectUsers"></input-select>
88
<form [formGroup]="updateAssignForm" (ngSubmit)="onSubmit()">
9-
<input-select title="Assignment" formControlName="taskId" [items]="assignTasks" [isBlankOptionDisabled]="assignNew"></input-select>
9+
<input-select title="Assignment" formControlName="taskId" [items]="assignTasks" [isBlankOptionDisabled]="assignNew" [blankOptionText]="'Unassign'"></input-select>
1010
</form>
1111
<input-select title="Cracker errors" formControlName="ignoreErrors" [items]="selectIgnorerrors"></input-select>
1212
<input-text title="Extra parameters" formControlName="cmdPars"></input-text>
@@ -21,15 +21,15 @@
2121
<span><b>Detailed Information</b></span>
2222
</mat-panel-title>
2323
</mat-expansion-panel-header>
24-
<div fxLayout="row wrap" fxLayoutAlign="start center" fxLayoutGap="10px">
24+
<div *ngIf="showagent" fxLayout="row wrap" fxLayoutAlign="start center" fxLayoutGap="10px">
2525
<simulate-form-field label="Last activity" message="{{ showagent['lastTime'] | uiDate }}"></simulate-form-field>
2626
<simulate-form-field label="Last Action" message="{{ showagent['lastAct'] }}"></simulate-form-field>
2727
<simulate-form-field label="IP" message="{{ showagent['lastIp'] }}"></simulate-form-field>
2828
<simulate-form-field label="Machine ID" message="{{ showagent['uid'] }}"></simulate-form-field>
2929
<simulate-form-field label="Access token" message="{{ showagent['token'] }}"></simulate-form-field>
3030
<simulate-form-field label="O.S." [message]="showagent['os'] === 0 ? 'Linux' : (showagent['os'] === 1 ? 'Windows' : (showagent['os'] === 2 ? 'IOS' : 'Unknown'))" icon="computer"></simulate-form-field>
3131
<simulate-form-field *ngIf="showagent['devices']" label="Graphic cards" message="{{ this.renderDevices(showagent['devices']) }}"></simulate-form-field>
32-
<simulate-form-field-multi label="Member of access groups" [items]="selectuserAgps" routerLink="/users/access-groups/"></simulate-form-field-multi>
32+
<simulate-form-field-multi label="Member of access groups" [items]="selectUserAgps" routerLink="/users/access-groups/"></simulate-form-field-multi>
3333
</div>
3434
<simulate-form-field label="Time spent cracking" message="{{ timespent | sectotime }}" *ngIf="timespent"></simulate-form-field>
3535
</mat-expansion-panel>

src/app/agents/edit-agent/edit-agent.component.ts

+61-73
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as echarts from 'echarts/core';
1+
import { LineChart } from 'echarts/charts';
22
import {
33
GridComponent,
44
GridComponentOption,
@@ -13,47 +13,51 @@ import {
1313
TooltipComponent,
1414
TooltipComponentOption
1515
} from 'echarts/components';
16-
17-
import { CanvasRenderer } from 'echarts/renderers';
18-
import { LineChart } from 'echarts/charts';
16+
import * as echarts from 'echarts/core';
1917
import { UniversalTransition } from 'echarts/features';
18+
import { CanvasRenderer } from 'echarts/renderers';
19+
import { firstValueFrom } from 'rxjs';
2020

21-
import { ActivatedRoute, Params, Router } from '@angular/router';
22-
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
21+
import { Component, OnDestroy, OnInit } from '@angular/core';
2322
import { FormGroup } from '@angular/forms';
23+
import { ActivatedRoute, Params, Router } from '@angular/router';
2424

25-
import { ASC, ignoreErrors } from '@src/app/core/_constants/agentsc.config';
25+
import { JAgentAssignment } from '@models/agent-assignment.model';
26+
import { JAgentStat } from '@models/agent-stats.model';
27+
import { JAgent } from '@models/agent.model';
28+
import { JChunk } from '@models/chunk.model';
29+
import { FilterType } from '@models/request-params.model';
30+
import { ResponseWrapper } from '@models/response.model';
31+
import { JTask } from '@models/task.model';
32+
import { JUser } from '@models/user.model';
33+
34+
import { JsonAPISerializer } from '@services/api/serializer-service';
35+
import { SERV } from '@services/main.config';
36+
import { GlobalService } from '@services/main.service';
37+
import { RequestParamBuilder } from '@services/params/builder-implementation.service';
38+
import { AlertService } from '@services/shared/alert.service';
39+
import { AutoTitleService } from '@services/shared/autotitle.service';
40+
import { UIConfigService } from '@services/shared/storage.service';
41+
import { UnsubscribeService } from '@services/unsubscribe.service';
2642

27-
import { UIConfigService } from 'src/app/core/_services/shared/storage.service';
28-
import { GlobalService } from 'src/app/core/_services/main.service';
29-
import { SERV } from '../../core/_services/main.config';
30-
import { AlertService } from 'src/app/core/_services/shared/alert.service';
31-
import { transformSelectOptions } from 'src/app/shared/utils/forms';
32-
import { UnsubscribeService } from 'src/app/core/_services/unsubscribe.service';
33-
import { AutoTitleService } from 'src/app/core/_services/shared/autotitle.service';
34-
import { TASKS_FIELD_MAPPING, USER_AGP_FIELD_MAPPING, USER_FIELD_MAPPING } from 'src/app/core/_constants/select.config';
35-
import { ResponseWrapper } from '../../core/_models/response.model';
36-
import { JAgent } from '../../core/_models/agent.model';
37-
import { JUser } from '../../core/_models/user.model';
38-
import { JsonAPISerializer } from '../../core/_services/api/serializer-service';
39-
import { JTask } from '../../core/_models/task.model';
40-
import { JChunk } from '../../core/_models/chunk.model';
41-
import { JAgentAssignment } from '../../core/_models/agent-assignment.model';
42-
import { FilterType } from 'src/app/core/_models/request-params.model';
43-
import { RequestParamBuilder } from '@src/app/core/_services/params/builder-implementation.service';
4443
import {
4544
EditAgentForm,
4645
UpdateAssignmentForm,
4746
getEditAgentForm,
4847
getUpdateAssignmentForm
4948
} from '@src/app/agents/edit-agent/edit-agent.form';
50-
import { firstValueFrom } from 'rxjs';
51-
import { JAgentStat } from '@models/agent-stats.model';
49+
import { ASC, ignoreErrors } from '@src/app/core/_constants/agentsc.config';
50+
import {
51+
ACCESS_GROUP_FIELD_MAPPING,
52+
DEFAULT_FIELD_MAPPING,
53+
TASKS_FIELD_MAPPING
54+
} from '@src/app/core/_constants/select.config';
55+
import { SelectOption, transformSelectOptions } from '@src/app/shared/utils/forms';
5256

5357
@Component({
54-
selector: 'app-edit-agent',
55-
templateUrl: './edit-agent.component.html',
56-
standalone: false
58+
selector: 'app-edit-agent',
59+
templateUrl: './edit-agent.component.html',
60+
standalone: false
5761
})
5862
export class EditAgentComponent implements OnInit, OnDestroy {
5963
/** Flag indicating whether data is still loading. */
@@ -67,54 +71,38 @@ export class EditAgentComponent implements OnInit, OnDestroy {
6771
isUpdatingLoading = false;
6872

6973
/** Select Options. */
70-
selectUsers: any;
74+
selectUsers: SelectOption[] = [];
7175
selectIgnorerrors = ignoreErrors;
72-
selectuserAgps: any;
73-
74-
/** Select Options Mapping */
75-
selectUserAgpMap = {
76-
fieldMapping: USER_AGP_FIELD_MAPPING
77-
};
78-
79-
selectUserMap = {
80-
fieldMapping: USER_FIELD_MAPPING
81-
};
82-
83-
selectAssignMap = {
84-
fieldMapping: TASKS_FIELD_MAPPING
85-
};
76+
selectUserAgps: SelectOption[];
8677

8778
/** Assign Tasks */
88-
assignTasks: any = [];
89-
assignNew: any;
90-
assignId: any;
79+
assignTasks: SelectOption[];
80+
assignNew: boolean;
81+
assignId: number;
9182

9283
// Edit Index
9384
editedAgentIndex: number;
94-
editedAgent: any;
95-
showagent: any = [];
85+
showagent: JAgent;
9686

9787
// Calculations
9888
timespent: number;
99-
getchunks: any;
89+
getchunks: JChunk[];
10090

10191
currentAssignment: JAgentAssignment;
10292

10393
constructor(
10494
private unsubscribeService: UnsubscribeService,
105-
private changeDetectorRef: ChangeDetectorRef,
10695
private titleService: AutoTitleService,
10796
private uiService: UIConfigService,
10897
private route: ActivatedRoute,
10998
private alert: AlertService,
11099
private gs: GlobalService,
111100
private router: Router,
112-
private serializer: JsonAPISerializer,
113-
private cdr: ChangeDetectorRef
101+
private serializer: JsonAPISerializer
114102
) {
115103
this.onInitialize();
116104
this.buildEmptyForms();
117-
titleService.set(['Edit Agent']);
105+
this.titleService.set(['Edit Agent']);
118106
}
119107

120108
/**
@@ -169,7 +157,7 @@ export class EditAgentComponent implements OnInit, OnDestroy {
169157
const responseBody = { data: response.data, included: response.included };
170158
const agent = this.serializer.deserialize<JAgent>(responseBody);
171159
this.showagent = agent;
172-
this.selectuserAgps = transformSelectOptions(agent.accessGroups, this.selectUserAgpMap);
160+
this.selectUserAgps = transformSelectOptions(agent.accessGroups, ACCESS_GROUP_FIELD_MAPPING);
173161
}
174162

175163
/**
@@ -185,7 +173,7 @@ export class EditAgentComponent implements OnInit, OnDestroy {
185173
const tasks = this.serializer.deserialize<JTask[]>(responseBody);
186174

187175
const filterTasks = tasks.filter((u) => u.keyspaceProgress < u.keyspace || Number(u.keyspaceProgress) === 0); //Remove completed tasks
188-
this.assignTasks = transformSelectOptions(filterTasks, this.selectAssignMap);
176+
this.assignTasks = transformSelectOptions(filterTasks, TASKS_FIELD_MAPPING);
189177
});
190178
this.unsubscribeService.add(loadTasksSubscription$);
191179
}
@@ -197,7 +185,11 @@ export class EditAgentComponent implements OnInit, OnDestroy {
197185
private loadSelectUsers() {
198186
const loadUsersSubscription$ = this.gs.getAll(SERV.USERS).subscribe((response: ResponseWrapper) => {
199187
const responseBody = { data: response.data, included: response.included };
200-
this.selectUsers = this.serializer.deserialize<JUser[]>(responseBody);
188+
this.selectUsers = transformSelectOptions(
189+
this.serializer.deserialize<JUser[]>(responseBody),
190+
DEFAULT_FIELD_MAPPING
191+
);
192+
console.log(this.selectUsers);
201193
});
202194
this.unsubscribeService.add(loadUsersSubscription$);
203195
}
@@ -301,7 +293,7 @@ export class EditAgentComponent implements OnInit, OnDestroy {
301293
onSubmit() {
302294
if (this.updateForm.valid) {
303295
if (this.updateAssignForm.valid) {
304-
this.onUpdateAssign(this.updateAssignForm.value);
296+
this.onUpdateAssign(this.updateAssignForm.value.taskId);
305297
}
306298
this.isUpdatingLoading = true;
307299
const onSubmitSubscription$ = this.gs
@@ -318,18 +310,17 @@ export class EditAgentComponent implements OnInit, OnDestroy {
318310
/**
319311
* Updates agent assignment based on the provided value.
320312
*
321-
* @param value The form value containing the task ID.
313+
* @param taskId The task ID.
322314
*/
323-
onUpdateAssign(value: FormGroup<UpdateAssignmentForm>['value']) {
324-
if (value.taskId) {
315+
onUpdateAssign(taskId: number) {
316+
if (taskId) {
325317
const payload = {
326-
taskId: value.taskId,
318+
taskId: taskId,
327319
agentId: this.editedAgentIndex
328320
};
329321
const onCreateSubscription$ = this.gs.create(SERV.AGENT_ASSIGN, payload).subscribe();
330322
this.unsubscribeService.add(onCreateSubscription$);
331-
}
332-
if (value.taskId === 0) {
323+
} else {
333324
const onDeleteSubscription$ = this.gs.delete(SERV.AGENT_ASSIGN, this.assignId).subscribe();
334325
this.unsubscribeService.add(onDeleteSubscription$);
335326
}
@@ -349,11 +340,10 @@ export class EditAgentComponent implements OnInit, OnDestroy {
349340
}
350341
});
351342

352-
// Format the result string with HTML line breaks
353-
const formattedDevices = Object.keys(deviceCountMap)
343+
// Format with HTML line breaks and return the formatted devices as string
344+
return Object.keys(deviceCountMap)
354345
.map((device) => `${deviceCountMap[device]} x ${device}`)
355346
.join('<br>');
356-
return formattedDevices;
357347
}
358348

359349
// //
@@ -425,11 +415,10 @@ export class EditAgentComponent implements OnInit, OnDestroy {
425415
templabel = '%';
426416
}
427417

428-
const data: any = agentStatList;
429418
const arr = [];
430419
const max = [];
431420
const devlabels = [];
432-
const result: any = agentStatList;
421+
const result = agentStatList;
433422

434423
for (let i = 0; i < result.length; i++) {
435424
const val = result[i].value;
@@ -450,7 +439,6 @@ export class EditAgentComponent implements OnInit, OnDestroy {
450439
const labels = [...new Set(devlabels)];
451440

452441
const startdate = Math.max(...max);
453-
const datelabel = this.transDate(startdate);
454442
const xAxis = this.generateIntervalsOf(1, +startdate - 500, +startdate);
455443

456444
const chartDom = document.getElementById(name);
@@ -518,8 +506,8 @@ export class EditAgentComponent implements OnInit, OnDestroy {
518506
return this.uiService.getUIsettings('agentTempThreshold2').value;
519507
}
520508

521-
transDate(dt) {
522-
const date: any = new Date(dt * 1000);
509+
transDate(dt: number) {
510+
const date = new Date(dt * 1000);
523511
return (
524512
date.getUTCDate() +
525513
'-' +
@@ -539,7 +527,7 @@ export class EditAgentComponent implements OnInit, OnDestroy {
539527
return (dt < 10 ? '0' : '') + dt;
540528
}
541529

542-
generateIntervalsOf(interval, start, end) {
530+
generateIntervalsOf(interval: number, start: number, end: number) {
543531
const result = [];
544532
let current = start;
545533

src/app/agents/edit-agent/edit-agent.form.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ export const getEditAgentForm = () => {
4646
*/
4747
export const getUpdateAssignmentForm = () => {
4848
return new FormGroup<UpdateAssignmentForm>({
49-
taskId: new FormControl(undefined)
49+
taskId: new FormControl(0)
5050
});
5151
};

src/app/agents/show-agents/show-agents.component.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { AutoTitleService } from 'src/app/core/_services/shared/autotitle.service';
21
import { Component } from '@angular/core';
32

3+
import { AutoTitleService } from '@services/shared/autotitle.service';
4+
45
@Component({
5-
selector: 'app-show-agents',
6-
templateUrl: './show-agents.component.html',
7-
standalone: false
6+
selector: 'app-show-agents',
7+
templateUrl: './show-agents.component.html',
8+
standalone: false
89
})
910
export class ShowAgentsComponent {
1011
constructor(private titleService: AutoTitleService) {

src/app/config/health-checks/new-health-check/new-health-checks.component.ts

+14-21
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@ import { Component, OnDestroy, OnInit } from '@angular/core';
22
import { FormControl, FormGroup, Validators } from '@angular/forms';
33
import { Router } from '@angular/router';
44

5+
import { JCrackerBinary, JCrackerBinaryType } from '@models/cracker-binary.model';
6+
import { Filter, FilterType } from '@models/request-params.model';
57
import { ResponseWrapper } from '@models/response.model';
8+
69
import { JsonAPISerializer } from '@services/api/serializer-service';
7-
import { JCrackerBinary, JCrackerBinaryType } from '@models/cracker-binary.model';
8-
import { CRACKER_TYPE_FIELD_MAPPING, CRACKER_VERSION_FIELD_MAPPING } from '@src/app/core/_constants/select.config';
10+
import { SERV } from '@services/main.config';
11+
import { GlobalService } from '@services/main.service';
12+
import { AlertService } from '@services/shared/alert.service';
13+
import { AutoTitleService } from '@services/shared/autotitle.service';
914
import { UnsubscribeService } from '@services/unsubscribe.service';
15+
1016
import { attack, hashtype } from '@src/app/core/_constants/healthchecks.config';
11-
import { AutoTitleService } from '@services/shared/autotitle.service';
12-
import { AlertService } from '@services/shared/alert.service';
13-
import { GlobalService } from '@services/main.service';
14-
import { SERV } from '@services/main.config';
17+
import { CRACKER_TYPE_FIELD_MAPPING, CRACKER_VERSION_FIELD_MAPPING } from '@src/app/core/_constants/select.config';
1518
import { transformSelectOptions } from '@src/app/shared/utils/forms';
16-
import { Filter, FilterType } from '@models/request-params.model';
1719

1820
@Component({
19-
selector: 'app-new-health-checks',
20-
templateUrl: './new-health-checks.component.html',
21-
standalone: false
21+
selector: 'app-new-health-checks',
22+
templateUrl: './new-health-checks.component.html',
23+
standalone: false
2224
})
2325
export class NewHealthChecksComponent implements OnInit, OnDestroy {
2426
/** Form group for Health Checks */
@@ -33,15 +35,6 @@ export class NewHealthChecksComponent implements OnInit, OnDestroy {
3335
selectCrackertype: any;
3436
selectCrackerversions: any = [];
3537

36-
/** Select Options Mapping */
37-
selectCrackertypeMap = {
38-
fieldMapping: CRACKER_TYPE_FIELD_MAPPING
39-
};
40-
41-
selectCrackervMap = {
42-
fieldMapping: CRACKER_VERSION_FIELD_MAPPING
43-
};
44-
4538
/**
4639
* @param {UnsubscribeService} unsubscribeService - The service managing unsubscribing from observables.
4740
* @param {AutoTitleService} titleService - The service for managing the title of the component.
@@ -101,7 +94,7 @@ export class NewHealthChecksComponent implements OnInit, OnDestroy {
10194
data: response.data,
10295
included: response.included
10396
});
104-
this.selectCrackertype = transformSelectOptions(crackerTypes, this.selectCrackertypeMap);
97+
this.selectCrackertype = transformSelectOptions(crackerTypes, CRACKER_TYPE_FIELD_MAPPING);
10598
});
10699
this.unsubscribeService.add(loadSubscription$);
107100
}
@@ -121,7 +114,7 @@ export class NewHealthChecksComponent implements OnInit, OnDestroy {
121114
data: response.data,
122115
included: response.included
123116
});
124-
this.selectCrackerversions = transformSelectOptions(crackers, this.selectCrackervMap);
117+
this.selectCrackerversions = transformSelectOptions(crackers, CRACKER_VERSION_FIELD_MAPPING);
125118
const lastItem = this.selectCrackerversions.slice(-1)[0]['id'];
126119
this.form.get('crackerBinaryId').patchValue(lastItem);
127120
});

0 commit comments

Comments
 (0)