Add a MAPPING table to remove duplicated loop logic
@@ -6,27 +6,25 @@ logger = logging.getLogger(__name__)
class SenderBase(object):
+ MAPPING = [
+ ('project', Project),
+ ('service', Service),
+ ]
+
def send(self, data):
sent = 0
for alert in data['alerts']:
- if 'project' in alert['labels']:
- logger.debug('Checking for projects')
- for project in Project.objects.filter(name=alert['labels']['project']):
- logger.debug('Checking %s', project)
- for sender in project.sender.all():
- logger.debug('Sending to %s', sender)
- if self._send(sender.value, alert, data):
- sent += 1
- if 'service' in alert['labels']:
- logger.debug('Checking for service')
- for service in Service.objects.filter(name=alert['labels']['service']):
- logger.debug('Checking %s', service)
- for sender in service.sender.all():
+ for label, klass in self.MAPPING:
+ logger.debug('Checking for %s', label)
+ if label in alert['labels']:
+ logger.debug('Checking for %s %s', label, klass)
+ for obj in klass.objects.filter(name=alert['labels'][label]):
+ for sender in obj.sender.filter(sender=self.__module__):
logger.debug('Sending to %s', sender)
if self._send(sender.value, alert, data):
sent += 1
if sent == 0:
- logger.debug('No senders configured for project or service %s', alert['labels']['project'])
+ logger.debug('No senders configured for project or service')
return sent
def test(self, target, alert):
