Skip to content

Commit

Permalink
Fix and update csv export (#1164)
Browse files Browse the repository at this point in the history
# Feat/Fix lat/long in CSV export and add new fields

## JIRA Ticket

[BSS-380](https://jira.csiro.com/browse/BSS-380)

## Description

CSV export extracts latitude and longitude for GPS locations as separate
columns as well as including the JSON fragment. These values were
switched in the current implementation.

## Proposed Changes

- Fix the order of lat/long in GPS points
- Add similar export code for map locations as long as they are points
- Add GPS accuracy as a separate column (user request)
- Add 'created' and 'created_by' fields in export
- Fix a bug that hangs CSV export when there are no records (or all
records are deleted)

Note that the late of these involved making a change to an index used to
access the database (the recordRevisions index) to
include the created_by field (created was already there). This means
that we won't see created_by for any existing notebooks unless we can
update the indexes (not currently implemented). It will show for any new
notebooks

## How to Test

Create a new notebook (to get the new index) that includes GPS location
and/or Map fields. Create some records in the app. Then go to the
notebook page on Conductor and export the data as CSV. The resulting CSV
file should include created, created_by and the lat/long/accuracy fields
for your GPS points.

Try exporting from a notebook with no records, should get a CSV with
just a header line.

## Checklist

- [x] I have confirmed all commits have been signed.
- [x] I have added JSDoc style comments to any new functions or classes.
- [x] Relevant documentation such as READMEs, guides, and class comments
are updated.
  • Loading branch information
stevecassidy authored Oct 10, 2024
2 parents cc72471 + 9bc1f7f commit a88b806
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
54 changes: 50 additions & 4 deletions api/src/couchdb/notebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,42 @@ const csvFormatValue = (

// gps locations
if (fieldType === 'faims-pos::Location') {
if (value instanceof Object && 'geometry' in value) {
if (
value instanceof Object &&
'geometry' in value &&
value.geometry.coordinates.length === 2
) {
result[fieldName] = value;
result[fieldName + '_latitude'] = value.geometry.coordinates[0];
result[fieldName + '_longitude'] = value.geometry.coordinates[1];
result[fieldName + '_latitude'] = value.geometry.coordinates[1];
result[fieldName + '_longitude'] = value.geometry.coordinates[0];
result[fieldName + '_accuracy'] = value.properties.accuracy || '';
} else {
result[fieldName] = value;
result[fieldName + '_latitude'] = '';
result[fieldName + '_longitude'] = '';
result[fieldName + '_accuracy'] = '';
}
return result;
}

if (fieldType === 'faims-core::JSON') {
// map location, if it's a point we can pull out lat/long
if (
value instanceof Object &&
'features' in value &&
value.features.length > 0 &&
value.features[0]?.geometry?.type === 'Point' &&
value.features[0].geometry.coordinates.length === 2
) {
result[fieldName] = value;
result[fieldName + '_latitude'] =
value.features[0].geometry.coordinates[1];
result[fieldName + '_longitude'] =
value.features[0].geometry.coordinates[0];
return result;
}
}

if (fieldType === 'faims-core::Relationship') {
if (value instanceof Array) {
result[fieldName] = value
Expand Down Expand Up @@ -743,6 +767,8 @@ export const streamNotebookRecordsAsCSV = async (
record.record_id,
record.revision_id,
record.type,
record.created_by,
record.created.toISOString(),
record.updated_by,
record.updated.toISOString(),
];
Expand All @@ -762,6 +788,8 @@ export const streamNotebookRecordsAsCSV = async (
'record_id',
'revision_id',
'type',
'created_by',
'created',
'updated_by',
'updated',
];
Expand All @@ -780,7 +808,25 @@ export const streamNotebookRecordsAsCSV = async (
record = next.record;
done = next.done;
}
if (stringifier) stringifier.end();
if (stringifier) {
stringifier.end();
} else {
// no records to export so just send the bare column headings
const columns = [
'identifier',
'record_id',
'revision_id',
'type',
'created_by',
'created',
'updated_by',
'updated',
];
stringifier = stringify({columns, header: true});
// pipe output to the respose
stringifier.pipe(res);
stringifier.end();
}
};

export const streamNotebookFilesAsZip = async (
Expand Down
1 change: 0 additions & 1 deletion app/src/gui/components/notebook/record_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
getMetadataForAllRecords,
getRecordsWithRegex,
} from '@faims3/data-model';
import {DEBUG_APP} from '../../../buildconfig';
import {NotebookDataGridToolbar} from './datagrid_toolbar';
import RecordDelete from './delete';
import getLocalDate from '../../fields/LocalDate';
Expand Down
3 changes: 2 additions & 1 deletion library/data-model/src/data_storage/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ export const addDesignDocsForNotebook = async (
if (doc.heads.length > 0) {
const conflict = doc.heads.length > 1;
const created = doc.created;
const created_by = doc.created_by;
const type = doc.type;
emit(doc._id, {_id: doc.heads[0], conflict, created, type});
emit(doc._id, {_id: doc.heads[0], conflict, created, created_by, type});
}
}`,
},
Expand Down
1 change: 1 addition & 0 deletions library/data-model/src/data_storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ export async function getSomeRecords(
record_id: doc.id,
revision_id: doc.value._id,
created: doc.value.created,
created_by: doc.value.created_by,
conflict: doc.value.conflict,
type: doc.value.type,
revision: doc.doc,
Expand Down

0 comments on commit a88b806

Please sign in to comment.