예제 #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 test_gsi1pk():
  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.gsi1pk() == { 'S': 'PAGE#/' }
예제 #3
0
def test_gsi1():
  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.gsi1() == {
    'GSI1PK': { 'S': 'PAGE#/' },
    'GSI1SK': { 'S': '#WEEK#2020-01' }
  }
예제 #4
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' }
예제 #5
0
def week( week_visits ):
  toPages = [ visit.nextSlug for visit in week_visits ]
  fromPages = [ visit.prevSlug for visit in week_visits ]
  return Week(
    week_visits[0].slug,
    week_visits[0].title,
    week_visits[0].date.strftime( '%Y-%U' ),
    len( { visit.id for visit in week_visits } ),
    np.mean( [
        visit.timeOnPage for visit in week_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 ) )
    }
  )
예제 #6
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
예제 #7
0
def test_repr():
  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 repr( week ) == 'Tyler Norlund - 2020/01'
예제 #8
0
def test_init_week_exception():
  with pytest.raises( ValueError ) as e:
    assert Week(
      '/', 'Tyler Norlund', '2020-53', 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 week'
예제 #9
0
def test_init_date_exception():
  with pytest.raises( ValueError ) as e:
    assert 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 str( e.value ) == 'Must give week as "<year>-<week>"'
예제 #10
0
def test_init():
  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.slug == '/'
  assert week.title == 'Tyler Norlund'
  assert week.year == 2020
  assert week.week == 1
  assert week.numberVisitors == 10
  assert week.averageTime == 0.1
  assert week.percentChurn == 0.25
  assert week.fromPage == { 'www': 0.5, '/blog': 0.25, '/resume': 0.25 }
  assert week.toPage == { 'www': 0.25, '/blog': 0.5, '/resume': 0.25 }