Skip to content

Commit

Permalink
add operation to update template
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Jan 1, 2025
1 parent 149f11f commit 0947354
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public ResponseEntity<?> runOperation(@Valid @RequestBody ServiceOperation opera
return handleRefreshNotifications(tenant);
case CLEAR_CACHE:
return handleClearCache(tenant, parameters);
case UPDATE_TEMPLATE:
return handleUpdateTemplate(tenant, parameters);
default:
throw new IllegalArgumentException("Unknown operation: " + operationType);
}
Expand All @@ -141,6 +143,13 @@ public ResponseEntity<?> runOperation(@Valid @RequestBody ServiceOperation opera
}
}

private ResponseEntity<?> handleUpdateTemplate(String tenant, Map<String, String> parameters) throws Exception {
String id = parameters.get("id");
Integer index = Integer.parseInt(parameters.get("index"));
mappingComponent.updateSourceTemplate(tenant, id, index);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

private ResponseEntity<?> handleReloadMappings(String tenant) throws ConnectorRegistryException {
mappingComponent.rebuildMappingOutboundCache(tenant);
List<Mapping> updatedMappingsInbound = mappingComponent.rebuildMappingInboundCache(tenant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,32 @@ public Mapping setFilterMapping(String tenant, String mappingId, String filterMa
return mapping;
}

public void updateSourceTemplate(String tenant, String id, Integer index) throws Exception {
// step 1. update debug for mapping
Mapping mapping = getMapping(tenant, id);
String newSourceTemplate = mapping.snoopedTemplates.get(index);
log.info("Tenant {} - Setting sourceTemplate for mapping: {} to: {}", tenant, id, newSourceTemplate);
mapping.setSourceTemplate(newSourceTemplate);
if (Direction.INBOUND.equals(mapping.direction)) {
// step 2. retrieve collected snoopedTemplates
mapping.setSnoopedTemplates(cacheMappingInbound.get(tenant).get(id).getSnoopedTemplates());
}
// step 3. update mapping in inventory
// don't validate mapping when setting active = false, this allows to remove
// mappings that are not working
updateMapping(tenant, mapping, true, true);
// step 4. delete mapping from update cache
removeDirtyMapping(tenant, mapping);
// step 5. update caches
if (Direction.OUTBOUND.equals(mapping.direction)) {
rebuildMappingOutboundCache(tenant);
} else {
deleteFromCacheMappingInbound(tenant, mapping);
addToCacheMappingInbound(tenant, mapping);
cacheMappingInbound.get(tenant).put(mapping.id, mapping);
}
}

public void setDebugMapping(String tenant, String id, Boolean debug) throws Exception {
// step 1. update debug for mapping
log.info("Tenant {} - Setting debug: {} got mapping: {}", tenant, id, debug);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ public enum Operation {
SNOOP_RESET,
RESET_DEPLOYMENT_MAP,
CLEAR_CACHE,
APPLY_MAPPING_FILTER
APPLY_MAPPING_FILTER,
UPDATE_TEMPLATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
</button>
</c8y-action-bar-item>

<div class="card card--fullpage">
<div class="col-lg-12">
<div class="card-block">
<div class="card content-fullpage d-grid grid__row--6-6--md">
<div class="card card--grid card--grid__inner-scroll">
<div class="p-l-16 p-r-16">
<d11r-mapping-connector-configuration #connectorConfigurationBroker
[selectable]="false"></d11r-mapping-connector-configuration>
</div>
</div>
</div>

<div class="elevation-sm">
<d11r-mapping-connector-log></d11r-mapping-connector-log>
<div class="card card--grid card--grid__inner-scroll">
<div class="p-l-16 p-r-16">
<d11r-mapping-connector-log></d11r-mapping-connector-log>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="card pointer card--d-col">
<div (click)="detail()" class="card-block text-center card-column-40">
<div class="card-appicon">
<i c8yIcon="plugin"></i>
<i c8yIcon="extension"></i>
</div>
<p class="e2e-appCardName text-medium" title="{{ app['name'] }}">
{{ app['name'] }}
Expand Down
9 changes: 9 additions & 0 deletions dynamic-mapping-ui/src/mapping/core/mapping.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ export class MappingService {
);
}

async updateTemplate(parameter: any): Promise<IFetchResponse> {
return await this.sharedService.runOperation(
{
operation: Operation.UPDATE_TEMPLATE,
parameter
}
);
}

resetCache() {
// this._mappingsInbound = undefined;
// this._mappingsOutbound = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,37 @@
<h1 c8yIcon="c8y-data-explorer"></h1>
<h4>Explore snooped templates</h4>
</div>
<c8y-modal
[labels]="labels"
(onClose)="onResetSnoop($event)"
(onDismiss)="onCancel($event)"
>
<c8y-modal [customFooter]="true" [labels]="labels" #modal>
<div class="p-b-24 p-l-24 p-r-24">
<div class="form-group">
<div class="c8y-select-wrapper" style="width: 50%">
<select
class="form-control"
(change)="onSelectSnoopedTemplate($event.target.value)"
[disabled]="mapping.snoopedTemplates?.length === 0"
>
<option
[value]="i"
*ngFor="
<select class="form-control" (change)="onSelectSnoopedTemplate($event.target.value)"
[disabled]="mapping.snoopedTemplates?.length === 0">
<option [value]="i" *ngFor="
let t of [].constructor(mapping.snoopedTemplates?.length);
let i = index
"
>
">
{{ 'Template - ' + (i + 1) }}
</option>
</select>
</div>
<div class="p-t-16">
<label>Template</label>
<d11r-mapping-json-editor2
[options]="editorOptionsGeneral"
[class]="'jse-main-small'"
[data]="template" #editorGeneral
></d11r-mapping-json-editor2>
<d11r-mapping-json-editor2 [options]="editorOptionsGeneral" [class]="'jse-main-small'" [data]="template"
#editorGeneral></d11r-mapping-json-editor2>
</div>
</div>
</div>
</c8y-modal>
<div class="modal-footer" c8y-modal-footer-custom>
<button class="btn btn-default" (click)="onCancel()">
{{ 'Cancel' }}
</button>
<button class="btn btn-primary" (click)="onResetSnoop()"
[disabled]="mapping.snoopedTemplates?.length === 0 || pending" [ngClass]="{ 'btn-pending': pending }">
{{ 'Delete templates' }}
</button>
<button class="btn btn-primary" (click)="onUpdateSourceTemplate()" [ngClass]="{ 'btn-pending': pending }">
{{ 'Set as source template' }}
</button>
</div>
</c8y-modal>
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ export class SnoopExplorerComponent implements OnInit {

@ViewChild('editorGeneral', { static: false })
editorGeneral: JsonEditorComponent;


@ViewChild('modal', { static: false }) private modal;

pending: boolean = false;
mapping: Mapping;
closeSubject: Subject<MappingSubstitution> = new Subject();
labels: ModalLabels;
isDisabled = isDisabled;
template: any;
index: number;

editorOptionsGeneral = {
mode: 'tree',
Expand All @@ -62,25 +66,29 @@ export class SnoopExplorerComponent implements OnInit {
ngOnInit(): void {
this.mapping = this.enrichedMapping.mapping;
this.onSelectSnoopedTemplate(0);
this.index = 0;
this.labels = {
ok: 'Delete templates',
cancel: 'Close'
};
}

onCancel() {
this.closeSubject.next(undefined);
this.modal._dismiss();
}

async onSelectSnoopedTemplate(index: any) {
this.index = index;
this.template = JSON.parse(this.mapping.snoopedTemplates[index]);
}

async onResetSnoop() {
console.log('Clicked onResetSnoop!');
// console.log('Clicked onResetSnoop!');
this.pending = true;
const result: IFetchResponse = await this.mappingService.resetSnoop({
id: this.mapping.id
});
this.pending = false;
if (result.status == HttpStatusCode.Created) {
this.alertService.success(
`Reset snooping for mapping ${this.mapping.id}`
Expand All @@ -89,6 +97,29 @@ export class SnoopExplorerComponent implements OnInit {
this.alertService.warning(
`Failed to reset snooping for mapping ${this.mapping.id}`
);
this.pending = false;
}
this.modal._dismiss();
}


async onUpdateSourceTemplate() {
this.pending = true;
const result: IFetchResponse = await this.mappingService.updateTemplate({
id: this.mapping.id,
index: this.index
});
this.pending = false;
if (result.status == HttpStatusCode.Created) {
this.alertService.success(
`Update source template for mapping ${this.mapping.id}`
);
} else {
this.alertService.warning(
`Failed to update source template for mapping ${this.mapping.id}`
);
this.pending = false;
}
this.modal._dismiss();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
~ @authors Christof Strack
-->

<c8y-data-grid #connectorGrid [title]="'Connectors'" [selectable]="selectable && !readOnly" [columns]="columns"
[selectionPrimaryKey]="'identifier'" [rows]="configurations$ | async" [pagination]="pagination"
[actionControls]="actionControls" (itemsSelect)="onSelectionChanged($event)">
<c8y-ui-empty-state [icon]="'c8y-icon dlt-c8y-icon-connected'" [title]="'No connectors exist.' | translate"
[subtitle]="
'Create your first connector.' | translate
" [horizontal]="true">
</c8y-ui-empty-state>
</c8y-data-grid>
<div class="">
<c8y-data-grid class="content-fullpage" #connectorGrid [title]="'Connectors'" [selectable]="selectable && !readOnly"
[columns]="columns" [selectionPrimaryKey]="'identifier'" [rows]="configurations$ | async" [pagination]="pagination"
[actionControls]="actionControls" (itemsSelect)="onSelectionChanged($event)">
<c8y-ui-empty-state [icon]="'c8y-icon dlt-c8y-icon-connected'" [title]="'No connectors exist.' | translate"
[subtitle]="
'Create your first connector.' | translate
" [horizontal]="true">
</c8y-ui-empty-state>
</c8y-data-grid>
</div>
<div *ngIf="selectable && !readOnly">
<label>Selected connectors for mapping</label>
<div class="bg-level-1 p-t-2 p-b-2">
<span class="text-10 label label-primary m-r-8" *ngFor="let identifier of deploymentMapEntry?.connectors; let i = index">
<span class="text-10 label label-primary m-r-8"
*ngFor="let identifier of deploymentMapEntry?.connectors; let i = index">
{{ findNameByIdent(identifier) }}</span>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
~ @authors Christof Strack
-->

<div class="p-24">
<div>
<c8y-form-group>
<div class="">
<c8y-form-group class="p-l-24">
<form class="form-inline">
<label class="control-label m-r-8" title="{{ 'Filter connector logs' | translate }}">Filter connector logs</label>
<div class="c8y-select-wrapper">
Expand Down Expand Up @@ -49,8 +48,7 @@
</button>
</form>
</c8y-form-group>
</div>
<div class="page-sticky-header hidden-xs c8y-list__item c8y-list--timeline">
<div class="hidden-xs c8y-list__item c8y-list--timeline">
<div class="d-flex">
<div class="c8y-list--timeline__item__date">
<p class="m-0 text-medium text-truncate" title="Date">Date</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
LoggingEventType,
LoggingEventTypeMap,
} from '..';
import { ConnectorStatusService } from '../service/connector-status.service';
import { ConnectorLogService } from '../service/connector-log.service';
import { ConnectorConfigurationService } from '../service/connector-configuration.service';

@Component({
Expand All @@ -54,7 +54,7 @@ export class ConnectorStatusComponent implements OnInit, OnDestroy {

constructor(
public bsModalService: BsModalService,
public connectorStatusService: ConnectorStatusService,
public connectorStatusService: ConnectorLogService,
public connectorConfigurationService: ConnectorConfigurationService,
public alertService: AlertService,
) {}
Expand Down
2 changes: 1 addition & 1 deletion dynamic-mapping-ui/src/shared/misc/mapping-tab.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class MappingTabFactory implements TabFactory {
path: `sag-ps-pkg-dynamic-mapping/${NODE3}/extension`,
priority: 500,
label: 'Processor extension',
icon: 'plugin',
icon: 'extension',
orientation: 'horizontal'
} as Tab);
tabs.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
@Injectable({
providedIn: 'root'
})
export class ConnectorStatusService {
export class ConnectorLogService {
constructor(
private eventService: EventService,
private sharedService: SharedService
Expand Down Expand Up @@ -140,7 +140,7 @@ export class ConnectorStatusService {
} else {
sortedAcc = val.concat(acc);
}
sortedAcc = sortedAcc?.slice(0, 9);
sortedAcc = sortedAcc?.slice(0, 20);
return sortedAcc;
}, []),
shareReplay(1)
Expand Down
4 changes: 3 additions & 1 deletion dynamic-mapping-ui/src/shared/service/shared.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export enum Operation {
SNOOP_RESET = 'SNOOP_RESET',
RESET_DEPLOYMENT_MAP = 'RESET_DEPLOYMENT_MAP',
CLEAR_CACHE = 'CLEAR_CACHE',
APPLY_MAPPING_FILTER = 'APPLY_MAPPING_FILTER'
APPLY_MAPPING_FILTER = 'APPLY_MAPPING_FILTER',
UPDATE_TEMPLATE = 'UPDATE_TEMPLATE',

}

export class ServiceOperation{
Expand Down

0 comments on commit 0947354

Please sign in to comment.