def bars_data(request):
    url = 'https://api.bitbucket.org/1.0/repositories/lukaszb/django-richtemplates/changesets/'
    response = urllib2.urlopen(url)
    jdata = loads(response.read())
    changesets = jdata['changesets']

    added = pyofc2.bar()
    modified = pyofc2.bar()
    removed = pyofc2.bar()
    added.values, modified.values, removed.values = [], [], []
    added.colour = '#00c700'
    added.text = 'Added'
    modified.colour = '#ffaa00'
    modified.text = 'Modified'
    removed.colour = '#f27474'
    removed.text = 'Removed'

    x = pyofc2.x_axis()
    y = pyofc2.y_axis(steps=5)
    y_max = 0
    labels = []

    for changeset in changesets:
        labels.append(pyofc2.x_axis_label(text='%s:%s' %
            (changeset['revision'], changeset['node'])))
        files = changeset['files']
        tip = '#val#<br>Date: %s<br>Revision: %s:%s<br>Author: %s<br>Message: %s'
        tip = tip % tuple([changeset[key] for key in (
            'timestamp', 'revision', 'node', 'author', 'message')])
        tip = '%s: ' + tip
        # Added files
        added_count = len([f for f in files if f['type'] == 'added'])
        added.values.append(pyofc2.barvalue(top=added_count, tip=tip % 'Added'))
        # Modified files
        modified_count = len([f for f in files if f['type'] == 'modified'])
        modified.values.append(pyofc2.barvalue(top=modified_count,
            tip=tip % 'Modified'))
        # Removed files
        removed_count = len([f for f in files if f['type'] == 'removed'])
        removed.values.append(pyofc2.barvalue(top=removed_count,
            tip=tip % 'Removed'))
        y_max = max([y_max, added_count, modified_count, removed_count])

    y.min = 0
    y.max = y_max

    title = pyofc2.title(text='django-richtemplates changesets')
    chart = pyofc2.open_flash_chart()
    chart.title = title
    chart.add_element(added)
    chart.add_element(modified)
    chart.add_element(removed)
    chart.bg_colour = '#ffffff'
    x.labels = pyofc2.x_axis_labels(steps=1, rotate=25.0)
    x.labels.labels = labels
    chart.x_axis = x
    chart.y_axis = y
    return HttpResponse(chart.render(), mimetype='application/json')
 def response(self, request, username, project_slug):
     import random
     import pyofc2
     t = pyofc2.title(text=_("Tasks"))
     b1 = pyofc2.bar()
     b1.values = range(10)
     b2 = pyofc2.bar()
     b2.values = [random.randint(0, 9) for i in range(9)]
     b2.colour = '#56acde'
     chart = pyofc2.open_flash_chart()
     chart.title = t
     chart.add_element(b1)
     chart.add_element(b2)
     return HttpResponse(chart.render())
 def response(self, request, username, project_slug):
     import random
     import pyofc2
     t = pyofc2.title(text=_("Tasks"))
     b1 = pyofc2.bar()
     b1.values = range(10)
     b2 = pyofc2.bar()
     b2.values = [random.randint(0,9) for i in range(9)]
     b2.colour = '#56acde'
     chart = pyofc2.open_flash_chart()
     chart.title = t
     chart.add_element(b1)
     chart.add_element(b2)
     return HttpResponse(chart.render())
Exemple #4
0
def generate_chart_data(request, address=None, fields=None, *args, **kwargs):
    '''Generates charting data for fields'''
    span = request.GET.get('span', None)
    colours = request.GET.get('colours', None)
    start = request.GET.get('start', None)
    end = request.GET.get('end', None)
    raw = request.GET.get('raw', "true").lower() == "true"
    smooth = request.GET.get('smooth', "false").lower() == "true"
    smooth_len = float(request.GET.get('smooth_factor', "0.2"))

    fields = fields.split(',')
    address = address.replace('_', ':')

    span = SPANS[span] * 60

    dev = models.SensorSDKRemoteDevice.objects.get(address=address)
    dev = models.get_subclass(dev)
    __check_field_is_valid(dev, fields)

    if end is None:
        end = datetime.now()

    if start is None:
        start = datetime.fromtimestamp(time.mktime(end.timetuple()) - span)

    qs = generate_data(dev=dev, fields=fields, start=start, end=end)
    data = qs.values('timestamp', *fields)
    if colours is not None:
        col = colours
        colours = {}
        for f in colours.split(','):
            field, colour = f.split('=', 1)
            colours[field] = colour
    else:
        colours = {}
        i = 0
        for field in fields:
            colours[field] = COLOURS[i]
            i += 1
            if i >= len(COLOURS):
                i = 0
        for field in fields:
            colours['%s_average' % field] = COLOURS[i]
            i += 1
            if i >= len(COLOURS):
                i = 0

    sets = {}

    for field in fields:
        sets[field] = clear_duplicates(data, field)

    if smooth:
        for f in fields:
            #sets[a] = [pyofc2.scatter_value(**vals) for vals in moving_average(sets(f), smooth_len)]
            s = [({'x': a['timestamp'], 'y': a[f]}) for a in data]
            sets["%s_average" % f] = low_pass_filter(s, smooth_len)

    x_axis = pyofc2.x_axis()

    x_axis.min = int(time.mktime(start.timetuple()))
    x_axis.max = int(time.mktime(end.timetuple()))
    x_axis.steps = (x_axis.max - x_axis.min) / 10

    if x_axis.max - x_axis.min <= 60 * 60 * 24:
        format = 'H:i'
    else:
        format = 'm-d-Y H:i'

    labels = [{
        'x': a,
        'text': '#date:%s#' % format,
    } for a in range(x_axis.min, x_axis.max + x_axis.steps, x_axis.steps)]
    x_axis.labels = pyofc2.x_axis_labels(labels=labels, rotate='45')

    y_axis = pyofc2.y_axis()
    y_axis.min = None
    y_axis.max = None
    for field in fields:
        b = qs.aggregate(min=Min(field), max=Max(field))
        if y_axis.min is None or y_axis.min > b['min']:
            y_axis.min = b['min']
        if y_axis.max is None or y_axis.max < b['max']:
            y_axis.max = b['max']
    if y_axis.min is None:
        y_axis.min = -1
    if y_axis.max is None:
        y_axis.max = 1
    y_axis.min = 0.9 * y_axis.min
    y_axis.max = 1.1 * y_axis.max

    y_axis.steps = (y_axis.max - y_axis.min) / 10

    chart = pyofc2.open_flash_chart()
    chart.title = pyofc2.title(text="%s [%s]: %s" %
                               (dev.friendly_name, address, fields))
    #chart.tooltip=pyofc2.tooltip(text="#date: Y-m-d H:i#<br>#y#\n")
    for c in sets:
        if not raw and not c.endswith('average'):
            continue
        if not smooth and c.endswith('average'):
            continue
        s = pyofc2.scatter_line()
        s.values = [pyofc2.scatter_value(x=x, y=y) for x, y in sets[c]]
        s.text = c
        s.dot_style = {
            'tip': "%s<br>#date:Y-m-d H:i#<br>%s: #val#" % (c, _("Value"))
        }
        if colours.get(c, None):
            s.colour = colours[c]
        chart.add_element(s)

    chart.x_axis = x_axis
    chart.y_axis = y_axis

    return HttpResponse(chart.render(), content_type='application/json')
def bars_data(request):
    url = 'https://api.bitbucket.org/1.0/repositories/lukaszb/django-richtemplates/changesets/'
    response = urllib2.urlopen(url)
    jdata = loads(response.read())
    changesets = jdata['changesets']

    added = pyofc2.bar()
    modified = pyofc2.bar()
    removed = pyofc2.bar()
    added.values, modified.values, removed.values = [], [], []
    added.colour = '#00c700'
    added.text = 'Added'
    modified.colour = '#ffaa00'
    modified.text = 'Modified'
    removed.colour = '#f27474'
    removed.text = 'Removed'

    x = pyofc2.x_axis()
    y = pyofc2.y_axis(steps=5)
    y_max = 0
    labels = []

    for changeset in changesets:
        labels.append(
            pyofc2.x_axis_label(text='%s:%s' %
                                (changeset['revision'], changeset['node'])))
        files = changeset['files']
        tip = '#val#<br>Date: %s<br>Revision: %s:%s<br>Author: %s<br>Message: %s'
        tip = tip % tuple([
            changeset[key]
            for key in ('timestamp', 'revision', 'node', 'author', 'message')
        ])
        tip = '%s: ' + tip
        # Added files
        added_count = len([f for f in files if f['type'] == 'added'])
        added.values.append(pyofc2.barvalue(top=added_count,
                                            tip=tip % 'Added'))
        # Modified files
        modified_count = len([f for f in files if f['type'] == 'modified'])
        modified.values.append(
            pyofc2.barvalue(top=modified_count, tip=tip % 'Modified'))
        # Removed files
        removed_count = len([f for f in files if f['type'] == 'removed'])
        removed.values.append(
            pyofc2.barvalue(top=removed_count, tip=tip % 'Removed'))
        y_max = max([y_max, added_count, modified_count, removed_count])

    y.min = 0
    y.max = y_max

    title = pyofc2.title(text='django-richtemplates changesets')
    chart = pyofc2.open_flash_chart()
    chart.title = title
    chart.add_element(added)
    chart.add_element(modified)
    chart.add_element(removed)
    chart.bg_colour = '#ffffff'
    x.labels = pyofc2.x_axis_labels(steps=1, rotate=25.0)
    x.labels.labels = labels
    chart.x_axis = x
    chart.y_axis = y
    return HttpResponse(chart.render(), mimetype='application/json')