4
4
restifyErrors = require ( 'restify-errors' ) ,
5
5
{ Measurement, Box } = require ( '../models' ) ,
6
6
csvstringify = require ( 'csv-stringify' ) ,
7
- streamTransform = require ( 'stream-transform' ) ,
8
- jsonstringify = require ( 'stringify-stream' ) ,
9
7
{ checkContentType } = require ( '../helpers/apiUtils' ) ,
10
8
{
11
9
retrieveParameters,
12
10
validateFromToTimeParams
13
11
} = require ( '../helpers/userParamHelpers' ) ,
14
- { outlierTransformer } = require ( '../statistics' ) ,
15
12
handleError = require ( '../helpers/errorHandler' ) ;
16
13
17
14
const { BadRequestError, UnsupportedMediaTypeError } = restifyErrors ;
@@ -33,12 +30,6 @@ const getLatestMeasurements = function getLatestMeasurements (req, res, next) {
33
30
} ) ;
34
31
} ;
35
32
36
- const getDataTransformerFunction = function getDataTransformerFunction ( data ) {
37
- data . createdAt = data . createdAt . toISOString ( ) ;
38
-
39
- return data ;
40
- } ;
41
-
42
33
/**
43
34
* @apiDefine SeparatorParam
44
35
*
@@ -60,70 +51,23 @@ const getDataTransformerFunction = function getDataTransformerFunction (data) {
60
51
* @apiUse SeparatorParam
61
52
*/
62
53
const getData = function getData ( req , res , next ) {
63
- let stringifier ;
64
-
65
- const { format, delimiter, outliers, outlierWindow } = req . _userParams ;
66
-
67
- // validate window
68
- if ( outlierWindow < 1 || outlierWindow >= 50 ) {
69
- return next ( new BadRequestError ( 'parameter outlier-window must be between 1 and 50, default is 15' ) ) ;
70
- }
54
+ const { sensorId, format, download } = req . _userParams ;
71
55
72
56
// IDEA: add geojson point featurecollection format
73
- if ( format === 'csv' ) {
57
+ if ( format === 'csv' || ( download === 'true' ) ) {
74
58
res . header ( 'Content-Type' , 'text/csv' ) ;
75
- const csvColumns = [ 'createdAt' , 'value' ] ;
76
- if ( outliers ) {
77
- csvColumns . push ( 'isOutlier' ) ;
78
- }
79
-
80
- stringifier = csvstringify ( { columns : csvColumns , header : 1 , delimiter } ) ;
59
+ res . header ( 'Content-Disposition' , `attachment; filename=${ sensorId } .${ format } ` ) ;
81
60
} else if ( format === 'json' ) {
82
61
res . header ( 'Content-Type' , 'application/json; charset=utf-8' ) ;
83
- stringifier = jsonstringify ( { open : '[' , close : ']' } , function replacer ( k , v ) {
84
- // dont send unnecessary nested location
85
- return ( k === 'location' ) ? v . coordinates : v ;
86
- } ) ;
87
62
}
88
63
89
- // offer download to browser
90
- if ( format === 'csv' || ( req . _userParams . download === 'true' ) ) {
91
- res . header ( 'Content-Disposition' , `attachment; filename=${ req . params . sensorId } .${ format } ` ) ;
92
- }
64
+ Measurement . getMeasurementsStream ( req . _userParams )
65
+ . on ( 'error' , function ( err ) {
66
+ res . end ( `Error: ${ err . message } ` ) ;
93
67
94
- // finally execute the query
95
- const queryLimit = 10000 ;
96
-
97
- const qry = {
98
- sensor_id : req . _userParams . sensorId ,
99
- createdAt : {
100
- $gte : req . _userParams . fromDate . toDate ( ) ,
101
- $lte : req . _userParams . toDate . toDate ( )
102
- }
103
- } ;
104
-
105
- const measurementsCursor = Measurement
106
- . find ( qry , { 'createdAt' : 1 , 'value' : 1 , 'location' : 1 , '_id' : 0 } )
107
- . cursor ( { lean : true , limit : queryLimit } ) ;
108
-
109
- if ( outliers ) {
110
- measurementsCursor
111
- . pipe ( streamTransform ( getDataTransformerFunction ) )
112
- . pipe ( outlierTransformer ( {
113
- window : Math . trunc ( outlierWindow ) , // only allow integer values
114
- replaceOutlier : ( outliers === 'replace' )
115
- } ) )
116
- . on ( 'error' , function ( err ) {
117
- res . end ( `Error: ${ err . message } ` ) ;
118
- } )
119
- . pipe ( stringifier )
120
- . pipe ( res ) ;
121
- } else {
122
- measurementsCursor
123
- . pipe ( streamTransform ( getDataTransformerFunction ) )
124
- . pipe ( stringifier )
125
- . pipe ( res ) ;
126
- }
68
+ return next ( err ) ;
69
+ } )
70
+ . pipe ( res ) ;
127
71
} ;
128
72
129
73
/**
0 commit comments