Tuesday, January 25, 2011

Blog Post: Running Totals

To my complete and utter surprise, I discovered that articles on my blog do not write themselves magically after I started with the first one! Nobody told me and I was convinced that a blog just grows like a weed, but, alas, I actually need to put some work into it. So here we go…

The StreamInsight team recently posted about the behavior of snapshot windows and today, I’d like to to talk about a nice query pattern that highlights the versatility of this window type. The business question is this:

Compute the sum of some payload value within one day, but update the result throughout the day.

Or graphically:

image

 

Now, up to the comma in this sentence, one would assume that this implies a tumbling window with a length of one day. But such a window would only produce a result after it has ended, hence we would only see the sum once, after the day has passed (i.e., a CTI has been received with a timestamp past midnight). This computation needs to be event-driven: We want to see a result whenever it changes, or in other words, whenever we receive new input that causes the sum within that day to change. We call this pattern “running totals”. We still need a window, of course, since sum is an aggregate, and aggregates require windows that define the sets to aggregate on. Here comes the snapshot window: it is event-based by nature, as opposed to the timeline-based hopping/tumbling windows. The remaining question is now how to prepare the original event stream for the snapshot window so that we get the desired result. Let’s think about that for a moment: At each point throughout the day, we want to compute the sum over all events that happened so far. The aggregate function needs to be able to “see” every event that happened so far on that day, until midnight, when we want to reset again. From this insight it’s only a small step to the solution: We have to extend the duration of each event until the following midnight, and then run the snapshot+aggregation over that modified stream. Here is a figure to illustrate this behavior:

image

 

Feeding this interval-shaped result into a point output adapter will yield the result displayed in the first diagram, as soon as a new sum is available.

The trickiest part when formalizing this query in LINQ is to find the proper expression that extends the events as needed. There are various ways to do this, here is a really simple one:

  1. var result = from win in source
  2.                  .AlterEventDuration(e => TimeSpan.FromHours(24).Subtract(e.StartTime.TimeOfDay))
  3.                  .SnapshotWindow(SnapshotWindowOutputPolicy.Clip)
  4.              select new { runningTotal = win.Sum(e => e.Value) };

 

However, if I want to change the resetting period from one day to, say, one hour, I need to change the TimeSpan methods. I like the following one better, because it is a little more generic:

  1. long PeriodAsTicks = TimeSpan.TicksPerHour * 24;
  2.  
  3. var result = from win in source
  4.                  .AlterEventDuration(e => TimeSpan.FromTicks(PeriodAsTicks - (e.StartTime.Ticks % PeriodAsTicks)))
  5.                  .SnapshotWindow(SnapshotWindowOutputPolicy.Clip)
  6.              select new { runningTotal = win.Sum(e => e.Value) };

 

You can find the end-to-end example as a LINQPad query attached.

Regards,
Roman

Sarah Michelle Gellar Olivia Munn Melissa Sagemiller Roselyn Sanchez Soft Cell

Google Testing Groupon Clone for SMBs

There was a time when online coupons were presumed dead. However, in 2011 they?re cool again, having established themselves as a trusted friend of local retailers and an ally in helping them attract customers online and in store. In 2010, coupons were named one of the five can?t miss mobile marketing trends and their popularity was further enhanced by both social coupon sites like Groupon. If you?re not familiar with Groupon, the site offers city-specific daily deals andRead More

From Small Business Trends

Google Testing Groupon Clone for SMBs

Ashlee Simpson Donna Feldman Jodi Lyn OKeefe Emma Watson Amy Smart

M&A Case Studies: Feedburner

This MBA Mondays M&A case study is about the effect that stock option acceleration provisions have on M&A transactions. I am reblogging a blog post that Feedburner founder/CEO Dick Costolo (now Twitter CEO) wrote in the wake of the acquisition...

Angela Marcello Paz Vega Rebecca Mader Eva Green Lauren Conrad

Blog Post: Windows Phone 7 auf den M-Days

Kurze Information in eigener Sache: Microsoft wird mit einem Windows Phone-Stand und Vortr�gen auf den M-Days 2011 vertreten sein, die am 27. und 28.01. im Congress Center Frankfurt stattfinden.

Insbesondere sind wir zu sehen und h�ren

Au�erdem steht am 28.01. die Developerb�hne von 11:30 ? 13:30 Uhr ganz im Zeichen von Windows Phone 7, zu den Themen

  • Apps f�r Windows Phone 7 ? Tools & Technologien
    - .NET, Silverlight & XNA f�r Apps & Games
    - Visual Studio & Expression Blend f�r Entwickler und Designer
    - Location, Push Notification & andere Services
  • Apps f�r Windows Phone 7 ? Training & Ressourcen
    - Die wichtigsten WP7 Sites f�r Entwickler
    - Libraries, Samples & Labs
    - Trainings, Webcasts, B�cher und Publikationen
  • Apps f�r Windows Phone 7 ? Vermarktung & Bewerbung
    - Windows Phone Marketplace: so funktionieren Anmeldung & Zertifizierung
    - Mobile Ads & In-App Business
    - Bewerbung der eigenen App ? so geht?s

Falls Sie also vor Ort sind, nutzen Sie die M�glichkeit, mit uns zu sprechen und sich zu informieren. Wir freuen uns auf und �ber reges Interesse!

Zhang Ziyi Nikki Reed Natasha Bedingfield Audrina Patridge Simone Mütherthies

Guest Hosting Radio: Bloomberg Surveillance

> If you are anywhere near a radio this mornings, I will be the guest hosting ?Bloomberg Surveillance? with Ken Prewitt from 7:00 – 10 am. I will be playing the role of Tom Keene ! You can catch it live, or via podcast at either Bloomberg or at iTunes. Should be fun . . [...]

Brittany Daniel Kate Moss Zhang Ziyi Nikki Reed Natasha Bedingfield

Media Appearance: The Kudlow Report (1/24/11)

> Back on the Kudlow Report at 7:00 pm this evening with the usual crowd. We will be discussing the Markets, Obama, and Inflation. I’m surprised we haven’t seen any articles headlined “Markets Rally on Jets Loss.” My takeaway: ? How much the Fed has distorted Equity markets. ? Inflation starting to perk up ? [...]

Rachel Perry Mary Elizabeth Winstead Piper Perabo Anna Kournikova Esther Cañadas

From U.S. to Germany to China?BoomTown Goes Around the (Digital) World in a Week

It's a big, wide and very digital world out there and that's why I'm headed around the globe--quite literally--for the next week to see some non-Silicon Valley tech trends and more.

Chloë Sevigny Janet Jackson Georgianna Robertson Reese Witherspoon Jordana Brewster