Class: Fluent::VikiInput
- Inherits:
-
HttpInput
- Object
- HttpInput
- Fluent::VikiInput
show all
- Includes:
- Geoip
- Defined in:
- lib/fluent/plugin/in_viki.rb
Constant Summary
collapse
- CROSSDOMAIN_XML =
<<EOF
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
EOF
Instance Method Summary
collapse
Methods included from Geoip
#city_of_ip, #country_code_of_ip, #geoip_db_path, #global_geoip
Instance Method Details
#gen_message_id(time) ⇒ Object
Generate a unique id for the event, length: 10+1+5 = 16 It’s relatively sortable
175
176
177
178
|
# File 'lib/fluent/plugin/in_viki.rb', line 175
def gen_message_id time
r = rand(36**5).to_s(36)
"#{time.to_s}-#{r}"
end
|
#on_request(path_info, params) ⇒ Object
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
|
# File 'lib/fluent/plugin/in_viki.rb', line 21
def on_request(path_info, params)
return_hash = nil
begin
tag = path_info[1..-1].split('/').join('.')
return return_200_crossdomain unless tag.downcase.match("crossdomain.xml").nil?
tag = @default_tag if tag == '' && @default_tag != ''
record = params.dup
time = if record['t_f']
t_f = record['t_f']
record.delete('t_f')
t_f.to_i
else
Engine.now
end
record['t'] = time.to_s unless record['t']
set_record_ip(params, record)
set_record_domain(record)
record['uuid'] = record.delete('viki_uuid') if record['viki_uuid']
record['content_provider'] = record.delete('type') if record['type']
record['device_id'] = record.delete('dev_model') if record['dev_model']
record.each { |k, v| record[k] = '' if v == 'null' }
unless record['mid']
record['mid'] = params['HTTP_X_REQUEST_ID'] || gen_message_id(time)
end
record['event'] = 'minute_view' if record['event'] == 'video_view'
if %w(video_play minute_view).include?(record['event'])
update_subtitles(record)
end
if record['app_id'] == '100105a'
record.delete_if {|key, _| !!(key =~ /\A[0-9]+{13}\z/) }
end
rescue
return return_400
end
begin
Engine.emit(tag, time, record)
rescue
return return_505
end
return_200(return_hash)
end
|
#remove_exceptions_from_hash(attr_hash, app_id) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/fluent/plugin/in_viki.rb', line 94
def remove_exceptions_from_hash(attr_hash, app_id)
app_hash = { '100004a' => 'ios', '100005a' => 'android', '65535a' => 'flash', '100017a' => 'android', '100016a' => 'mweb', '100018a' => 'xbox' }
source = app_hash[app_id]
if source.nil?
puts "Warning: Could not identify source with app_id: "+app_id
return nil
end
attr_hash.each { |k, v|
if v.any? && v.include?(source)
attr_hash.delete(k)
end
}
attr_hash.keys
end
|
#resolve_correct_ip(ips) ⇒ Object
139
140
141
142
143
144
145
146
147
|
# File 'lib/fluent/plugin/in_viki.rb', line 139
def resolve_correct_ip(ips)
ips.each do |ip|
geo_country = country_code_of_ip(ip)
return [ip, geo_country] unless geo_country.nil?
end unless ips.nil?
[ips.first, nil]
end
|
#return_200(json_hash) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/fluent/plugin/in_viki.rb', line 157
def return_200 json_hash
if json_hash
["200 OK", { 'Content-type' => 'application/json' }, json_hash.to_json]
else
if @respond_with_empty_img
["200 OK", { 'Content-type' => 'image/gif', 'Access-Control-Allow-Origin' => '*' }, "GIF89a\u0001\u0000\u0001\u0000\x80\xFF\u0000\xFF\xFF\xFF\u0000\u0000\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0002D\u0001\u0000;"]
else
["200 OK", { 'Content-type' => 'text/plain', 'Access-Control-Allow-Origin' => '*' }, ""]
end
end
end
|
#return_200_crossdomain ⇒ Object
169
170
171
|
# File 'lib/fluent/plugin/in_viki.rb', line 169
def return_200_crossdomain
["200 OK", { 'Content-type' => 'text/xml', 'Access-Control-Allow-Origin' => '*', 'Cache-Control' => 'public; max-age=36000' }, CROSSDOMAIN_XML]
end
|
#return_400 ⇒ Object
153
154
155
|
# File 'lib/fluent/plugin/in_viki.rb', line 153
def return_400
["400 Bad Request", { 'Content-type' => 'text/plain' }, "400 Bad Request\n#{$!}\n"]
end
|
#return_505 ⇒ Object
149
150
151
|
# File 'lib/fluent/plugin/in_viki.rb', line 149
def return_505
["500 Internal Server Error", { 'Content-type' => 'text/plain' }, "500 Internal Server Error\n#{$!}\n"]
end
|
#set_record_domain(record) ⇒ Object
112
113
114
115
|
# File 'lib/fluent/plugin/in_viki.rb', line 112
def set_record_domain(record)
site = record['site']
record['domain'] = site.gsub(/^https?:\/\//, '').gsub(/([^\/]*)(.*)/, '\1').gsub(/^www\./, '') if site
end
|
#set_record_ip(params, record) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/fluent/plugin/in_viki.rb', line 117
def set_record_ip(params, record)
record['ip'] ||= params['HTTP_X_FORWARDED_FOR'] || params['REMOTE_ADDR']
ips = unless record['ip'].nil?
if record['ip'].kind_of? String
record['ip'].gsub(' ', ',').split(',')
elsif record['ip'].kind_of? Array
record['ip']
.map {|e| e.split(",").map(&:strip) }
.inject([]) {|accum, e| accum + e}
end
end
record['country'] = record.delete('country_code') if record['country_code']
valid_ip, geo_country = resolve_correct_ip(ips)
record['ip_raw'], record['ip'] = record['ip'], valid_ip
record['country'] = geo_country || record['country']
record.merge! city_of_ip(valid_ip)
end
|
#update_subtitles(record) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/fluent/plugin/in_viki.rb', line 80
def update_subtitles(record)
record['subtitle_lang'] = record.delete('bottom_subtitle') if record['bottom_subtitle'] and record['subtitle_lang'].nil?
if record['subtitle_visible'].nil?
record['subtitle_visible'] = record['subtitle_lang'] && record['subtitle_lang'].size > 0
end
if %w(100106a 100105a).include?(record['app_id'])
record['subtitle_enabled'] = true
record['subtitle_lang'] = 'zh'
end
end
|