def type_change_metric(request, metric_id, type_id): try: metric = Metric.objects.get(id=metric_id) except Metric.DoesNotExist: return HttpResponseForbidden() metric.widget_type = get_widget_type_choices()[int(type_id)][0] metric.save() return HttpResponseRedirect(request.META['HTTP_REFERER'])
def render(self, metric, context, minimal): from holodeck.utils import get_widget_type_choices context.update(self.get_context(metric)) if minimal: template_name = "holodeck/widgets/base_minimal.html" else: template_name = "holodeck/widgets/base_full.html" context.update({"template_name": template_name, "widget_types": get_widget_type_choices()}) return render_to_string(self.template_name, context)
def render(self, metric, context, minimal): from holodeck.utils import get_widget_type_choices context.update(self.get_context(metric)) if minimal: template_name = "holodeck/widgets/base_minimal.html" else: template_name = "holodeck/widgets/base_full.html" context.update({ 'template_name': template_name, 'widget_types': get_widget_type_choices(), }) return render_to_string(self.template_name, context)
class Metric(models.Model): name = models.CharField(max_length=255) dashboard = models.ForeignKey('holodeck.Dashboard') widget_type = models.CharField(max_length=64, choices=get_widget_type_choices()) api_key = models.CharField(max_length=32, unique=True, blank=True, null=True) position = models.IntegerField( blank=True, null=True, ) def __unicode__(self): return self.name def render(self, context): return load_class_by_string(self.widget_type)().render(self, context) def export(self, workbook): """ Given a xlwt Excel workbook creates a sheet and populates it with samples for this metric. """ samples = {} worksheet = workbook.add_sheet(self.name) samples = self.sample_set.all() date_format = 'D-MMM-YY H:MM:SS' date_style = xlwt.easyxf(num_format_str=date_format) # Write distinct reverse sorted timestamps as first column. timestamps = list(set([sample.timestamp for sample in samples])) timestamps.sort(reverse=True) for i, timestamp in enumerate(timestamps): worksheet.write(i + 1, 0, timestamp, date_style) # Set timestamp column width. # Each character's approximated width is 256 worksheet.col(0).width = (1 + len(date_format)) * 256 # Write distinct sorted string values as first row. string_values = list(set([sample.string_value for sample in samples])) string_values.sort() for i, string_value in enumerate(string_values): worksheet.write(0, i + 1, string_value) worksheet.col(i + 1).width = (1 + len(string_value)) * 256 # Write sample values as they correspond to timestamp and string_value. for sample in samples: row = timestamps.index(sample.timestamp) + 1 col = string_values.index(sample.string_value) + 1 try: worksheet.write(row, col, sample.integer_value) if len(str(sample.integer_value)) > len(sample.string_value): worksheet.col( col).width = (1 + len(str(sample.integer_value))) * 256 except Exception, e: if 'overwrite' in e.message: # Ignore duplicate samples. # XXX: Enforce on import. pass else: raise e
class Metric(models.Model): name = models.CharField(max_length=255) export_name = models.CharField( max_length=31, null=True, blank=True, ) description = models.TextField( blank=True, null=True, ) dashboard = models.ForeignKey('holodeck.Dashboard') widget_type = models.CharField(max_length=64, choices=get_widget_type_choices()) api_key = models.CharField(max_length=32, unique=True, blank=True, null=True) share_key = models.CharField(max_length=32, unique=True, blank=True, null=True) position = models.IntegerField( blank=True, null=True, ) def __unicode__(self): return self.name @property def widget(self): return load_class_by_string(self.widget_type)() def render(self, context, minimal=False): return self.widget.render(self, context, minimal) def export(self, workbook, sheet_names): """ Given a xlwt Excel workbook creates a sheet and populates it with samples for this metric. """ # Resolve naming conflicts by appending a counter to the sheet name def add_sheet(name): counter = 1 suffix = "" while True: new_name = "%s%s" % (name[:31 - len(suffix)], suffix) if new_name not in sheet_names: return workbook.add_sheet(new_name) counter += 1 suffix = " %d" % counter # Use export_name instead of name if it exists worksheet = add_sheet( self.export_name if self.export_name else self.name) samples = self.sample_set.all() date_format = 'D-MMM-YY H:MM:SS' date_style = xlwt.easyxf(num_format_str=date_format) # Write distinct reverse sorted timestamps as first column. timestamps = list(set([sample.timestamp for sample in samples])) timestamps.sort(reverse=True) for i, timestamp in enumerate(timestamps): worksheet.write(i + 1, 0, timestamp, date_style) # Set timestamp column width. # Each character's approximated width is 256 worksheet.col(0).width = (1 + len(date_format)) * 256 # Write distinct sorted string values as first row. string_values = list(set([sample.string_value for sample in samples])) string_values.sort() for i, string_value in enumerate(string_values): worksheet.write(0, i + 1, string_value) worksheet.col(i + 1).width = (1 + len(string_value)) * 256 # Write sample values as they correspond to timestamp and string_value. for sample in samples: row = timestamps.index(sample.timestamp) + 1 col = string_values.index(sample.string_value) + 1 try: worksheet.write(row, col, sample.integer_value) if len(str(sample.integer_value)) > len(sample.string_value): worksheet.col( col).width = (1 + len(str(sample.integer_value))) * 256 except Exception, e: if 'overwrite' in e.message: # Ignore duplicate samples. # XXX: Enforce on import. pass else: raise e return worksheet