diff --git a/elk-traffic-monitor-api/flows/monitoringApiApi-getRouterServiceOpsSearchByServiceID.json b/elk-traffic-monitor-api/flows/monitoringApiApi-getRouterServiceOpsSearchByServiceID.json index 5462ff03..d617c70a 100644 --- a/elk-traffic-monitor-api/flows/monitoringApiApi-getRouterServiceOpsSearchByServiceID.json +++ b/elk-traffic-monitor-api/flows/monitoringApiApi-getRouterServiceOpsSearchByServiceID.json @@ -248,7 +248,7 @@ { "name": "code", "type": "string", - "value": "\"var queryFilters = data.queryFilters;\\n if(data.params.ago) {\\n var ago = data.params.ago;\\n var now = Date.now()\\n var diff = convertAgo(ago);\\n var filter = {\\n range: {\\n \\\"timestampOriginal\\\": {\\n gt: now\\n }\\n }\\n };\\n queryFilters.push(filter);\\n return queryFilters;\\n }\\n \\n function convertAgo(ago) {\\n if(ago.endsWith('m')) {\\n return ago.substring(0, ago.length-1) * 60000;\\n } else if(ago.endsWith('h')) {\\n return ago.substring(0, ago.length-1) * 3600 * 1000;\\n }\\n console.log(`ERRRRO ${ago}`);\\n }\"", + "value": "\"var queryFilters = data.queryFilters;\\n if(data.params.ago) {\\n var ago = data.params.ago;\\n var diff = convertAgo(ago);\\n var filter = {\\n range: {\\n \\\"timestampOriginal\\\": {\\n gt: diff\\n }\\n }\\n };\\n queryFilters.push(filter);\\n return queryFilters;\\n }\\n \\n function convertAgo(ago) {\\n var diff = ago.substring(0, ago.length-1);\\n var now = new Date();\\n if(ago.endsWith('m')) {\\n return now.setMinutes(now.getMinutes() - diff);\\n } else if(ago.endsWith('h')) {\\n return now.setMinutes(now.getHours() - diff);\\n }\\n console.log(`ERRRRO ${ago}`);\\n }\"", "metaName": "code", "metaDescription": "A JavaScript function body. Supports `await` and returning promises" } diff --git a/elk-traffic-monitor-api/test/documents/basic/search_test_documents.js b/elk-traffic-monitor-api/test/documents/basic/search_test_documents.js index dab14ca8..4e9762c9 100644 --- a/elk-traffic-monitor-api/test/documents/basic/search_test_documents.js +++ b/elk-traffic-monitor-api/test/documents/basic/search_test_documents.js @@ -1,3 +1,5 @@ +const getDate = require('../util'); + module.exports = [ { "processInfo": { @@ -150,7 +152,7 @@ module.exports = [ } }, "correlationId": "c8705e5ecc00adca32be7472", - "timestampOriginal": "2020-03-03T14:59:20.467Z", + "timestampOriginal": getDate('8m'), "transactionSummary": { "serviceContexts": [ { @@ -234,7 +236,7 @@ module.exports = [ } }, "correlationId": "c9705e5ecd000322778d2ec4", - "timestampOriginal": "2020-03-03T14:59:21.273Z", + "timestampOriginal": getDate('15m'), "transactionSummary": { "serviceContexts": [], "path": "/favicon.ico", @@ -301,7 +303,7 @@ module.exports = [ } }, "correlationId": "fc705e5ede00654de6d15daf", - "timestampOriginal": "2020-03-03T15:00:12.175Z", + "timestampOriginal": getDate('120h'), "transactionSummary": { "serviceContexts": [], "path": "/v2/pet/findByStatus", @@ -375,7 +377,7 @@ module.exports = [ "leg": 0 } }, - "timestampOriginal": "2020-03-03T14:06:06.351Z", + "timestampOriginal": getDate('65m'), "processInfo": { "groupName": "QuickStart Group", "hostname": "api-env", diff --git a/elk-traffic-monitor-api/test/documents/util.js b/elk-traffic-monitor-api/test/documents/util.js new file mode 100644 index 00000000..6ced9fcc --- /dev/null +++ b/elk-traffic-monitor-api/test/documents/util.js @@ -0,0 +1,12 @@ +function getDate(ago) { + var now = new Date(); + var diff = ago.substring(0, ago.length-1); + if(ago.endsWith('m')) { + now.setMinutes(now.getMinutes() - diff); + } else if(ago.endsWith('h')) { + now.setHours(now.getHours() - diff); + } + return now.toISOString(); +} + +module.exports = getDate; \ No newline at end of file diff --git a/elk-traffic-monitor-api/test/endpoints.js b/elk-traffic-monitor-api/test/endpoints.js index 290b25df..885994cb 100644 --- a/elk-traffic-monitor-api/test/endpoints.js +++ b/elk-traffic-monitor-api/test/endpoints.js @@ -136,7 +136,7 @@ describe('Endpoints', function () { }; return requestAsync({ method: 'GET', - uri: `http://localhost:${server.apibuilder.port}/api/elk/v1/api/router/service/instance-1/ops/search?ago=120h`, + uri: `http://localhost:${server.apibuilder.port}/api/elk/v1/api/router/service/instance-1/ops/search?ago=5m`, // The first entry is generated with 8 minutes in the past auth: auth, json: true }).then(({ response, body }) => { @@ -370,6 +370,60 @@ describe('Endpoints', function () { expect(body.data[0].uri).to.equals('/v2/pet/123'); }); }); + it('[Endpoint-0019] Should return 1 entry in the last 10 minutes (ago=10m)', () => { + const auth = { + user: server.apibuilder.config.apikey || 'test', + password: '' + }; + return requestAsync({ + method: 'GET', + uri: `http://localhost:${server.apibuilder.port}/api/elk/v1/api/router/service/instance-1/ops/search?ago=10m`, + auth: auth, + json: true + }).then(({ response, body }) => { + expect(response.statusCode).to.equal(200); + expect(body).to.be.an('Object'); + expect(body).to.have.property('data'); + expect(body.data).to.have.lengthOf(1); + expect(body.data[0].uri).to.equals('/v2/pet/123'); + }); + }); + it('[Endpoint-0020] Should return 2 entries in the last 30 minutes (ago=30m)', () => { + const auth = { + user: server.apibuilder.config.apikey || 'test', + password: '' + }; + return requestAsync({ + method: 'GET', + uri: `http://localhost:${server.apibuilder.port}/api/elk/v1/api/router/service/instance-1/ops/search?ago=30m`, + auth: auth, + json: true + }).then(({ response, body }) => { + expect(response.statusCode).to.equal(200); + expect(body).to.be.an('Object'); + expect(body).to.have.property('data'); + expect(body.data).to.have.lengthOf(2); + expect(body.data[0].uri).to.equals('/v2/pet/123'); + }); + }); + it('[Endpoint-0020] Should only 2 entries in the last 2 hours (ago=120h)', () => { + const auth = { + user: server.apibuilder.config.apikey || 'test', + password: '' + }; + return requestAsync({ + method: 'GET', + uri: `http://localhost:${server.apibuilder.port}/api/elk/v1/api/router/service/instance-1/ops/search?ago=120h`, + auth: auth, + json: true + }).then(({ response, body }) => { + expect(response.statusCode).to.equal(200); + expect(body).to.be.an('Object'); + expect(body).to.have.property('data'); + expect(body.data).to.have.lengthOf(3); + expect(body.data[0].uri).to.equals('/v2/pet/123'); + }); + }); }); });