from framework import db, session

from decorator import decorator
from datetime import datetime

collection = db['pagecounters']


def increment_user_activity_counters(user_id, action, date):
    collection = db['useractivitycounters']
    date = date.strftime('%Y/%m/%d') # todo remove slashes
    d = {'$inc': {}}
    d['$inc']['total'] = 1
    d['$inc']['date.{0}.total'.format(date)] = 1
    d['$inc']['action.{0}.total'.format(action)] = 1
    d['$inc']['action.{0}.date.{1}'.format(action, date)] = 1
    collection.update({'_id': user_id}, d, True, False)
    return True


def get_total_activity_count(user_id):
    collection = db['useractivitycounters']
    result = collection.find_one(
        {'_id': user_id}, {'total': 1}
    )
    if result and 'total' in result:
        return result['total']
    return 0


def update_counters(rex):
    def wrapped(func, *args, **kwargs):
        date = datetime.utcnow()
        date = date.strftime('%Y/%m/%d')
        try:
            page = rex.format(**kwargs).replace('.', '_')
        except KeyError:
            return func(*args, **kwargs)

        d = {'$inc': {}}

        visited_by_date = session.data.get('visited_by_date')
        if not visited_by_date:
            visited_by_date = {'date': date, 'pages': []}

        if date == visited_by_date['date']:
            if page not in visited_by_date['pages']:
                d['$inc']['date.%s.unique' % date] = 1
                visited_by_date['pages'].append(page)
                session.data['visited_by_date'] = visited_by_date
        else:
            visited_by_date['date'] = date
            visited_by_date['pages'] = []
            d['$inc']['date.%s.unique' % date] = 1
            visited_by_date['pages'].append(page)
            session.data['visited_by_date'] = visited_by_date

        d['$inc']['date.%s.total' % date] = 1

        visited = session.data.get('visited') # '/project/x/, project/y/'
        if not visited:
            visited = []
        if page not in visited:
            d['$inc']['unique'] = 1
            visited.append(page)
            session.data['visited'] = visited
        d['$inc']['total'] = 1
        collection.update({'_id': page}, d, True, False)
        return func(*args, **kwargs)

    return decorator(wrapped)


def get_basic_counters(page):
    unique = 0
    total = 0
    result = collection.find_one(
        {'_id': page}, {'total': 1, 'unique': 1}
    )
    if result:
        if 'unique' in result:
            unique = result['unique']
        if 'total' in result:
            total = result['total']
        return (unique, total)
    else:
        return (None, None)

@@ -289,29 +289,42 @@
 slashes%0A    
-d = %7B
+query = %7B%0A        
 '$inc': %7B%7D%7D%0A
@@ -324,52 +324,45 @@
 ': %7B
-%7D%7D
 %0A    
-d%5B'$inc'%5D%5B
+        
 'total'
-%5D =
+:
  1
+,
 %0A    
-d%5B'$inc'%5D%5B
+        
 'dat
@@ -386,36 +386,33 @@
 at(date)
-%5D =
+:
  1
+,
 %0A    
-d%5B'$inc'%5D%5B
+        
 'action.
@@ -440,28 +440,25 @@
 ion)
-%5D =
+:
  1
+,
 %0A    
-d%5B'$inc'%5D%5B
+        
 'act
@@ -495,21 +495,36 @@
 n, date)
-%5D = 1
+: 1,%0A        %7D%0A    %7D
 %0A    col
@@ -530,32 +530,41 @@
 llection.update(
+%0A        
 %7B'_id': user_id%7D
@@ -564,31 +564,83 @@
 ser_id%7D,
- d, True, False
+%0A        query,%0A        upsert=True,%0A        manipulate=False,%0A    
 )%0A    re

clean up style