def filter(self, *args, **kwargs): self.cached_results = None attrs = get_attrs(*args, **kwargs) pks = [DataAttribute.make(key, val) for key, val in attrs.items()] for key, val in attrs.items(): if val == '*': self.qs = self.qs.filter(attributes__key=key) q_or = self.qs.filter(attributes__key=key) else: val = val.replace('\\*', '*') self.qs = self.qs.filter(attributes__pk=DataAttribute.make(key, val)) return self
def incr(name, amt, tag_names, *args, **kwargs): attrs = get_attrs(*args, **kwargs) data_attrs = [] for key, val in attrs.items(): id = DataAttribute.make(key, val) attr, created = DataAttribute.objects.get_or_create(key=key, value=val, id=id) data_attrs.append(attr) def get_tag(name): tag, _ = Tag.objects.get_or_create(name=name) return tag if isinstance(tag_names, str): tags = [get_tag(tag_names),] else: tags = [get_tag(name) for name in tag_names] dp = DataPoint.objects.create( name=name, count=amt, dt=this_minute(), ) dp.attributes.add(*data_attrs) if tags: dp.tags.add(*tags) dp.save()
def attrs(self): xs = DataAttribute.objects.filter(points__in=self.qs).values('id').annotate() values = defaultdict(lambda: []) for row in xs: key, val = DataAttribute.split(row['id']) values[key].append(val) return dict(values)