缺少json格式的写入值

时间:2013-12-18 11:18:35

标签: python json graphite

我尝试从石墨webapp请求json格式的数据时缺少最后一个值。以下是来自石墨网络应用程序中的render / views.py。我试图使用阈值函数来获取常量数据。当使用constantLine / threshold函数时,如何强制石墨webapp以json格式输出两个点。

原始格式:

if format == 'raw':
      response = HttpResponse(mimetype='text/plain')
      for series in data:
        response.write( "%s,%d,%d,%d|" % (series.name, series.start, series.end, series.step) )
        response.write( ','.join(map(str,series)) )
        response.write('\n')

我的输出是:

stats.gauges.server1.throughput,1387364850,1387364910,10|1190.0,1190.0,1190.0,1190.0,1190.0,1190.0
hello,1387364847,1387364907,60|45,45

然而,写给json

if format == 'json':
      series_data = []
      for series in data:
        timestamps = range(int(series.start), int(series.end), int(series.step))
        datapoints = zip(series, timestamps)
        series_data.append( dict(target=series.name, datapoints=datapoints) )

我的输出是:

[{"target": "stats.gauges.server1.throughput", "datapoints": [[1190.0, 1387364980], [1190.0, 1387364990], [1190.0, 1387365000], [1190.0, 1387365010], [1190.0, 1387365020], [1190.0, 1387365030]]}, {"target": "hello", "datapoints": [[45, 1387364979]]}]

理想情况下我会想要它:

[{"target": "stats.gauges.server1.throughput", "datapoints": [[1190.0, 1387364980], [1190.0, 1387364990], [1190.0, 1387365000], [1190.0, 1387365010], [1190.0, 1387365020], [1190.0, 1387365030]]}, {"target": "hello", "datapoints": [[45, 1387364979], [45, 1387365030 ]]}]

我想bug就在这里:

def constantLine(requestContext, value):
  """
  Takes a float F.

  Draws a horizontal line at value F across the graph.

  Example:

  .. code-block:: none

    &target=constantLine(123.456)

  """
  start = timestamp( requestContext["startTime"] )
  end = timestamp( requestContext["endTime"])
  #step = int((end - start) / 1)
  step = 5000
  series = TimeSeries(str(value), start, end, step, [value])
  return [series]

1 个答案:

答案 0 :(得分:0)

Graphite仅为constantLine函数或阈值函数提供单个值。考虑到我们仅使用石墨来查询json中的值并使用第三方图表工具(如D3,Rickshaw或Highcharts),然后我们将遇到只有一个值的问题。在这种情况下,我们需要修改constantLine函数,如下所示:

start = timestamp( requestContext["startTime"] )
  end = timestamp( requestContext["endTime"])
  #step = (end -start -1)/1.0
  step = 10
  val_tmp = [value] * int((end-start))
  series = TimeSeries(str(value), start, end, step,val_tmp)
  return [series]

如果使用Django 1.6,请不要忘记更改导入函数,如[here](https://github.com/graphite-project/graphite-web/commit/fc3f018544c19b90cc63797d18970a4cc27ef2ad#diff-e383725a971fca0685db19bfe7c65b32

希望这有助于某人。