def listVisitors(self): '''Lists all visitors in the table. Returns ------- visitors : list[ Visitor ] The list of visitors from the table. ''' # Use a list to store the locations returned from the table. visitors = [] try: result = self.client.scan(TableName=self.tableName, ScanFilter={ 'Type': { 'AttributeValueList': [{ 'S': 'visitor' }], 'ComparisonOperator': 'EQ' } }) for item in result['Items']: visitors.append(itemToVisitor(item)) # DynamoDB is limited in 1MB of query results. Continue to query from the # 'LastEvaluatedKey' when this condition is met. if 'LastEvaluatedKey' in result.keys(): still_querying = True while still_querying: result = self.client.scan( TableName=self.tableName, ScanFilter={ 'Type': { 'AttributeValueList': [{ 'S': 'location' }], 'ComparisonOperator': 'EQ' } }, ExclusiveStartKey=result['LastEvaluatedKey']) for item in result['Items']: visitors.append(itemToVisitor(item)) if 'LastEvaluatedKey' not in result.keys(): still_querying = False return visitors except ClientError as e: print(f'ERROR listVisitors: { e }') return {'error': 'Could not get visitors from table'}
def _parseVisitorDetails(data, result): '''Parses the DynamoDB items to their respective objects. Parameters ---------- data : dict The parsed data as a dictionary. result : dict The result of the DynamoDB query. Returns data : dict The original parsed data combined with the new parsed data. ''' for item in result['Items']: if item['Type']['S'] == 'visitor': data['visitor'] = itemToVisitor(item) elif item['Type']['S'] == 'visit': data['visits'].append(itemToVisit(item)) elif item['Type']['S'] == 'session': data['sessions'].append(itemToSession(item)) elif item['Type']['S'] == 'location': data['location'] = itemToLocation(item) elif item['Type']['S'] == 'browser': data['browsers'].append(itemToBrowser(item)) return data
def test_itemToVisit_exception(): with pytest.raises( Exception ) as e: assert itemToVisitor( {} ) assert str( e.value ) == "Could not parse visitor"
def test_itemToVisitor(): visitor = Visitor( visitor_id, 1 ) newVisitor = itemToVisitor( visitor.toItem() ) assert newVisitor.id == visitor.id assert newVisitor.numberSessions == visitor.numberSessions
def test_itemToVisitor(): visitor = Visitor('0.0.0.0', 1) newVisitor = itemToVisitor(visitor.toItem()) assert newVisitor.ip == visitor.ip assert newVisitor.numberSessions == visitor.numberSessions