Skip to content

Commit

Permalink
download cleaner, core metadata below csv data
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiggr committed Nov 4, 2022
1 parent bfe0283 commit 479505a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
1 change: 0 additions & 1 deletion src/Blocks/EmbedEEAVisualization/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const Edit = (props) => {
use_live_data: true,
vis_url: data.vis_url,
with_sources: data.show_sources,
include_core_metadata_download: data.include_core_metadata_download,
include_sources_download: data?.include_sources_download,
include_other_org_download: data?.include_other_org_download,
include_temporal_coverage_download:
Expand Down
2 changes: 0 additions & 2 deletions src/Blocks/EmbedEEAVisualization/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ const View = (props) => {
use_live_data: true,
vis_url: data.vis_url,
with_sources: data.show_sources,
include_core_metadata_download:
data.include_core_metadata_download,
include_sources_download: data?.include_sources_download,
include_other_org_download: data?.include_other_org_download,
include_temporal_coverage_download:
Expand Down
27 changes: 9 additions & 18 deletions src/Blocks/EmbedEEAVisualization/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ const makeMetadataOptions = (data) => {
const Schema = (props) => {
const hasSources =
props.data_provenance &&
props.data.download_button &&
props.data_provenance.data &&
props.data_provenance.data.length > 0;
const hasOtherOrg =
props.other_organisations && props.other_organisations.length > 0;
props.data.download_button &&
props.other_organisations &&
props.other_organisations.length > 0;
const hasTemporalCoverage =
props.data.download_button &&
props.temporal_coverage &&
props.temporal_coverage.temporal &&
props.temporal_coverage.temporal.length > 0;
Expand Down Expand Up @@ -51,18 +55,10 @@ const Schema = (props) => {
title: 'Download',
fields: [
'download_button',
...(props.data.download_button
? ['include_core_metadata_download']
: []),
...(props.data.download_button &&
props.data.include_core_metadata_download
? [
...(hasSources ? ['include_sources_download'] : []),
...(hasOtherOrg ? ['include_other_org_download'] : []),
...(hasTemporalCoverage
? ['include_temporal_coverage_download']
: []),
]
...(hasSources ? ['include_sources_download'] : []),
...(hasOtherOrg ? ['include_other_org_download'] : []),
...(hasTemporalCoverage
? ['include_temporal_coverage_download']
: []),
],
},
Expand Down Expand Up @@ -109,11 +105,6 @@ const Schema = (props) => {
title: 'Toggle download',
type: 'boolean',
},
include_core_metadata_download: {
title: 'Download core metadata',
description: 'Include core metadata in the dowloaded CSV',
type: 'boolean',
},
include_sources_download: {
title: 'Download sources',
description: 'Include sources in the dowloaded CSV',
Expand Down
22 changes: 12 additions & 10 deletions src/ConnectedChart2/ConnectedChart2.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ import SourcesWidget from './Sources';
const LoadablePlotly = loadable(() => import('react-plotly.js'));

const filterItemsIds = (items, allowedIds) => {
const newItems = items.map((item) => {
var newItem = {};
allowedIds.forEach((id) => {
newItem[id] = item[id];
});
return newItem;
});
const newItems =
items && items.length > 0
? items
.map((item) => {
var newItem = {};
allowedIds.forEach((id) => {
newItem[id] = item[id];
});
return newItem;
})
.filter((value) => Object.keys(value).length !== 0)
: [];
return newItems;
};

Expand Down Expand Up @@ -144,9 +149,6 @@ function ConnectedChart2(props) {
}
provider_data={provider_data}
provider_metadata={provider_metadata}
include_core_metadata_download={
props.data.include_core_metadata_download
}
core_metadata={{
data_provenance: props.data?.include_sources_download
? filterItemsIds(
Expand Down
45 changes: 22 additions & 23 deletions src/ConnectedChart2/Download.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ function exportCSVFile(csv, title = 'data') {
}
}

const spreadCoreMetadata = (core_metadata, maxRowsProvData) => {
const spreadCoreMetadata = (core_metadata) => {
let spread_metadata = {};
Object.keys(core_metadata).forEach((key) => {
if (core_metadata[key].length > 0) {
core_metadata[key].forEach((item) => {
Object.keys(item).forEach((subkey) => {
if (!spread_metadata['Core metadata']) {
spread_metadata['Core metadata'] = [' '];
if (!spread_metadata['EEA Core Metadata']) {
spread_metadata['EEA Core Metadata'] = [' '];
} else {
spread_metadata['Core metadata'].push(' ');
spread_metadata['EEA Core Metadata'].push(' ');
}
if (!spread_metadata[`${key}_${subkey}`]) {
spread_metadata[`${key}_${subkey}`] = [item[subkey]];
Expand All @@ -139,12 +139,10 @@ const spreadCoreMetadata = (core_metadata, maxRowsProvData) => {
a.length > b.length ? a : b,
).length;

const maxRows = maxRowsProvData > coreMaxRows ? maxRowsProvData : coreMaxRows;

let evenMatrix = spread_metadata;
Object.entries(evenMatrix).forEach(([key, items]) => {
if (items.length < maxRows) {
for (let i = items.length; i < maxRows; i++) {
if (items.length < coreMaxRows) {
for (let i = items.length; i < coreMaxRows; i++) {
items.push('');
}
}
Expand All @@ -162,10 +160,11 @@ const Download = (props) => {
providers_data,
providers_metadata,
core_metadata,
include_core_metadata_download,
} = props;

const handleDownloadData = () => {
let array = [];
let core_metadata_array = [];
let readme = provider_metadata?.readme ? [provider_metadata?.readme] : [];
const mappedData = {
...provider_data,
Expand All @@ -181,22 +180,22 @@ const Download = (props) => {
core_metadata?.other_organisation?.length > 0 ||
core_metadata?.temporal_coverage?.length > 0;

if (include_core_metadata_download && hasCoreMetadata) {
const maxRowsMappedData = Object.values(mappedData).reduce((a, b) =>
a.length > b.length ? a : b,
).length;

Object.entries(
spreadCoreMetadata(core_metadata, maxRowsMappedData),
).forEach(([key, items]) => {
items.forEach((item, index) => {
if (!array[index]) array[index] = {};
array[index][key] = item;
});
});
if (hasCoreMetadata) {
Object.entries(spreadCoreMetadata(core_metadata)).forEach(
([key, items]) => {
items.forEach((item, index) => {
if (!core_metadata_array[index]) core_metadata_array[index] = {};
core_metadata_array[index][key] = item;
});
},
);
}

const csv = convertToCSV(array, readme);
const data_csv = convertToCSV(array, readme);
const core_metadata_csv = hasCoreMetadata
? convertToCSV(core_metadata_array, readme)
: '';
const csv = hasCoreMetadata ? data_csv + core_metadata_csv : data_csv;
exportCSVFile(csv, title);
};

Expand Down

0 comments on commit 479505a

Please sign in to comment.