【发布时间】:2014-12-31 21:21:24
【问题描述】:
您好,我正在编写一个从票务系统中提取数据的脚本。提取数据后,它会分析其内容,如果内容符合特定条件,则需要构建一个数据结构文件,该文件将转储到同一服务器中。
我能够解析JSON格式的数据,下面是内容:
[{"id"=>10423,
"type"=>"Ticket",
"lastUpdated"=>"2014-11-04T10:58:47Z",
"shortSubject"=>"FOO STATUS UPDATE",
"shortDetail"=>"Reply to this message if all systems are functional..",
"displayClient"=>"No Client",
"updateFlagType"=>0,
"prettyLastUpdated"=>"54 minutes ago",
"latestNote"=>
{"id"=>16850,
"type"=>"TechNote",
"mobileListText"=>"<b>t. trust: </b> All Systems are OK",
"noteColor"=>"clear",
"noteClass"=>"bubble right"}},
{"id"=>10422,
"type"=>"Ticket",
"lastUpdated"=>"2014-11-04T10:54:07Z",
"shortSubject"=>"FOO STATUS UPDATE",
"shortDetail"=>"Reply to this message if all systems are functional..",
"displayClient"=>"No Client",
"updateFlagType"=>0,
"prettyLastUpdated"=>"58 minutes ago",
"latestNote"=>nil},
{"id"=>10421,
"type"=>"Ticket",
"lastUpdated"=>"2014-11-04T10:53:17Z",
"shortSubject"=>"FOO STATUS UPDATE",
"shortDetail"=>"Reply to this message if all systems are functional..",
"displayClient"=>"No Client",
"updateFlagType"=>0,
"prettyLastUpdated"=>"59 minutes ago",
"latestNote"=>nil}]
在上面的数据中,您可以看到每张工单都有一个 id、lastupdate、short Subject、short Detail 和 lastest note 如果没有人回复工单但如果有人回复,则最新便笺的值将为空value mobileListText 会有东西。
所以我几乎需要做的是,一旦我得到这些数据,如果该值匹配,脚本将查找符合“FOO STATUS UPDATE”的主题,然后查找 shortDetail 匹配“回复此消息”的内容如果所有系统都正常运行..”,如果符合,则查找它的 latestNote,如果 latestNote 为 nill,则它将创建一个日志文件,指定运行时的日期和时间、具有此状态的票证的 ID 和一条消息,票证没有被回复,但如果最新的注释的值为“mobileListText”=>“t.trust: All Systems are OK”,则创建以下数据结构:
{"LastUpdate":1415130257,"Service":[{"time":"11-04-2014 10:58:47 GMT","region:":"","id":"","description":"All Systems are OK","service":""},{"time":"11-04-2014 10:54:07 GMT","region:":"","id":"","description":"All Systems are OK","service":""},{"time":"11-04-2014 10:53:17 GMT","region:":"","id":"","description":"All Systems are OK","service":""}]}
我能够得到其中的一部分,但是根据上面的数据,只有一张票所有系统都正常,这意味着只有一张票正在回复,它应该只写如下内容:
{"LastUpdate":1415130257,"Service":[{"time":"11-04-2014 10:58:47 GMT","region:":"","id":"","description":"All Systems are OK","service":""}]}
但相反,它会重复这个唯一被多次回复的票。
到目前为止这是我的代码:
require 'rubygems'
require 'json'
require 'net/http'
require 'highline/import'
require 'pp'
require 'logger'
@usersol='foo'
@passol= 'foo123'
@urlsol= "http://dev-webhelpdesk.foo.corp:8081/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets?list=group&page=1&limit=#{@limit}&username=#{@usersol}&password=#{@passol}"
@limit = '25'
@log = @log= Logger.new( 'message_solar.log')
def ticket_data #looks for ticket data in solarwinds
resp = Net::HTTP.get_response(URI.parse(@urlsol))
url_output = resp.body
JSON.parse(url_output)
end
#CRONJOB THAT START ALL
#echo "Reply to this message if all systems are functional.." | mail -r noc@foo.com -s "FOO STATUS UPDATE:" noc-team@FOO.com >> /dev/null
# Looking for all the tickets with the following content
# ticket id, ticket subject and content
def search_allok(allok)
description = []
allok.each do |systems|
output1 = systems.has_key?'id'
if output1
systems.values_at('shortSubject').each do | subject |
output2 = subject.match(%r(TRUST STATUS UPDATE))
if output2
latestnote = systems.values_at('latestNote')
latestnote.each do |content|
if content
final = content.values_at('mobileListText')
final_ok = final[0].sub!(/^\<b\>.*\<\/b\>\s/, "")
systems_ok = final_ok.match(%r(All Systems are OK))
if systems_ok
ids = systems['id']
notify = {"LastUpdate" => Time.now.to_i, "Service" => []}
allok.each do |lastup|
reference = lastup.has_key? 'id'
if reference
timeid = lastup.values_at('lastUpdated')
timeid.each do |lines|
final=lines.split(/[-, T, Z]/)
notify["Service"] << { "time" => "#{final[1]}-#{final[2]}-#{final[0]} #{final[3]} GMT", "region:" => "", "id" => "#{ids}", "description" => "#{systems_ok}" , "service" => ''}
end
end
end
File.open("notify.json", "w") do |fileformatted|
fileformatted.puts (JSON.dump(notify))
end
else
time = Time.now
@log.info("#{time} - Ticket ID #{systems['id']} has not being updated")
end
else
@log.info("#{time} - Ticket ID #{systems['id']} has not being reply")
end
end
end
end
end
end
end
# If the content is there then it need to create
# the data structure including the lastupdated
# (time when it run the script), and the lastupdate for the ticket
# and the description All Systems OK
#This method below I added to the one above, but I was thinking on doing it separate but I encouter issues passing the information needed from above to below
def datastructure(format_file) #creates JSON file lastupdated of each ticket in the queue
notify = {"LastUpdate" => Time.now.to_i, "Service" => []}
format_file.each do |lastup|
reference = lastup.has_key? 'id'
if reference
timeid = lastup.values_at('lastUpdated')
timeid.each do |lines|
final=lines.split(/[-, T, Z]/)
notify["Service"] << { "time" => "#{final[1]}-#{final[2]}-#{final[0]} #{final[3]} GMT", "region:" => "", "id" => "", "description" => region , "service" => ''}
end
end
end
File.open("notify.json", "w") do |fileformatted|
fileformatted.puts (JSON.dump(notify))
end
end
#ticket_data
#datastructure(ticket_data)
search_allok(ticket_data)
【问题讨论】:
-
您能否将其简化为您想要实现的最小示例?
-
如果要追加到文件中,需要使用
a而不是w -
我想要实现的是这个结构:{"LastUpdate":1415130257,"Service":[{"time":"11-04-2014 10:58:47 GMT","region :":"","id":"","description":"所有系统都正常","service":""},{"time":"11-04-2014 10:54:07 GMT" ,"region:":"","id":"","description":"所有系统都正常","service":""},{"time":"11-04-2014 10:53: 17 GMT","region:":"","id":"","description":"All Systems are OK","service":""}]}