From e5ed663c4c1b49385121e133c8f1e16f2967adc5 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Tue, 17 Oct 2017 22:14:12 +0300 Subject: [PATCH] added graph examples --- .docker/kibana/Dockerfile | 2 + routes/graphs.js | 61 ++++++++++++++- services/productGraphService.js | 128 ++++++++++++++++++++++++++++++++ views/graphs.twig | 107 +++++++++++++++++++++++++- 4 files changed, 294 insertions(+), 4 deletions(-) create mode 100644 services/productGraphService.js diff --git a/.docker/kibana/Dockerfile b/.docker/kibana/Dockerfile index ebf09b7..d4588c4 100644 --- a/.docker/kibana/Dockerfile +++ b/.docker/kibana/Dockerfile @@ -2,4 +2,6 @@ FROM docker.elastic.co/kibana/kibana:5.6.2 ENV ELASTICSEARCH_URL "http://elasticsearch:9200" +RUN bin/kibana-plugin remove x-pack + EXPOSE 5601:5601 \ No newline at end of file diff --git a/routes/graphs.js b/routes/graphs.js index daf80af..0234d2f 100644 --- a/routes/graphs.js +++ b/routes/graphs.js @@ -1,8 +1,67 @@ var express = require('express'); var router = express.Router(); +var productGraphService = require('../services/productGraphService'); +var waterfall = require('async/waterfall'); + +var dynamicColors = function() { + var r = Math.floor(Math.random() * 255); + var g = Math.floor(Math.random() * 255); + var b = Math.floor(Math.random() * 255); + return "rgb(" + r + "," + g + "," + b + ")"; +}; + router.get('/', function(req, res, next) { - res.render('graphs'); + + waterfall( + [ + function(waterfallCallback) { + productGraphService.getProductCountByDate({}, function(err, result) { + if(err) { waterfallCallback(true, {}); } + var colors = []; + for (var i in result['vals']) { + colors.push(dynamicColors()); + } + waterfallCallback(false, {"chart1": {'labels':'"' + result['keys'].join('","') + '"', 'datasets': [ {'data': result['vals'].join(','), 'label': 'Num of Product', 'colors': '"'+colors.join('","')+'"'} ]}}); + return; + }); + }, + function(data, waterfallCallback) { + productGraphService.getProductQuantities({}, function(err, result) { + if(err) { waterfallCallback(true, {}); } + var colors = []; + for (var i in result['vals']) { + colors.push(dynamicColors()); + } + data['chart2'] = {'labels':'"' + result['keys'].join('","') + '"', 'datasets': [ {'data': result['vals'].join(','), 'label': 'Total Product Quantity', 'colors': '"'+colors.join('","')+'"'}, {'data': result['counts'].join(','), 'label': 'Num of Product'} ]}; + waterfallCallback(false, data); + return; + }); + }, + function(data, waterfallCallback) { + productGraphService.getCategoriyQuantitySum({}, function(err, result) { + if(err) { waterfallCallback(true, {}); } + var colors1 = []; + var colors2 = []; + for (var i in result['vals']) { + colors1.push(dynamicColors()); + } + + for (var i in result['counts']) { + colors2.push(dynamicColors()); + } + data['chart3'] = {'labels':'"' + result['keys'].join('","') + '"', 'datasets': [ {'data': result['vals'].join(','), 'label': 'Total Quantity of Category', 'colors': '"'+colors1.join('","')+'"' }, {'data': result['counts'].join(','), 'label': 'Num of Product by Category', 'colors': '"'+colors2.join('","')+'"'} ]}; + waterfallCallback(false, data); + return; + }); + } + ], + function(err, data) { + if(err) { res.send(500,"Server Error"); return; } + console.log(data); + res.render('graphs', data); + } + ); return; }); diff --git a/services/productGraphService.js b/services/productGraphService.js new file mode 100644 index 0000000..0d757f5 --- /dev/null +++ b/services/productGraphService.js @@ -0,0 +1,128 @@ +var db = require('./../libraries/elasticsearch'); + +exports.getProductCountByDate = function(params, callback) { + body = { + "size": 0, + "aggs": { + "product_counts": { + "date_histogram": { + "field": "created_at", + "interval": "week" + } + } + } + }; + + db.search({ + index: 'products', + type: 'product', + body: body + }).then(function (resp) { + var aggs = resp.aggregations.product_counts.buckets; + var result = { + "keys": [], + "vals": [] + }; + for (i in aggs) { + result['keys'].push(aggs[i].key_as_string); + result['vals'].push(aggs[i].doc_count); + } + callback(false, result); + return; + }, function (err) { + callback(true); + console.trace(err.message); + return; + }); +}; + +exports.getCategoriyQuantitySum = function(params, callback) { + body = { + "size": 0, + "aggs": { + "product_categories": { + "terms": { + "field": "categories.id", + "size": 10 + }, + "aggs": { + "sum": { + "sum": { + "field": "quantity" + } + } + } + } + } + }; + + db.search({ + index: 'products', + type: 'product', + body: body + }).then(function (resp) { + var aggs = resp.aggregations.product_categories.buckets; + var result = { + "keys": [], + "vals": [], + "counts": [] + }; + for (i in aggs) { + result['keys'].push(aggs[i].key); + result['counts'].push(aggs[i].doc_count); + result['vals'].push(aggs[i].sum.value); + } + callback(false, result); + return; + }, function (err) { + callback(true); + console.trace(err.message); + return; + }); +}; + + +exports.getProductQuantities = function(params, callback) { + body = { + "size": 0, + "aggs": { + "product_counts": { + "date_histogram": { + "field": "created_at", + "interval": "week" + }, + "aggs": { + "product_quantities": { + "sum": { + "field": "quantity" + } + } + } + } + } + }; + + db.search({ + index: 'products', + type: 'product', + body: body + }).then(function (resp) { + var aggs = resp.aggregations.product_counts.buckets; + var result = { + "keys": [], + "vals": [], + "counts": [] + }; + for (i in aggs) { + result['keys'].push(aggs[i].key_as_string); + result['counts'].push(aggs[i].doc_count); + result['vals'].push(aggs[i].product_quantities.value); + } + callback(false, result); + return; + }, function (err) { + callback(true); + console.trace(err.message); + return; + }); +}; \ No newline at end of file diff --git a/views/graphs.twig b/views/graphs.twig index 4c74c20..7cdad47 100644 --- a/views/graphs.twig +++ b/views/graphs.twig @@ -1,8 +1,109 @@ {% extends 'layout.twig' %} {% block body %} -
-

From Kibana

- +
+
+
+
+ +
+
+ +
+
+ +
+
+ + + {% endblock %}