#!/usr/bin/env python

import sys
from collections import defaultdict
from pymongo import MongoClient


def midnight(dt):
    return dt.replace(hour=0, minute=0, second=0, microsecond=0)


def hours_since_midnight(dt):
    return dt.hour


def main():
    client = MongoClient()
    db = client.chameleon

    for sample in db.samples_old.find():
        dt = sample['timestamp']
        ts = midnight(dt)
        hours = hours_since_midnight(dt)
        q = {'user': sample['user'], 'timestamp': ts}
        up = {"$set": {
            "reputation.{:d}".format(hours): sample['reputation'],
        }}
        db.samples.update_one(q, up, upsert=True)

    return 0


if __name__ == "__main__":
    sys.exit(main())
