Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #137 from CatalystCode/inline-csv
Browse files Browse the repository at this point in the history
Inline CSV report creation
  • Loading branch information
c-w committed Sep 12, 2017
2 parents 6f944ac + 02be015 commit 61e9cc1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
9 changes: 1 addition & 8 deletions src/resolvers-cassandra/Edges/index.js
Expand Up @@ -8,12 +8,5 @@ module.exports = {
topSources: queries.topSources,
topTerms: queries.topTerms,
geofenceplaces: queries.geofenceplaces,
conjunctiveTerms: queries.conjunctiveTopics,

csv_topLocations: queries.csv_popularLocations,
csv_timeSeries: queries.csv_timeSeries,
csv_topSources: queries.csv_topSources,
csv_topTerms: queries.csv_topTerms,
csv_geofenceplaces: queries.csv_geofenceplaces,
csv_conjunctiveTerms: queries.csv_conjunctiveTopics
conjunctiveTerms: queries.conjunctiveTopics
};
30 changes: 13 additions & 17 deletions src/resolvers-cassandra/Edges/queries.js
Expand Up @@ -5,7 +5,7 @@ const moment = require('moment');
const Long = require('cassandra-driver').types.Long;
const cassandraConnector = require('../../clients/cassandra/CassandraConnector');
const featureServiceClient = require('../../clients/locations/FeatureServiceClient');
const { tilesForBbox, withRunTime, asCsvExporter, toConjunctionTopics, fromTopicListToConjunctionTopics } = require('../shared');
const { tilesForBbox, withRunTime, withCsvExporter, toConjunctionTopics, fromTopicListToConjunctionTopics } = require('../shared');
const { makeSet, makeMap, aggregateBy } = require('../../utils/collections');
const { trackEvent } = require('../../clients/appinsights/AppInsightsClient');

Expand Down Expand Up @@ -149,9 +149,12 @@ function locations(args, res) { // eslint-disable-line no-unused-vars
const { bbox } = args;

featureServiceClient.fetchByBbox({ north: bbox[0], west: bbox[1], south: bbox[2], east: bbox[3] }, 'bbox')
.then(locations =>
resolve(locations.map(location => ({ name: location.name, placeid: location.id, layer: location.layer, bbox: location.bbox })))
)
.then(locations => {
const places = locations.map(location => ({ name: location.name, placeid: location.id, layer: location.layer, bbox: location.bbox }));
resolve({
places
});
})
.catch(reject);
});
}
Expand Down Expand Up @@ -306,17 +309,10 @@ function conjunctiveTopics(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
popularLocations: trackEvent(withRunTime(popularLocations), 'popularLocations'),
timeSeries: trackEvent(timeSeries, 'timeSeries'),
topTerms: trackEvent(topTerms, 'topTerms'),
geofenceplaces: trackEvent(withRunTime(locations), 'locations'),
conjunctiveTopics: trackEvent(conjunctiveTopics, 'conjunctiveTopics'),
topSources: trackEvent(topSources, 'topSources'),

csv_popularLocations: trackEvent(asCsvExporter(popularLocations, 'edges'), 'csv_popularLocations'),
csv_timeSeries: trackEvent(asCsvExporter(timeSeries, 'graphData'), 'csv_timeSeries'),
csv_topTerms: trackEvent(asCsvExporter(topTerms, 'edges'), 'csv_topTerms'),
csv_geofenceplaces: trackEvent(asCsvExporter(locations, 'places'), 'csv_locations'),
csv_conjunctiveTopics: trackEvent(asCsvExporter(conjunctiveTopics, 'edges'), 'csv_conjunctiveTopics'),
csv_topSources: trackEvent(asCsvExporter(topSources, 'edges'), 'csv_topSources')
popularLocations: trackEvent(withRunTime(withCsvExporter(popularLocations, 'edges')), 'popularLocations'),
timeSeries: trackEvent(withCsvExporter(timeSeries, 'graphData'), 'timeSeries'),
topTerms: trackEvent(withCsvExporter(topTerms, 'edges'), 'topTerms'),
geofenceplaces: trackEvent(withRunTime(withCsvExporter(locations, 'places')), 'locations'),
conjunctiveTopics: trackEvent(withCsvExporter(conjunctiveTopics, 'edges'), 'conjunctiveTopics'),
topSources: trackEvent(withCsvExporter(topSources, 'edges'), 'topSources')
};
20 changes: 16 additions & 4 deletions src/resolvers-cassandra/shared.js
Expand Up @@ -139,7 +139,7 @@ function parseLimit(limit) {
const DEFAULT_CSV_CONTAINER = 'csv-export';
const DEFAULT_CSV_EXPIRY_MINUTES = 2 * 60;

function asCsvExporter(promiseFunc, exportPropertyName, container, expiryMinutes) {
function withCsvExporter(promiseFunc, exportPropertyName, container, expiryMinutes) {
container = container || DEFAULT_CSV_CONTAINER;
expiryMinutes = expiryMinutes || DEFAULT_CSV_EXPIRY_MINUTES;

Expand All @@ -151,14 +151,26 @@ function asCsvExporter(promiseFunc, exportPropertyName, container, expiryMinutes
}

function csvExporter(...args) {
const reportName = promiseFunc.name;

if (!args || !args.length || !args[0] || !args[0].csv) {
console.log(`No CSV requested for ${reportName}, skipping creation of report`);
return promiseFunc(...args);
}

return new Promise((resolve, reject) => {
console.log(`CSV requested, creating report for ${reportName} based on ${exportPropertyName}`);
promiseFunc(...args)
.then(returnValue => {
const csvItems = returnValue && returnValue[exportPropertyName];
const csvText = csvItems && csvItems.length ? json2csv({ data: csvItems, withBOM: true }) : '';
return createFile(container, formatCsvFilename(promiseFunc.name), csvText, expiryMinutes);
createFile(container, formatCsvFilename(reportName), csvText, expiryMinutes)
.then(csv => {
returnValue.csv = csv;
resolve(returnValue);
})
.catch(reject);
})
.then(resolve)
.catch(reject);
});
}
Expand All @@ -176,6 +188,6 @@ module.exports = {
limitForInClause,
getSiteDefintion,
fromTopicListToConjunctionTopics,
asCsvExporter,
withCsvExporter,
withRunTime
};
25 changes: 12 additions & 13 deletions src/schemas/EdgesSchema.js
Expand Up @@ -2,19 +2,12 @@ const graphql = require('graphql');

module.exports = graphql.buildSchema(`
type Query {
geofenceplaces(bbox: [Float]!): GeofencePlacesCollection
conjunctiveTerms(maintopic: String!, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!): ConjunctionTermCollection
timeSeries(maintopics: [String]!, fromDate: String!, toDate: String!, periodType: String!, pipelinekeys: [String]!, maintopics: [String]!, conjunctivetopics: [String], bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!): FeatureTimeSeriesCollection
topLocations(maintopic: String!, limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, conjunctivetopics: [String]!, bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!): TopPlacesCollection
topSources(maintopic: String!, limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, conjunctivetopics: [String]!, bbox: [Float]!, zoomLevel: Int!): TopSourcesCollection
topTerms(limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, externalsourceid: String!, bbox: [Float]!, zoomLevel: Int): TopTermsCollection
csv_geofenceplaces(bbox: [Float]!): Csv
csv_conjunctiveTerms(maintopic: String!, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!): Csv
csv_timeSeries(maintopics: [String]!, fromDate: String!, toDate: String!, periodType: String!, pipelinekeys: [String]!, maintopics: [String]!, conjunctivetopics: [String], bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!): Csv
csv_topLocations(maintopic: String!, limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, conjunctivetopics: [String]!, bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!): Csv
csv_topSources(maintopic: String!, limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, conjunctivetopics: [String]!, bbox: [Float]!, zoomLevel: Int!): Csv
csv_topTerms(limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, externalsourceid: String!, bbox: [Float]!, zoomLevel: Int): Csv
geofenceplaces(bbox: [Float]!, csv: Boolean): GeofencePlacesCollection
conjunctiveTerms(maintopic: String!, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!, csv: Boolean): ConjunctionTermCollection
timeSeries(maintopics: [String]!, fromDate: String!, toDate: String!, periodType: String!, pipelinekeys: [String]!, maintopics: [String]!, conjunctivetopics: [String], bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!, csv: Boolean): FeatureTimeSeriesCollection
topLocations(maintopic: String!, limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, conjunctivetopics: [String]!, bbox: [Float]!, zoomLevel: Int!, externalsourceid: String!, csv: Boolean): TopPlacesCollection
topSources(maintopic: String!, limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, conjunctivetopics: [String]!, bbox: [Float]!, zoomLevel: Int!, csv: Boolean): TopSourcesCollection
topTerms(limit: Int, fromDate: String!, periodType: String!, toDate: String!, pipelinekeys: [String]!, externalsourceid: String!, bbox: [Float]!, zoomLevel: Int, csv: Boolean): TopTermsCollection
}
type Csv {
Expand Down Expand Up @@ -53,21 +46,25 @@ module.exports = graphql.buildSchema(`
type GeofencePlacesCollection {
places: [OsmPlace]!
csv: Csv
}
type TopSourcesCollection{
runTime: String,
edges: [ExternalSource]!
csv: Csv
}
type TopPlacesCollection{
runTime: String,
edges: [Place]!
csv: Csv
}
type TopTermsCollection{
runTime: String,
edges: [Term]!
csv: Csv
}
type ConjuntiveTerm {
Expand All @@ -80,6 +77,7 @@ module.exports = graphql.buildSchema(`
type ConjunctionTermCollection {
runTime: String
edges: [ConjuntiveTerm]!
csv: Csv
}
type TimeSeriesLabel {
Expand All @@ -90,6 +88,7 @@ module.exports = graphql.buildSchema(`
labels: [TimeSeriesLabel]!
graphData: [TimeSeriesEntry]!
tiles: [String]
csv: Csv
}
type TimeSeriesEntry{
Expand Down

0 comments on commit 61e9cc1

Please sign in to comment.