Пример #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_gsi1pk():
    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.gsi1pk() == {'S': 'PAGE#/'}
Пример #3
0
def test_key():
    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.key() == {'PK': {'S': 'PAGE#/'}, 'SK': {'S': '#YEAR#2020'}}
Пример #4
0
def year( year_visits ):
  toPages = [ visit.nextSlug for visit in year_visits ]
  fromPages = [ visit.prevSlug for visit in year_visits ]
  return Year(
    year_visits[0].slug,
    year_visits[0].title,
    year_visits[0].date.strftime( '%Y' ),
    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 ) )
    }
  )
Пример #5
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
Пример #6
0
def test_repr():
    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 repr(year) == 'Tyler Norlund - 2020'
Пример #7
0
def test_init_year_exception():
    with pytest.raises(ValueError) as e:
        assert Year('/', 'Tyler Norlund', '202', 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 year'
Пример #8
0
def test_init():
    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.slug == '/'
    assert year.title == 'Tyler Norlund'
    assert year.year == 2020
    assert year.numberVisitors == 10
    assert year.averageTime == 0.1
    assert year.percentChurn == 0.25
    assert year.fromPage == {'www': 0.5, '/blog': 0.25, '/resume': 0.25}
    assert year.toPage == {'www': 0.25, '/blog': 0.5, '/resume': 0.25}
Пример #9
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'
                }
            }
        }
    }