bellotastudios/gradecraft-development — pull request #466 by JangoSteve (2013-10-02 23:03:22+00:00)
Title: Remove analytics events

This is for #464.

It adds the `Analytics::Aggregate.decrement(event)` module method, which inspects the aggregate class to see what values it incremented based on that event, and does an equivalent decrement.

It then cleans up the aggregate attributes hash, removing the keys for any values that end up being either `0` or `NaN` after the decrement. I.e. the opposite of mongo's `upsert` (since there's no such thing as a downsert).

This comes bundled with two new rake tasks as well:

```
rake 'analytics:delete_events[criteria]'
```

Where `criteria` would be e.g. `event_type=predictor possible=0`.

The above deletes all analytics events that match the given criteria (i.e. "predictor" events logged with a possible score of 0). It also decrements all of the affected aggregates by the event values before deleting the event.

And:

```
rake analytics:rebuild[aggregate]
```

Where `aggregate` is the class name of the aggregate you want to rebuild, e.g. `CoursePrediction`.

This rake task is used for when aggregate data is corrupted beyond repair (e.g. our `CoursePrediction` aggregate, since the totals for the entire course were set to `NaN` as soon as one of the assignments with `0` possible score was set in a predictor).

Once we deploy and merge this to the server, we need to SSH into the server and:

1. Backup the analytics mongo db.

2. Run the following tasks:

```
rake 'analytics:delete_event[event_type=predictor possible=0]'
rake analytics:rebuild_aggregate[CoursePrediction]
```

Then we should be all good to go.