From 881921461c892399b27913179b7727e8ad6de06b Mon Sep 17 00:00:00 2001 From: oof1lab Date: Sun, 19 Nov 2017 17:45:19 +0530 Subject: [PATCH] refactor : chart gets new querying way steppair --- README.md | 28 +++++++++++++++++++++++----- lib/xapi.js | 10 ++++++++++ lib/xsql.js | 39 +++++++++++++++++++++++++++++++++++++++ tests/tests.js | 25 +++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 92dce27ba0..d4e74f3882 100644 --- a/README.md +++ b/README.md @@ -441,7 +441,7 @@ response body Chart API returns distribution of a numeric column in a table -It comes in **SIX** powerful flavours +It comes in **SEVEN** powerful flavours 1. Chart : With min, max, step in query params :fire::fire: [:arrow_heading_up:](#api-overview) @@ -514,7 +514,25 @@ Response ``` -3. Chart : with no params :fire::fire: +3. Chart : With step paits in params :fire::fire: +[:arrow_heading_up:](#api-overview) + +This API returns distribution between each step pair + +``` +/api/payments/chart?_fields=amount&steppair=0,50000,40000,100000 + +Response + +[ + {"amount":"0 to 50000","_count":231}, + {"amount":"40000 to 100000","_count":80} +] + + +``` + +4. Chart : with no params :fire::fire: [:arrow_heading_up:](#api-overview) This API figures out even distribution of a numeric column in table and returns the data @@ -556,7 +574,7 @@ Response ``` -4. Chart : range, min, max, step in query params :fire::fire: +5. Chart : range, min, max, step in query params :fire::fire: [:arrow_heading_up:](#api-overview) This API returns the number of rows where amount is between (0,25000), (0,50000) ... (0,maxValue) @@ -593,7 +611,7 @@ Response ``` -5. Range can be specified with step array like below +6. Range can be specified with step array like below ``` /api/payments/chart?_fields=amount&steparray=0,10000,20000,70000,140000&range=1 @@ -618,7 +636,7 @@ Response ] ``` -6. Range can be specified without any step params like below +7. Range can be specified without any step params like below ``` /api/payments/chart?_fields=amount&range=1 diff --git a/lib/xapi.js b/lib/xapi.js index e8f766d62b..aaae5d0a84 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -762,6 +762,14 @@ class Xapi { isRange) + } else if (req.query && req.query.steppair && req.query.steppair.length > 1) { + + obj = this.mysql.getChartQueryAndParamsFromStepPair(req.app.locals._tableName, + req.query._fields, + (req.query.steppair.split(',')).map(Number), + false) + + } else { query = 'select min(??) as min,max(??) as max,stddev(??) as stddev,avg(??) as avg from ??'; @@ -790,6 +798,8 @@ class Xapi { } + this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, obj, ' where ') + let results = await this.mysql.exec(obj.query, obj.params); diff --git a/lib/xsql.js b/lib/xsql.js index f19d44b318..d35a9805a7 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -659,6 +659,45 @@ class Xsql { } + getChartQueryAndParamsFromStepPair(tableName, columnName, stepArray, isRange = false) { + + let obj = {} + + obj.query = '' + obj.params = [] + + //console.log('getChartQueryAndParamsFromStepArray',isRange); + + //select ? as ??, count(*) as _count from ?? where ?? between ? and ? + + if (stepArray.length && stepArray.length >= 2 && stepArray.length%2 === 0) { + + for (let i = 0; i < stepArray.length && stepArray.length >= 2; i = i + 2) { + + obj.query = obj.query + dataHelp.getChartQuery(); + + if (i + 2 < stepArray.length) { + obj.query = obj.query + ' union ' + } + + obj.params.push((stepArray[i]) + ' to ' + stepArray[i + 1]) + obj.params.push(columnName) + obj.params.push(tableName) + obj.params.push(columnName) + obj.params.push(stepArray[i]) + obj.params.push(stepArray[i + 1]) + + } + + } + + //console.log('step spread query', obj); + + return obj; + + } + + getChartQueryAndParamsFromStepArray(tableName, columnName, stepArray, isRange = false) { let obj = {} diff --git a/tests/tests.js b/tests/tests.js index fb8818df10..834e7eb910 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1674,6 +1674,31 @@ describe('xmysql : tests', function () { }); }); + + + it('GET /api/payments/chart?_fields=amount&steppair=0,50000,40000,100000 should PASS', function (done) { + + //post to an url with data + agent.get('/api/payments/chart?_fields=amount&steppair=0,50000,40000,100000') //enter url + .expect(200)//200 for success 4xx for failure + .end(function (err, res) { + + // Handle /api/v error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equals(2) + res.body[0]['_count'].should.be.equals(231) + res.body[1]['_count'].should.be.equals(80) + + return done(); + + }); + }); + + // it('GET /api/xjoin?_join=pl.productlines,_j,pr.products,_j,ord.orderDetails&_on1=(pl.productline,eq,pr.productline)&_on2=(pr.productcode,eq,ord.productcode) should PASS', function (done) { // // //post to an url with data