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

streaming + s3 for v2 csv exports api side #2963

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 95 additions & 15 deletions api/src/paths/v2/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getuid } from 'process';
import SQL, { SQLStatement } from 'sql-template-strings';
import { InvasivesRequest } from 'utils/auth-utils';
import { getLogger } from '../../utils/logger';
import { streamActivitiesResult } from '../../utils/iapp-json-utils';

const defaultLog = getLogger('activity');
const CACHENAME = 'Activities v2 - Fat';
Expand Down Expand Up @@ -201,6 +202,9 @@ function sanitizeActivityFilterObject(filterObject: any, req: any) {
sanitizedSearchCriteria.serverFilterGeometries = serverFilterGeometries;
sanitizedSearchCriteria.clientFilterGeometries = clientFilterGeometries;
sanitizedSearchCriteria.clientReqTableFilters = sanitizedTableFilters;
//todo actually validate:
sanitizedSearchCriteria.isCSV = filterObject?.isCSV;
sanitizedSearchCriteria.CSVType = filterObject?.CSVType;
defaultLog.debug({
label: 'getActivitiesBySearchFilterCriteria',
message: 'sanitizedObject',
Expand Down Expand Up @@ -237,16 +241,22 @@ function getActivitiesBySearchFilterCriteria(): RequestHandler {
}

sql = getActivitiesSQLv2(filterObject);
const response = await connection.query(sql.text, sql.values);

return res.status(200).json({
message: 'fetched activities by criteria',
request: req.body,
result: response.rows,
count: response.rowCount,
namespace: 'activities',
code: 200
});

if (filterObject.isCSV && filterObject.CSVType) {
res.status(200);
await streamActivitiesResult(filterObject, res, sql);
} else {
const response = await connection.query(sql.text, sql.values);

return res.status(200).json({
message: 'fetched activities by criteria',
request: req.body,
result: response.rows,
count: response.rowCount,
namespace: 'activities',
code: 200
});
}
} catch (error) {
defaultLog.debug({ label: 'getActivitiesBySearchFilterCriteria', message: 'error', error });
return res.status(500).json({
Expand Down Expand Up @@ -420,18 +430,88 @@ activities as (

function selectStatement(sqlStatement: SQLStatement, filterObject: any) {
if (filterObject.selectColumns) {
const select = sqlStatement.append(
`select ${filterObject.selectColumns.map((column) => `activities.${column}`).join(',')} `
);
return select;
if (filterObject.isCSV) {
const select = sqlStatement.append(SQL` select extract.* `);
return select;
} else {
const select = sqlStatement.append(
`select ${filterObject.selectColumns.map((column) => `activities.${column}`).join(',')} `
);
return select;
}
} else {
const select = sqlStatement.append(`select * `);
return select;
}
}

function fromStatement(sqlStatement: SQLStatement, filterObject: any) {
const from = sqlStatement.append(`from activities `);
let from = sqlStatement.append(`from activities `);
if (filterObject.isCSV) {
from = sqlStatement.append(` b `);
switch (filterObject.CSVType) {
case 'terrestrial_plant_observation':
sqlStatement.append(
'join observation_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'aquatic_plant_observation':
sqlStatement.append('join observation_aquatic_plant_summary extract ON extract.activity_id = b.activity_id ');
break;
case 'terrestrial_chemical_treatment':
sqlStatement.append(
'join treatment_chemical_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'aquatic_chemical_treatment':
sqlStatement.append(
'join treatment_chemical_aquatic_plant_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'terrestrial_mechanical_treatment':
sqlStatement.append(
'join treatment_mechanical_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'aquatic_mechanical_treatment':
sqlStatement.append(
'join treatment_mechanical_aquatic_plant_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'biocontrol_release':
sqlStatement.append('join biocontrol_release_summary extract ON extract.activity_id = b.activity_id ');
break;
case 'biocontrol_collection':
sqlStatement.append('join biocontrol_collection_summary extract ON extract.activity_id = b.activity_id ');
break;
case 'biocontrol_dispersal':
sqlStatement.append(
'join biocontrol_dispersal_monitoring_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'chemical_treatment_monitoring':
sqlStatement.append(
'join chemical_treatment_monitoring_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'mechanical_treatment_monitoring':
sqlStatement.append(
'join mechanical_treatment_monitoring_summary extract ON extract.activity_id = b.activity_id '
);
break;
case 'biocontrol_release_monitoring':
sqlStatement.append(
'join biocontrol_release_monitoring_summary extract ON extract.activity_id = b.activity_id '
);
break;

default:
sqlStatement.append(
'join observation_terrestrial_plant_summary extract ON extract.activity_id = b.activity_id '
);
break;
}
}
return from;
}

Expand Down
54 changes: 51 additions & 3 deletions api/src/paths/v2/iapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getuid } from 'process';
import SQL, { SQLStatement } from 'sql-template-strings';
import { InvasivesRequest } from 'utils/auth-utils';
import { getLogger } from '../../utils/logger';
import { streamIAPPResult } from '../../utils/iapp-json-utils';
import { filter } from 'lodash';

const defaultLog = getLogger('IAPP');
const CACHENAME = 'IAPPv2 - Fat';
Expand Down Expand Up @@ -248,6 +250,13 @@ function getIAPPSitesBySearchFilterCriteria(): RequestHandler {
}

sql = getIAPPSQLv2(filterObject);

if(filterObject.isCSV)
{
await streamIAPPResult(filterObject, res, sql);
}
else
{
const response = await connection.query(sql.text, sql.values);

return res.status(200).json({
Expand All @@ -258,7 +267,7 @@ function getIAPPSitesBySearchFilterCriteria(): RequestHandler {
namespace: 'IAPP',
code: 200
});
} catch (error) {
}} catch (error) {
defaultLog.debug({ label: 'getIAPPBySearchFilterCriteria', message: 'error', error });
return res.status(500).json({
message: 'Error getting sites by search filter criteria',
Expand Down Expand Up @@ -406,8 +415,8 @@ sites as (

sqlStatement.append(`
from iapp_sites a
join invasivesbc.iapp_site_summary_and_geojson b on a.site_id = b.site_id
`);
join invasivesbc.iapp_site_summary_and_geojson b on a.site_id = b.site_id`);


if (filterObject?.serverFilterGeometries?.length > 0) {
sqlStatement.append(`
Expand All @@ -430,6 +439,11 @@ sites as (
}

function selectStatement(sqlStatement: SQLStatement, filterObject: any) {
if(filterObject.isCSV)
{
const select = sqlStatement.append(`select pe.* `);
return select;
}
if (filterObject.selectColumns) {
const select = sqlStatement.append(
`select ${filterObject.selectColumns.map((column) => `sites.${column}`).join(',')} `
Expand All @@ -443,6 +457,40 @@ function selectStatement(sqlStatement: SQLStatement, filterObject: any) {

function fromStatement(sqlStatement: SQLStatement, filterObject: any) {
const from = sqlStatement.append(`from sites `);
if (filterObject.isCSV) {
sqlStatement.append(` i `)
switch (filterObject.CSVType) {
case 'site_selection_extract':
sqlStatement.append(SQL` INNER JOIN site_selection_extract pe ON i.site_id = pe.site_id `);
break;
case 'survey_extract':
sqlStatement.append(SQL` INNER JOIN survey_extract pe ON i.site_id = pe.site_id`);
break;
case 'chemical_treatment_extract':
sqlStatement.append(SQL` INNER JOIN chemical_treatment_extract pe ON i.site_id = pe.site_id`);
break;
case 'mechanical_treatment_extract':
sqlStatement.append(SQL` INNER JOIN mechanical_treatment_extract pe ON i.site_id = pe.site_id`);
break;
case 'chemical_monitoring_extract':
sqlStatement.append(SQL` INNER JOIN chemical_monitoring_extract pe ON i.site_id = pe.site_id`);
break;
case 'mechanical_monitoring_extract':
sqlStatement.append(SQL` INNER JOIN mechanical_monitoring_extract pe ON i.site_id = pe.site_id`);
break;
case 'biological_treatment_extract':
sqlStatement.append(SQL` INNER JOIN biological_treatment_extract pe ON i.site_id = pe.site_id`);
break;
case 'biological_monitoring_extract':
sqlStatement.append(SQL` INNER JOIN biological_monitoring_extract pe ON i.site_id = pe.site_id`);
break;
case 'biological_dispersal_extract':
sqlStatement.append(SQL` INNER JOIN biological_dispersal_extract pe ON i.site_id = pe.site_id`);
break;
default:
sqlStatement.append(SQL` INNER JOIN site_selection_extract pe ON i.site_id = pe.site_id `);
break;
}}
return from;
}

Expand Down
14 changes: 10 additions & 4 deletions api/src/utils/iapp-json-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ function upload(S3, key) {
return pass;
}

export async function streamActivitiesResult(searchCriteria: any, res: any) {
export async function streamActivitiesResult(searchCriteria: any, res: any, sqlStatementOverride?: SQLStatement) {
const connection = await getDBConnection();

const sqlStatement: SQLStatement = getActivitiesSQL(searchCriteria, false, true);

const sqlStatement: SQLStatement = (sqlStatementOverride)? sqlStatementOverride: getActivitiesSQL(searchCriteria, false, true);

if (!sqlStatement) {
throw {
Expand All @@ -409,6 +410,8 @@ export async function streamActivitiesResult(searchCriteria: any, res: any) {

readable.pipe(upload(S3, key)).on('end', async () => {
// get signed url

await new Promise(r => setTimeout(r, 2000));
const url = await getS3SignedURL(key);
res.status(200).send(url);
res.end();
Expand All @@ -420,7 +423,7 @@ export async function streamActivitiesResult(searchCriteria: any, res: any) {
}
}

export const streamIAPPResult = async (searchCriteria: any, res: any) => {
export const streamIAPPResult = async (searchCriteria: any, res: any, sqlStatementOverride?: any) => {
let connection;
try {
connection = await getDBConnection();
Expand All @@ -440,7 +443,7 @@ export const streamIAPPResult = async (searchCriteria: any, res: any) => {
};
}

const sqlStatement: SQLStatement = getSitesBasedOnSearchCriteriaSQL(searchCriteria);
const sqlStatement: SQLStatement = sqlStatementOverride? sqlStatementOverride: getSitesBasedOnSearchCriteriaSQL(searchCriteria);

if (!sqlStatement) {
throw {
Expand All @@ -460,6 +463,9 @@ export const streamIAPPResult = async (searchCriteria: any, res: any) => {
readable.pipe(upload(S3, key)).on('end', async () => {
// get signed url
const url = await getS3SignedURL(key);

await new Promise(r => setTimeout(r, 2000));

res.status(200).send(url);
res.end();
connection.release();
Expand Down
6 changes: 6 additions & 0 deletions appv2/src/UI/Overlay/Records/ExcelExporter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.CSV-spinner > * {
position: relative;
top: 0;
left: 0;
margin: 0 1rem;
}
Loading
Loading