Browse Source

refactor : chart gets new querying way steppair

pull/13/head
oof1lab 7 years ago
parent
commit
881921461c
  1. 28
      README.md
  2. 10
      lib/xapi.js
  3. 39
      lib/xsql.js
  4. 25
      tests/tests.js

28
README.md

@ -441,7 +441,7 @@ response body
Chart API returns distribution of a numeric column in a table 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: 1. Chart : With min, max, step in query params :fire::fire:
[:arrow_heading_up:](#api-overview) [: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) [:arrow_heading_up:](#api-overview)
This API figures out even distribution of a numeric column in table and returns the data 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) [:arrow_heading_up:](#api-overview)
This API returns the number of rows where amount is between (0,25000), (0,50000) ... (0,maxValue) 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 /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 /api/payments/chart?_fields=amount&range=1

10
lib/xapi.js

@ -762,6 +762,14 @@ class Xapi {
isRange) 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 { } else {
query = 'select min(??) as min,max(??) as max,stddev(??) as stddev,avg(??) as avg from ??'; 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 let results = await
this.mysql.exec(obj.query, obj.params); this.mysql.exec(obj.query, obj.params);

39
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) { getChartQueryAndParamsFromStepArray(tableName, columnName, stepArray, isRange = false) {
let obj = {} let obj = {}

25
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) { // 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 // //post to an url with data

Loading…
Cancel
Save