def addMonth( self, visits ): '''Adds a month item to the table. Parameters ---------- visits : list[ Visit ] The list of visits from a specific month for the specific page. Returns ------- result : dict The result of adding the month to the table. This could be the error that occurs or the month object added to the table. ''' if not isinstance( visits, list ): raise ValueError( 'Must pass a list' ) if len( {visit.slug for visit in visits } ) != 1: raise ValueError( 'List of visits must have the same slug' ) if len( {visit.title for visit in visits } ) != 1: raise ValueError( 'List of visits must have the same title' ) if len( {visit.date.strftime( '%Y-%m' ) for visit in visits } ) != 1: raise ValueError( 'List of visits must be from the same year and month' ) # Find all the pages visited before and after this one. toPages = [ visit.nextSlug for visit in visits ] fromPages = [ visit.prevSlug for visit in visits ] # Calculate the average time spent on the page. pageTimes = [ visit.timeOnPage for visit in visits if isinstance( visit.timeOnPage, float ) ] if len( pageTimes ) > 1: averageTime = np.mean( pageTimes ) elif len( pageTimes ) == 1: averageTime = pageTimes[0] else: averageTime = None # Create the month object month = Month( visits[0].slug, visits[0].title, visits[0].date.strftime( '%Y-%m' ), len( { visit.ip for visit in visits } ), averageTime, toPages.count( None ) / len( toPages ), pagesToDict( fromPages ), pagesToDict( toPages ) ) try: # Add the month to the table self.client.put_item( TableName = self.tableName, Item = month.toItem() ) # Return the month added to the table return { 'month': month } except ClientError as e: print( f'ERROR addMonth: { e }' ) return { 'error': 'Could not add new month to table' }
def test_pk(): month = Month('/', 'Tyler Norlund', '2020-01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert month.pk() == {'S': 'PAGE#/'}
def month( month_visits ): toPages = [ visit.nextSlug for visit in month_visits ] fromPages = [ visit.prevSlug for visit in month_visits ] return Month( month_visits[0].slug, month_visits[0].title, month_visits[0].date.strftime( '%Y-%m' ), len( { visit.id for visit in month_visits } ), np.mean( [ visit.timeOnPage for visit in month_visits if visit.timeOnPage is not None ] ), toPages.count( None ) / len( toPages ), { ( 'www' if page is None else page ): fromPages.count( page ) / len( fromPages ) for page in list( set( fromPages ) ) }, { ( 'www' if page is None else page ): toPages.count( page ) / len( toPages ) for page in list( set( toPages ) ) } )
def test_key(): month = Month('/', 'Tyler Norlund', '2020-01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert month.key() == { 'PK': { 'S': 'PAGE#/' }, 'SK': { 'S': '#MONTH#2020-01' } }
def test_itemToMonth(): month = Month('/', 'Tyler Norlund', '2020-01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) newMonth = itemToMonth(month.toItem()) assert month.slug == newMonth.slug assert month.title == newMonth.title assert month.year == newMonth.year assert month.month == newMonth.month assert month.numberVisitors == newMonth.numberVisitors assert month.averageTime == newMonth.averageTime assert month.percentChurn == newMonth.percentChurn assert month.fromPage == newMonth.fromPage assert month.toPage == newMonth.toPage
def test_repr(): month = Month('/', 'Tyler Norlund', '2020-01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert repr(month) == 'Tyler Norlund - 2020/01'
def test_init_month_exception(): with pytest.raises(ValueError) as e: assert Month('/', 'Tyler Norlund', '2020-00', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert str(e.value) == 'Must give valid month'
def test_init_date_exception(): with pytest.raises(ValueError) as e: assert Month('/', 'Tyler Norlund', '2020/01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert str(e.value) == 'Must give month as "<year>-<month>"'
def test_init(): month = Month('/', 'Tyler Norlund', '2020-01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert month.slug == '/' assert month.title == 'Tyler Norlund' assert month.year == 2020 assert month.month == 1 assert month.numberVisitors == 10 assert month.averageTime == 0.1 assert month.percentChurn == 0.25 assert month.fromPage == {'www': 0.5, '/blog': 0.25, '/resume': 0.25} assert month.toPage == {'www': 0.25, '/blog': 0.5, '/resume': 0.25}
def test_toItem(): month = Month('/', 'Tyler Norlund', '2020-01', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert month.toItem() == { 'PK': { 'S': 'PAGE#/' }, 'SK': { 'S': '#MONTH#2020-01' }, 'GSI1PK': { 'S': 'PAGE#/' }, 'GSI1SK': { 'S': '#MONTH#2020-01' }, 'Type': { 'S': 'month' }, 'Title': { 'S': 'Tyler Norlund' }, 'Slug': { 'S': '/' }, 'NumberVisitors': { 'N': '10' }, 'AverageTime': { 'N': '0.1' }, 'PercentChurn': { 'N': '0.25' }, 'FromPage': { 'M': { 'www': { 'N': '0.5' }, '/blog': { 'N': '0.25' }, '/resume': { 'N': '0.25' } } }, 'ToPage': { 'M': { 'www': { 'N': '0.25' }, '/blog': { 'N': '0.5' }, '/resume': { 'N': '0.25' } } } }