Skip to content

Commit

Permalink
Add prepare step functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Borzenko committed Jul 19, 2023
1 parent 20071a4 commit b524c9e
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export interface EditFormType {
kind: PreprocessKind.SQL;
query?: string;
};
prepare?: {
kind: string;
command?: string[] | string;
format?: string;
subPath?: string;
}[];
}

export interface EditFormParseType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
></app-stepper-navigation>
</ng-container>
<ng-container *ngIf="currentStep === steps.PREPARE">
<app-prepare-step></app-prepare-step>
<app-prepare-step
[eventYamlByHash]="eventYamlByHash"
[sectionName]="steps.PREPARE"
></app-prepare-step>
<app-stepper-navigation
[prevStep]="steps.FETCH"
[nextStep]="steps.READ"
Expand Down Expand Up @@ -89,6 +92,7 @@
</form>
</div>
</div>
<!-- <pre>{{ pollingSourceForm.value | json }}</pre> -->
</div>
<ng-template #errorBlock>
<app-page-not-found *ngIf="datasetKind"></app-page-not-found>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
FetchKind,
ReadKind,
MergeKind,
PrepareKind,
PreprocessKind,
PreprocessStepValue,
} from "./add-polling-source-form.types";
import { FinalYamlModalComponent } from "../final-yaml-modal/final-yaml-modal.component";
Expand Down Expand Up @@ -48,7 +46,6 @@ import { SupportedEvents } from "src/app/dataset-block/metadata-block/components
export class AddPollingSourceComponent extends BaseComponent implements OnInit {
public currentStep: SetPollingSourceSection = SetPollingSourceSection.FETCH;
public steps: typeof SetPollingSourceSection = SetPollingSourceSection;

public showPreprocessStep = false;
public errorMessage = "";
public history: DatasetHistoryUpdate;
Expand All @@ -58,10 +55,6 @@ export class AddPollingSourceComponent extends BaseComponent implements OnInit {
engine: "",
queries: [],
};

// --------------------------------
private readonly DEFAULT_PREPARE_KIND = PrepareKind.PIPE;
private readonly DEFAULT_PREPROCESS_KIND = PreprocessKind.SQL;
// ---------------------------------
public readonly FETCH_STEP_RADIO_DATA = FETCH_STEP_RADIO_CONTROLS;
public readonly FETCH_FORM_DATA = FETCH_FORM_DATA;
Expand All @@ -79,6 +72,7 @@ export class AddPollingSourceComponent extends BaseComponent implements OnInit {
fetch: this.fb.group({
kind: [this.FETCH_DEFAULT_KIND],
}),
prepare: this.fb.array([]),
read: this.fb.group({
kind: [this.READ_DEFAULT_KIND],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "./process-form.service.types";
import { SetPollingSource } from "src/app/api/kamu.graphql.interface";
import { SetPollingSourceSection } from "src/app/shared/shared.types";
import { FetchKind } from "./add-polling-source-form.types";
import { EditFormType, FetchKind } from "./add-polling-source-form.types";

@Injectable({
providedIn: "root",
Expand All @@ -19,6 +19,8 @@ export class ProcessFormService {
this.processFetchOrderControl(formGroup);
this.removeEmptyControls(formGroup);
this.processEventTimeControl(formGroup);
this.processEmptyPrepareStep(formGroup);
this.processPipeCommandControl(formGroup);
}

private transformSchema(formGroup: FormGroup): void {
Expand All @@ -37,6 +39,27 @@ export class ProcessFormService {
}
}

private processPipeCommandControl(formGroup: FormGroup): void {
const form = formGroup.value as EditFormType;
if (form.prepare && form.prepare.length > 0) {
form.prepare.map((item) => {
if (item.kind === "pipe" && typeof item.command === "string") {
item.command = item.command.trim().split(" ");
}
if (item.kind === "decompress" && !item.subPath) {
delete item.subPath;
}
});
}
}

private processEmptyPrepareStep(formGroup: FormGroup): void {
const form = formGroup.value as EditFormType;
if (form.prepare && !form.prepare.length) {
delete form.prepare;
}
}

private processEventTimeControl(formGroup: FormGroup): void {
const form = formGroup.value as OrderControlType;
if (form.fetch.eventTime && form.fetch.kind === "container") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,102 @@
<p>prepare-step works!</p>
<h2 class="text-center">Prepare</h2>
<div class="d-flex justify-content-center flex-column align-items-center mt-4">
<p class="text-center mt-2">
Defines how raw data is prepared before reading. You can add
<a class="prepare-item" (click)="addPipe()">Pipe</a> and
<a class="prepare-item" (click)="addDecompress()"> Decompress</a> steps.
</p>
</div>
<div [formArrayName]="sectionName">
<div *ngFor="let item of sectionForm.controls; index as i">
<ng-container *ngIf="item.value.kind === 'pipe'" [formGroupName]="i">
<div
class="p-4 border rounded-3 m-4 w-50 mx-auto position-relative"
>
<h3 class="text-center">Pipe</h3>
<div>
<label class="form-label"> Command </label>
</div>
<input class="form-control" formControlName="command" />
<div class="buttons-section d-flex flex-row">
<button
class="no-button"
title="Move down"
attr.data-test-id="move-down-query-{{ i }}"
(click)="swap(i, 1)"
[disabled]="isLastItem(i)"
>
<mat-icon class="fs-4 hover">expand_more</mat-icon>
</button>
<button
class="no-button"
title="Move up"
attr.data-test-id="move-up-query-{{ i }}"
(click)="swap(i, -1)"
[disabled]="isFirstItem(i)"
>
<mat-icon class="fs-4 mx-2 hover">expand_less</mat-icon>
</button>
<button
class="no-button"
title="Delete query"
attr.data-test-id="remove-query-{{ i }}"
(click)="delete(i)"
>
<mat-icon class="fs-4 hover">delete</mat-icon>
</button>
</div>
</div>
</ng-container>
<ng-container
*ngIf="item.value.kind === 'decompress'"
[formGroupName]="i"
>
<div
class="p-4 border rounded-3 m-4 w-50 mx-auto position-relative"
>
<h3 class="text-center">Decompress</h3>
<div class="mb-3">
<label class="form-label"
>Format <span class="text-danger">*</span></label
>
<select class="form-select" formControlName="format">
<option value="zip">ZIP</option>
<option value="gzip">GZIP</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Path</label>
<input class="form-control" formControlName="subPath" />
</div>
<div class="buttons-section d-flex flex-row">
<button
class="no-button"
title="Move down"
attr.data-test-id="move-down-query-{{ i }}"
(click)="swap(i, 1)"
[disabled]="isLastItem(i)"
>
<mat-icon class="fs-4 hover">expand_more</mat-icon>
</button>
<button
class="no-button"
title="Move up"
attr.data-test-id="move-up-query-{{ i }}"
(click)="swap(i, -1)"
[disabled]="isFirstItem(i)"
>
<mat-icon class="fs-4 mx-2 hover">expand_less</mat-icon>
</button>
<button
class="no-button"
title="Delete query"
attr.data-test-id="remove-query-{{ i }}"
(click)="delete(i)"
>
<mat-icon class="fs-4 hover">delete</mat-icon>
</button>
</div>
</div>
</ng-container>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.prepare-item
color: #0d6efd
text-decoration: none

.buttons-section
position: absolute
top: 20px
right: 30px
Original file line number Diff line number Diff line change
@@ -1,9 +1,117 @@
import { ChangeDetectionStrategy, Component } from "@angular/core";
import {
ChangeDetectionStrategy,
Component,
Input,
OnInit,
} from "@angular/core";
import {
ControlContainer,
FormArray,
FormBuilder,
FormGroup,
FormGroupDirective,
} from "@angular/forms";
import { MaybeNull } from "src/app/common/app.types";
import { BaseComponent } from "src/app/common/base.component";
import { SetPollingSourceSection } from "src/app/shared/shared.types";
import { EditFormType, PrepareKind } from "../../add-polling-source-form.types";
import { EditPollingSourceService } from "../../edit-polling-source.service";

@Component({
selector: "app-prepare-step",
templateUrl: "./prepare-step.component.html",
styleUrls: ["./prepare-step.component.sass"],
changeDetection: ChangeDetectionStrategy.OnPush,
viewProviders: [
{ provide: ControlContainer, useExisting: FormGroupDirective },
],
})
export class PrepareStepComponent {}
export class PrepareStepComponent extends BaseComponent implements OnInit {
@Input() public eventYamlByHash: MaybeNull<string> = null;
@Input() public sectionName: SetPollingSourceSection;
public parentForm: FormGroup;
public setPollingSourceEvent: MaybeNull<EditFormType> = null;

constructor(
private rootFormGroupDirective: FormGroupDirective,
private fb: FormBuilder,
private editService: EditPollingSourceService,
) {
super();
}

ngOnInit(): void {
this.parentForm = this.rootFormGroupDirective.form;
if (this.eventYamlByHash) {
this.setPollingSourceEvent = this.editService.parseEventFromYaml(
this.eventYamlByHash,
);
this.initExistingPrepareStep(this.setPollingSourceEvent);
}
}

private initExistingPrepareStep(event: EditFormType): void {
let group: FormGroup;
if (event.prepare && this.sectionForm.length === 0) {
event.prepare.forEach((item) => {
if (
item.kind === PrepareKind.PIPE &&
Array.isArray(item.command)
) {
group = this.fb.group({
kind: this.fb.control(PrepareKind.PIPE),
command: this.fb.control(item.command.join(" ")),
});
} else if (item.kind === PrepareKind.DECOMPRESS) {
group = this.fb.group({
kind: this.fb.control(PrepareKind.DECOMPRESS),
format: this.fb.control(item.format),
subPath: this.fb.control(item.subPath),
});
}
this.sectionForm.push(group);
});
}
}

public get sectionForm(): FormArray {
return this.parentForm.get(this.sectionName) as FormArray;
}

public addPipe(): void {
this.sectionForm.push(
this.fb.group({
kind: this.fb.control(PrepareKind.PIPE),
command: this.fb.control(""),
}),
);
}

public addDecompress(): void {
this.sectionForm.push(
this.fb.group({
kind: this.fb.control(PrepareKind.DECOMPRESS),
format: this.fb.control(""),
subPath: this.fb.control(""),
}),
);
}

public delete(index: number): void {
this.sectionForm.removeAt(index);
}

public swap(index: number, direction: number): void {
const current = this.sectionForm.at(index);
this.sectionForm.removeAt(index);
this.sectionForm.insert(index + direction, current);
}

public isLastItem(index: number): boolean {
return index === this.sectionForm.length - 1;
}

public isFirstItem(index: number): boolean {
return index === 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input
text-overflow: ellipsis

0 comments on commit b524c9e

Please sign in to comment.