示例#1
0
def test_toItem():
  week = Week(
    '/', '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 week.toItem() == {
    'PK': { 'S': 'PAGE#/' },
    'SK': { 'S': '#WEEK#2020-01' },
    'GSI1PK': { 'S': 'PAGE#/' },
    'GSI1SK': { 'S': '#WEEK#2020-01' },
    'Type': { 'S': 'week' },
    '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' }
      }
    }
  }
示例#2
0
  def addWeek( self, visits ):
    '''Adds a week item to the table.

    Parameters
    ----------
    visits : list[ Visit ]
      The list of visits from a specific week for the specific page.

    Returns
    -------
    result : dict
      The result of adding the week to the table. This could be the error that
      occurs or the week 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-%U' ) for visit in visits } ) != 1:
      raise ValueError( 'List of visits must be from the same year and week' )
    # 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 week object
    week = Week(
      visits[0].slug,
      visits[0].title,
      visits[0].date.strftime( '%Y-%U' ),
      len( { visit.ip for visit in visits } ),
      averageTime,
      toPages.count( None ) / len( toPages ),
      pagesToDict( fromPages ),
      pagesToDict( toPages )
    )
    try:
      # Add the week to the table
      self.client.put_item( TableName = self.tableName, Item = week.toItem() )
      # Return the week added to the table
      return { 'week': week }
    except ClientError as e:
      print( f'ERROR addWeek: { e }' )
      return { 'error': 'Could not add new week to table' }
示例#3
0
def test_itemToWeek():
  week = Week(
    '/', '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 }
  )
  newWeek = itemToWeek( week.toItem() )
  assert week.slug == newWeek.slug
  assert week.title == newWeek.title
  assert week.year == newWeek.year
  assert week.week == newWeek.week
  assert week.numberVisitors == newWeek.numberVisitors
  assert week.averageTime == newWeek.averageTime
  assert week.percentChurn == newWeek.percentChurn
  assert week.fromPage == newWeek.fromPage
  assert week.toPage == newWeek.toPage