Class Stats
In: app/models/stats.rb
Parent: Object

Methods

Constants

DRB = 'http://localhost:5984/ilog3'

Public Class methods

Devuelve las 10 últimas búsquedas que han entrado de Google.

[Source]

    # File app/models/stats.rb, line 37
37:   def self.last_googles
38:     dbr = CouchRest.database(Stats::DRB)
39:     
40:     # moment = since_hours.hours.ago
41:     
42:     googles = []
43:     begin
44:       ag = dbr.view('select/extref', {:descending => true,
45:                                       :limit => 300,
46:                                       :endkey => [2010, 1, 1, 0, 0] })
47:     rescue => err
48:       ActiveRecord::Base.logger.error "Could not get data form CouchDB: #{err}"
49:       googles << "No hay conexion con la base de datos"
50:     else
51:       ag['rows'].each do |r|
52:          # pp r['key'], r['value']
53:          referer =  r['value']
54:          isgoogle = Regexp.compile(/\.google\./)
55:          if /\.google\./ =~ referer then
56:             sterms0 = referer.gsub(/.*?[&?]q=([^&]+)&?.*$/, '\1')
57:             sterms = CGI.unescape(sterms0)
58:             # puts referer
59:             googles << sterms
60:             # puts "---"
61:          end
62:       end
63:     end
64:     return googles[0..9]
65:   end

Devuelve el número de diferentes IPs que se han conectado a servidor

[Source]

     # File app/models/stats.rb, line 101
101:   def self.last_ips_counter
102:     dbr = CouchRest.database(Stats::DRB)
103:     
104:     moment = 5.minutes.ago
105:     
106:     ips = Hash.new(0)
107:     begin
108:       ag = dbr.view('counter/ips', { :descending => true,
109:                                   :limit => 10000,
110:                                   :group => true,
111:                                   :endkey => [moment.year, moment.month, moment.day, moment.hour, moment.min, "" ] })
112:     rescue => err
113:       ActiveRecord::Base.logger.error "Could not get data form CouchDB: #{err}"
114:     else
115:       ag['rows'].each do |r|
116:         ip = r['key'][5]
117:         ips[ip] += 1
118:       end
119:     end
120:     return ips.size
121:   end

Devuelve los últimos 10 referers que han entrado en los logs en las últimas since_hours horas.

[Source]

    # File app/models/stats.rb, line 8
 8:   def self.last_referers(since_hours)
 9:     dbr = CouchRest.database(Stats::DRB)
10:     
11:     moment = since_hours.hours.ago
12: 
13:     refs = Hash.new(0)
14:     begin
15:       ag = dbr.view('select/extref', { :descending => true,
16:                                        :limit => 100000,
17:                                        :endkey => [moment.year, moment.month, moment.day, moment.hour, 0] })
18:     rescue => err
19:       ActiveRecord::Base.logger.error "Could not get data form CouchDB: #{err}"
20:       refs["No hay conexión con la base de datos"] = 0
21:     else
22:       ag['rows'].each do |r|
23:         # p r['key'], r['value']
24:         referer =  r['value']
25:         isgoogle = Regexp.compile(/\.google\./)
26:         if !(/\.google\./ =~ referer) then
27:           # site = referer.gsub(/http:\/\/([^\/]+)\/.*$/, '\1')
28:           # puts referer, site
29:           refs[referer]+=1
30:         end
31:       end
32:     end
33:     referers = refs.sort {|a,b| b[1] <=> a[1]}[0..9]
34:   end

Devuelve un hash con las salas de streaming y el número de personas que lo están viendo. Tanto si no aparece como si el contador está en 0, quiere decir que en esa sala no hay nada

[Source]

     # File app/models/stats.rb, line 169
169:   def self.streaming_watchers
170:     # log fields:
171:     const_set("Cx_severity", 0)
172:     const_set("Cx_category", 1)
173:     const_set("Cx_event", 2)
174:     const_set("Cdate", 3)
175:     const_set("Ctime", 4)
176:     const_set("Cc_client_id", 5)
177:     const_set("Cc_ip", 6)
178:     const_set("Cc_port", 7)
179:     const_set("Ccs_bytes", 8)
180:     const_set("Csc_bytes", 9)
181:     const_set("Cx_duration", 10)
182:     const_set("Cx_sname", 11)
183:     const_set("Cx_stream_id", 12)
184:     const_set("Csc_stream_bytes", 13)
185:     const_set("Ccs_stream_bytes", 14)
186:     const_set("Cx_file_size", 15)
187:     const_set("Cx_file_length", 16)
188:     const_set("Cx_ctx", 17)
189:     const_set("Cx_comment", 18)
190:     
191:     onusers = 0
192:     honuser = Hash.new(0)
193:     # honuser['SBI01.SDP'] = 0
194:     # honuser['SAR02.SDP'] = 0
195:     file = "/usr/local/WowzaMediaServerPro/logs/wowzamediaserverpro_access.log"
196:     if File.exists?(file)
197:       logfile = File.new(file)
198:       logfile.each do |logline|
199:         if logline[0] == 35
200:           ; # puts 'comment:'
201:         else
202:           logdl = logline.split(/\t/)
203:           # if logdl[Cx_event]=='play' || logdl[Cx_event]=='stop'
204:           if logdl[Cx_event]=='play'
205:             ## puts logdl[Cdate]+' '+logdl[Ctime] + " : " + logdl[Cc_client_id]  + ":"+ logdl[Cx_event] + ', ' + logdl[Csc_bytes]
206:             onusers += 1
207:             # puts logdl[Cdate]+' '+logdl[Ctime] + ", " + logdl[Cx_sname]  + ", " + onusers.to_s
208:             # puts logdl[Cx_sname]
209:             honuser[logdl[Cx_sname]] += 1
210:           end
211:           if logdl[Cx_event]=='stop'
212:             ## puts logdl[Cdate]+' '+logdl[Ctime] + " : " + logdl[Cc_client_id]  + ":"+ logdl[Cx_event] + ', ' + logdl[Csc_bytes]
213:             onusers -= 1
214:             honuser[logdl[Cx_sname]] -= 1
215:             # puts logdl[Cdate]+' '+logdl[Ctime] + ", " + logdl[Cx_sname]  + ", " + onusers.to_s
216:           end
217:         end
218:       end
219:     end
220: 
221:     # honuser = {"SBI01.SDP" => 3}
222:     # StreamFlow.all.each {|sf| honuser[sf.code] = 3}
223: 
224:     data = honuser.delete_if {|k, v| v==0}
225:     output = {}
226:     data.each do |code, counter|
227:       stream = StreamFlow.find_by_code(code)
228:       title = stream ? stream.title_es : code
229:       output[title] = counter
230:     end
231:     return output
232:   end

Devuelve las 10 páginas más visitadas en los últimos since_hours horas

[Source]

    # File app/models/stats.rb, line 68
68:   def self.top_pages(since_hours)
69:     dbr = CouchRest.database(Stats::DRB)
70:     
71:     moment = since_hours.hours.ago
72:     
73:     # the most recent, back to :endkey
74:     totv = Hash.new(0)
75:     begin
76:       ag = dbr.view('counter/pages', { :descending => true,
77:                                    :limit => 10000,
78:                                    :endkey => [moment.year, moment.month, moment.day, moment.hour, ''],
79:                                    :group => true })
80:     rescue => err
81:       ActiveRecord::Base.logger.error "Could not get data form CouchDB: #{err}"
82:       totv["No hay conexión con la base de datos"] = ""
83:     else
84:       ag['rows'].each do |r|
85:          # pp r['key'], r['value']
86:          path = r['key'][4]
87:          ntimes = r['value']
88:          if !( (/news\/image/ =~ path) ||
89:                (/news\/photo/ =~ path) ||
90:                (/site\/change_locale/ =~ path) ||
91:                (/site\/search/ =~ path) ) then
92:            # pp path, ntimes
93:            totv[path] += ntimes
94:          end
95:       end
96:     end
97:     topp = totv.sort {|a,b| b[1] <=> a[1]}[0..9]
98:   end

[Source]

     # File app/models/stats.rb, line 145
145:   def self.top_rsss(since_days)
146:     dbr = CouchRest.database(Stats::DRB)
147:     moment = since_days.days.ago
148:     
149:     ag = dbr.view('counter/rsss', { :descending => true,
150:                                       :limit => 10000000,
151:                                       :endkey => [moment.year, moment.month, moment.day, 0, ''],
152:                                       :group => true })
153: 
154:     sum = 0
155:     totv = Hash.new(0)
156:     ag['rows'].each do |r|
157:       # pp r['key'], r['value']
158:       path = r['key'][4].gsub(/p=[0-9a-f]+&/, '')
159:       ntimes = r['value']
160:       # pp path, ntimes
161:       totv[path] += ntimes
162:       sum += ntimes
163:     end
164:     return {:videos => totv.sort {|a,b| b[1] <=> a[1]}[0..14], :sum => sum}
165:   end

[Source]

     # File app/models/stats.rb, line 123
123:   def self.top_videos(since_days)
124:     dbr = CouchRest.database(Stats::DRB)
125:     moment = since_days.days.ago
126:     
127:     ag = dbr.view('counter/videos', { :descending => true,
128:                                       :limit => 10000000,
129:                                       :endkey => [moment.year, moment.month, moment.day, 0, ''],
130:                                       :group => true })
131: 
132:     sum = 0
133:     totv = Hash.new(0)
134:     ag['rows'].each do |r|
135:        # pp r['key'], r['value']
136:        path = r['key'][4]
137:        ntimes = r['value']
138:        # pp path, ntimes
139:        totv[path] += ntimes
140:        sum += ntimes
141:     end
142:     return {:videos => totv.sort {|a,b| b[1] <=> a[1]}[0..9], :sum => sum}
143:   end

[Validate]