First draft of macro expansion
# Copyright (c) 2017 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE
+import collections
+
from django import template
register = template.Library()
@@ -17,6 +19,14 @@ def to_prom(value):
@register.filter()
def rulemacro(value, rule):
- print(value)
- print(rule)
- return value.replace('{macro}', 'foo="bar"')
+ labels = collections.defaultdict(list)
+ for r in rule.overrides.all():
+ labels['service'].append(r.service.name)
+
+ filters = {
+ k: '|'.join(labels[k]) for k in labels
+ }
+ macro = ','.join(
+ '{}!~"{}"'.format(k, v) for k, v in filters.items()
+ )
+ return value.replace('{macro}', macro)
