Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show/change experiment default template in UI #51

Merged
merged 14 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class ExperimentWorkspaceDetailComponent implements OnInit {
}

private async updateTemplateId(templateId: string | null) {
if (templateId == null) {
if (templateId == null || templateId === "") {
return; // no template selected
}
if (templateId === this.templateId) {
Expand Down
20 changes: 19 additions & 1 deletion src/app/components/experiment/experiment.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,29 @@ <h1 class="t-page-headline">Experiment</h1>
</button>
</mat-card-title>
<mat-card-content>
<mat-form-field>
<mat-label>Default Template</mat-label>
<mat-select (selectionChange)="changeDefaultTemplate($event.value)" [value]="uiTemplateId">
<mat-paginator [pageSize]="itemCount" [length]="uiTemplateCollectionSize" [pageIndex]="uiTemplatePageIndex"
(page)="getTemplatePage($event.pageIndex)" aria-label="Select page">
</mat-paginator>
<mat-divider></mat-divider>
<mat-option [value]="null">
-- none --
</mat-option>
<mat-option *ngIf="uiTemplateId && !uiTemplates.has(uiTemplateId)" [value]="uiTemplateId">
{{uiTemplateName}}
</mat-option>
<mat-divider></mat-divider>
<mat-option *ngFor="let template of uiTemplates | keyvalue:originalOrder" [value]="template.key">
{{template.value}}
</mat-option>
</mat-select>
</mat-form-field>
<qhana-markdown class="description" [markdown]="experimentDescription"
(markdownChanges)="updateDescription($event)" [editable]="true">
</qhana-markdown>
</mat-card-content>

</mat-card>
<div class="updates">
<div class="update-status" *ngIf="updateStatus === 'changed'">
Expand Down
63 changes: 61 additions & 2 deletions src/app/components/experiment/experiment.component.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { KeyValue } from '@angular/common';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs';
import { debounceTime, filter, map } from 'rxjs/operators';
import { ExportExperimentDialog } from 'src/app/dialogs/export-experiment/export-experiment.component';
import { PageApiObject } from 'src/app/services/api-data-types';
import { CurrentExperimentService } from 'src/app/services/current-experiment.service';
import { ExperimentApiObject, QhanaBackendService } from 'src/app/services/qhana-backend.service';
import { PluginRegistryBaseService } from 'src/app/services/registry.service';
import { TemplatesService } from 'src/app/services/templates.service';

@Component({
selector: 'qhana-experiment',
templateUrl: './experiment.component.html',
styleUrls: ['./experiment.component.sass']
})
export class ExperimentComponent implements OnInit, OnDestroy {

private routeSubscription: Subscription | null = null;

private experimentSubscription: Subscription | null = null;
Expand All @@ -24,6 +27,7 @@ export class ExperimentComponent implements OnInit, OnDestroy {
private descriptionUpdates: BehaviorSubject<string | null> = new BehaviorSubject<string | null>(null);
private autoSaveTitleSubscription: Subscription | null = null;
private autoSaveDescriptionSubscription: Subscription | null = null;
private uiTemplateIdSubscription: Subscription | null = null;

updateStatus: "original" | "changed" | "saved" = "original";

Expand All @@ -34,7 +38,15 @@ export class ExperimentComponent implements OnInit, OnDestroy {
experimentDescription: string = ""; // only updated on initial experiment load
currentExperimentDescription: string = "";

constructor(private route: ActivatedRoute, private router: Router, private experimentService: CurrentExperimentService, private backend: QhanaBackendService, public dialog: MatDialog) { }
readonly itemCount: number = 10;
uiTemplatePageIndex: number = 0;

uiTemplates: Map<string, string> = new Map<string, string>();
uiTemplateId: string | null = null;
uiTemplateName: string | null = null;
uiTemplateCollectionSize: number = 0;

constructor(private route: ActivatedRoute, private router: Router, private experimentService: CurrentExperimentService, private backend: QhanaBackendService, private registry: PluginRegistryBaseService, private templates: TemplatesService, public dialog: MatDialog) { }

ngOnInit(): void {
this.routeSubscription = this.route.params.pipe(map(params => params.experimentId)).subscribe(experimentId => {
Expand All @@ -48,6 +60,17 @@ export class ExperimentComponent implements OnInit, OnDestroy {
this.lastSavedDescription = experiment?.description ?? "";
this.experimentDescription = experiment?.description ?? "";
this.currentExperimentDescription = experiment?.description ?? "";
this.uiTemplateId = experiment?.templateId?.toString() ?? null;
});
this.uiTemplateIdSubscription = this.experimentService.experimentTemplateId.subscribe(templateId => {
this.uiTemplateId = templateId?.toString() ?? null;
this.getTemplatePage();
if (this.uiTemplateId == null) {
return;
}
this.templates.getTemplate(this.uiTemplateId).then(template => {
this.uiTemplateName = template?.data.name ?? null;
});
});
this.autoSaveTitleSubscription = this.titleUpdates.pipe(
filter(value => value != null && value !== this.lastSavedTitle),
Expand All @@ -64,6 +87,7 @@ export class ExperimentComponent implements OnInit, OnDestroy {
this.experimentSubscription?.unsubscribe();
this.autoSaveTitleSubscription?.unsubscribe();
this.autoSaveDescriptionSubscription?.unsubscribe();
this.uiTemplateIdSubscription?.unsubscribe();
}

cloneExperiment() {
Expand Down Expand Up @@ -156,4 +180,39 @@ export class ExperimentComponent implements OnInit, OnDestroy {
});
}

async changeDefaultTemplate(templateId: string | null) {
if (this.experimentId == null) {
this.uiTemplatePageIndex = 0;
this.uiTemplateId = null;
this.uiTemplateName = null;
return;
}
this.uiTemplateName = this.uiTemplates.get(templateId ?? "") ?? null;
await this.templates.setExperimentDefaultTemplate(this.experimentId, templateId);
this.experimentService.reloadExperiment();
}

async getTemplatePage(pageIndex: number = 0) {
const uiTemplates = new Map<string, string>();
const params = new URLSearchParams();
params.set("sort", "name");
params.set("item-count", this.itemCount.toString());
params.set("cursor", (pageIndex + 1).toString());
await this.registry.getByRel<PageApiObject>([["ui-template", "collection"]], params).then(result => {
this.uiTemplateCollectionSize = result?.data.collectionSize ?? 0;
result?.data.items.forEach(item => {
const templateId = item.resourceKey?.uiTemplateId;
const name = item.name;
if (templateId != null && name != null) {
uiTemplates.set(templateId, name);
}
});
});
this.uiTemplates = uiTemplates;
this.uiTemplatePageIndex = pageIndex;
}

originalOrder = (a: KeyValue<string, string>, b: KeyValue<string, string>): number => {
return 0;
}
}
7 changes: 5 additions & 2 deletions src/app/services/current-experiment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class CurrentExperimentService {
return this.currentExperiment.asObservable().pipe(map(experiment => experiment?.description ?? null));
}

get experimentTemplateId() {
return this.currentExperiment.asObservable().pipe(map(experiment => experiment?.templateId ?? null));
}

constructor(private backend: QhanaBackendService) { }

private updateCurrentExperiment(experimentId: string | null) {
Expand All @@ -64,7 +68,7 @@ export class CurrentExperimentService {
this.currentExperimentId.next(experimentId);
}
const current = this.currentExperiment.getValue();
if (current != experiment || current?.name !== experiment?.name || current?.description !== experiment?.description) {
if (current != experiment || current?.name !== experiment?.name || current?.description !== experiment?.description || current?.templateId !== experiment?.templateId) {
this.currentExperiment.next(experiment);
}
}
Expand All @@ -81,5 +85,4 @@ export class CurrentExperimentService {
this.updateCurrentExperiment(experimentId);
}
}

}
21 changes: 21 additions & 0 deletions src/app/services/qhana-backend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ export interface TimelineStepPageOptions {
unclearedSubstep?: number;
}

export interface TemplateApiObject extends ApiObject {
templateId: string;
}

export interface TemplatePostResponseApiObject extends ApiObject {
experimentId: number;
templateId?: string;
}

function urlIsString(url: string | null): url is string {
return url != null;
}
Expand Down Expand Up @@ -491,4 +500,16 @@ export class QhanaBackendService {
rootUrl => this.http.get<TimelineSubStepApiObject>(`${rootUrl}/experiments/${experimentId}/timeline/${step}/substeps/${substep}`)
);
}

public getExperimentDefaultTemplate(experimentId: number | string): Observable<TemplateApiObject> {
return this.callWithRootUrl<TemplateApiObject>(
rootUrl => this.http.get<TemplateApiObject>(`${rootUrl}/experiments/${experimentId}/template`)
);
}

public updateExperimentDefaultTemplate(experimentId: number | string, templateId: string | number | null): Observable<TemplatePostResponseApiObject> {
return this.callWithRootUrl<TemplatePostResponseApiObject>(
rootUrl => this.http.post<TemplatePostResponseApiObject>(`${rootUrl}/experiments/${experimentId}/template`, { templateId: templateId })
);
}
}
8 changes: 7 additions & 1 deletion src/app/services/registry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,13 @@ export class PluginRegistryBaseService {

public async getByApiLink<T>(link: ApiLink, searchParams: URLSearchParams | null = null, ignoreCache: boolean | "ignore-embedded" = false): Promise<ApiResponse<T> | null> {
const url = new URL(link.href)
searchParams?.forEach((value, key) => url.searchParams.append(key, value));
searchParams?.forEach((value, key) => {
if (url.searchParams.has(key)) {
url.searchParams.set(key, value);
} else {
url.searchParams.append(key, value);
infacc marked this conversation as resolved.
Show resolved Hide resolved
}
});
infacc marked this conversation as resolved.
Show resolved Hide resolved
return await this._fetch<ApiResponse<T>>(url.toString(), ignoreCache);
}

Expand Down
15 changes: 12 additions & 3 deletions src/app/services/templates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { ApiLink, ApiObject, PageApiObject } from './api-data-types';
import { CurrentExperimentService } from './current-experiment.service';
import { PluginRegistryBaseService } from './registry.service';
import { EnvService } from './env.service';
import { distinctUntilChanged } from 'rxjs/operators';
import { distinctUntilChanged, take } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { QhanaBackendService } from './qhana-backend.service';


export interface TemplateApiObject extends ApiObject { // TODO check fields
Expand Down Expand Up @@ -104,7 +105,7 @@ export class TemplatesService {
return this.currentTemplateSubject.asObservable();
}

constructor(private registry: PluginRegistryBaseService, private env: EnvService, private currentExperiment: CurrentExperimentService, private route: ActivatedRoute) {
constructor(private registry: PluginRegistryBaseService, private env: EnvService, private currentExperiment: CurrentExperimentService, private backend: QhanaBackendService, private route: ActivatedRoute) {
this.envSubscription = env.uiTemplateId.subscribe((defaultTemplateId) => {
this.envTemplateIdSubject.next(defaultTemplateId);
});
Expand Down Expand Up @@ -200,7 +201,7 @@ export class TemplatesService {
}

private async updateTemplate(templateId: string | null, subject: BehaviorSubject<TemplateApiObject | null>) {
if (templateId == null) {
if (templateId == null || templateId === "") {
subject.next(null);
return;
}
Expand Down Expand Up @@ -231,4 +232,12 @@ export class TemplatesService {
const templateResponse = await this.getTemplate(templateId, ignoreCache);
return templateResponse?.data?.groups ?? [];
}

async setExperimentDefaultTemplate(experimentId: string, templateId: string | null) {
this.backend.updateExperimentDefaultTemplate(experimentId, templateId).pipe(take(1)).subscribe(
response => {
this.experimentTemplateIdSubject.next(response?.templateId ?? null);
}
);
}
}
Loading