def test_toItem(): day = Day( '/', 'Tyler Norlund', '2020-01-02', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 } ) assert day.toItem() == { 'PK': { 'S': 'PAGE#/' }, 'SK': { 'S': '#DAY#2020-01-02' }, 'GSI1PK': { 'S': 'PAGE#/' }, 'GSI1SK': { 'S': '#DAY#2020-01-02' }, 'Type': { 'S': 'day' }, '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' } } } }
def test_gsi1pk(): day = Day( '/', 'Tyler Norlund', '2020-01-02', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 } ) assert day.gsi1pk() == { 'S': 'PAGE#/' }
def addDay( self, visits ): '''Adds a day item to the table. Parameters ---------- visits : list[ Visit ] The list of visits from a specific day for the specific page. Returns ------- result : dict The result of adding the day to the table. This could be the error that occurs or the day 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-%d' ) for visit in visits } ) != 1: raise ValueError( 'List of visits must be from the same year, month, and day' ) # 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 day object. day = Day( visits[0].slug, visits[0].title, visits[0].date.strftime( '%Y-%m-%d' ), len( { visit.ip for visit in visits } ), averageTime, toPages.count( None ) / len( toPages ), pagesToDict( fromPages ), pagesToDict( toPages ) ) try: # Add the day to the table self.client.put_item( TableName = self.tableName, Item = day.toItem() ) # Return the day added to the table return { 'day': day } except ClientError as e: print( f'ERROR addDay: { e }') return { 'error': 'Could not add new day to table' }
def test_gsi1(): day = Day( '/', 'Tyler Norlund', '2020-01-02', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 } ) assert day.gsi1() == { 'GSI1PK': { 'S': 'PAGE#/' }, 'GSI1SK': { 'S': '#DAY#2020-01-02' } }
def day( day_visits ): toPages = [ visit.nextSlug for visit in day_visits ] fromPages = [ visit.prevSlug for visit in day_visits ] return Day( day_visits[0].slug, day_visits[0].title, day_visits[0].date.strftime( '%Y-%m-%d' ), len( { visit.id for visit in day_visits } ), np.mean( [ visit.timeOnPage for visit in day_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_repr(): day = Day( '/', 'Tyler Norlund', '2020-01-02', 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( day ) == 'Tyler Norlund - 2020/01/02'
def test_init_day_exception(): with pytest.raises( ValueError ) as e: assert Day( '/', 'Tyler Norlund', '2020-01-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 day of the month'
def test_init_date_exception(): with pytest.raises( ValueError ) as e: assert Day( '/', 'Tyler Norlund', '2020/01/02', 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 day as "<year>-<month>-<day>"'
def test_itemToDay(): day = Day( '/', 'Tyler Norlund', '2020-01-02', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 } ) newDay = itemToDay( day.toItem() ) assert day.slug == newDay.slug assert day.title == newDay.title assert day.year == newDay.year assert day.month == newDay.month assert day.day == newDay.day assert day.numberVisitors == newDay.numberVisitors assert day.averageTime == newDay.averageTime assert day.percentChurn == newDay.percentChurn assert day.fromPage == newDay.fromPage assert day.toPage == newDay.toPage
def test_init(): day = Day( '/', 'Tyler Norlund', '2020-01-02', 10, 0.1, 0.25, { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }, { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 } ) assert day.slug == '/' assert day.title == 'Tyler Norlund' assert day.year == 2020 assert day.month == 1 assert day.day == 2 assert day.numberVisitors == 10 assert day.averageTime == 0.1 assert day.percentChurn == 0.25 assert day.fromPage == { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 } assert day.toPage == { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }