How to build Easy Stats with LoopBack (Part 1)

I bet you have been in that situation when a customer request you to build statistical information from a system you have built, many times developers would easily get scared of that situation, it may seems to be difficult and yes sometimes it is, but this is about to change.

It has been a while since I wrote my previous post, but truth is…. I don’t want to write because of the sake of write, instead I have been working in multiple projects where I quickly found that I needed to deliver statistical information in 3 of the 4 projects I’m involved! so I really needed to build something that could work in these 3 projects but in fact that could work in any other project.

Presenting loopback-stats-mixin

So after some hours of analysis, effort and coffee I built a mixin that beautifully works with StrongLoop’s LoopBack Framework, I created some documentation and published under the name of loopback-stats-mixin in npm, but it would not be complete if I did not write a blog with an actual example of use.

Being honest… This simple but powerful tool can do for us more than any can think, but first lets try with a simple example on this part of my series, then we can do more sophisticated use of it in my next post.

StrongLoop’s LoopBack Framework

For those who are not aware of what LoopBack is I will quickly talk about it.
LoopBack is NodeJS Framework built by StrongLoop a company recently acquired by IBM, it may not seems very fancy at the beginning but the truth is these guys have done a really good job, once you get to know it you can build reliable and scalable RESTful API Services.

Project Description

In this tutorial we are going to build a RESTful API in LoopBack that provides the ability to create Orders and Statistical information over these.

This project assumes that you have previous knowledge in LoopBack since I won’t go in details about basic loopback functionality, but since we are going to build a really basic API, any developer that follow the tutorial should be able to get this working.

Articles Index

  • Part 1: Setting up the REST API.
  • Part 2: Creating Stats Endpoints.
  • Part 3: Setting up Angular 2 App.
  • Part 4: Build Chart Component
  • Bonus: Bulding a Real-Time Chart Application with [FireLoop]

Demo API

I have published a demo API in github so you can see the code we will create in the following tutorial (Easy Stats on GitHub)

Prerequisites

In order for you to follow this tutorial, I’m assuming you already have the minimum required knowledge of npm just to install some of the dependencies.

You will need to have Node and NPM installed before you install the tools:

1
npm install -g strongloop

Installation

We will create a LoopBack Application as we normally would by creating a project folder and then running the $ slc loopback command

1
2
3
$ mkdir easy-stats-api
$ cd easy-stats-api
$ slc loopback // Hit return on request

Dependency Installation

Now we need to install loopback-stats-mixin from npm

1
$ npm install --save loopback-stats-mixin

And configure your server/model-config.js file as stated here.

We are also going to need to timestamp when we create our instances, so we can create statistical information over a time line.

1
$ npm install --save loopback-ds-timestamp-mixin

And configure your server/model-config.js file as stated here.

Create a Model

For this project we are going to build a model that will extend from PersistedModel.

1
$ slc loopback:model Order

For now do not setup any property so hit return when properties are requested.

Configure Timestamp Mixin

You will need to configure the loopback-ds-timestamp-mixin in the model json file.

1
2
3
4
5
{
"mixins": {
"TimeStamp": true
}
}

Note. Newly created models does not have a mixins section within the model.json file, therefore you will need to create that section the first time.

Configure Stats Mixin

You will need to configure the loopback-stats-mixin in the model json files as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"mixins": {
"Stats": {
"method": "stats",
"endpoint": "/stats",
"type": "model",
"count": {
"on": "createdAt",
"by": "index"
}
}
}
}

The final configuration in the Order.json file should looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"mixins": {
"TimeStamp": true,
"Stats": {
"method": "stats",
"endpoint": "/stats",
"type": "model",
"count": {
"on": "createdAt",
"by": "index"
}
}
}
}

And we are ready to GO!

For incredible that it may seems, that is everything we need to do in order to have a perfectly usable API with statistical functionality, I know I did not talk about creating data sources to persist data, so for now everything will be stored in memory but as I said before, this tutorial does not cover StrongLoop’s LoopBack Framework basic generic setup, if you feel you need to understand more about what we have just done, then I recommend you to check the Getting Started documentation.

Verify Endpoints

Now we can verify everything is working by running our API as follows:

1
2
3
$ slc run
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer

Now if we load the API Explorer into a browser by using the http://0.0.0.0:3000/explorer url we will be able to see a set of endpoints under the Order category, open the [POST] /orders section and click the Try it out! button several times.

After you have successfully created many instances of Orders, now you can get basic statistical information about Orders by using [GET] /orders/stats and adding a valid range, for this example use weekly as range parameter, and you will receive a 5 weeks result json with the number of instances created in the last day corresponding the number of new instances created by you, example.

[GET] /orders/stats?range=weekly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[
{
"date": "2016-03-16T19:18:33.377Z",
"universal": 1458155913377,
"count": 0
},
{
"date": "2016-03-23T19:18:33.377Z",
"universal": 1458760713377,
"count": 0
},
{
"date": "2016-03-30T19:18:33.377Z",
"universal": 1459365513377,
"count": 0
},
{
"date": "2016-04-06T18:18:33.377Z",
"universal": 1459966713377,
"count": 0
},
{
"date": "2016-04-13T18:18:33.377Z",
"universal": 1460571513377,
"count": 8
}
]

Awesome, you should be able to play the ranges and see the results with the minimum effort.

What is next?

In the next part I will talk about much more advanced use of this mixin, then we will create a couple of front end applications to display the data in both Angular 1 and Angular 2

Thanks for reading.


Comments:

...