def test_unicode(self): """ Test if Unicode is correctly specified """ informer = BaseInformer() expected = u'A small and explicit description from informer.' self.assertEqual(expected, str(informer))
def get(self, request, namespace, classname): """ GET /informer/:name/ """ result = { 'name': classname, 'operational': None, 'measures': [], 'message': 'pending' } try: cls = BaseInformer.get_class(namespace, classname) informer = cls() measures = cls.get_measures() measures = [measure.replace('check_', '') for measure in measures] operational, message = informer.check_availability() result['operational'] = operational result['message'] = message result['measures'] = measures except InformerException as error: result['message'] = '%s' % error return JsonResponse(result)
def test_check_availability(self): """ Test if 'check' raises a NotImplementedError """ informer = BaseInformer() self.assertRaises(NotImplementedError, informer.check_availability)
def test_no_generate_raw_data(self, m_get_or_create): """ If interval was not defined, does not generate the raw data. """ generate_raw_data(BaseInformer(), 'foo', 'bar') self.assertFalse(m_get_or_create.called)
def get(self, request): """ GET /informer/ """ interval = getattr(settings, 'DJANGO_INFORMER_PREVENT_SAVE_UNTIL', 0) DJANGO_INFORMERS = getattr(settings, 'DJANGO_INFORMERS', ()) result = { 'status': 'All systems are operational', 'result': [] } for namespace, classname in DJANGO_INFORMERS: informer = BaseInformer.get_class(namespace, classname)() operational, message = informer.check_availability() if not operational: result['status'] = 'Oh no. Houston we have problemns' name = classname.replace('Informer', '').lower() urlname = 'informer-%s' % name result['result'].append({ 'name': name, 'operational': operational, 'message': message, 'url': reverse(urlname) }) return render(request, self.template, result, content_type='text/html')
def test_generate_raw_data(self, m_get_or_create): generate_raw_data(BaseInformer(), 'foo', 'bar') m_get_or_create.assert_called_once_with(indicator='Base', value='bar', measure='foo', date=datetime.now())
def handle(self, *args, **options): self.stdout.write(' Django Informer Management Command') self.stdout.write('-' * 79) self.stdout.write(' Get informations from each Informer.\n\n') self.stdout.write( ' For a complete help and documentation, visit' ' http://github.com/rodrigobraga/informer.') DJANGO_INFORMERS = getattr(settings, 'DJANGO_INFORMERS', ()) if not DJANGO_INFORMERS: self.stdout.write('\n No informer was found.') self.stdout.write('-' * 79) self.stdout.write(' Missing configuration. ') return if options['list']: self.stdout.write( '\n Below the informers that appear in settings.') self.stdout.write('-' * 79) for namespace, classname in DJANGO_INFORMERS: self.stdout.write(' %s.%s' % (namespace, classname)) return names = options['informers'] informers = self._get_informers(names) if not informers: message = '\n No informer was found with names provided (%s).' message %= ', '.join(names) self.stdout.write(message) return self.stdout.write('\n Checking Informers.') self.stdout.write('-' * 79) output = '\t Checking {0}... {1}' for namespace, classname in informers: try: cls = BaseInformer.get_class(namespace, classname) informer = cls() operational, message = informer.check_availability() except InformerException as error: self.stdout.write(output.format(classname, error)) except Exception as error: self.stderr.write('A generic exception occurred: %s' % error) else: self.stdout.write(output.format(classname, message))
def handle(self, *args, **options): self.stdout.write(' Django Informer Management Command') self.stdout.write('-' * 79) self.stdout.write(' Get informations from each Informer.\n\n') self.stdout.write( ' For a complete help and documentation, visit' ' http://github.com/rodrigobraga/informer.') DJANGO_INFORMERS = getattr(settings, 'DJANGO_INFORMERS', ()) if not DJANGO_INFORMERS: self.stdout.write('\n No informer was found.') self.stdout.write('-' * 79) self.stdout.write(' Missing configuration. ') return if options['list']: self.stdout.write( '\n Below the informers that appear in settings.') self.stdout.write('-' * 79) for namespace, classname in DJANGO_INFORMERS: self.stdout.write(' %s.%s' % (namespace, classname)) return names = options['informers'] informers = self._get_informers(names) if not informers: message = '\n No informer was found with names provided (%s).' message %= ', '.join(names) self.stdout.write(message) return self.stdout.write('\n Checking Informers.') self.stdout.write('-' * 79) output = '\t Checking {0}... {1}' for namespace, classname in informers: try: cls = BaseInformer.get_class(namespace, classname) informer = cls() operational, message = informer.check_availability() except InformerException as error: self.stderr.write(output.format(classname, error)) except Exception as error: self.stderr.write('A generic exception occurred: %s' % error) else: self.stdout.write(output.format(classname, message))
def test_get_class(self): """ Test if 'get_class' can instantiate a Informer """ cls = BaseInformer.get_class( 'informer.checker.database', 'DatabaseInformer') informer = cls() expected = isinstance(informer, (BaseInformer, DatabaseInformer)) self.assertTrue(expected)
def test_no_generate_raw_data_if_limit_not_exceeded(self, m_get_or_create): """ Prevents the generation of multiple data when the limit was not exceeded. """ RawFactory(indicator='Base', measure='foo', value='bar', date=datetime.now()) generate_raw_data(BaseInformer(), 'foo', 'bar') self.assertFalse(m_get_or_create.called)
def get(self, request, namespace, classname): """ GET /informer/:name/ """ indicator = classname.replace('Informer', '') try: informer = BaseInformer.get_class(namespace, classname)() operational, message = informer.check_availability() measures = informer.get_measures() measures = [measure.replace('check_', '') for measure in measures] result = { 'name': indicator, 'operational': operational, 'measures': [], 'message': message } for measure in measures: fields = ['id', 'indicator', 'measure', 'date', 'value'] data = Raw.objects.filter( indicator=indicator, measure=measure).order_by( '-date').values(*fields) urlname = 'informer-%s-%s' % (indicator.lower(), measure) result['measures'].append({ 'name': measure, 'url': reverse(urlname), 'data': data[0:3] }) except InformerException as error: result = { 'name': indicator, 'operational': False, 'measures': [], 'message': '%s' % error } return render(request, self.template, result, content_type='text/html')
url(r'^feed/$', InformerFeed(), name='feed-informer') ] DJANGO_INFORMERS = getattr(settings, 'DJANGO_INFORMERS', ()) for namespace, classname in DJANGO_INFORMERS: data = {'namespace': namespace, 'classname': classname} informer = classname.replace('Informer', '').lower() alias = 'informer-%s' % informer uri = url(r'^%s/$' % informer, InformerView.as_view(), data, name=alias) urlpatterns.append(uri) # append url from measures cls = BaseInformer.get_class(namespace, classname) view = MeasureView.as_view() for measure in cls.get_measures(): measure = measure.replace('check_', '') alias = 'informer-%s-%s' % (informer, measure) data = { 'namespace': namespace, 'classname': classname, 'measure': measure } uri = url(r'^%s/%s/$' % (informer, measure), view, data, name=alias) urlpatterns.append(uri)