#NEWLINE# def update_counter( page , db = None ) : #NEWLINE# #INDENT# 'Update counters for page.\n\n    :param str page: Colon-delimited page key in analytics collection\n    :param db: MongoDB database or `None`\n    ' #NEWLINE# db = ( db or database ) #NEWLINE# collection = db [ 'pagecounters' ] #NEWLINE# date = datetime . utcnow ( ) #NEWLINE# date = date . strftime ( '%Y/%m/%d' ) #NEWLINE# page = clean_page ( page ) #NEWLINE# d = { #NEWLINE# #INDENT# '$inc' : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# visited_by_date = session . data . get ( 'visited_by_date' ) #NEWLINE# if ( not visited_by_date ) : #NEWLINE# #INDENT# visited_by_date = { #NEWLINE# #INDENT# 'date' : date , #NEWLINE# 'pages' : [ ] , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( date == visited_by_date [ 'date' ] ) : #NEWLINE# #INDENT# if ( page not in visited_by_date [ 'pages' ] ) : #NEWLINE# #INDENT# d [ '$inc' ] [ ( 'date.%s.unique' % date ) ] = 1 #NEWLINE# visited_by_date [ 'pages' ] . append ( page ) #NEWLINE# session . data [ 'visited_by_date' ] = visited_by_date #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# visited_by_date [ 'date' ] = date #NEWLINE# visited_by_date [ 'pages' ] = [ ] #NEWLINE# d [ '$inc' ] [ ( 'date.%s.unique' % date ) ] = 1 #NEWLINE# visited_by_date [ 'pages' ] . append ( page ) #NEWLINE# session . data [ 'visited_by_date' ] = visited_by_date #NEWLINE# #UNINDENT# d [ '$inc' ] [ ( 'date.%s.total' % date ) ] = 1 #NEWLINE# visited = session . data . get ( 'visited' ) #NEWLINE# if ( not visited ) : #NEWLINE# #INDENT# visited = [ ] #NEWLINE# #UNINDENT# if ( page not in visited ) : #NEWLINE# #INDENT# d [ '$inc' ] [ 'unique' ] = 1 #NEWLINE# visited . append ( page ) #NEWLINE# session . data [ 'visited' ] = visited #NEWLINE# #UNINDENT# d [ '$inc' ] [ 'total' ] = 1 #NEWLINE# collection . update ( { #NEWLINE# #INDENT# '_id' : page , #NEWLINE# #UNINDENT# } , d , True , False )