Introducing Sleek
10 May 2013
For some time before, I have been working on a real-time analytics app for online shops. It got me interested in analytics to some extent.
The analytics engine I've created for ShopStream isn't very generic and tied to how it currently does analytics. Recently, I've had an idea to create a more generic, low(er)-level library for doing analytics in Ruby with MongoDB.
Meet Sleek. It is very simplistic and far from begin feature-rich for now, but I'm just starting. I plan to add more cool stuff to it.
Sleek supports the following:
- recording events
- basic timeframe & property filtering
- calculating simple numeric metrics on sets of events (count, count unique, minimum, maximum, average, sum)
- analyzing trends in metric values by breaking timeframes into intervals
- basic maintenance tasks
Here are some examples:
sleek = Sleek[:default]
sleek.record(:purchases, {
sleek: { timestamp: 1.day.ago },
customer: { id: 1, name: "First Last", email: "first@last.com" },
items: [{ sku: "APLIP5", name: "iPhone 5", price: 649_99 }],
total: 649_99
})
sleek.record(:purchases, {
customer: { id: 2, name: "Another Customer", email: "first@another.com" },
items: [{ sku: "APLTD", name: "Thunderbolt Display", price: 1499_99 }],
total: 1499_99
})
sleek.record(:purchases, {
customer: { id: 2, name: "Another Customer", email: "first@another.com" },
items: [{ sku: "APLIP5", name: "iPhone 5", price: 649_99 }],
total: 649_99
})
# Simply counting all purchases
sleek.queries.count(:purchases)
# => 3
# Calculating average of all purchases
sleek.queries.average(:purchase, target_property: :total)
# => 93332.33333333333
# How many purchases had total greater than $1000?
sleek.queries.count(:purchases, filter: [:total, :gt, 1000_00])
# => 1
# How much did we get in sales yesterday?
sleek.queries.sum(:purchases, target_property: :total, timeframe: :previous_day)
# => 64999
# How much did we make in past 2 days (yesterday and today),
# broken down by days
sleek.queries.sum(:purchases, target_property: :total, timeframe: :this_2_days, interval: :daily)
# => [
# {:timeframe=>#<Sleek::Timeframe 2013-05-08 21:00:00 UTC..2013-05-09 21:00:00 UTC>, :value=>64999},
# {:timeframe=>#<Sleek::Timeframe 2013-05-09 21:00:00 UTC..2013-05-10 21:00:00 UTC>, :value=>214998}
# ]