예제 #1
0
  def addYear( self, visits ):
    '''Adds a year item to the table.

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

    Returns
    -------
    result : dict
      The result of adding the year to the table. This could be the error that
      occurs or the year 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' ) for visit in visits } ) != 1:
      raise ValueError( 'List of visits must be from the same year' )
    # 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 year object
    year = Year(
      visits[0].slug,
      visits[0].title,
      visits[0].date.strftime( '%Y' ),
      len( { visit.ip for visit in visits } ),
      averageTime,
      toPages.count( None ) / len( toPages ),
      pagesToDict( fromPages ),
      pagesToDict( toPages )
    )
    try:
      # Add the year to the table
      self.client.put_item( TableName = self.tableName, Item = year.toItem() )
      # Return the year added to the table
      return { 'year': year }
    except ClientError as e:
      print( f'ERROR addYear: { e }' )
      return { 'error': 'Could not add new year to table' }
예제 #2
0
def test_itemToYear():
    year = Year('/', 'Tyler Norlund', '2020', 10, 0.1, 0.25, {
        'www': 0.5,
        '/blog': 0.25,
        '/resume': 0.25
    }, {
        'www': 0.25,
        '/blog': 0.5,
        '/resume': 0.25
    })
    newYear = itemToYear(year.toItem())
    assert year.slug == newYear.slug
    assert year.title == newYear.title
    assert year.year == newYear.year
    assert year.numberVisitors == newYear.numberVisitors
    assert year.averageTime == newYear.averageTime
    assert year.percentChurn == newYear.percentChurn
    assert year.fromPage == newYear.fromPage
    assert year.toPage == newYear.toPage
예제 #3
0
def test_toItem():
    year = Year('/', 'Tyler Norlund', '2020', 10, 0.1, 0.25, {
        'www': 0.5,
        '/blog': 0.25,
        '/resume': 0.25
    }, {
        'www': 0.25,
        '/blog': 0.5,
        '/resume': 0.25
    })
    assert year.toItem() == {
        'PK': {
            'S': 'PAGE#/'
        },
        'SK': {
            'S': '#YEAR#2020'
        },
        'GSI1PK': {
            'S': 'PAGE#/'
        },
        'GSI1SK': {
            'S': '#YEAR#2020'
        },
        'Type': {
            'S': 'year'
        },
        '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'
                }
            }
        }
    }