def build_report(report_id, filters): """Lookup system report by report_id. :param report_id: the identifer for the report :param fiters: filters for the report :returns: json report data """ # We want aggregate counts on the fingerprints # Find all fingerprints with this report_id fingerprints = SystemFingerprint.objects.filter( report_id__id=report_id) if len(fingerprints) is 0: return None group = filters.get('group_count', None) if group is not None: return build_grouped_report(report_id, fingerprints, group) # Build response dictionary report = {'report_id': report_id, 'report': []} filterkeys = filter_keys(filters) for fingerprint in fingerprints: serializer = FingerprintSerializer(fingerprint) if filterkeys == set(): report['report'].append(serializer.data) else: filtered_data = {} for key in filterkeys: visible_data = serializer.data.get(key, None) filtered_data[key] = visible_data report['report'].append(filtered_data) return report
def process_fact_collection(sender, instance, **kwargs): """Process the fact collection. :param sender: Class that was saved :param instance: FactCollection that was saved :param kwargs: Other args :returns: None """ # pylint: disable=unused-argument logger.info('Fingerprint engine (report id=%d) - start processing', instance.id) # Invoke ENGINE to create fingerprints from facts fingerprints_list = _process_sources(instance) number_valid = 0 number_invalid = 0 for fingerprint_dict in fingerprints_list: serializer = FingerprintSerializer(data=fingerprint_dict) if serializer.is_valid(): number_valid += 1 serializer.save() else: number_invalid += 1 logger.error('Invalid fingerprint: %s', fingerprint_dict) logger.error('Fingerprint errors: %s', serializer.errors) logger.info( 'Fingerprint engine (report id=%d) - end processing ' '(valid fingerprints=%d, invalid fingerprints=%d)', instance.id, number_valid, number_invalid) # Mark completed because engine has process raw facts instance.status = FactCollection.FC_STATUS_COMPLETE instance.save()
def process_fact_collection(sender, instance, **kwargs): """Process facts using engine and convert to fingerprints. :param sender: Class that was saved :param instance: FactCollection that was saved :param kwargs: Other args :returns: None """ # pylint: disable=unused-argument # Convert to python dictionary fact_collection = FactCollectionSerializer(instance).data # Extract facts and collection id fact_collection_id = fact_collection['id'] facts = fact_collection['facts'] # Invoke ENGINE to create fingerprints from facts fingerprints_list = ENGINE.process_facts(fact_collection_id, facts) for fingerprint_dict in fingerprints_list: serializer = FingerprintSerializer(data=fingerprint_dict) if serializer.is_valid(): serializer.save() else: logger.error('%s could not persist fingerprint. SystemFacts: %s', __name__, fingerprint_dict) logger.error('Errors: %s', serializer.errors)
def test_empty_fingerprint(self): """Create an empty fingerprint.""" fingerprint_dict = {'fact_collection_id': self.fact_collection.id, 'metadata': {}} serializer = FingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save()
def test_entitlement_fingerprint(self): """Create a fingerprint with entitlements.""" entitlement_dict = { 'name': 'RHEL Server', 'entitlement_id': '69', 'metadata': {} } fingerprint_dict = { 'report_id': self.fact_collection.id, 'metadata': {}, 'entitlements': [entitlement_dict], 'sources': [] } serializer = FingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save()
def test_product_fingerprint(self): """Create a fingerprint with products.""" product_dict = { 'name': 'product1', 'presence': 'unknown', 'metadata': {} } fingerprint_dict = { 'report_id': self.fact_collection.id, 'metadata': {}, 'products': [product_dict], 'sources': [] } serializer = FingerprintSerializer(data=fingerprint_dict) is_valid = serializer.is_valid() if not is_valid: print(serializer.errors) self.assertTrue(is_valid) serializer.save()