def _partyline(self, party_results): if self.request.GET.get('percentages') == 'true': key = 'percentage' else: key = 'count' maxcount = 0 allcounts = [] granularity = self.request.GET.get('granularity') months = [] for party, results in party_results.iteritems(): counts = [x.get(key) for x in results['results']] allcounts.append(counts) if max(counts) > maxcount: maxcount = max(counts) if granularity == 'month': months = [x['month'] for x in results['results']] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [int((x / float(len(months))) * 100) for x in january_indexes] #times = [x.get(granularity) for x in results['results']] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 318)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if granularity == 'month': index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [label, ]) chart.set_axis_positions(index, [100, ]) for n, counts in enumerate(allcounts): chart.add_data(counts) chart.set_line_style(n, thickness=2) # Set line thickness colors = {'R': 'bb3110', 'D': '295e72', } chart_colors = [] chart_legend = [] for k in party_results.keys(): chart_colors.append(colors.get(k, '000000')) chart_legend.append(k) chart.legend_position = 'b' chart.set_colours(chart_colors) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend(chart_legend) return chart.get_url()
def show(request, isa_id): isa = get_object_or_404(ISA, pk=isa_id) isafunds = isa.isafund_set.all() start_date = isa.created end_date = date.today() current_date = start_date data = [] while current_date <= end_date: value = 0 for isafund in isafunds: try: fundprice = isafund.fund.fundprice_set.get(date=current_date) value += fundprice.price * isafund.quantity except FundPrice.DoesNotExist: pass data.append(value) current_date += timedelta(1) chart = SimpleLineChart(500, 200, y_range=[min(data), max(data)]) chart.add_data(data) chart.add_data([data[0], data[0]]) chart.set_colours(['0000FF', 'AAAAAA']) chart.fill_solid('bg', 'DDDDFF') chart.set_axis_labels(Axis.LEFT, [int(min(data)), int(max(data))]) chart.set_axis_labels(Axis.BOTTOM, [start_date, end_date]) url = chart.get_url() return render_to_response('isas/show.html', {'isa': isa, 'chart_url': url})
def group(request, group_id): group = get_object_or_404(GroupedMessage, pk=group_id) message_list = group.message_set.all() obj = message_list.order_by('-id')[0] if '__sentry__' in obj.data: module, args, frames = obj.data['__sentry__']['exc'] obj.class_name = str(obj.class_name) # We fake the exception class due to many issues with imports/builtins/etc exc_type = type(obj.class_name, (Exception,), {}) exc_value = exc_type(obj.message) exc_value.args = args reporter = ImprovedExceptionReporter(obj.request, exc_type, exc_value, frames, obj.data['__sentry__'].get('template')) traceback = mark_safe(reporter.get_traceback_html()) elif group.traceback: traceback = mark_safe('<pre>%s</pre>' % (group.traceback,)) unique_urls = message_list.filter(url__isnull=False).values_list('url', 'logger', 'view', 'checksum').annotate(times_seen=Count('url')).values('url', 'times_seen').order_by('-times_seen') unique_servers = message_list.filter(server_name__isnull=False).values_list('server_name', 'logger', 'view', 'checksum').annotate(times_seen=Count('server_name')).values('server_name', 'times_seen').order_by('-times_seen') def iter_data(obj): for k, v in obj.data.iteritems(): if k.startswith('_') or k in ['url']: continue yield k, v json_data = iter_data(obj) # TODO: this should be a template tag engine = get_db_engine() if SimpleLineChart and not engine.startswith('sqlite'): today = datetime.datetime.now() chart_qs = message_list\ .filter(datetime__gte=today - datetime.timedelta(hours=24))\ .extra(select={'hour': 'extract(hour from datetime)'}).values('hour')\ .annotate(num=Count('id')).values_list('hour', 'num') rows = dict(chart_qs) if rows: max_y = max(rows.values()) else: max_y = 1 chart = SimpleLineChart(300, 80, y_range=[0, max_y]) chart.add_data([max_y]*30) chart.add_data([rows.get((today-datetime.timedelta(hours=d)).hour, 0) for d in range(0, 24)][::-1]) chart.add_data([0]*30) chart.fill_solid(chart.BACKGROUND, 'eeeeee') chart.add_fill_range('eeeeee', 0, 1) chart.add_fill_range('e0ebff', 1, 2) chart.set_colours(['eeeeee', '999999', 'eeeeee']) chart.set_line_style(1, 1) chart_url = chart.get_url() return render_to_response('sentry/group/details.html', locals())
def _line(self, results): key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' counts = [x.get(key) for x in results] maxcount = max(counts) granularity = self.request.GET.get('granularity') times = [x.get(granularity) for x in results] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 300)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.add_data(counts) chart.set_line_style(0, thickness=2) # Set line thickness chart.set_colours([ 'E0B300', ]) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if self.request.GET.get('granularity') == 'month': months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [ int((x / float(len(months))) * 100) for x in january_indexes ] index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [ label, ]) chart.set_axis_positions(index, [ 100, ]) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend([ self.request.GET.get('phrase'), ]) return chart.get_url()
def show(request, fund_id): fund = get_object_or_404(Fund, pk=fund_id) prices = fund.fundprice_set.order_by('-date') data = [] for price in prices: data.append(price.price) data.reverse() chart = SimpleLineChart(500, 200, y_range=[min(data), max(data)]) chart.add_data(data) chart.set_colours(['0000FF']) chart.fill_solid('bg', 'DDDDFF') chart.set_axis_labels(Axis.LEFT, [min(data), max(data)]) start_date = prices[len(prices) - 1].date.isoformat() end_date = prices[0].date.isoformat() chart.set_axis_labels(Axis.BOTTOM, [start_date, end_date]) url = chart.get_url() return render_to_response('funds/show.html', {'fund': fund, 'prices': prices, 'graph_url': url})
def _line(self, results): key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' counts = [x.get(key) for x in results] maxcount = max(counts) granularity = self.request.GET.get('granularity') times = [x.get(granularity) for x in results] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 300)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.add_data(counts) chart.set_line_style(0, thickness=2) # Set line thickness chart.set_colours(['E0B300', ]) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if self.request.GET.get('granularity') == 'month': months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [int((x / float(len(months))) * 100) for x in january_indexes] index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [label, ]) chart.set_axis_positions(index, [100, ]) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend([self.request.GET.get('phrase'), ]) return chart.get_url()
def group_message_list(request, group_id): group = get_object_or_404(GroupedMessage, pk=group_id) message_list = group.message_set.all().order_by('-datetime') unique_urls = message_list.filter(url__isnull=False).values_list('url', 'logger', 'view', 'checksum').annotate(times_seen=Count('url')).values('url', 'times_seen').order_by('-times_seen') unique_servers = message_list.filter(server_name__isnull=False).values_list('server_name', 'logger', 'view', 'checksum').annotate(times_seen=Count('server_name')).values('server_name', 'times_seen').order_by('-times_seen') engine = get_db_engine() if SimpleLineChart and not engine.startswith('sqlite'): today = datetime.datetime.now() chart_qs = message_list\ .filter(datetime__gte=today - datetime.timedelta(hours=24))\ .extra(select={'hour': 'extract(hour from datetime)'}).values('hour')\ .annotate(num=Count('id')).values_list('hour', 'num') rows = dict(chart_qs) if rows: max_y = max(rows.values()) else: max_y = 1 chart = SimpleLineChart(300, 80, y_range=[0, max_y]) chart.add_data([max_y]*30) chart.add_data([rows.get((today-datetime.timedelta(hours=d)).hour, 0) for d in range(0, 24)][::-1]) chart.add_data([0]*30) chart.fill_solid(chart.BACKGROUND, 'eeeeee') chart.add_fill_range('eeeeee', 0, 1) chart.add_fill_range('e0ebff', 1, 2) chart.set_colours(['eeeeee', '999999', 'eeeeee']) chart.set_line_style(1, 1) chart_url = chart.get_url() page = 'messages' return render_to_response('sentry/group/message_list.html', locals())
def chart_url(group): today = datetime.datetime.now() chart_qs = group.message_set.all()\ .filter(datetime__gte=today - datetime.timedelta(hours=24))\ .extra(select={'hour': 'extract(hour from datetime)'}).values('hour')\ .annotate(num=Count('id')).values_list('hour', 'num') rows = dict(chart_qs) if rows: max_y = max(rows.values()) else: max_y = 1 chart = SimpleLineChart(300, 80, y_range=[0, max_y]) chart.add_data([max_y]*30) chart.add_data([rows.get((today-datetime.timedelta(hours=d)).hour, 0) for d in range(0, 24)][::-1]) chart.add_data([0]*30) chart.fill_solid(chart.BACKGROUND, 'eeeeee') chart.add_fill_range('eeeeee', 0, 1) chart.add_fill_range('e0ebff', 1, 2) chart.set_colours(['eeeeee', '999999', 'eeeeee']) chart.set_line_style(1, 1) return chart.get_url()
def read(self, request, *args, **kwargs): self.request = request if kwargs.get('chart_type') == 'timeline': handler = PhraseOverTimeHandler() if request.GET.get('split_by_party') == 'true': resultsets = {} for party in ['R', 'D', ]: kwargs['party'] = party resultsets[party] = handler.read(request, *args, **kwargs) return {'results': {'url': self._partyline(resultsets), }, } elif request.GET.get('compare') == 'true': phrases = request.GET.get('phrases', '').split(',')[:5] # Max of 5 phrases parties = request.GET.get('parties', '').split(',') states = request.GET.get('states', '').split(',') #chambers = request.GET.get('chambers', '').split(',') colors = ['8E2844', 'A85B08', 'AF9703', ] metadata = [] legend_items = [] months = None key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' granularity = self.request.GET.get('granularity') width = int(request.GET.get('width', 575)) height = int(request.GET.get('height', 300)) chart = SimpleLineChart(width, height) chart.set_grid(0, 50, 2, 5) # Set gridlines chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_colours(colors) maxcount = 0 # Use phrases as a baseline; that is, assume that # there's a corresponding value for the other filters. # If a filter doesn't have as many values as the number # of phrases, the corresponding phrase will not be # filtered. # (However, if a value is set for 'party' or 'state' # in the querystring, that will override any values # set in 'phrases' or 'parties.') for n, phrase in enumerate(phrases): chart.set_line_style(n, thickness=2) # Set line thickness if not phrase.strip(): continue kwargs['phrase'] = phrase legend = phrase try: kwargs['party'] = parties[n] except IndexError: pass try: kwargs['state'] = states[n] except IndexError: pass if kwargs.get('party') and kwargs.get('state'): legend += ' (%(party)s, %(state)s)' % kwargs elif kwargs.get('party'): legend += ' (%(party)s)' % kwargs elif kwargs.get('state'): legend += ' (%(state)s)' % kwargs legend_items.append(legend) data = handler.read(request, *args, **kwargs) results = data['results'] counts = [x.get(key) for x in results] if max(counts) > maxcount: maxcount = max(counts) chart.add_data(counts) metadata.append(kwargs) # Duplicated code; should move into separate function. if self.request.GET.get('granularity') == 'month': if not months: months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [int((x / float(len(months))) * 100) for x in january_indexes] index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) chart.y_range = (0, maxcount) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [label, ]) chart.set_axis_positions(index, [100, ]) # Always include a legend when comparing. chart.set_legend(legend_items) return {'results': {'metadata': metadata, 'url': chart.get_url()}} #return resultsets else: data = handler.read(request, *args, **kwargs) return {'results': {'url': self._line(data['results']), }, } elif kwargs.get('chart_type') == 'pie': handler = PhraseByCategoryHandler() kwargs['entity_type'] = request.GET.get('entity_type') data = handler.read(request, *args, **kwargs) if request.GET.get('output') == 'data': return {'data': self._pie(data['results'])} return {'results': {'url': self._pie(data['results']), }, } return {'error': 'Invalid chart type.', }
def get_chart_url(): cache_key = 'phonedb-chart-url-%s' % settings.LANGUAGE_CODE url = cache.get(cache_key) if url is not None: return url enddate = datetime.datetime.now() # This works badly, we will rather render only chart for month after # it has finished #+ datetime.timedelta(days=30) endyear = enddate.year endmonthlast = enddate.month endmonth = 12 dates = [] unsupported = [] supported = [] totals = [] alls = [] years = [] for year in xrange(2006, endyear + 1): if year == endyear: endmonth = endmonthlast for month in xrange(1, endmonth + 1): if month == 1: years.append('%d' % year) else: years.append('') time_range = (datetime.date(1900, 1, 1), datetime.date(year, month, 1)) supported_val = Phone.objects.exclude(state = 'deleted').filter(connection__isnull = False).filter(created__range = time_range).count() unsupported_val = Phone.objects.exclude(state = 'deleted').filter(connection__isnull = True).filter(created__range = time_range).count() all_val = Phone.objects.filter(created__lt = datetime.date(year, month, 1)).count() supported.append(supported_val) unsupported.append(unsupported_val) totals.append(unsupported_val + supported_val) alls.append(all_val) dates.append('%d-%02d' % (year, month)) #print dates #print unsupported #print supported #print totals #print alls max_y = ((max(alls) / 100) + 1) * 100 chart = SimpleLineChart(800, 300, y_range=[0, max_y]) chart.fill_solid(chart.BACKGROUND, 'ffd480') chart.fill_solid(chart.CHART, 'ffd480') # Chart data chart.add_data(supported) chart.add_data(totals) chart.add_data(alls) # Lowest value chart.add_data([0] * 2) # Set the line colour to blue chart.set_colours(['00FF00', 'FF0000', '0000FF', '00000000']) #chart.add_fill_range('76A4FB', 2, 3) # Set the vertical stripes month_stripes = 3.0 chart.fill_linear_stripes(Chart.CHART, 0, 'ffe4a0', month_stripes / len(alls), 'ffd480', month_stripes / len(alls)) # Set the horizontal dotted lines chart.set_grid(0, 10, 5, 5) chart.set_legend([ _('Supported phones').encode('utf-8'), _('Approved records').encode('utf-8'), _('Total records').encode('utf-8')]) left_axis = map(lambda x: '%d' % x, xrange(0, max_y + 1, max_y / 10)) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, years) url = chart.get_url() cache.set(cache_key, url, 3600) return url
def _partyline(self, party_results): if self.request.GET.get('percentages') == 'true': key = 'percentage' else: key = 'count' maxcount = 0 allcounts = [] granularity = self.request.GET.get('granularity') months = [] for party, results in party_results.iteritems(): counts = [x.get(key) for x in results['results']] allcounts.append(counts) if max(counts) > maxcount: maxcount = max(counts) if granularity == 'month': months = [x['month'] for x in results['results']] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [ int((x / float(len(months))) * 100) for x in january_indexes ] #times = [x.get(granularity) for x in results['results']] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 318)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if granularity == 'month': index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [ label, ]) chart.set_axis_positions(index, [ 100, ]) for n, counts in enumerate(allcounts): chart.add_data(counts) chart.set_line_style(n, thickness=2) # Set line thickness colors = { 'R': 'bb3110', 'D': '295e72', } chart_colors = [] chart_legend = [] for k in party_results.keys(): chart_colors.append(colors.get(k, '000000')) chart_legend.append(k) chart.legend_position = 'b' chart.set_colours(chart_colors) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend(chart_legend) return chart.get_url()
def render(self,data={}, context={}): assert self.series is not None, "'chart.series' must be set" converter = wfcommon.units.Converter(context["units"]) # merge builtin defaults, context and renderer config config = ChartConfig() if context.has_key('chart'): config.__dict__.update(context['chart']) config.__dict__.update(self.__dict__) # create the chart chart = SimpleLineChart(config.width, config.height) colors = [] legend_set = False legend = [] chart_min = sys.maxint chart_max = -sys.maxint # Prepare series config ordered_series = [] for key, serie in self.series.iteritems(): serie_config = ChartConfig() serie_config.__dict__.update(config.__dict__) serie_config.__dict__.update(serie) ordered_series.append( (serie_config.order, key, serie) ) ordered_series.sort( cmp=lambda x,y: cmp(x[0],y[0]) ) ordered_keys = [] for order, key, serie in ordered_series: ordered_keys.append(key) # Draws for each serie index=0 for order, key, serie in ordered_series: serie_config = ChartConfig() serie_config.__dict__.update(config.__dict__) serie_config.__dict__.update(serie) serie_data = copy.copy(data[key.split('.')[0]]['series'][key.split('.')[1]]) measure = key.split('.')[0] if flat(serie_data): # Series with all data = None continue if serie_config.accumulate: serie_data = accumulate(serie_data) elif serie_config.interpolate: serie_data = interpolate(serie_data) # Compute min and max value for the serie and the whole chart min_data = amin(serie_data) chart_min = rmin(chart_min, min_data) if serie_data.__contains__(min_data): min_index = serie_data.index(min_data) else: min_index = None max_data = max(serie_data) chart_max = max(chart_max, max_data) if serie_data.__contains__(max_data): max_index = serie_data.index(max_data) else: max_index = None (serie_data, min_index, max_index) = compress_to(serie_data, config.nval, min_index, max_index) chart.add_data(serie_data) colors.append(_valid_color(serie_config.color)) if serie_config.max and not max_index == None : max_config = ChartConfig() max_config.__dict__.update(serie_config.__dict__) max_config.__dict__.update(serie_config.max) str_max_data = str(round(converter.convert(measure, max_data), 1)) chart.add_marker(index, max_index, 't'+str_max_data, _valid_color(max_config.text), max_config.size) chart.add_marker(index, max_index, max_config.style, _valid_color(max_config.color), max_config.thickness) if serie_config.min and not min_index == None: min_config = ChartConfig() min_config.__dict__.update(serie_config.__dict__) min_config.__dict__.update(serie_config.min) str_min_data = str(round(converter.convert(measure, min_data), 1)) chart.add_marker(index, min_index, 't'+str_min_data, _valid_color(min_config.text), min_config.size) chart.add_marker(index, min_index, min_config.style, _valid_color(min_config.color), min_config.thickness) if serie_config.last: last_config = ChartConfig() last_config.__dict__.update(serie_config.__dict__) last_config.__dict__.update(serie_config.last) last_index=len(serie_data)-1 last_data = serie_data[last_index] if last_data: str_last_data = str(round(converter.convert(measure, last_data), 1)) chart.add_marker(index, last_index, 't'+str(last_data), _valid_color(last_config.text), last_config.size) chart.add_marker(index, last_index, last_config.style, _valid_color(last_config.color), last_config.thickness) if serie_config.area: fill_config = ChartConfig() fill_config.__dict__.update(serie_config.__dict__) fill_config.__dict__.update(serie_config.area) to = ordered_keys.index(fill_config.to) chart.add_fill_range(_valid_color(fill_config.color), index, to) if serie_config.dash: chart.set_line_style(index, serie_config.thickness, serie_config.dash, serie_config.dash) else: chart.set_line_style(index, serie_config.thickness) if serie_config.legend: legend.append(serie_config.legend) legend_set = True else: legend.append('') if serie_config.marks: mark_config = ChartConfig() mark_config.__dict__.update(serie_config.__dict__) mark_config.__dict__.update(serie_config.marks) mark_data = copy.copy(data[mark_config.serie.split('.')[0]]['series'][mark_config.serie.split('.')[1]]) mark_data = compress_to(mark_data, config.nval, min_index, max_index)[0] for i, m in enumerate(mark_data): if not m: mark_data[i] = " " density = max(1.0, 1.0 * mark_config.space * len("".join(mark_data))*mark_config.size / config.width) for i, v in enumerate(mark_data): if (i +1) % round(density) == 0: if serie_data[i] != 0: text = str(mark_data[i]) else: text = " " chart.add_marker(index, i, 't'+text, _valid_color(mark_config.color), mark_config.size) index = index + 1 # Compute vertical range if config.axes: range_min_ref_units = 0 if not chart_min == sys.maxint and not chart_max == -sys.maxint: range_min = chart_min-config.ymargin[0] range_max = chart_max+config.ymargin[1] range_min_target_units = math.floor(converter.convert(measure, range_min)) range_max_target_units = math.ceil(converter.convert(measure, range_max)) range_min_ref_units = converter.convert_back(measure, range_min_target_units) range_max_ref_units = converter.convert_back(measure, range_max_target_units) self.logger.debug("Y range: "+str(range_min_target_units) +" "+str(range_max_target_units)) chart.set_axis_range(Axis.LEFT, range_min_target_units, range_max_target_units+1) chart.add_data([range_min_ref_units, range_max_ref_units]) colors.append("00000000") else: chart.set_axis_range(Axis.LEFT, 0, 100) chart.set_axis_style(0, _valid_color(config.text), config.size, 0, Axis.BOTH if config.ticks else Axis.AXIS_LINES) else: chart.set_axis_labels(Axis.LEFT, []) chart.set_axis_style(0, _valid_color(config.text), config.size, 0, Axis.TICK_MARKS, _valid_color(config.bgcolor)) if config.zero and config.axes and range_min_ref_units < 0 and range_max_ref_units > 0: zero_config = ChartConfig() zero_config.__dict__.update(config.__dict__) zero_config.__dict__.update(config.zero) chart.add_data([0]*2) colors.append(_valid_color(zero_config.color)) chart.set_line_style(index, zero_config.thickness) chart.set_colours(colors) chart.fill_solid(Chart.BACKGROUND, _valid_color(config.bgcolor)) if legend_set: chart.set_legend(legend) chart.set_legend_position(config.legend_pos) if self.labels: labels_data = copy.copy(data[self.labels.split('.')[0]]['series'][self.labels.split('.')[1]]) labels_data = compress_to(labels_data, config.nval, None, None)[0] if config.axes: density = 1.0 * len("".join(labels_data))*config.size / config.width if density > LABEL_DENSITY_THRESHOLD: for i, v in enumerate(labels_data): if i % round(density) != 0: labels_data[i] = ' ' chart.set_axis_labels(Axis.BOTTOM, labels_data) chart.set_axis_style(1, _valid_color(config.text), config.size, 0, Axis.BOTH if config.ticks else Axis.AXIS_LINES) else: chart.set_axis_labels(Axis.BOTTOM, []) chart.set_axis_style(1, _valid_color(config.text), config.size, 0, Axis.TICK_MARKS, _valid_color(config.color)) try: return chart.get_url()+"&chma=10,10,10,10" # add a margin except: self.logger.exception("Could not render chart") return "http://chart.apis.google.com/chart?cht=lc&chs="+str(config.width)+"x"+str(config.height)
def read(self, request, *args, **kwargs): self.request = request if kwargs.get('chart_type') == 'timeline': handler = PhraseOverTimeHandler() if request.GET.get('split_by_party') == 'true': resultsets = {} for party in [ 'R', 'D', ]: kwargs['party'] = party resultsets[party] = handler.read(request, *args, **kwargs) return { 'results': { 'url': self._partyline(resultsets), }, } elif request.GET.get('compare') == 'true': phrases = request.GET.get( 'phrases', '').split(',')[:5] # Max of 5 phrases parties = request.GET.get('parties', '').split(',') states = request.GET.get('states', '').split(',') #chambers = request.GET.get('chambers', '').split(',') colors = [ '8E2844', 'A85B08', 'AF9703', ] metadata = [] legend_items = [] months = None key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' granularity = self.request.GET.get('granularity') width = int(request.GET.get('width', 575)) height = int(request.GET.get('height', 300)) chart = SimpleLineChart(width, height) chart.set_grid(0, 50, 2, 5) # Set gridlines chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_colours(colors) maxcount = 0 # Use phrases as a baseline; that is, assume that # there's a corresponding value for the other filters. # If a filter doesn't have as many values as the number # of phrases, the corresponding phrase will not be # filtered. # (However, if a value is set for 'party' or 'state' # in the querystring, that will override any values # set in 'phrases' or 'parties.') for n, phrase in enumerate(phrases): chart.set_line_style(n, thickness=2) # Set line thickness if not phrase.strip(): continue kwargs['phrase'] = phrase legend = phrase try: kwargs['party'] = parties[n] except IndexError: pass try: kwargs['state'] = states[n] except IndexError: pass if kwargs.get('party') and kwargs.get('state'): legend += ' (%(party)s, %(state)s)' % kwargs elif kwargs.get('party'): legend += ' (%(party)s)' % kwargs elif kwargs.get('state'): legend += ' (%(state)s)' % kwargs legend_items.append(legend) data = handler.read(request, *args, **kwargs) results = data['results'] counts = [x.get(key) for x in results] if max(counts) > maxcount: maxcount = max(counts) chart.add_data(counts) metadata.append(kwargs) # Duplicated code; should move into separate function. if self.request.GET.get('granularity') == 'month': if not months: months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [ int((x / float(len(months))) * 100) for x in january_indexes ] index = chart.set_axis_labels( Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions( index, [x for x in january_percentages[::2]]) chart.y_range = (0, maxcount) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [ label, ]) chart.set_axis_positions(index, [ 100, ]) # Always include a legend when comparing. chart.set_legend(legend_items) return { 'results': { 'metadata': metadata, 'url': chart.get_url() } } #return resultsets else: data = handler.read(request, *args, **kwargs) return { 'results': { 'url': self._line(data['results']), }, } elif kwargs.get('chart_type') == 'pie': handler = PhraseByCategoryHandler() kwargs['entity_type'] = request.GET.get('entity_type') data = handler.read(request, *args, **kwargs) if request.GET.get('output') == 'data': return {'data': self._pie(data['results'])} return { 'results': { 'url': self._pie(data['results']), }, } return { 'error': 'Invalid chart type.', }
def group(request, group_id): group = GroupedMessage.objects.get(pk=group_id) message_list = group.message_set.all() obj = message_list[0] if '__sentry__' in obj.data: module, args, frames = obj.data['__sentry__']['exc'] obj.class_name = str(obj.class_name) # We fake the exception class due to many issues with imports/builtins/etc exc_type = type(obj.class_name, (Exception,), {}) exc_value = exc_type(obj.message) exc_value.args = args fake_request = FakeRequest() fake_request.META = obj.data.get('META', {}) fake_request.GET = obj.data.get('GET', {}) fake_request.POST = obj.data.get('POST', {}) fake_request.FILES = obj.data.get('FILES', {}) fake_request.COOKIES = obj.data.get('COOKIES', {}) fake_request.url = obj.url if obj.url: fake_request.path_info = '/' + obj.url.split('/', 3)[-1] else: fake_request.path_info = '' reporter = ImprovedExceptionReporter(fake_request, exc_type, exc_value, frames, obj.data['__sentry__'].get('template')) traceback = mark_safe(reporter.get_traceback_html()) elif group.traceback: traceback = mark_safe('<pre>%s</pre>' % (group.traceback,)) unique_urls = message_list.filter(url__isnull=False).values_list('url', 'logger', 'view', 'checksum').annotate(times_seen=Count('url')).values('url', 'times_seen').order_by('-times_seen') unique_servers = message_list.filter(server_name__isnull=False).values_list('server_name', 'logger', 'view', 'checksum').annotate(times_seen=Count('server_name')).values('server_name', 'times_seen').order_by('-times_seen') if SimpleLineChart: today = datetime.datetime.now() chart_qs = message_list\ .filter(datetime__gte=today - datetime.timedelta(hours=24))\ .extra(select={'hour': 'extract(hour from datetime)'}).values('hour')\ .annotate(num=Count('id')).values_list('hour', 'num') rows = dict(chart_qs) if rows: max_y = max(rows.values()) else: max_y = 1 chart = SimpleLineChart(300, 80, y_range=[0, max_y]) chart.add_data([max_y]*30) chart.add_data([rows.get((today-datetime.timedelta(hours=d)).hour, 0) for d in range(0, 24)][::-1]) chart.add_data([0]*30) chart.fill_solid(chart.BACKGROUND, 'eeeeee') chart.add_fill_range('eeeeee', 0, 1) chart.add_fill_range('e0ebff', 1, 2) chart.set_colours(['eeeeee', '999999', 'eeeeee']) chart.set_line_style(1, 1) chart_url = chart.get_url() return render_to_response('sentry/group.html', locals())
def group(request, group_id): group = GroupedMessage.objects.get(pk=group_id) message_list = group.message_set.all() obj = message_list[0] if "__sentry__" in obj.data: module, args, frames = obj.data["__sentry__"]["exc"] obj.class_name = str(obj.class_name) # We fake the exception class due to many issues with imports/builtins/etc exc_type = type(obj.class_name, (Exception,), {}) exc_value = exc_type(obj.message) exc_value.args = args fake_request = FakeRequest() fake_request.META = obj.data.get("META", {}) fake_request.GET = obj.data.get("GET", {}) fake_request.POST = obj.data.get("POST", {}) fake_request.FILES = obj.data.get("FILES", {}) fake_request.COOKIES = obj.data.get("COOKIES", {}) fake_request.url = obj.url if obj.url: fake_request.path_info = "/" + obj.url.split("/", 3)[-1] else: fake_request.path_info = "" reporter = ImprovedExceptionReporter( fake_request, exc_type, exc_value, frames, obj.data["__sentry__"].get("template") ) traceback = mark_safe(reporter.get_traceback_html()) else: traceback = mark_safe("<pre>%s</pre>" % (group.traceback,)) unique_urls = ( message_list.filter(url__isnull=False) .values_list("url", "logger", "view", "checksum") .annotate(times_seen=Count("url")) .values("url", "times_seen") .order_by("-times_seen") ) unique_servers = ( message_list.filter(server_name__isnull=False) .values_list("server_name", "logger", "view", "checksum") .annotate(times_seen=Count("server_name")) .values("server_name", "times_seen") .order_by("-times_seen") ) today = datetime.datetime.now() chart_qs = ( message_list.filter(datetime__gte=today - datetime.timedelta(hours=24)) .extra(select={"hour": "extract(hour from datetime)"}) .values("hour") .annotate(num=Count("id")) .values_list("hour", "num") ) rows = dict(chart_qs) if rows: max_y = max(rows.values()) else: max_y = 1 if SimpleLineChart: chart = SimpleLineChart(384, 80, y_range=[0, max_y]) chart.add_data([max_y] * 30) chart.add_data([rows.get((today - datetime.timedelta(hours=d)).hour, 0) for d in range(0, 24)][::-1]) chart.add_data([0] * 30) chart.fill_solid(chart.BACKGROUND, "eeeeee") chart.add_fill_range("eeeeee", 0, 1) chart.add_fill_range("e0ebff", 1, 2) chart.set_colours(["eeeeee", "999999", "eeeeee"]) chart.set_line_style(1, 1) chart_url = chart.get_url() return render_to_response("sentry/group.html", locals())
def index(request): logger_names = SortedDict((l, l) for l in GroupedMessage.objects.values_list("logger", flat=True).distinct()) server_names = SortedDict((l, l) for l in Message.objects.values_list("server_name", flat=True).distinct()) level_names = SortedDict((str(k), v) for k, v in LOG_LEVELS) logger = request.GET.get("logger") server_name = request.GET.get("server_name") or "" level = request.GET.get("level") or "" try: page = int(request.GET.get("p", 1)) except (TypeError, ValueError): page = 1 realtime = page == 1 if logger not in logger_names: logger = "" if server_name not in server_names: server_name = "" if level not in level_names: level = "" # this only works in postgres message_list = ( GroupedMessage.objects.filter(status=0) .extra( select={"score": "times_seen / (pow((floor(extract(epoch from now() - last_seen) / 3600) + 2), 1.25) + 1)"} ) .order_by("-score", "-last_seen") ) today = datetime.datetime.now() chart_qs = ( Message.objects.filter(datetime__gte=today - datetime.timedelta(hours=24)) .extra(select={"hour": "extract(hour from datetime)"}) .values("hour") .annotate(num=Count("id")) .values_list("hour", "num") ) if logger: message_list = message_list.filter(logger=logger) chart_qs = chart_qs.filter(logger=logger) if level: message_list = message_list.filter(level=level) chart_qs = chart_qs.filter(level=level) if server_name: message_list = message_list.filter(message_set__server_name=server_name).distinct() chart_qs = chart_qs.filter(server_name=server_name) rows = dict(chart_qs) if rows: max_y = max(rows.values()) else: max_y = 1 if SimpleLineChart: chart = SimpleLineChart(384, 80, y_range=[0, max_y]) chart.add_data([max_y] * 30) chart.add_data([rows.get((today - datetime.timedelta(hours=d)).hour, 0) for d in range(0, 24)][::-1]) chart.add_data([0] * 30) chart.fill_solid(chart.BACKGROUND, "eeeeee") chart.add_fill_range("eeeeee", 0, 1) chart.add_fill_range("e0ebff", 1, 2) chart.set_colours(["eeeeee", "999999", "eeeeee"]) chart.set_line_style(1, 1) chart_url = chart.get_url() return render_to_response("sentry/index.html", locals())
def render(self, data={}, context={}): assert self.series is not None, "'chart.series' must be set" converter = wfcommon.units.Converter(context["units"]) # merge builtin defaults, context and renderer config config = ChartConfig() if context.has_key('chart'): config.__dict__.update(context['chart']) config.__dict__.update(self.__dict__) # create the chart chart = SimpleLineChart(config.width, config.height) colors = [] legend_set = False legend = [] chart_min = sys.maxint chart_max = -sys.maxint # Prepare series config ordered_series = [] for key, serie in self.series.iteritems(): serie_config = ChartConfig() serie_config.__dict__.update(config.__dict__) serie_config.__dict__.update(serie) ordered_series.append((serie_config.order, key, serie)) ordered_series.sort(cmp=lambda x, y: cmp(x[0], y[0])) ordered_keys = [] for order, key, serie in ordered_series: ordered_keys.append(key) # Draws for each serie index = 0 for order, key, serie in ordered_series: serie_config = ChartConfig() serie_config.__dict__.update(config.__dict__) serie_config.__dict__.update(serie) serie_data = copy.copy( data[key.split('.')[0]]['series'][key.split('.')[1]]) measure = key.split('.')[0] if flat(serie_data): # Series with all data = None continue if serie_config.accumulate: serie_data = accumulate(serie_data) elif serie_config.interpolate: serie_data = interpolate(serie_data) # Compute min and max value for the serie and the whole chart min_data = amin(serie_data) chart_min = rmin(chart_min, min_data) if serie_data.__contains__(min_data): min_index = serie_data.index(min_data) else: min_index = None max_data = max(serie_data) chart_max = max(chart_max, max_data) if serie_data.__contains__(max_data): max_index = serie_data.index(max_data) else: max_index = None (serie_data, min_index, max_index) = compress_to(serie_data, config.nval, min_index, max_index) chart.add_data(serie_data) colors.append(_valid_color(serie_config.color)) if serie_config.max and not max_index == None: max_config = ChartConfig() max_config.__dict__.update(serie_config.__dict__) max_config.__dict__.update(serie_config.max) str_max_data = str( round(converter.convert(measure, max_data), 1)) chart.add_marker(index, max_index, 't' + str_max_data, _valid_color(max_config.text), max_config.size) chart.add_marker(index, max_index, max_config.style, _valid_color(max_config.color), max_config.thickness) if serie_config.min and not min_index == None: min_config = ChartConfig() min_config.__dict__.update(serie_config.__dict__) min_config.__dict__.update(serie_config.min) str_min_data = str( round(converter.convert(measure, min_data), 1)) chart.add_marker(index, min_index, 't' + str_min_data, _valid_color(min_config.text), min_config.size) chart.add_marker(index, min_index, min_config.style, _valid_color(min_config.color), min_config.thickness) if serie_config.last: last_config = ChartConfig() last_config.__dict__.update(serie_config.__dict__) last_config.__dict__.update(serie_config.last) last_index = len(serie_data) - 1 last_data = serie_data[last_index] if last_data: str_last_data = str( round(converter.convert(measure, last_data), 1)) chart.add_marker(index, last_index, 't' + str(last_data), _valid_color(last_config.text), last_config.size) chart.add_marker(index, last_index, last_config.style, _valid_color(last_config.color), last_config.thickness) if serie_config.area: fill_config = ChartConfig() fill_config.__dict__.update(serie_config.__dict__) fill_config.__dict__.update(serie_config.area) to = ordered_keys.index(fill_config.to) chart.add_fill_range(_valid_color(fill_config.color), index, to) if serie_config.dash: chart.set_line_style(index, serie_config.thickness, serie_config.dash, serie_config.dash) else: chart.set_line_style(index, serie_config.thickness) if serie_config.legend: legend.append(serie_config.legend) legend_set = True else: legend.append('') if serie_config.marks: mark_config = ChartConfig() mark_config.__dict__.update(serie_config.__dict__) mark_config.__dict__.update(serie_config.marks) mark_data = copy.copy(data[mark_config.serie.split( '.')[0]]['series'][mark_config.serie.split('.')[1]]) mark_data = compress_to(mark_data, config.nval, min_index, max_index)[0] for i, m in enumerate(mark_data): if not m: mark_data[i] = " " density = max( 1.0, 1.0 * mark_config.space * len("".join(mark_data)) * mark_config.size / config.width) for i, v in enumerate(mark_data): if (i + 1) % round(density) == 0: if serie_data[i] != 0: text = str(mark_data[i]) else: text = " " chart.add_marker(index, i, 't' + text, _valid_color(mark_config.color), mark_config.size) index = index + 1 # Compute vertical range if config.axes: range_min_ref_units = 0 if not chart_min == sys.maxint and not chart_max == -sys.maxint: range_min = chart_min - config.ymargin[0] range_max = chart_max + config.ymargin[1] range_min_target_units = math.floor( converter.convert(measure, range_min)) range_max_target_units = math.ceil( converter.convert(measure, range_max)) range_min_ref_units = converter.convert_back( measure, range_min_target_units) range_max_ref_units = converter.convert_back( measure, range_max_target_units) self.logger.debug("Y range: " + str(range_min_target_units) + " " + str(range_max_target_units)) chart.set_axis_range(Axis.LEFT, range_min_target_units, range_max_target_units + 1) chart.add_data([range_min_ref_units, range_max_ref_units]) colors.append("00000000") else: chart.set_axis_range(Axis.LEFT, 0, 100) chart.set_axis_style( 0, _valid_color(config.text), config.size, 0, Axis.BOTH if config.ticks else Axis.AXIS_LINES) else: chart.set_axis_labels(Axis.LEFT, []) chart.set_axis_style(0, _valid_color(config.text), config.size, 0, Axis.TICK_MARKS, _valid_color(config.bgcolor)) if config.zero and config.axes and range_min_ref_units < 0 and range_max_ref_units > 0: zero_config = ChartConfig() zero_config.__dict__.update(config.__dict__) zero_config.__dict__.update(config.zero) chart.add_data([0] * 2) colors.append(_valid_color(zero_config.color)) chart.set_line_style(index, zero_config.thickness) chart.set_colours(colors) chart.fill_solid(Chart.BACKGROUND, _valid_color(config.bgcolor)) if legend_set: chart.set_legend(legend) chart.set_legend_position(config.legend_pos) if self.labels: labels_data = copy.copy(data[self.labels.split('.')[0]]['series'][ self.labels.split('.')[1]]) labels_data = compress_to(labels_data, config.nval, None, None)[0] if config.axes: density = 1.0 * len( "".join(labels_data)) * config.size / config.width if density > LABEL_DENSITY_THRESHOLD: for i, v in enumerate(labels_data): if i % round(density) != 0: labels_data[i] = ' ' chart.set_axis_labels(Axis.BOTTOM, labels_data) chart.set_axis_style( 1, _valid_color(config.text), config.size, 0, Axis.BOTH if config.ticks else Axis.AXIS_LINES) else: chart.set_axis_labels(Axis.BOTTOM, []) chart.set_axis_style(1, _valid_color(config.text), config.size, 0, Axis.TICK_MARKS, _valid_color(config.color)) try: return chart.get_url() + "&chma=10,10,10,10" # add a margin except: self.logger.exception("Could not render chart") return "http://chart.apis.google.com/chart?cht=lc&chs=" + str( config.width) + "x" + str(config.height)