|
9 | 9 | * OF ANY KIND, either express or implied. See the License for the specific language |
10 | 10 | * governing permissions and limitations under the License. |
11 | 11 | */ |
| 12 | +const { Response } = require('node-fetch'); |
12 | 13 | const { wrap } = require('@adobe/openwhisk-action-utils'); |
13 | 14 | const { logger } = require('@adobe/openwhisk-action-logger'); |
14 | | -const { wrap: status } = require('@adobe/helix-status'); |
15 | | -const { epsagon } = require('@adobe/helix-epsagon'); |
| 15 | +const { wrap: helixStatus } = require('@adobe/helix-status'); |
16 | 16 | const embed = require('./embed'); |
17 | 17 | const { loadquerystring } = require('./querybuilder/url'); |
18 | 18 | const { createfilter } = require('./querybuilder/filter'); |
19 | 19 | const dataSource = require('./data-source.js'); |
20 | 20 |
|
21 | 21 | const MAX_DATA_SIZE = 750000; |
22 | 22 |
|
23 | | -async function main(params) { |
| 23 | +async function main(req, context) { |
24 | 24 | /* istanbul ignore next */ |
25 | | - const { __ow_logger: log = console } = params; |
26 | | - const url = dataSource((params)); |
| 25 | + const { log = console } = context; |
| 26 | + const url = dataSource(req, context); |
27 | 27 | if (!url) { |
28 | | - return { |
29 | | - statusCode: 400, |
30 | | - body: 'Expecting a datasource', |
31 | | - }; |
| 28 | + return new Response('Expecting a datasource', { |
| 29 | + status: 400, |
| 30 | + }); |
32 | 31 | } |
33 | | - log.info(`data-embed for datasource ${url}`); |
34 | | - const qbquery = loadquerystring(params.__ow_query, 'hlx_'); |
35 | | - log.debug('QB query', qbquery); |
36 | | - const filter = createfilter(qbquery); |
37 | | - log.debug('QB filter', filter); |
38 | | - const result = await embed(url, params, log); |
| 32 | + try { |
| 33 | + const { searchParams } = new URL(req.url); |
39 | 34 |
|
40 | | - const { body } = result; |
41 | | - delete result.body; |
42 | | - log.debug('result', result); |
43 | | - log.debug(`result body size: ${JSON.stringify(body).length}`); |
44 | | - const filtered = filter(body); |
45 | | - let size = JSON.stringify(filtered).length; |
46 | | - log.info(`filtered result ${filtered.length} rows. size: ${size}`); |
47 | | - if (size > MAX_DATA_SIZE) { |
48 | | - // todo: could be optimized to be more accurate using some binary search approach |
49 | | - const avgRowSize = size / filtered.length; |
50 | | - const retain = Math.floor(MAX_DATA_SIZE / avgRowSize); |
51 | | - filtered.splice(retain, filtered.length - retain); |
52 | | - size = JSON.stringify(filtered).length; |
53 | | - log.info(`result truncated to ${filtered.length} rows. size: ${size}`); |
54 | | - } |
55 | | - return { |
56 | | - ...result, |
57 | | - body: { |
| 35 | + log.info(`data-embed for datasource ${url}`); |
| 36 | + const qbquery = loadquerystring(searchParams, 'hlx_'); |
| 37 | + log.debug('QB query', qbquery); |
| 38 | + const filter = createfilter(qbquery); |
| 39 | + log.debug('QB filter', filter); |
| 40 | + const params = Array.from(searchParams.entries()).reduce((p, [key, value]) => { |
| 41 | + // eslint-disable-next-line no-param-reassign |
| 42 | + p[key] = value; |
| 43 | + return p; |
| 44 | + }, {}); |
| 45 | + const result = await embed(url, params, context.env, log); |
| 46 | + |
| 47 | + const { |
| 48 | + body, |
| 49 | + statusCode: status, |
| 50 | + headers, |
| 51 | + } = result; |
| 52 | + log.debug(`result body size: ${JSON.stringify(body).length}`); |
| 53 | + const filtered = filter(body); |
| 54 | + let size = JSON.stringify(filtered).length; |
| 55 | + log.info(`filtered result ${filtered.length} rows. size: ${size}`); |
| 56 | + if (size > MAX_DATA_SIZE) { |
| 57 | + // todo: could be optimized to be more accurate using some binary search approach |
| 58 | + const avgRowSize = size / filtered.length; |
| 59 | + const retain = Math.floor(MAX_DATA_SIZE / avgRowSize); |
| 60 | + filtered.splice(retain, filtered.length - retain); |
| 61 | + size = JSON.stringify(filtered).length; |
| 62 | + log.info(`result truncated to ${filtered.length} rows. size: ${size}`); |
| 63 | + } |
| 64 | + const bodyText = JSON.stringify({ |
58 | 65 | total: body.length, |
59 | 66 | offset: filter.offset || 0, |
60 | 67 | limit: filtered.length, |
61 | 68 | data: filtered, |
62 | | - }, |
63 | | - }; |
| 69 | + }); |
| 70 | + return new Response(bodyText, { |
| 71 | + status, |
| 72 | + headers, |
| 73 | + }); |
| 74 | + } catch (e) { |
| 75 | + log.error('error fetching data', e); |
| 76 | + return new Response('error fetching data', { |
| 77 | + status: 500, |
| 78 | + headers: { |
| 79 | + 'content-type': 'application/json', |
| 80 | + 'cache-control': 'no-store, private, must-revalidate', |
| 81 | + }, |
| 82 | + }); |
| 83 | + } |
64 | 84 | } |
65 | 85 |
|
66 | 86 | module.exports.main = wrap(main) |
67 | | - .with(epsagon) |
68 | | - .with(status) |
| 87 | + .with(helixStatus) |
69 | 88 | .with(logger.trace) |
70 | 89 | .with(logger); |
0 commit comments