Skip to content

Commit

Permalink
DEV list importable lpm samples uvp5
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Oct 1, 2024
1 parent 0d0510a commit 40b6adc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/domain/entities/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export interface HeaderSampleModel {
endImg: string;
yoyo: string;
stationId: string;
sampleType: string;
integrationTime: string;
argoId: string;
pixelSize: string;
sampleDateTime: string;
sampleType: string | undefined;
integrationTime: string | undefined;
argoId: string | undefined;
pixelSize: string | undefined;
sampleDateTime: string | undefined;
}
2 changes: 1 addition & 1 deletion src/domain/interfaces/repositories/sample-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { PublicSampleResponseModel } from "../../entities/sample";

export interface SampleRepository {
ensureFolderExists(root_folder_path: string): Promise<void>;
listImportableSamples(root_folder_path: string): Promise<PublicSampleResponseModel[]>;
listImportableSamples(root_folder_path: string, instrument_model: string): Promise<PublicSampleResponseModel[]>;
}
50 changes: 38 additions & 12 deletions src/domain/repositories/sample-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// import { PreparedSearchOptions, SearchResult } from "../entities/search";
// import { SampleRepository } from "../interfaces/repositories/sample-repository";

import { head } from "shelljs";
import { HeaderSampleModel, PublicHeaderSampleResponseModel } from "../entities/sample";
import { SampleRepository } from "../interfaces/repositories/sample-repository";

Expand Down Expand Up @@ -37,38 +36,49 @@ export class SampleRepositoryImpl implements SampleRepository {
}
}

async listImportableSamples(root_folder_path: string): Promise<PublicHeaderSampleResponseModel[]> {
async listImportableSamples(root_folder_path: string, instrument_model: string): Promise<PublicHeaderSampleResponseModel[]> {
let samples: PublicHeaderSampleResponseModel[] = [];
const folderPath = path.join(root_folder_path);
// read from folderPath/meta/*header*.txt and return the list of samples
const meta_header_samples = await this.getSamplesFromHeaders(folderPath);

// read from folderPath/ecodata and return the list of samples
const ecodata_samples = await this.getSamplesFromEcodata(folderPath);
if (instrument_model.startsWith('UVP6')) {
// read from folderPath/ecodata and return the list of samples
const samples_ecodata = await this.getSamplesFromEcodata(folderPath);
samples = await this.setupSamples(meta_header_samples, samples_ecodata, "ecodata");

} else if (instrument_model.startsWith('UVP5')) {
// read from folderPath/work and return the list of samples
const samples_work = await this.getSamplesFromWork(folderPath);
samples = await this.setupSamples(meta_header_samples, samples_work, "work");
}

return samples;
}
// Function to setup samples
async setupSamples(meta_header_samples: HeaderSampleModel[], samples: string[], folder: string): Promise<PublicHeaderSampleResponseModel[]> {
// flag qc samples to flase if not in both lists, and add qc message
const samples: PublicHeaderSampleResponseModel[] = [];
const samples_response: PublicHeaderSampleResponseModel[] = [];
for (const sample of meta_header_samples) {
samples.push({
sample_name: sample.filename,
samples_response.push({
sample_name: sample.profileId,
raw_file_name: sample.filename,
station_id: sample.stationId,
first_image: sample.firstImage,
last_image: sample.endImg,
comment: sample.comment,
qc_lvl1: ecodata_samples.includes(sample.filename) ? true : false,
qc_lvl1_comment: ecodata_samples.includes(sample.filename) ? '' : 'Sample not found in ecodata folder'
qc_lvl1: samples.includes(sample.profileId) ? true : false,
qc_lvl1_comment: samples.includes(sample.profileId) ? '' : 'Sample not found in ' + folder + ' folder'
});
}
return samples;
return samples_response;
}

// Function to read and return samples from header.txt files
async getSamplesFromHeaders(folderPath: string): Promise<HeaderSampleModel[]> {
const samples: HeaderSampleModel[] = [];
try {
const header_path = path.join(folderPath, 'meta');
const files = await fs.readdir(header_path);
console.log('header files', files);
for (const file of files) {
if (file.includes('header') && file.endsWith('.txt')) {
const filePath = path.join(header_path, file);
Expand Down Expand Up @@ -138,6 +148,22 @@ export class SampleRepositoryImpl implements SampleRepository {
return samples;
}

// Function to read and return samples from work folder names
async getSamplesFromWork(folderPath: string): Promise<string[]> {
const samples: string[] = [];
try {
const files = await fs.readdir(path.join(folderPath, 'work'));

for (const file of files) {
samples.push(file);
}
} catch (err) {
throw new Error(`Error reading files: ${err.message}`);
}

return samples;
}



// async createSample(sample: SampleRequestCreationModel): Promise<number> {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/use-cases/sample/list-importable-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ListImportableSamples implements ListImportableSamplesUseCase {

private async listImportableSamples(project: ProjectResponseModel): Promise<PublicSampleResponseModel[]> {
await this.sampleRepository.ensureFolderExists(project.root_folder_path);
const samples = await this.sampleRepository.listImportableSamples(project.root_folder_path);
const samples = await this.sampleRepository.listImportableSamples(project.root_folder_path, project.instrument_model);
return samples;
}

Expand Down

0 comments on commit 40b6adc

Please sign in to comment.