1- /**
2- * This class encapsulates the Logmet client code, used to query data from Logmet.
3- */
4-
5- var https = require ( 'https' ) ;
6-
7- var logger = require ( './logger' ) ;
8-
9-
10- /*
11- * @constructor
12- * @this {LogmetConsumer}
13- *
14- * @param {string } logmetQueryEndpoint The hostname or IP address of the Logmet server used for querying
15- *
16- */
17- function LogmetConsumer ( logmetQueryEndpoint ) {
18- this . endpoint = logmetQueryEndpoint ;
19- }
20-
21- //Export the constructor
22- module . exports = LogmetConsumer ;
23-
24-
25- /*******************************************
26- * Public Interface of LogmetClient
27- *******************************************/
28-
29- /*
30- * Queries Logmet for data.
31- *
32- * @param {string } tenantId The id of the tenant who owns the data
33- * @param {string } bearerToken The bearer token of the tenant
34- * @param {string } type The type associated with the Elasticsearch documents to be searched for
35- * @param {object } queryDSLBody An object representing a query expressed in terms of the Elasticsearch query DSL
36- * @param {function(error, data) } callback A callback function used to process errors or the data returned by the query
37- *
38- * @return An array of objects, where each object corresponds to an Elasticsearch document
39- */
40- LogmetConsumer . prototype . query = function ( tenantId , bearerToken , type , queryDSLBody , callback ) {
41- var validatedBearerToken = validateToken ( bearerToken ) ;
42- var path = '/elasticsearch/logstash-' + tenantId + '-*/' + type + '/_search' ;
43- var queryOptions = {
44- hostname : this . endpoint ,
45- path : path ,
46- headers : {
47- 'Accept' : 'application/json' ,
48- 'X-Auth-Token' : validatedBearerToken ,
49- 'X-Auth-Project-Id' : tenantId
50- } ,
51- method : 'POST'
52- } ;
53- var queryBodyJSONString = JSON . stringify ( queryDSLBody )
54-
55- logger . info ( 'Performing Logmet query' , { tenant_id : tenantId , doc_type : type , query_body : queryBodyJSONString } ) ;
56-
57- var retrievedString = '' ;
58- var retrievedObject ;
59-
60- var request = https . request ( queryOptions , function ( result ) {
61- result . on ( 'data' , function ( chunk ) {
62- logger . debug ( 'Received a chunk of Logmet data: ' + chunk ) ;
63- retrievedString += chunk ;
64- } ) ;
65-
66- result . on ( 'end' , function ( ) {
67- if ( retrievedString ) {
68- retrievedObject = JSON . parse ( retrievedString ) ;
69- callback ( '' , retrievedObject . hits . hits ) ;
70- } else {
71- callback ( '' , [ ] ) ;
72- }
73- } ) ;
74- } ) ;
75- request . write ( queryBodyJSONString ) ;
76- request . end ( ) ;
77-
78- request . on ( 'error' , function ( error ) {
79- logger . warn ( 'ERROR returned by Logmet query: ' + error ) ;
80- callback ( error ) ;
81- } ) ;
82- } ;
83-
84-
85- /*******************************************
86- * Module's private functions
87- *******************************************/
88-
89- function validateToken ( bearerToken ) {
90- var validatedToken = bearerToken ;
91- if ( bearerToken . startsWith ( 'bearer ' ) ) {
92- validatedToken = bearerToken . replace ( 'bearer ' , '' ) ;
93- }
94- return validatedToken ;
1+ /**
2+ * This class encapsulates the Logmet client code, used to query data from Logmet.
3+ */
4+
5+ var https = require ( 'https' ) ;
6+
7+ var logger = require ( './logger' ) ;
8+
9+
10+ /*
11+ * @constructor
12+ * @this {LogmetConsumer}
13+ *
14+ * @param {string } logmetQueryEndpoint The hostname or IP address of the Logmet server used for querying
15+ *
16+ */
17+ function LogmetConsumer ( logmetQueryEndpoint ) {
18+ this . endpoint = logmetQueryEndpoint ;
19+ }
20+
21+ //Export the constructor
22+ module . exports = LogmetConsumer ;
23+
24+
25+ /*******************************************
26+ * Public Interface of LogmetClient
27+ *******************************************/
28+
29+ /*
30+ * Queries Logmet for data.
31+ *
32+ * @param {string } tenantId The id of the tenant who owns the data
33+ * @param {string } bearerToken The bearer token of the tenant
34+ * @param {string } type The type associated with the Elasticsearch documents to be searched for
35+ * @param {object } queryDSLBody An object representing a query expressed in terms of the Elasticsearch query DSL
36+ * @param {function(error, data) } callback A callback function used to process errors or the data returned by the query
37+ *
38+ * @return An array of objects, where each object corresponds to an Elasticsearch document
39+ */
40+ LogmetConsumer . prototype . query = function ( tenantId , bearerToken , type , queryDSLBody , callback ) {
41+ var validatedBearerToken = validateToken ( bearerToken ) ;
42+ var path = '/elasticsearch/logstash-' + tenantId + '-*/' + type + '/_search' ;
43+ var queryOptions = {
44+ hostname : this . endpoint ,
45+ path : path ,
46+ headers : {
47+ 'Accept' : 'application/json' ,
48+ 'X-Auth-Token' : validatedBearerToken ,
49+ 'X-Auth-Project-Id' : tenantId
50+ } ,
51+ method : 'POST'
52+ } ;
53+ var queryBodyJSONString = JSON . stringify ( queryDSLBody ) ;
54+
55+ logger . info ( 'Performing Logmet query' , { tenant_id : tenantId , doc_type : type , query_body : queryBodyJSONString } ) ;
56+
57+ var retrievedString = '' ;
58+ var retrievedObject ;
59+
60+ var request = https . request ( queryOptions , function ( result ) {
61+ result . on ( 'data' , function ( chunk ) {
62+ logger . debug ( 'Received a chunk of Logmet data: ' + chunk ) ;
63+ retrievedString += chunk ;
64+ } ) ;
65+
66+ result . on ( 'end' , function ( ) {
67+ if ( retrievedString ) {
68+ retrievedObject = JSON . parse ( retrievedString ) ;
69+ callback ( '' , retrievedObject . hits . hits ) ;
70+ } else {
71+ callback ( '' , [ ] ) ;
72+ }
73+ } ) ;
74+ } ) ;
75+ request . write ( queryBodyJSONString ) ;
76+ request . end ( ) ;
77+
78+ request . on ( 'error' , function ( error ) {
79+ logger . warn ( 'ERROR returned by Logmet query: ' + error ) ;
80+ callback ( error ) ;
81+ } ) ;
82+ } ;
83+
84+
85+ /*******************************************
86+ * Module's private functions
87+ *******************************************/
88+
89+ function validateToken ( bearerToken ) {
90+ var validatedToken = bearerToken ;
91+ if ( bearerToken . startsWith ( 'bearer ' ) ) {
92+ validatedToken = bearerToken . replace ( 'bearer ' , '' ) ;
93+ }
94+ return validatedToken ;
9595}
0 commit comments