This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
koreapi.rb
141 lines (121 loc) · 3.72 KB
/
koreapi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require 'v8'
require 'json'
require 'sinatra/base'
require 'parseconfig'
require 'tempfile'
require 'logger'
if $log.nil?
$log = Logger.new(STDOUT)
$log.level = Logger::DEBUG
end
require_relative 'helpers/config_utils'
require_relative 'helpers/param_utils'
require_relative 'helpers/s3_utils'
require_relative 'lib/metrics'
require_relative 'lib/domains'
require_relative 'lib/script_metadata'
class KoreaPI < Sinatra::Base
set :public_folder, File.dirname(__FILE__) + '/public_folder'
helpers Sinatra::KoreaPI::ParamUtils
helpers Sinatra::KoreaPI::S3Utils
def initialize()
super()
script_path = "scriptlets"
@scriptlets = {}
Dir.foreach(script_path) do |f|
unless f =~ /\.js$/
next
end
name = f.sub(/.js/, '')
path = script_path + "/" + f
@scriptlets[name] = path
puts ("Got " + name + " => " + path)
end
@domains = Domains.new().get_domains();
puts "Loaded domains #{@domains}"
end
get "/?" do
erb :index
end
get "/q/:entity" do
params.delete("_")
entity = params.delete("entity")
puts params.inspect
puts entity
Metrics.new.__get(entity, params)
end
get "/q/:entity/range" do
params.delete("_")
entity = params.delete("entity")
puts params.inspect
puts entity
Metrics.new.__range(entity, params)
end
get "/q/:entity/series" do
params.delete("_")
entity = params.delete("entity")
puts params.inspect
puts entity
Metrics.new.__series(entity, params)
end
get "/domains" do
@domains.to_json
end
get "/scriptlets" do
@scriptlets.to_json
end
get "/scriptlets/:name/info" do
metaData = ScriptMetaData.new
cxt = V8::Context.new
cxt['scriptlet'] = metaData
cxt.load(@scriptlets[params["name"]])
out = cxt.eval('info()')
out
end
get "/scriptlet/:name" do
params.delete("_")
cxt = V8::Context.new
metaData = ScriptMetaData.new
tempFile = Tempfile.new('koreapi-tmp-metrics', '/tmp/')
cxt['m'] = Metrics.new
cxt['scriptlet'] = metaData
cxt['domains'] = @domains || {}
cxt['tempFile'] = tempFile
load_into_context(cxt, params)
if !metaData.errors.nil?
content_type "text/html"
return metaData.errors
end
File.open("js/underscore-min.js") do |file|
cxt.eval(file, "js/underscore-min.js")
end
File.open("js/csv/csv.js") do |file|
cxt.eval(file, "js/csv/csv.js")
end
cxt.eval('runAndWriteToFile()')
if !metaData.errors.nil?
content_type ( metaData.content_type || "text/html" )
return metaData.errors
end
tempFile.flush
if params['push_to_s3'] == 'true'
bucket = cxt['s3_bucket']
unless bucket.nil?
config = ParseConfig.new('/etc/koreapi.properties')
destinationFile = "domain_report.csv"
content_type "text/html"
return push_to_s3(bucket, tempFile.path, destinationFile)
end
end
send_file(tempFile, :disposition => 'attachment', :filename => "#{params["name"]}.csv")
tempFile.delete
end
end
puts 'Configuration settings'
puts "Environment: #{Sinatra::KoreaPI::ConfigUtils.get_first_attr('environment')}"
puts "Core Server: #{Sinatra::KoreaPI::ConfigUtils.get_first_attr('core.server.host')}:#{Sinatra::KoreaPI::ConfigUtils.get_first_attr('core.server.port')}"
puts "Balboa Server: #{Sinatra::KoreaPI::ConfigUtils.get_first_attr('metric-config.balboa.server.host')}:#{Sinatra::KoreaPI::ConfigUtils.get_first_attr('metric-config.balboa.server.port')}"
puts "AWS Access: #{Sinatra::KoreaPI::ConfigUtils.get_first_attr('aws.access_key_id')}
AWS Secret: #{Sinatra::KoreaPI::ConfigUtils.get_first_attr('aws.secret_access_key')}
AWS Region: #{Sinatra::KoreaPI::ConfigUtils.get_first_attr('aws.region')}"
KoreaPI.run!