# rest-analytics

> Module that provides analytics for REST APIs.

Latest version **0.1.3** (published 2014-12-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install rest-analytics
pnpm add rest-analytics
yarn add rest-analytics
bun add rest-analytics
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2014-12-14 |
| First published | 2014-12-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Fotis Xenikoudakis |
| Maintainers | fxenik |
| Keywords | analytics, rest, api, stats |

## Links

- npm: https://www.npmjs.com/package/rest-analytics
- Repository: https://github.com/fxenik/rest-analytics
- Issues: https://github.com/fxenik/rest-analytics/issues
- npm.io page: https://npm.io/package/rest-analytics

## Dependencies (1)

- [exectimer](https://npm.io/package/exectimer.md) ^0.2.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.1.3 (latest) — 2014-12-14
- 0.1.2 — 2014-12-14
- 0.1.1 — 2014-12-05
- 0.1.0 — 2014-12-01

## README

rest-analytics
==============

A simple node.js module that provides analytics for REST services.

This is still a work in progress and lots of info are still missing from the analytics.

## Usage

Add it to your connect/express application by using it's middleware.

`npm install rest-analytics`

```javascript
// Initialize analytics;
var RestAnalytics = require('rest-analytics');
var analytics = new RestAnalytics();
```

```javascript
// Use it in your express app
var express = require('express');
var app = express();
app.use(analytics.middleware());

```

```javascript
// Get a collection of analytics
analytics.analytics();

// Get a collection of analytics for a specific method/url
analytics.analytics('get', '/users');

```

## Analytics
Currently, analytics per method/path pair contain the following information:

* **count**: Number of calls for a specific method/pair
* **time.min**: Minimum ellapsed time processing a request (in nanoseconds).
* **time.max**: Minimum ellapsed time processing a request (in nanoseconds).
* **time.avg**: Average ellapsed time processing a request (in nanoseconds).

You can get analytics on method/path pairs by calling the analytics method:

```javascript
// read analytics for a specific method/path
var data = analytics.analytics('get', '/user');
console.log(data);

// {
//   'count': ...
//   'time': {
//     'min': ...
//     'max': ...
//     'avg': ...
//   }
// }
```

```javascript
// read analytics for all calls to a path
var data = analytics.analytics('/user');
console.log(data);

// {
//   'get': ...
//   'post': ...
// }
```

```javascript
// read all analytics
var data = analytics.analytics();
console.log(data);

// {
//   '/user': {
//     'get': ...
//     'post': ...
//   },
//   '/company': {
//     'get': ...
//   }
// }
```

## Events

### call

Α `'call'` event will be emitted when a request is completed (after the response is sent). It can be used to integrate collected data with other analytics systems like [Elasticsearch](http://www.elasticsearch.org) or [StatsD](https://github.com/etsy/statsd/).

 The payload of the event will contain information about this specific request:

```javascript
var express = require('express');
var RestAnalytics = require('rest-analytics');

var app = express();
var analytics = new RestAnalytics();
data.analytics.on('call', function(payload) {
    console.log(payload);
});

app.use(analytics.middleware());

```

A payload example json is:

```javascript
{
    "request": {
        "path": "/user",
        "ip": "127.0.0.1",
        "method": "GET",
        "headers": [
            {
                "key": "header",
                "value": "header value"
            }
        ],
        "query": {},
        "parameters": []
    },
    "response": {
        "status": 200,
        "headers": [
            {
                "key": "header",
                "value": "header value"
            }
        ]
    },
    "timestamp": 1418557961891,     // timestamp of call (in ms)
    "duration": 0.40668             // duration of the call (in ms)
}

```

---
_Source: https://npm.io/package/rest-analytics · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
