Permalink
Browse files

Changes to add drop off message

  • Loading branch information...
1 parent f6ce27b commit ebe48d6ec9684ea20f6dd4db4b7c9b0f6c136e3f @pingles committed May 15, 2010
Showing with 21 additions and 13 deletions.
  1. +1 −1 beanstalk.rb
  2. +11 −4 src/redirectly_realtime/beanstalk_sample.clj
  3. +4 −0 web/scripts/ws.js
  4. +5 −8 web/server/tweeamer.js
View
2 beanstalk.rb
@@ -33,6 +33,6 @@
}
}
-50.times do
+1500.times do
beanstalk.put(click.to_json)
end
View
15 src/redirectly_realtime/beanstalk_sample.clj
@@ -19,12 +19,20 @@
"Called once with each new event handled by Esper."
[event]
(beanstalk/post-message (keyword-count-message event) (beanstalk/publisher "interesting")))
+
+(defn drop-off-message
+ "Creates message with keyword and associated data"
+ [event]
+ (let [the-keyword (.get event "keyword") clicks-count (.get event "cnt") avg-count (.get event "avgCnt")]
+ {"eventType" "CountDropOff" "keyword" the-keyword "clicks" clicks-count "avgClicks" avg-count}))
(defn log-drop-off
"Called when a drop-off in count is detected."
[event]
- (let [message (format "%s %s received in last 10 seconds, average was %s" (.get event "cnt") (.get event "keyword") (.get event "avgCnt"))]
- (beanstalk/post-message {"keyword" message} (beanstalk/publisher "interesting"))))
+ (let [message (drop-off-message event)]
+ (do
+ (println (str "Drop-off detected: " message))
+ (beanstalk/post-message message (beanstalk/publisher "interesting")))))
(def clicks-per-second-statement "
insert into ClicksPerSecond
@@ -50,12 +58,11 @@
(println (str "ClickEvent received: " click-event))
(esper/send-event click-event "ClickEvent"))))
-; (esper/send-event (click-event message) "ClickEvent")
-
(defn run-client
[]
(do
(esper/attach-listener (esper/create-statement clicks-per-second-statement) (esper/create-listener log-count))
+ (esper/attach-listener (esper/create-statement clicks-dropoff-statement) (esper/create-listener log-drop-off))
(beanstalk/listen-to handler (beanstalk/consumer "clicks"))))
(defn -main
View
4 web/scripts/ws.js
@@ -105,6 +105,10 @@ var TwitterStream = (function(stream, $) {
$('<li class="keyword-count"><dl><dt class="keyword">Keyword</dt><dd class="keyword">' + parsed.keyword + '</dd><dt class="clicks">Clicks (last 10 seconds)</dt><dd class="clicks">' + parsed.clicks + '</dd></dl></li>').hide().prependTo('#outputarea ul').fadeIn();
}
+ if (parsed.eventType == "CountDropOff") {
+ $('<li class="drop-off"><span class="clicks">' + parsed.clicks + '</span> clicks for <span class="keyword">' + parsed.keyword + '</span> less than the average (' + parsed.avgClicks + ') over the last 10 seconds.</li>').hide().prependTo('#outputarea ul').fadeIn();
+ }
+
if ($("#outputarea > ul > li").length > MAX_ITEMS) {
$('li:last', $('#outputarea ul')).remove();
}
View
13 web/server/tweeamer.js
@@ -6,20 +6,17 @@ var bt = require('./beanstalk_client');
var connectedClients = [];
-var sendToClients = function(data) {
- for (var i=0; i < connectedClients.length; i++) {
- connectedClients[i].write(data)
- };
-}
-
bt.Client.connect('127.0.0.1:11300').addCallback(function(conn) {
conn.watch('interesting');
var messageHandler = function(job_id, job_json) {
var parsed = JSON.parse(job_json);
- sendToClients(job_json)
- sys.puts('got job: ' + job_id + ' keyword: ' + parsed.keyword);
+ sys.puts('I: ' + job_json);
+
+ for (var i=0; i < connectedClients.length; i++) {
+ connectedClients[i].write(job_json)
+ };
conn.destroy(job_id);
conn.reserve().addCallback(messageHandler);

6 comments on commit ebe48d6

@amitrathore

Hey Paul...!

Question - where is your ClickEvent class defined?

Regards,
Amit.

@pingles
Owner

Hi Amit,

It's actually defined in esper.clj (apologies for the messiness of all this- I did it a few months ago as a bit of a tech spike). It's defined using Esper's Configuration.addEventType method: it associates a properties map with the stuff it's expecting to set on the event type.

(def click-properties
(doto (Properties.)
(.put "keyword" "string")))

(def configuration
(doto (Configuration.)
(.addEventType "ClickEvent" click-properties)))

@amitrathore

Yes, I figured it out. Thanks for replying.

Esper looks rather interesting... I'm spiking something myself, for use at work. Did you end up using it in production?

@pingles
Owner

We haven't, at least not yet. But it's definitely something I've been meaning to go back to- even if it's just for application health monitoring!

@amitrathore

That's exactly what I'm looking at it for... starting with some business-level events to monitor the health of the system at a business level...

Please do keep us posted of your exploits with this, through blogs or some such... take care, say hi to Fred and the rest!

@pingles
Owner

Thanks Amit, will do!

Please sign in to comment.