Skip to content

Commit

Permalink
moded repository to own sevice
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Nov 28, 2023
1 parent ef28e83 commit d60a527
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import { RepositoryService } from "./repository.service";
</thead>
<tbody>
<tr *ngFor="let repository of repositories; let i = index">
<td style="padding-top: 4px" width="15%">
<td style="padding-top: 4px" width="5%">
{{ i }}
</td>
<td style="padding-top: 4px" width="15%">
{{ repository.name }}
</td>
<td style="padding-top: 4px" width="600%">
<td style="padding-top: 4px" width="70%">
{{ repository.url }}
</td>
<td width="10%" style="padding-top: 0px; padding-bottom: 0px">
Expand Down
75 changes: 69 additions & 6 deletions analytics-ui/src/sample/editor/repository.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import { Injectable } from "@angular/core";
import { BehaviorSubject, Observable } from "rxjs";
import { Repository } from "../../shared/analytics.model";
import { AnalyticsService } from "../../shared/analytics.service";
import {
ANALYTICS_REPOSITORIES_TYPE,
REPO_SAMPLES_BLOCKSDK,
Repository,
uuidCustom,
} from "../../shared/analytics.model";
import { IManagedObject, InventoryService } from "@c8y/client";

@Injectable({
providedIn: "root",
Expand All @@ -12,13 +17,14 @@ export class RepositoryService {
private repositories: Repository[] = [];
private repositoriesSubject: BehaviorSubject<Repository[]> =
new BehaviorSubject<Repository[]>([]);
private _repositories: Promise<Repository[]> | Repository[];

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

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

Expand Down Expand Up @@ -48,7 +54,64 @@ export class RepositoryService {
this.repositoriesSubject.next([...this.repositories]);
}

async updateRepositories(): Promise<void> {
await this.analyticsService.updateRepositories(this.repositories);
async loadRepositories(): Promise<Repository[]> {
if (!this._repositories) {
this._repositories = this.loadUncachedRepositories();
}
return this._repositories;
}

async loadUncachedRepositories(): Promise<Repository[]> {
let result = [] as Repository[];
const filter: object = {
pageSize: 100,
withTotalPages: true,
};
const query: object = {
type: ANALYTICS_REPOSITORIES_TYPE,
};
let { data } = await this.inventoryService.listQuery(query, filter);
if (!data || data.length == 0) {
const reposMO: Partial<IManagedObject> = {
name: "AnalyticsRepositories",
type: ANALYTICS_REPOSITORIES_TYPE,
};
reposMO[ANALYTICS_REPOSITORIES_TYPE] = [
{
id: uuidCustom(),
name: "Block SDK Samples",
url: REPO_SAMPLES_BLOCKSDK,
},
] as Repository[];
this.inventoryService.create(reposMO);
result = reposMO[ANALYTICS_REPOSITORIES_TYPE];
} else if (data.length > 0) {
result = data[0][ANALYTICS_REPOSITORIES_TYPE];
}
this._repositories = result;
return result;
}

async saveRepositories(repositories: Repository[]): Promise<void> {
const filter: object = {
pageSize: 100,
withTotalPages: true,
};
const query: object = {
type: ANALYTICS_REPOSITORIES_TYPE,
};
let { data } = await this.inventoryService.listQuery(query, filter);
if (!data || data.length == 0) {
const reposMO: Partial<IManagedObject> = {
name: "AnalyticsRepositories",
type: ANALYTICS_REPOSITORIES_TYPE,
};
reposMO[ANALYTICS_REPOSITORIES_TYPE] = repositories;
this.inventoryService.create(reposMO);
} else if (data.length > 0) {
data[0][ANALYTICS_REPOSITORIES_TYPE] = repositories;
this.inventoryService.update(data[0]);
}
this._repositories = repositories;
}
}
2 changes: 1 addition & 1 deletion analytics-ui/src/sample/list/sample.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class SampleGridComponent implements OnInit {
modalRef.content.closeSubject.subscribe(async (conf) => {
console.log("Configuration after edit:", conf);
if (conf) {
const response = await this.repositoryService.updateRepositories();
const response = await this.repositoryService.saveRepositories();
this.alertService.success(
gettext(`Updated repositories successfully‚`)
);
Expand Down
4 changes: 2 additions & 2 deletions analytics-ui/src/shared/analytics-tab.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export class AnalyticsTabFactory implements TabFactory {
tabs.push({
path: "sag-ps-pkg-analytics-extension/list",
priority: 920,
label: "List Blocks",
label: "List Blocks Installed",
icon: "flow-chart",
orientation: "horizontal",
} as Tab);
tabs.push({
path: "sag-ps-pkg-analytics-extension/sample",
priority: 900,
label: "List Block Samples",
label: "Repositories Samples",
icon: "test",
orientation: "horizontal",
} as Tab);
Expand Down
71 changes: 4 additions & 67 deletions analytics-ui/src/shared/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ import {
STATUS_MESSAGE_01,
BASE_URL,
ENDPOINT_EXTENSION,
ANALYTICS_REPOSITORIES_TYPE,
Repository,
uuidCustom,
REPO_SAMPLES_BLOCKSDK,
} from "./analytics.model";
import { filter, map, pairwise } from "rxjs/operators";
import { HttpClient } from "@angular/common/http";
import { RepositoryService } from "../sample/editor/repository.service";

@Injectable({ providedIn: "root" })
export class AnalyticsService {
Expand All @@ -50,7 +48,6 @@ export class AnalyticsService {
private restart: BehaviorSubject<string> = new BehaviorSubject<string>(null);
protected baseUrl: string;
private _cepId: Promise<string>;
private _repositories: Promise<Repository[]> | Repository[];
private realtime: Realtime;
private subscription: Subscription;

Expand All @@ -61,7 +58,8 @@ export class AnalyticsService {
private inventoryService: InventoryService,
private inventoryBinaryService: InventoryBinaryService,
private fetchClient: FetchClient,
private githubFetchClient: HttpClient
private githubFetchClient: HttpClient,
private repositoryService: RepositoryService
) {
this.realtime = new Realtime(this.fetchClient);
}
Expand All @@ -85,67 +83,6 @@ export class AnalyticsService {
return result;
}

async getRepositories(): Promise<Repository[]> {
if (!this._repositories) {
this._repositories = this.getUncachedRepositories();
}
return this._repositories;
}

async getUncachedRepositories(): Promise<Repository[]> {
let result = [] as Repository[];
const filter: object = {
pageSize: 100,
withTotalPages: true,
};
const query: object = {
type: ANALYTICS_REPOSITORIES_TYPE,
};
let { data } = await this.inventoryService.listQuery(query, filter);
if (!data || data.length == 0) {
const reposMO: Partial<IManagedObject> = {
name: "AnalyticsRepositories",
type: ANALYTICS_REPOSITORIES_TYPE,
};
reposMO[ANALYTICS_REPOSITORIES_TYPE] = [
{
id: uuidCustom(),
name: "Block SDK Samples",
url: REPO_SAMPLES_BLOCKSDK,
},
] as Repository[];
this.inventoryService.create(reposMO);
result = reposMO[ANALYTICS_REPOSITORIES_TYPE];
} else if (data.length > 0) {
result = data[0][ANALYTICS_REPOSITORIES_TYPE];
}
this._repositories = result;
return result;
}

async updateRepositories(repositories: Repository[]): Promise<void> {
const filter: object = {
pageSize: 100,
withTotalPages: true,
};
const query: object = {
type: ANALYTICS_REPOSITORIES_TYPE,
};
let { data } = await this.inventoryService.listQuery(query, filter);
if (!data || data.length == 0) {
const reposMO: Partial<IManagedObject> = {
name: "AnalyticsRepositories",
type: ANALYTICS_REPOSITORIES_TYPE,
};
reposMO[ANALYTICS_REPOSITORIES_TYPE] = repositories;
this.inventoryService.create(reposMO);
} else if (data.length > 0) {
data[0][ANALYTICS_REPOSITORIES_TYPE] = repositories;
this.inventoryService.update(data[0]);
}
this._repositories = repositories;
}

async createExtensionsZIP(name: string, monitors: string[]): Promise<any> {
console.log(`Create extensions for : ${name}, ${monitors},`);
return this.fetchClient.fetch(`${BASE_URL}/${ENDPOINT_EXTENSION}`, {
Expand Down Expand Up @@ -208,7 +145,7 @@ export class AnalyticsService {

async getCEP_BlockSamplesFromRepositories(): Promise<CEP_Block[]> {
const promises: Promise<CEP_Block[]>[] = [];
const reps: Repository[] = await this.getRepositories();
const reps: Repository[] = await this.repositoryService.loadRepositories();

for (let i = 0; i < reps.length; i++) {
const promise: Promise<CEP_Block[]> =
Expand Down

0 comments on commit d60a527

Please sign in to comment.