bellycard/redistat — pull request #23 by joedivita (2014-01-29 04:23:30+00:00)
Title: Adding support for attributes + unique counters with no associated id

Adding functionality for using "attributes" in unique counter queries.

Attributes are unique counter stats that are not associated with an id (so kind of like "global" sets). These can be mashed up with other unique countesr that are associated with ids, to give the same result as if they were associated with that id. The main advantages for doing this are:

-To save a tremendous amount of memory (don't have to store all this data, for every resolution interval, for every id)
-It's easier to update / maintain / rebuilding data is much quicker ( instead of having to update these values for every id at every interval, you can just do it once, at the "global" level).

For example, lets say we have a unique counter "Customers", and a unique counter "Males". Instead of storing them both at the id & daily level we can get the number of males / day / id with the following:

``` ruby
class Customers < Redistat::Model
type :unique
resolution :days
end

class Males < Redistat::Model
type :unique
end
```

Add some customers and some males:

```ruby
Customers.increment(id: 1, timestamp: '2014-01-01', unique_id: 1000)
Males.increment(unique_id: 1000)
# etc..
```

Now you can query customers, just as you normally would, but if I want to mash it up against Males I just pass in an attribute into the parameters:

```ruby
Customers.aggregate(id: 1, start_date, '2014-01-01', end_date: '2014-01-09', attributes: [:males])
```

You can even mash up multiple attributes. Suppose I want to see all the customers who are male, and Android users. First add the AndroidUsers class:

```ruby
class AndroidUsers < Redistat::Model
type: unique
end
```

then just add that into the query:

```ruby
Customers.aggregate(id: 1, start_date, '2014-01-01', end_date: '2014-01-09', attributes: [:males, :android_users])
```