This JavaScript library provides methods for collecting and aggregating numerical data, useful for analytics, charts, and real-time metrics.
service/statistics/statistics.mak in your project's makefile.config.json and add API announcement and service files:{
"javascript": {
"eval": [
"service/*",
"vendor-appnameservice.js"
]
},
"apis": {
"vendor-appname": {
"com.innovaphone.statistics": {}
}
}
}
To initialize the library, pass an options object specifying table names:
var stats = new Statistics({
datasetsTable: "statistics_datasets",
dataTable: "statistics_data"
});
createDataset(datasetName, type, callback)Creates a dataset if it doesn't already exist.
string – The name of the dataset."sum" or "max" – Aggregation type. Default is "sum".function(obj, callback) – Called when dataset is created or retrieved.
If a callback function is defined, the dataset exists only at runtime and is not saved in the database. In this case, the type parameter is ignored.
Examples:
stats.createDataset("pageViews", "sum");
stats.createDataset("cpuUsage", "max");
stats.createDataset("existingData", null, function(obj, callback){});
insertData(datasetName, value)Inserts a numerical value into the current hour's bucket for the dataset.
string – The dataset name.number – The value to insert.stats.insertData("pageViews", 1);
"customDataFromYourApp" uses a callback to a custom function getCustomData, which you can define within your app service.next property should be set to the timestamp of the next data request.counts property is an array containing the aggregated data. For mode === "live", it typically contains a single value based on the current result.var stats = new Statistics({
datasetsTable: "appname_stat_datasets",
dataTable: "appname_stat_data"
})
.createDataset("usage")
.createDataset("cpu", "max")
.createDataset("customDataFromYourApp", null, getCustomData);
function getCustomData(obj, callback) {
if (obj.mode === "live") {
getCountsLive(obj.start, function (err, result) {
if (err) {
callback(err.error || "Unknown Error");
} else if (result) {
var data = { next: result.next, counts: result.counts };
callback(null, data);
} else {
callback("No Results");
}
});
} else {
getCountsForPeriod(obj.start, obj.end, obj.mode, function (err, result) {
if (err) {
callback(err.error || "Unknown Error");
} else if (result) {
var data = { next: result.next, counts: result.counts };
callback(null, data);
} else {
callback("No Results");
}
});
}
}