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

BI-1914 - Improve exp dataset sort #303

Merged
merged 11 commits into from
Nov 8, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import org.brapi.v2.model.core.*;
import org.brapi.v2.model.core.response.BrAPIListsSingleResponse;
import org.brapi.v2.model.germ.BrAPIGermplasm;

import org.brapi.v2.model.pheno.*;
import org.breedinginsight.brapi.v2.constants.BrAPIAdditionalInfoFields;
import org.breedinginsight.brapi.v2.dao.BrAPIGermplasmDAO;
import org.breedinginsight.brapi.v2.model.request.query.ExperimentExportQuery;
import org.breedinginsight.brapps.importer.daos.*;
import org.breedinginsight.brapps.importer.model.exports.FileType;
import org.breedinginsight.brapps.importer.model.imports.experimentObservation.ExperimentObservation;
import org.breedinginsight.brapps.importer.model.imports.experimentObservation.ExperimentObservation.Columns;
import org.breedinginsight.brapps.importer.services.ExternalReferenceSource;
import org.breedinginsight.brapps.importer.services.FileMappingUtil;
import org.breedinginsight.model.BrAPIConstants;
Expand All @@ -30,6 +32,7 @@
import org.breedinginsight.services.writers.CSVWriter;
import org.breedinginsight.services.writers.ExcelWriter;
import org.breedinginsight.utilities.Utilities;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -57,6 +60,7 @@ public class BrAPITrialService {
private final BrAPIObservationUnitDAO ouDAO;
private final BrAPIGermplasmDAO germplasmDAO;
private final FileMappingUtil fileMappingUtil;
private static final String SHEET_NAME = "Data";

@Inject
public BrAPITrialService(@Property(name = "brapi.server.reference-source") String referenceSource,
Expand Down Expand Up @@ -227,15 +231,17 @@ public DownloadFile exportObservations(
// Initialize key with empty list if it is not present.
if (!rowsByStudyId.containsKey(studyId))
{
rowsByStudyId.put(studyId, new ArrayList<Map<String, Object>>());
rowsByStudyId.put(studyId, new ArrayList<>());
}
// Add row to appropriate list in rowsByStudyId.
rowsByStudyId.get(studyId).add(row);
}
List<DownloadFile> files = new ArrayList<>();
// Generate a file for each study.
for (Map.Entry<String, List<Map<String, Object>>> entry: rowsByStudyId.entrySet()) {
StreamedFile streamedFile = writeToStreamedFile(columns, entry.getValue(), fileType, "Experiment Data");
List<Map<String, Object>> rows = entry.getValue();
sortDefaultForExportRows(rows);
StreamedFile streamedFile = writeToStreamedFile(columns, rows, fileType, SHEET_NAME);
String name = makeFileName(experiment, program, studyByDbId.get(entry.getKey()).getStudyName()) + fileType.getExtension();
// Add to file list.
files.add(new DownloadFile(name, streamedFile));
Expand All @@ -252,8 +258,9 @@ public DownloadFile exportObservations(
}
} else {
List<Map<String, Object>> exportRows = new ArrayList<>(rowByOUId.values());
sortDefaultForExportRows(exportRows);
// write export data to requested file format
StreamedFile streamedFile = writeToStreamedFile(columns, exportRows, fileType, "Experiment Data");
StreamedFile streamedFile = writeToStreamedFile(columns, exportRows, fileType, SHEET_NAME);
// Set filename.
String envFilenameFragment = params.getEnvironments() == null ? "All Environments" : params.getEnvironments();
String fileName = makeFileName(experiment, program, envFilenameFragment) + fileType.getExtension();
Expand Down Expand Up @@ -308,6 +315,7 @@ public Dataset getDatasetData(Program program, UUID experimentId, UUID datsetId,
log.debug("fetching observations for dataset: " + datsetId);
List<BrAPIObservation> data = observationDAO.getObservationsByObservationUnitsAndVariables(ouDbIds, obsVarDbIds, program);
log.debug("building dataset object for dataset: " + datsetId);
sortDefaultForObservationUnit(datasetOUs);
Dataset dataset = new Dataset(experimentId.toString(), data, datasetOUs, datasetObsVars);
if (stats) {
Integer ouCount = datasetOUs.size();
Expand Down Expand Up @@ -549,4 +557,16 @@ private List<BrAPIObservation> filterDatasetByEnvironment(
.collect(Collectors.toList());
}

private void sortDefaultForObservationUnit(List<BrAPIObservationUnit> ous) {
ous.sort(Comparator.comparing(BrAPIObservationUnit::getStudyName)
.thenComparing(BrAPIObservationUnit::getObservationUnitName));
}

private void sortDefaultForExportRows(@NotNull List<Map<String, Object>> exportRows) {
Comparator<Map<String, Object>> envComparator = Comparator.comparing(row -> (row.get(Columns.ENV).toString()));
Comparator<Map<String, Object>> expUnitIdComparator =
Comparator.comparing(row -> (row.get(Columns.EXP_UNIT_ID).toString()));

exportRows.sort(envComparator.thenComparing(expUnitIdComparator));
}
}
Loading