Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 64
Jko/record events#1
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| Copyright 2014 Catamorphic, Co. | ||
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,27 +2,28 @@ var requestify = require('requestify'); | ||
| var sha1 = require('node-sha1'); | ||
| var util = require('util'); | ||
| var defaultConfig = { | ||
| base_uri : 'https://app.launchdarkly.com' | ||
| }; | ||
| var VERSION = "0.0.3"; | ||
| global.setImmediate = global.setImmediate || process.nextTick.bind(process); | ||
| var VERSION = "0.0.2"; | ||
| var new_client = function(api_key, config) { | ||
| var client = {}; | ||
| if (!config) { | ||
| client.config = defaultConfig; | ||
| } | ||
| else { | ||
| client.config = config; | ||
| } | ||
| config = config || {}; | ||
| client.base_uri = config.base_uri || 'https://app.launchdarkly.com'; | ||
| client.connect_timeout = config.connect_timeout || 2; | ||
| client.read_timeout = config.read_timeout || 10; | ||
| client.capacity = config.capacity || 1000; | ||
| client.flush_interval = config.flush_interval || 5; | ||
| client.api_key = api_key; | ||
| client.queue = []; | ||
| if (!api_key) { | ||
| throw new Error("You must configure the client with an API key"); | ||
| } | ||
| client.api_key = api_key; | ||
| requestify.cacheTransporter({ | ||
| cache: {}, | ||
| @@ -42,26 +43,61 @@ var new_client = function(api_key, config) { | ||
| client.get_flag = function(key, user, default_val, fn) { | ||
| if (!key || !user) { | ||
| send_flag_event(client, key, user, default_val); | ||
| fn(default_val); | ||
| } | ||
| requestify.request(client.config.base_uri + '/api/eval/features/' + key, { | ||
| requestify.request(this.base_uri + '/api/eval/features/' + key, { | ||
| method: "GET", | ||
| headers: { | ||
| 'Authorization': 'api_key ' + client.api_key, | ||
| 'Authorization': 'api_key ' + this.api_key, | ||
| 'User-Agent': 'NodeJSClient/' + VERSION | ||
| } | ||
| }) | ||
| .then(function(response) { | ||
| var result = evaluate(response.getBody(), user); | ||
| if (result == null) { | ||
| send_flag_event(client, key, user, default_val); | ||
| fn(default_val); | ||
| } else { | ||
| send_flag_event(client, key, user, result); | ||
| fn(result); | ||
| } | ||
| }); | ||
| } | ||
| client.track = function(eventName, user, data) { | ||
| var event = {"key": eventName, | ||
| "kind": "custom", | ||
| "creationDate": new Date().getTime()}; | ||
| if (data) { | ||
| event.data = data; | ||
| } | ||
| enqueue(client, event); | ||
| } | ||
| client.flush = function(fn) { | ||
| if (!this.queue.length) { | ||
| process.nextTick(fn); | ||
| } | ||
| requestify.request(this.base_uri + '/api/events/bulk', { | ||
| method: "POST", | ||
| headers: { | ||
| 'Authorization': 'api_key ' + this.api_key, | ||
| 'User-Agent': 'NodeJSClient/' + VERSION | ||
| }, | ||
| body: this.queue | ||
| }) | ||
| .then(function(response) { | ||
| this.queue = []; | ||
| fn(response); | ||
| }); | ||
| } | ||
| setTimeout(client.flush.bind(client), client.flush_interval); | ||
| return client; | ||
| }; | ||
| @@ -70,6 +106,27 @@ module.exports = { | ||
| init: new_client | ||
| } | ||
| function enqueue(client, event) { | ||
| client.queue.push(event); | ||
| if (client.queue.length >= client.capacity) { | ||
| client.flush(); | ||
| } | ||
| } | ||
| function send_flag_event(client, key, user, value) { | ||
| var event = { | ||
| "kind": "feature", | ||
| "key": key, | ||
| "user": user, | ||
| "value": value, | ||
| "creationDate": new Date().getTime() | ||
| } | ||
| enqueue(client, event); | ||
| } | ||
| function param_for_user(feature, user) { | ||
| var idHash, hashKey, hashVal, result; | ||
| @@ -179,9 +236,19 @@ function intersect_safe(a, b) | ||
| var main = function(){ | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm probably missing something here, but should this be in the main published/distributed code? Or should it be in some test package or something? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not exported in the module-- it's just here for convenience. The lines below it: determine whether this is running a script or as a module. In the former case, it calls | ||
| var client = new_client("7f60f21f-0552-4756-ae32-ca65a0c96ca8", {base_uri: "http://localhost:3000"}); | ||
| client.get_flag("engine.enable", {"key": "user@test.com"}, false, function(flag) { | ||
| console.log(flag); | ||
| }); | ||
| for (i = 0; i < 1000; i++) { | ||
| (function(i) { | ||
| client.get_flag("engine.enable", {"key": "user@test.com"}, false, function(flag) { | ||
| console.log("Iteration " + i + ": " +flag); | ||
| if (i == 999) { | ||
| client.flush(function() { | ||
| console.log("Flushing client"); | ||
| }) | ||
| } | ||
| }) | ||
| })(i); | ||
| } | ||
| } | ||
| if (require.main === module) { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested this out yet-- the small main driver doesn't quite exercise this functionality.