def test_gsi1pk(): page = Page('/', 'Tyler Norlund', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert page.gsi1pk() == {'S': 'PAGE#/'}
def test_gsi1(): page = Page('/', 'Tyler Norlund', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert page.gsi1() == {'GSI1PK': {'S': 'PAGE#/'}, 'GSI1SK': {'S': '#PAGE'}}
def addPage( self, visits ): '''Adds the page item to the table. Parameters ---------- visits : list[ Visit ] The list of all visits for the specific page. Returns ------- result : dict The result of adding the page to the table. This could be the error that occurs or the page 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' ) # 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 page object. page = Page( visits[0].slug, visits[0].title, len( { visit.ip for visit in visits } ), averageTime, toPages.count( None ) / len( toPages ), pagesToDict( fromPages ), pagesToDict( toPages ) ) try: # Add the page to the table self.client.put_item( TableName = self.tableName, Item = page.toItem() ) # Return the page added to the table return { 'page': page } except ClientError as e: print( f'ERROR addPage: { e }') return { 'error': 'Could not add new page to table' }
def page( year_visits ): toPages = [ visit.nextSlug for visit in year_visits ] fromPages = [ visit.prevSlug for visit in year_visits ] return Page( year_visits[0].slug, year_visits[0].title, len( { visit.id for visit in year_visits } ), np.mean( [ visit.timeOnPage for visit in year_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_itemToPage(): page = Page('/', 'Tyler Norlund', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) newPage = itemToPage(page.toItem()) assert page.slug == newPage.slug assert page.title == newPage.title assert page.numberVisitors == newPage.numberVisitors assert page.averageTime == newPage.averageTime assert page.percentChurn == newPage.percentChurn assert page.fromPage == newPage.fromPage assert page.toPage == newPage.toPage
def page(): return Page('/', 'Tyler Norlund', 2, 90, 0.3333333333333333, { '/': 0.3333333333333333, 'www': 0.6666666666666666 }, { '/blog': 0.3333333333333333, '/resume': 0.3333333333333333, 'www': 0.3333333333333333 })
def test_repr(): page = Page('/', 'Tyler Norlund', 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(page) == 'Tyler Norlund - 0.25'
def test_init(): page = Page('/', 'Tyler Norlund', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert page.slug == '/' assert page.title == 'Tyler Norlund' assert page.numberVisitors == 10 assert page.averageTime == 0.1 assert page.percentChurn == 0.25 assert page.fromPage == {'www': 0.5, '/blog': 0.25, '/resume': 0.25} assert page.toPage == {'www': 0.25, '/blog': 0.5, '/resume': 0.25}
def test_toItem(): page = Page('/', 'Tyler Norlund', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }) assert page.toItem() == { 'PK': { 'S': 'PAGE#/' }, 'SK': { 'S': '#PAGE' }, 'GSI1PK': { 'S': 'PAGE#/' }, 'GSI1SK': { 'S': '#PAGE' }, 'Type': { 'S': 'page' }, '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' } } } }