Skip to content

Commit

Permalink
added configuration for repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Nov 28, 2023
1 parent 1effbe2 commit 8f718ea
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 15 deletions.
9 changes: 7 additions & 2 deletions analytics-ui/src/analytics-extension.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { HttpClientModule } from "@angular/common/http";
import { NameExtensionComponent } from "./wizard/name-extension-modal.component";
import { EditorStepperComponent } from "./sample/editor/editor-stepper.component";
import { EditorModalComponent } from "./sample/editor/editor-modal.component";
import { RepositoriesModalComponent } from "./sample/editor/repositories-modal.component";
import { RepositoryService } from "./sample/editor/repository.service";

const routes: Route[] = [
{
Expand Down Expand Up @@ -62,7 +64,8 @@ const routes: Route[] = [
BlockGridComponent,
SampleGridComponent,
EditorStepperComponent,
EditorModalComponent
EditorModalComponent,
RepositoriesModalComponent
],
entryComponents: [
AnalyticsExtensionComponent,
Expand All @@ -72,10 +75,12 @@ const routes: Route[] = [
BlockGridComponent,
SampleGridComponent,
EditorStepperComponent,
EditorModalComponent
EditorModalComponent,
RepositoriesModalComponent
],
providers: [
AnalyticsService,
RepositoryService,
hookNavigator(AnalyticsNavigationFactory),
hookWizard({
wizardId: "uploadAnalyticsExtention",
Expand Down
146 changes: 146 additions & 0 deletions analytics-ui/src/sample/editor/repositories-modal.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { Component, OnInit, Output, ViewEncapsulation } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { ModalLabels } from "@c8y/ngx-components";
import { Subject } from "rxjs";
import { Repository, uuidCustom } from "../../shared/analytics.model";
import { RepositoryService } from "./repository.service";

@Component({
selector: "name-repositories-modal",
styleUrls: ["./editor-stepper.component.css"],
template: `<c8y-modal
title="Show repositories for blocks"
(onClose)="onSave($event)"
[labels]="labels"
[headerClasses]="'modal-header dialog-header'"
>
<div>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Url</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let repository of repositories; let i = index">
<td style="padding-top: 4px" width="15%">
{{ i }}
</td>
<td style="padding-top: 4px" width="15%">
{{ repository.name }}
</td>
<td style="padding-top: 4px" width="600%">
{{ repository.url }}
</td>
<td width="10%" style="padding-top: 0px; padding-bottom: 0px">
<button
title="{{ 'Update' | translate }}"
class="btn btn-icon btn-clean p-r-4"
(click)="editRepository(repository)"
>
<i c8yIcon="pencil" class="text-danger"></i>
<span class="sr-only" translate>Update controller</span>
</button>
<button
title="{{ 'Remove' | translate }}"
class="btn btn-icon btn-clean"
(click)="removeRepository(repository.id)"
>
<i c8yIcon="trash-o" class="text-danger"></i>
<span class="sr-only" translate>Remove</span>
</button>
</td>
</tr>
</tbody>
</table>
</div>
<div class="legend form-block">Update Repository</div>
<form [formGroup]="repositoryForm">
<input type="hidden" formControlName="id" />
<c8y-form-group>
<label for="name">Name</label>
<input
type="text"
id="name"
formControlName="name"
class="form-control"
/>
</c8y-form-group>
<c8y-form-group>
<label for="name">Url</label>
<input
type="text"
id="url"
formControlName="url"
class="form-control"
/>
</c8y-form-group>
<!-- Add any other form controls based on your repository model -->
<button (click)="addRepository()" class="btn btn-default">
Add Repository
</button>
<button (click)="updateRepository()" class="btn btn-default">
Update Repository
</button>
</form>
</c8y-modal>`,
encapsulation: ViewEncapsulation.None,
})
export class RepositoriesModalComponent implements OnInit {
repositories: Repository[];
@Output() closeSubject: Subject<Repository[]> = new Subject();
repositoryForm: FormGroup;

labels: ModalLabels = { ok: "Close" };

constructor(
private repositoryService: RepositoryService,
private fb: FormBuilder
) {
this.repositoryForm = this.fb.group({
id: [null],
name: ["", Validators.required],
url: ["", Validators.required],
// Add any other form controls based on your repository model
});
}

ngOnInit(): void {
this.repositoryService.getRepositories().subscribe((repositories) => {
this.repositories = repositories;
});
}

addRepository(): void {
if (this.repositoryForm.valid) {
const newRepository: Repository = this.repositoryForm.value;
newRepository.id = uuidCustom();
this.repositoryService.addRepository(newRepository);
this.repositoryForm.reset();
}
}

editRepository(repository: Repository): void {
this.repositoryForm.patchValue(repository);
}

updateRepository(): void {
if (this.repositoryForm.valid) {
const updatedRepository: Repository = this.repositoryForm.value;
this.repositoryService.updateRepository(updatedRepository);
this.repositoryForm.reset();
}
}

removeRepository(repositoryId: string): void {
this.repositoryService.removeRepository(repositoryId);
}

onSave(event) {
console.log("Save");
this.closeSubject.next(this.repositories);
}
}
54 changes: 54 additions & 0 deletions analytics-ui/src/sample/editor/repository.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// repository.service.ts

import { Injectable } from "@angular/core";
import { BehaviorSubject, Observable } from "rxjs";
import { Repository } from "../../shared/analytics.model";
import { AnalyticsService } from "../../shared/analytics.service";

@Injectable({
providedIn: "root",
})
export class RepositoryService {
private repositories: Repository[] = [];
private repositoriesSubject: BehaviorSubject<Repository[]> =
new BehaviorSubject<Repository[]>([]);

constructor(public analyticsService: AnalyticsService) {
this.init();
}

async init() {
this.repositories = await this.analyticsService.getRepositories();
this.repositoriesSubject.next([...this.repositories]);
}

getRepositories(): Observable<Repository[]> {
return this.repositoriesSubject.asObservable();
}

addRepository(repository: Repository): void {
this.repositories.push(repository);
this.repositoriesSubject.next([...this.repositories]);
}

updateRepository(updatedRepository: Repository): void {
const index = this.repositories.findIndex(
(repo) => repo.id === updatedRepository.id
);
if (index !== -1) {
this.repositories[index] = updatedRepository;
this.repositoriesSubject.next([...this.repositories]);
}
}

removeRepository(repositoryId: string): void {
this.repositories = this.repositories.filter(
(repo) => repo.id !== repositoryId
);
this.repositoriesSubject.next([...this.repositories]);
}

async updateRepositories(): Promise<void> {
await this.analyticsService.updateRepositories(this.repositories);
}
}
10 changes: 10 additions & 0 deletions analytics-ui/src/sample/list/sample.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
{{ "Refresh" | translate }}
</button>
</c8y-action-bar-item>
<c8y-action-bar-item [placement]="'right'">
<button
class="btn btn-link"
title="{{ 'Repositories' | translate }}"
(click)="updateRepositories()"
>
<i c8yIcon="change-theme"></i>
{{ "Repositories" | translate }}
</button>
</c8y-action-bar-item>
<div class="card">
<div class="card-block">
<c8y-data-grid
Expand Down
22 changes: 22 additions & 0 deletions analytics-ui/src/sample/list/sample.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { CEP_Block } from "../../shared/analytics.model";
import { BsModalService } from "ngx-bootstrap/modal";
import { NameExtensionComponent } from "../../wizard/name-extension-modal.component";
import { EditorModalComponent } from "../editor/editor-modal.component";
import { RepositoriesModalComponent } from "../editor/repositories-modal.component";
import { RepositoryService } from "../editor/repository.service";

@Component({
selector: "c8y-sample-grid",
Expand Down Expand Up @@ -81,6 +83,7 @@ export class SampleGridComponent implements OnInit {

constructor(
public analyticsService: AnalyticsService,
public repositoryService: RepositoryService,
public alertService: AlertService,
private bsModalService: BsModalService
) {}
Expand Down Expand Up @@ -129,6 +132,25 @@ export class SampleGridComponent implements OnInit {
}).content as EditorModalComponent;
}

async updateRepositories() {
const initialState = {};
const modalRef = this.bsModalService.show(RepositoriesModalComponent, {
class: "modal-lg",
initialState,
ignoreBackdropClick: true,
});

modalRef.content.closeSubject.subscribe(async (conf) => {
console.log("Configuration after edit:", conf);
if (conf) {
const response = await this.repositoryService.updateRepositories();
this.alertService.success(
gettext(`Updated repositories successfully‚`)
);
}
});
}

public async createExtension(ids: string[]) {
const initialState = {};
const modalRef = this.bsModalService.show(NameExtensionComponent, {
Expand Down
12 changes: 12 additions & 0 deletions analytics-ui/src/shared/analytics.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export interface CEP_Block {
category: Category;
}

export interface Repository {
id: string;
name: string;
url: string;
// Add any other properties as needed
}

export const PATH_CEP_BASE = "service/cep";
export const PATH_CEP_CORRELATOR = `${PATH_CEP_BASE}/apamacorrelator`;
export const PATH_CEP_EN = `${PATH_CEP_CORRELATOR}/EN`;
Expand All @@ -84,7 +91,12 @@ export const REPO_SAMPLES_PATH = "samples/blocks";

export const BASE_URL = "service/analytics-ext-service";
export const ENDPOINT_EXTENSION = "extension";
export const ANALYTICS_REPOSITORIES_TYPE = "extension";

export function uuidCustom(): string {
let id = Math.random().toString(36).slice(-6);
return id;
}

// https://api.github.com/repos/SoftwareAG/apama-analytics-builder-block-sdk/contents/samples
// http://localhost:9000/cep/apamacorrelator/EN/block-metadata.json
Expand Down
Loading

0 comments on commit 8f718ea

Please sign in to comment.