DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Visualization Dashboard With Couchbase Analytics

Visualization Dashboard With Couchbase Analytics

In the world of Big Data, where we have gogolbytes of information available about all aspects of our applications and the users that are interacting with them, being able to analyze all of this data and provide insightful intelligence is paramount.

Brett Lawson user avatar by
Brett Lawson
·
Nov. 29, 16 · Opinion
Like (1)
Save
Tweet
Share
5.49K Views

Join the DZone community and get the full member experience.

Join For Free

In the world of Big Data, where we have gogolbytes of information available about all aspects of our applications and the users that are interacting with them, being able to analyze all of this data and provide insightful intelligence is paramount.  At Couchbase, we are working to build data management platforms that let you take advantage of this data.  This will then eventually be accessible from a variety of industry-standard analysis platforms like Tableau, QlikView and Microstrategy as well as next generation NoSQL analytics platforms such as Cloud9 and SlamData, however it is also important that developers have APIs to access the systems that store and analyze this data.  With this level of direct access, these developers have the flexibility to build everything from simple dashboards to complete custom reporting for their users.

As of version 2.2.4 of our Node.js SDK, we now have experimental support for performing Couchbase Analytics queries against a cluster.  The API is very similar to the existing APIs available for performing N1QL queries in the Node.js SDK.  

// Connect to the local cluster and enable experimental Couchbase Analytics support.
var cluster = new couchbase.Cluster('couchbase://localhost');
cluster.enableCbas(['localhost:8095']);

// Perform a simple query, selecting all users with a specific email.
var qs = 'SELECT * FROM users WHERE email="tom@example.com"';
var q = couchbase.CbasQuery.fromString(qs);
cluster.query(q, function(err, res) {
 if (err) {
    throw err;
 }

 console.log(res);
});

As you might notice, the difference between our N1QL APIs and the Couchbase Analytics APIs is that with Couchbase Analytics you must perform your queries at the cluster-level and use CbasQuery to instantiate your query object.

Using these new APIs, we have also built a new dashboard demonstration application which pulls together a few well-known libraries in the Node.js world to show how you might turn these new APIs into a useful interface that can display business-specific metrics that are useful to you and your colleagues.

How the Dashboard Works

The dashboard we have put together consists of two separate components.  First, a server-side component which is running the Node.js SDK and provides a restful interface.  Second,  a client-side component which is a simple HTML5 page.  The server-side component will serve the client-side component from the root of its web-service to make things easier.

The client-side of the application is a simple HTML and Javascript page which contains a small form allowing you to specify some parameters, a large text section which displays all the queries which have been executed against your Couchbase Analytics cluster, as well as 3 graphs which are used to present the data from your queries.  When the page loads, or when you click the update button, we dispatch requests to the server-side application requesting various pieces of information about our travel-sample data.  When a reply is received from the server, we then pass this information to Chart.js which generates our graphs.

Screen+Shot+2016-11-01+at+11.20.39+AM.png

The code for this is pretty straightforward.

$.get('/pricestats?' + formData)
.done(function(data) {
  addNarration(data.narration);

  var info = data.data;

  config1.data.labels = [];
  config1.data.datasets[0].data = [];

  for (var i = 0; i < info.length; ++i) {
      config1.data.labels.push(info[i].grpdate);
      config1.data.datasets[0].data.push(info[i].metric);
  }

  myChart1.update();
}).fail(function(err) {
  addNarrationText(err.toString());
});

It simply  uses Express to start a web-server which responds to any root requests with the client-side page for this application.  It additionally registers a couple of REST endpoints which the application uses to update its graphs.  Each of these REST endpoints front-ends a SQL++ query against the Couchbase Analytics cluster and then returns the results of that query back to the client-side of the application to be graphed.

var qs =
  'SELECT ' +
  '  parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") AS grpdate, ' +
  'SUM(e.price) AS metric ' +
  'FROM users u ' +
  '  UNNEST u.flights e ' +
  'WHERE parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") >= parse_date("2016-10-01", "Y-M-D") ' +
  '  AND parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") < parse_date("2016-11-01", "Y-M-D") ' +
  'GROUP BY grpdate ' +
  'ORDER BY grpdate;';

var q = couchbase.CbasQuery.fromString(qs);
cluster.query(q, function(err, qres) {
if (err) {
  res.status(400).send(err);
  return;
}

res.send({
  data: qres,
  narration: [
    ['sql', qs]
  ]
});
});

In addition to the RESTful service provided by the server-side application, we have additionally included automatic creation of the necessary Couchbase Analytics shadow buckets for the travel sample and  inserting 1000 entries worth of randomly generated flight purchase data to give us something interesting to look at.

var qs =
  'CREATE BUCKET tsBucket WITH {"name":"travel-sample","nodes":"127.0.0.1"};' +
  'CREATE SHADOW DATASET airlines ON tsBucket WHERE `type` = "airline";' +
  'CREATE SHADOW DATASET airports ON tsBucket WHERE `type` = "airport";' +
  'CREATE SHADOW DATASET hotels ON tsBucket WHERE `type` = "hotel";' +
  'CREATE SHADOW DATASET landmarks ON tsBucket WHERE `type` = "landmark";' +
  'CREATE SHADOW DATASET routes ON tsBucket WHERE `type` = "route";' +
  'CREATE SHADOW DATASET users ON tsBucket WHERE `type` = "user";' +
  'CONNECT BUCKET tsBucket WITH {"password":""};';


var q = couchbase.CbasQuery.fromString(qs);
cluster.query(q, function(err, res) {
 // We ignore errors here since they are usually just
 //  'dataset already exists'.
 callback(null);
});

Getting the Example Dashboard

Getting started with our new demonstration is as simple as visiting is GitHub repo which is located at:https://github.com/couchbaselabs/node-cbasdashdemo and following the Getting Started guide available in the included README.

Getting the Node.js Release

As with all of our Node.js releases, we have published this version to NPM and it can be installed with a simple invocation of npm:

npm install Couchbase@2.2.4.

We also ship prebuilt binaries as part of our npm release process, allow you to get started with the Node.js SDK without any further hassles (note that Node.js v7.0.0 does not currently have prebuilt binaries of the Couchbase module available and requires a working compiler to install).

Analytics Dashboard (Mac OS) application Node.js Data (computing) Database Visualization (graphics)

Published at DZone with permission of Brett Lawson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Understanding gRPC Concepts, Use Cases, and Best Practices
  • How To Use Terraform to Provision an AWS EC2 Instance
  • ChatGPT Prompts for Agile Practitioners
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: