Created
August 11, 2015 17:25
-
-
Save tashian/c29fd3d15f73897a5df7 to your computer and use it in GitHub Desktop.
A node-based HTTP API query buffer for Keen.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KeenIO = require 'keen-js' | |
Q = require 'q' | |
class KeenClient | |
constructor: -> | |
@client = new KeenIO( | |
projectId: process.env.KEEN_PROJECT_ID | |
readKey: process.env.KEEN_READ_KEY | |
) | |
@started = false | |
@queries = [] | |
start: -> | |
unless @started | |
setInterval => | |
if @queries.length > 0 | |
[query, cb] = @queries.shift() | |
@client.run(query, cb) | |
, 1000 | |
@started = true | |
schedule: (query, cb) -> | |
@queries = @queries.concat [[query, cb]] | |
scheduleWithPromise: (query) -> | |
deferred = Q.defer() | |
@schedule(query, (err, response) -> | |
if err | |
console.log "Query error #{err}" | |
deferred.reject(err) | |
else | |
deferred.resolve(response) | |
) | |
deferred.promise | |
KeenClient.Query = KeenIO.Query | |
module.exports = KeenClient |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment