diff options
author | Sonja Galovic <galovicsonja@gmail.com> | 2022-04-12 22:59:12 +0200 |
---|---|---|
committer | Sonja Galovic <galovicsonja@gmail.com> | 2022-04-12 22:59:12 +0200 |
commit | 1330e8000924aa64232f1f160b32f49e20bd9958 (patch) | |
tree | 626f701403b59c5040a911714ce50f5e0fde707c /frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts | |
parent | 3f17af55326b0c901fddd6eb20767b4c068a779b (diff) | |
parent | dc4d2497435a7c038034f02641542cde10cf31a1 (diff) |
Merge branch 'dev' of http://gitlab.pmf.kg.ac.rs/igrannonica/neuronstellar into dev
# Conflicts:
# frontend/src/app/app.module.ts
Diffstat (limited to 'frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts')
-rw-r--r-- | frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts | 86 |
1 files changed, 40 insertions, 46 deletions
diff --git a/frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts b/frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts index 7421fbcf..3adc16f3 100644 --- a/frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts +++ b/frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts @@ -4,7 +4,8 @@ import Dataset from 'src/app/_data/Dataset'; import { DatasetsService } from 'src/app/_services/datasets.service'; import { ModelsService } from 'src/app/_services/models.service'; import shared from 'src/app/Shared'; -import { DatatableComponent } from '../datatable/datatable.component'; +import { DatatableComponent, TableData } from '../datatable/datatable.component'; +import { CsvParseService } from 'src/app/_services/csv-parse.service'; @Component({ selector: 'app-add-new-dataset', @@ -14,13 +15,10 @@ import { DatatableComponent } from '../datatable/datatable.component'; export class AddNewDatasetComponent { @Output() newDatasetAdded = new EventEmitter<string>(); - @ViewChild(DatatableComponent) datatable?: DatatableComponent; + @ViewChild(DatatableComponent) datatable!: DatatableComponent; delimiterOptions: Array<string> = [",", ";", "\t", "razmak", "|"]; //podrazumevano "," - //hasHeader: boolean = true; - hasInput: boolean = false; - csvRecords: any[] = []; files: File[] = []; rowsNumber: number = 0; @@ -28,7 +26,9 @@ export class AddNewDatasetComponent { dataset: Dataset; //dodaj ! potencijalno - constructor(private ngxCsvParser: NgxCsvParser, private modelsService: ModelsService, private datasetsService: DatasetsService) { + tableData: TableData = new TableData(); + + constructor(private modelsService: ModelsService, private datasetsService: DatasetsService, private csv: CsvParseService) { this.dataset = new Dataset(); } @@ -39,12 +39,13 @@ export class AddNewDatasetComponent { if (this.files.length == 0 || this.files[0] == null) { //console.log("NEMA FAJLA"); //this.loaded.emit("not loaded"); - this.hasInput = false; + this.tableData.hasInput = false; return; } else - this.hasInput = true; + this.tableData.hasInput = true; + this.tableData.loaded = false; this.update(); } @@ -53,34 +54,27 @@ export class AddNewDatasetComponent { if (this.files.length < 1) return; - this.datatable!.loaded = false; - this.datatable!.hasInput = this.hasInput; - - this.ngxCsvParser.parse(this.files[0], { header: false, delimiter: (this.dataset.delimiter == "razmak") ? " " : (this.dataset.delimiter == "") ? "," : this.dataset.delimiter }) - .pipe().subscribe((result) => { - - console.log('Result', result); - if (result.constructor === Array) { - if(this.dataset.hasHeader) - this.csvRecords = result.splice(0,11); - else - this.csvRecords=result.splice(0,10); - if (this.dataset.hasHeader) - this.rowsNumber = this.csvRecords.length - 1; - else - this.rowsNumber = this.csvRecords.length; - this.colsNumber = this.csvRecords[0].length; - - if (this.dataset.hasHeader) - this.dataset.header = this.csvRecords[0]; - - this.datatable!.data = this.csvRecords; - this.datatable!.hasHeader = this.dataset.hasHeader; - this.datatable!.loaded = true; - } - }, (error: NgxCSVParserError) => { - console.log('Error', error); - }); + const fileReader = new FileReader(); + fileReader.onload = (e) => { + if (typeof fileReader.result === 'string') { + const result = this.csv.csvToArray(fileReader.result, (this.dataset.delimiter == "razmak") ? " " : (this.dataset.delimiter == "") ? "," : this.dataset.delimiter) + + if (this.dataset.hasHeader) + this.csvRecords = result.splice(0, 11); + else + this.csvRecords = result.splice(0, 10); + + this.colsNumber = result[0].length; + this.rowsNumber = result.length; + + this.tableData.data = this.csvRecords + this.tableData.hasHeader = this.dataset.hasHeader; + this.tableData.loaded = true; + this.tableData.numCols = this.colsNumber; + this.tableData.numRows = this.rowsNumber; + } + } + fileReader.readAsText(this.files[0]); } checkAccessible() { @@ -96,17 +90,17 @@ export class AddNewDatasetComponent { this.modelsService.uploadData(this.files[0]).subscribe((file) => { //console.log('ADD MODEL: STEP 2 - ADD DATASET WITH FILE ID ' + file._id); - this.dataset.fileId = file._id; - this.dataset.username = shared.username; - - this.datasetsService.addDataset(this.dataset).subscribe((dataset) => { - this.newDatasetAdded.emit("added"); - shared.openDialog("Obaveštenje", "Uspešno ste dodali novi izvor podataka u kolekciju. Molimo sačekajte par trenutaka da se procesira."); - }, (error) => { - shared.openDialog("Neuspeo pokušaj!", "Izvor podataka sa unetim nazivom već postoji u Vašoj kolekciji. Izmenite naziv ili iskoristite postojeći dataset."); - }); //kraj addDataset subscribe + this.dataset.fileId = file._id; + this.dataset.username = shared.username; + + this.datasetsService.addDataset(this.dataset).subscribe((dataset) => { + this.newDatasetAdded.emit("added"); + shared.openDialog("Obaveštenje", "Uspešno ste dodali novi izvor podataka u kolekciju. Molimo sačekajte par trenutaka da se procesira."); + }, (error) => { + shared.openDialog("Neuspeo pokušaj!", "Izvor podataka sa unetim nazivom već postoji u Vašoj kolekciji. Izmenite naziv ili iskoristite postojeći dataset."); + }); //kraj addDataset subscribe }, (error) => { - + }); //kraj uploadData subscribe } |