appdynamics-analytics-events-api
Version:
AppDynamics Analytics Events API
170 lines (149 loc) • 4.78 kB
JavaScript
/*
* Copyright 2020 Severin Neumann <severin.neumann@appdynamics.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class AnalyticsAPI {
constructor(endpoint, accountName, apiKey, httpClient) {
this.endpoint = endpoint;
this.accountName = accountName;
this.apiKey = apiKey;
this.httpClient = httpClient
}
_http(method, path, payload) {
return new Promise((resolve, reject) => {
const url = new URL(path, this.endpoint)
let headers = {
'X-Events-API-AccountName': this.accountName,
'X-Events-API-Key': this.apiKey
}
if (method === 'POST') {
headers['Content-Type'] = 'application/vnd.appd.events+json;v=2'
headers['Accept'] = 'application/vnd.appd.events+json;v=2'
}
const req = this.httpClient.request(url, {
method,
headers
}, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
if (resp.statusCode >= 400) {
const json = JSON.parse(data)
const error = new Error(json.message)
error.code = json.code
error.statusCode = json.statusCode
reject(error)
} else {
const result = data ? JSON.parse(data) : resp.statusCode
resolve(result)
}
});
}).on("error", (error) => {
reject(error)
});
if(payload) {
req.write(JSON.stringify(payload));
}
req.end();
});
}
retrieveSchema(name) {
// GET http://analytics.api.example.com/events/schema/{schemaName}
return new Promise((resolve, reject) => {
this._http('GET', `/events/schema/${name}`).then(response => {
resolve(new Schema(name, response.schema, this))
}).catch(error => {
reject(error)
})
})
}
retrieveOrCreateSchema(name, definition) {
return new Promise((resolve, reject) => {
this.retrieveSchema(name).then((schema) => {
resolve(schema)
}).catch(error => {
if(error.code !== 'Missing.EventType') {
reject(error)
}
this.createSchema(name, definition).then(resolve).catch(reject)
})
})
}
with(name, definition) {
return this.retrieveOrCreateSchema(name, definition)
}
createSchema(name, definition) {
return new Promise((resolve, reject) => {
this._http('POST', `/events/schema/${name}`, { schema: definition }).catch(error => {
reject(error)
}).then(() => {
resolve(new Schema(name, definition, this))
})
})
}
}
class Schema {
constructor(name, definition, API) {
this.name = name
this.definition = definition
this.API = API
}
publish(events) {
if(!Array.isArray(events)) {
events = [events]
}
return new Promise((resolve, reject) => {
this.API._http('POST', `/events/publish/${this.name}`, events).catch(error => {
reject(error)
}).then((response) => {
resolve(response)
})
})
}
query(query, options = {}) {
const start = options.start ? options.start : Date.now() - (2 * 60 * 60 * 1000)
const end = options.end ? options.end : Date.now()
const limit = options.limit ? options.limit : 100
if(!Array.isArray(query)) {
query = [{
"query": query
}]
}
return new Promise((resolve, reject) => {
this.API._http('POST', `/events/query?limit=${limit}&start=${start}&end=${end}`, query).then(response => {
resolve(response)
}).catch(error => {
reject(error)
})
})
}
delete() {
return new Promise((resolve, reject) => {
this.API._http('DELETE', `/events/schema/${this.name}`).then(response => {
resolve(response)
}).catch(error => {
reject(error)
})
})
}
}
exports.connectAnalyticsAPI = function(endpoint, accountName, apiKey) {
// Add verification
const urlEndpoint = new URL(endpoint)
return new AnalyticsAPI(urlEndpoint, accountName, apiKey, urlEndpoint.protocol == 'https:' ? require('https') : require('http'))
}