Python 在json格式中缺少写入值
尝试从graphite webapp请求json格式的数据时,缺少最后一个值。以下内容来自graphite web应用程序中的render/views.py。我正在尝试使用阈值函数来获取常量数据。在使用constantLine/threshold函数时,我如何强制graphite webapp以json格式输出两点 对于原始格式:Python 在json格式中缺少写入值,python,json,graphite,Python,Json,Graphite,尝试从graphite webapp请求json格式的数据时,缺少最后一个值。以下内容来自graphite web应用程序中的render/views.py。我正在尝试使用阈值函数来获取常量数据。在使用constantLine/threshold函数时,我如何强制graphite webapp以json格式输出两点 对于原始格式: if format == 'raw': response = HttpResponse(mimetype='text/plain') for
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
[{"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]]}]
但是,对于写入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) )
我的输出是:
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
[{"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]
Graphite仅为constantLine函数或阈值函数提供一个值。考虑到我们只使用石墨来查询JSON中的值,并使用第三方图表工具,如D3、RKSHAW或HealStand,那么我们将跨越这个问题,我们只有一个值来绘图。在这种情况下,我们需要修改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]()
希望这对其他人有所帮助。我认为这是由于问题格式,但输出和理想值看起来是一样的?在理想情况下,对于目标hello,有两个数据点。。虽然当前输出只有一个数据点,但我认为您需要向我们显示原始数据。我不知道这些值应该来自哪里。嗨,丹尼尔,我展示的函数是时间序列数据库graphite的一部分。这里,一个名为constant line的函数使用graphite.com/render?target=constantLine(value)&from=-2mins&till=now给出数据;constantLine函数的源代码是