我想从散列中的ackedOns时间中减去startOn时间,以获得确认警报所需的时间间隔。
以下是代码:
url = "https://xyz"
uri = URI(url)
http = Net::HTTP.new(uri.host, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Get.new(uri.request_uri)
response = http.request(req)
jsonResponse = JSON.parse(response.body)
#troubelshooting step to see how many total alerts there are
total = jsonResponse['data']['total']
pp total
# create the collected_alerts hash
collected_alerts = { 'hosts'=> [],'dataPoints' => [], 'startOns' => [], 'ackedOns' => [], 'timeToAcks' => [] }
# iterate through the json
jsonResponse['data']['alerts'].each do |alerts|
# store the interested json values into the appropriate hash
collected_alerts['hosts'] << alerts['host']
collected_alerts['dataPoints'] << alerts['dataPoint']
collected_alerts['startOns'] << alerts['startOn']
collected_alerts['ackedOns'] << alerts['ackedOn']
# Calculate mins it took to acknowledge alert and store it in timeToAcks
# Formula is Ack time - Start time which would give us seconds / 60 to give mins.
alerts['timeToAcks'] = alerts['ackedOn'].zip(alerts['startOn']).map{|a,s| a-s/60}
end
pp collected_alerts
CSV.open("data.csv", "wb") {|csv| collected_alerts.to_a.each {|elem| csv << elem} }下面是我试图解析的json响应
{
"status": 200,
"data": {
"total": 3,
"alerts": [
{
"dataPoint": "average",
"ackedBy": "x",
"dataSourceInstance": "Ping50Packets",
"dataSource": "Ping50Packets",
"host": "x",
"endOn": 0,
"ackedOn": 1392218853,
"dataSourceInstanceId": 400554,
"hostId": 1829,
"type": "alert",
"dataSourceId": 560,
"ackedOnLocal": "2014-02-12 07:27:33 PST",
"id": 6862895,
"startOn": 1392197595,
"thresholds": "> 200",
"endOnLocal": "",
"level": "warn",
"ackComment": "Ack.",
"value": "206.00",
"hostDataSourceId": 137481,
"acked": true,
"hostGroups": [{
"alertEnable": true,
"createdOn": 1367604091,
"id": 106,
"parentId": 105,
"description": "",
"appliesTo": "",
"name": "x",
"fullPath": "x"
}],
"startOnLocal": "2014-02-12 01:33:15 PST"
},发布于 2014-02-13 03:50:35
根据您的数据,您只需做:
collected_alerts['timeToAcks'] << (alerts['ackedOn'] - alerts['startOn'])/60collected_alerts,而不是当前的alert,对吗?或者,在您遍历了所有警报之后:
ca = collected_alerts
ca['timeToAcks'] = ca['ackedOns'].zip(ca['startOns']).map{ |ack,st| (ack-st)/60 }此外,我不会一次一次地循环处理警报并取出碎片,而是这样做:
alerts = jsonResponse['data']['alerts']
collected_alerts = {}
%w[host dataPoint startOn ackedOn].each do |field|
collected_alerts[field] = alerts.map{ |alert| alert[field] }
end甚至更好:
alert_fields = %w[host dataPoint startOn ackedOn]
collected_alerts = Hash[ alert_fields.map{ |f| [f,alerts.map{|a| a[f] }] } ]解释,大致按执行顺序。
alert_fields = %w[ … ] -创建引用字符串值数组(源代码中分隔的空格)的变量,即["ackedOn", "host", … ]alerts.map{ |a| … } --通过接受alerts数组中的每个值创建一个新数组,创建一个引用该值的名为a的变量,并将块的结果用作新数组中的值。a[f] - for每个警报在alerts数组中,查找值为f的键。例如,当f是"ackedOn"时,它将查找a["ackedOn"]的值。
alert_fields.map{ |f| … } -对于数组中的每个元素,创建一个名为f的变量(用于“字段”),并运行该块的内容。为每个条目创建一个新的数组,无论块的最终值是什么。[f, … ] --在为所有警报中的特定字段创建一个所有值的数组之后,这个块的结果是一个两个元素数组,它是字段的名称,后面是这些值。例如,[ "ackedOn", [1,3,16,4,44,7] ]。
Hash[ … ] - given一个由两个值数组组成的数组,创建一个散列将每对中的第一个值映射到第二个值,即{ "ackedOn" => [1,3,16,4,44,7] } 。Hash[…]方法也可以接受偶数的参数,将每个偶数参数与下面的参数配对。
举几个例子:
alert_fields = %w[ackedOn host dataPoint startOn]
#=> ["ackedOn", "host", "dataPoint", "startOn" ]
alerts.map{|a| a[f] }
#=> [5,3,4,5,1,6,8,2,…]
[f,alerts.map{|a| a[f] }]
#=> ["ackedOn",[5,3,4,5,1,6,8,2,…]]
alert_fields.map{ |f| [f,alerts.map{|a| a[f] }] }
#=> [
#=> ["host",['foo','bar',…]],
#=> …
#=> ["ackedOn",[5,3,4,5,1,6,8,2,…]]
#=> ]
Hash[ alert_fields.map{ |f| [f,alerts.map{|a| a[f] }] } ]
#=> {
#=> "host" => ['foo','bar',…],
#=> …
#=> "ackedOn" => [5,3,4,5,1,6,8,2,…]
#=> }发布于 2014-02-13 02:52:38
类似的东西;
alerts['tta'] = alerts['ackedOns'].zip(alerts['startOns']).map{|a,s| a-s/60}https://stackoverflow.com/questions/21744188
复制相似问题