-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
125 lines (115 loc) · 4.48 KB
/
index.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Reporter for storing data in ElasticSearch
*
* --reporter elasticsearch
* --elasticsearch-host localhost
* --elasticsearch-port 9200
* --elasticsearch-index "myapp"
* --elasticsearch-type "phantomas-report"
*
* Debugging:
* curl -s -XGET 'http://localhost:9200/phantomas/report/<id>/_source'
*
* Options:
* <host>:<port>:<index>:<type>
*/
'use strict';
module.exports = function (results, reporterOptions, options) {
var debug = require('debug')('phantomas:reporter:elasticsearch'),
params;
// -R elasticsearch:<host>:<port>:<index>:<type>
if (reporterOptions.length > 0) {
options['elasticsearch-host'] = reporterOptions[0];
options['elasticsearch-port'] = reporterOptions[1];
options['elasticsearch-index'] = reporterOptions[2];
options['elasticsearch-type'] = reporterOptions[3];
}
params = {
host: (options['elasticsearch-host'] || 'localhost') + ':' + (options['elasticsearch-port'] || 9200),
type: (options['elasticsearch-type'] || 'report'),
index: (options['elasticsearch-index'] || 'phantomas')
};
debug('Parameters: %j', params);
// public API
return {
render: function (done) {
var elasticsearch = require('elasticsearch'),
client = new elasticsearch.Client({
host: params.host
}),
metrics = results.getMetricsNames(),
documentBody = {
url: results.getUrl(),
reportDate: new Date()
},
mappingFields = {
url: {
type: 'keyword',
index: true
},
reportDate: {
type: 'date'
}
};
// create and index an elasticsearch document with metrics data
function indexReport(documentBody) {
client.index({
index: params.index,
type: params.type,
body: documentBody
}, function (error, data) {
if (typeof error != "undefined") {
done(new Error('Indexing error: ' + error));
} else {
debug('Stored under id %s', data._id);
}
done(undefined, {id: data._id});
});
}
// store metrics value and mapping types
metrics.forEach(function (metric) {
var value = results.getMetric(metric);
documentBody[metric] = value;
mappingFields[metric] = {
type: (isNaN(value) ? 'text' : 'integer')
};
});
client.indices.exists({
index: params.index
}, function (err, exists) {
if (typeof(err) == "undefined") {
// index does not exists, we have to create it and define the mapping
if (!exists) {
client.indices.create({
index: params.index
}, function (err) {
if (typeof(err) == "undefined") {
var mapping = {};
mapping[params.type] = {
properties: mappingFields
};
client.indices.putMapping({
type: params.type,
index: params.index,
body: mapping
}, function (err, data) {
if (typeof err == "undefined") {
indexReport(documentBody);
} else {
done(new Error('Create mapping error: ' + err));
}
});
} else {
done(new Error('Create index error: ' + err));
}
});
} else {
indexReport(documentBody);
}
} else {
done(new Error('Index exists check error:' + err));
}
});
}
};
};