<s> from __future__ import division <EOL> from datetime import timedelta <EOL> from django . utils import timezone <EOL> from django . contrib . auth import get_user_model <EOL> from django . db import models <EOL> from django . db . models import Count , Avg <EOL> from tracking . settings import TRACK_PAGEVIEWS , TRACK_ANONYMOUS_USERS <EOL> from tracking . cache import CacheManager <EOL> class VisitorManager ( CacheManager ) : <EOL> def active ( self , registered_only = True ) : <EOL> "<STR_LIT>" <EOL> visitors = self . filter ( <EOL> expiry_time__gt = timezone . now ( ) , <EOL> end_time = None <EOL> ) <EOL> if registered_only : <EOL> visitors = visitors . filter ( user__isnull = False ) <EOL> return visitors <EOL> def registered ( self ) : <EOL> return self . get_queryset ( ) . filter ( user__isnull = False ) <EOL> def guests ( self ) : <EOL> return self . get_queryset ( ) . filter ( user__isnull = True ) <EOL> def stats ( self , start_date , end_date , registered_only = False ) : <EOL> """<STR_LIT>""" <EOL> visitors = self . filter ( <EOL> start_time__gte = start_date , <EOL> start_time__lt = end_date <EOL> ) <EOL> stats = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> stats [ '<STR_LIT>' ] = total_count = visitors . count ( ) <EOL> unique_count = <NUM_LIT:0> <EOL> if not total_count : <EOL> return stats <EOL> total_time_on_site = visitors . aggregate ( <EOL> avg_tos = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> stats [ '<STR_LIT>' ] = timedelta ( seconds = int ( total_time_on_site ) ) <EOL> registered_visitors = visitors . filter ( user__isnull = False ) <EOL> registered_total_count = registered_visitors . count ( ) <EOL> if registered_total_count : <EOL> registered_unique_count = registered_visitors . values ( <EOL> '<STR_LIT:user>' <EOL> ) . distinct ( ) . count ( ) <EOL> time_on_site = registered_visitors . aggregate ( <EOL> avg_tos = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> unique_count += registered_unique_count <EOL> returns = ( registered_total_count - registered_unique_count ) <EOL> stats [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : registered_total_count , <EOL> '<STR_LIT>' : registered_unique_count , <EOL> '<STR_LIT>' : ( returns / registered_total_count ) * <NUM_LIT:100> , <EOL> '<STR_LIT>' : timedelta ( seconds = int ( time_on_site ) ) , <EOL> } <EOL> if TRACK_ANONYMOUS_USERS and not registered_only : <EOL> guests = visitors . filter ( user__isnull = True ) <EOL> guest_total_count = guests . count ( ) <EOL> if guest_total_count : <EOL> guest_unique_count = guests . values ( <EOL> '<STR_LIT>' <EOL> ) . distinct ( ) . count ( ) <EOL> guest_time_on_site = guests . aggregate ( <EOL> avg_tos = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> returns = ( guest_total_count - guest_unique_count ) <EOL> return_ratio = ( returns / guest_total_count ) * <NUM_LIT:100> <EOL> time_on_site = timedelta ( seconds = int ( guest_time_on_site ) ) <EOL> else : <EOL> guest_total_count = <NUM_LIT:0> <EOL> guest_unique_count = <NUM_LIT:0> <EOL> return_ratio = <NUM_LIT:0.0> <EOL> time_on_site = timedelta ( <NUM_LIT:0> ) <EOL> unique_count += guest_unique_count <EOL> stats [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : guest_total_count , <EOL> '<STR_LIT>' : guest_unique_count , <EOL> '<STR_LIT>' : return_ratio , <EOL> '<STR_LIT>' : time_on_site , <EOL> } <EOL> returns = ( total_count - unique_count ) <EOL> stats [ '<STR_LIT>' ] = unique_count <EOL> stats [ '<STR_LIT>' ] = ( returns / total_count ) * <NUM_LIT:100> <EOL> if TRACK_PAGEVIEWS : <EOL> if '<STR_LIT>' in stats : <EOL> pages_per_visit = registered_visitors . annotate ( <EOL> page_count = Count ( '<STR_LIT>' ) <EOL> ) . filter ( page_count__gt = <NUM_LIT:0> ) . aggregate ( <EOL> pages_per_visit = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> stats [ '<STR_LIT>' ] [ '<STR_LIT>' ] = pages_per_visit <EOL> if TRACK_ANONYMOUS_USERS and not registered_only : <EOL> stats [ '<STR_LIT>' ] [ '<STR_LIT>' ] = guests . annotate ( <EOL> page_count = Count ( '<STR_LIT>' ) <EOL> ) . filter ( page_count__gt = <NUM_LIT:0> ) . aggregate ( <EOL> pages_per_visit = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> total_per_visit = visitors . annotate ( <EOL> page_count = Count ( '<STR_LIT>' ) <EOL> ) . filter ( page_count__gt = <NUM_LIT:0> ) . aggregate ( <EOL> pages_per_visit = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> else : <EOL> if '<STR_LIT>' in stats : <EOL> total_per_visit = stats [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> else : <EOL> total_per_visit = <NUM_LIT:0> <EOL> stats [ '<STR_LIT>' ] = total_per_visit <EOL> return stats <EOL> def user_stats ( self , start_date = None , end_date = None ) : <EOL> user_kwargs = { <EOL> '<STR_LIT>' : end_date , <EOL> } <EOL> visit_kwargs = { <EOL> '<STR_LIT>' : end_date , <EOL> } <EOL> if start_date : <EOL> user_kwargs [ '<STR_LIT>' ] = start_date <EOL> visit_kwargs [ '<STR_LIT>' ] = start_date <EOL> else : <EOL> user_kwargs [ '<STR_LIT>' ] = False <EOL> visit_kwargs [ '<STR_LIT>' ] = False <EOL> users = list ( get_user_model ( ) . objects . filter ( ** user_kwargs ) . annotate ( <EOL> visit_count = Count ( '<STR_LIT>' ) , <EOL> time_on_site = Avg ( '<STR_LIT>' ) , <EOL> ) . filter ( visit_count__gt = <NUM_LIT:0> ) . order_by ( '<STR_LIT>' ) ) <EOL> for user in users : <EOL> user . pages_per_visit = user . visit_history . filter ( <EOL> ** visit_kwargs <EOL> ) . annotate ( <EOL> page_count = Count ( '<STR_LIT>' ) <EOL> ) . filter ( page_count__gt = <NUM_LIT:0> ) . aggregate ( <EOL> pages_per_visit = Avg ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> user . time_on_site = timedelta ( seconds = int ( user . time_on_site ) ) <EOL> return users <EOL> class PageviewManager ( models . Manager ) : <EOL> def stats ( self , start_date = None , end_date = None , registered_only = False ) : <EOL> """<STR_LIT>""" <EOL> pageviews = self . filter ( <EOL> visitor__start_time__lt = end_date , <EOL> visitor__start_time__gte = start_date , <EOL> ) . select_related ( '<STR_LIT>' ) <EOL> stats = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> stats [ '<STR_LIT>' ] = total_views = pageviews . count ( ) <EOL> unique_count = <NUM_LIT:0> <EOL> if not total_views : <EOL> return stats <EOL> registered_pageviews = pageviews . filter ( visitor__user__isnull = False ) <EOL> registered_count = registered_pageviews . count ( ) <EOL> if registered_count : <EOL> registered_unique_count = registered_pageviews . values ( <EOL> '<STR_LIT>' , '<STR_LIT:url>' ) . distinct ( ) . count ( ) <EOL> unique_count += registered_unique_count <EOL> stats [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : registered_count , <EOL> '<STR_LIT>' : registered_unique_count , <EOL> } <EOL> if TRACK_ANONYMOUS_USERS and not registered_only : <EOL> guest_pageviews = pageviews . filter ( visitor__user__isnull = True ) <EOL> guest_count = guest_pageviews . count ( ) <EOL> if guest_count : <EOL> guest_unique_count = guest_pageviews . values ( <EOL> '<STR_LIT>' , '<STR_LIT:url>' ) . distinct ( ) . count ( ) <EOL> unique_count += guest_unique_count <EOL> stats [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : guest_count , <EOL> '<STR_LIT>' : guest_unique_count , <EOL> } <EOL> stats [ '<STR_LIT>' ] = unique_count <EOL> return stats </s>
