rail_stat_generator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/MIT-LICENSE +21 -0
  2. data/USAGE +21 -0
  3. data/rail_stat_generator.rb +40 -0
  4. data/templates/README +21 -0
  5. data/templates/app/controllers/rail_stat_controller.rb +111 -0
  6. data/templates/app/helpers/rail_stat_helper.rb +184 -0
  7. data/templates/app/models/iptoc.rb +5 -0
  8. data/templates/app/models/rail_stat.rb +130 -0
  9. data/templates/app/models/search_term.rb +19 -0
  10. data/templates/app/views/layouts/rail_stat.rhtml +22 -0
  11. data/templates/app/views/rail_stat/_menu.rhtml +17 -0
  12. data/templates/app/views/rail_stat/hits.rhtml +28 -0
  13. data/templates/app/views/rail_stat/lang.rhtml +25 -0
  14. data/templates/app/views/rail_stat/other.rhtml +51 -0
  15. data/templates/app/views/rail_stat/path.rhtml +81 -0
  16. data/templates/app/views/rail_stat/platform.rhtml +25 -0
  17. data/templates/app/views/rail_stat/refs.rhtml +25 -0
  18. data/templates/db/ip-to-country.mysql.sql +22 -0
  19. data/templates/db/railstat.mysql.sql +43 -0
  20. data/templates/lib/path_tracker.rb +325 -0
  21. data/templates/public/images/railstat/1.gif +0 -0
  22. data/templates/public/images/railstat/10.gif +0 -0
  23. data/templates/public/images/railstat/11.gif +0 -0
  24. data/templates/public/images/railstat/12.gif +0 -0
  25. data/templates/public/images/railstat/13.gif +0 -0
  26. data/templates/public/images/railstat/14.gif +0 -0
  27. data/templates/public/images/railstat/15.gif +0 -0
  28. data/templates/public/images/railstat/16.gif +0 -0
  29. data/templates/public/images/railstat/17.gif +0 -0
  30. data/templates/public/images/railstat/18.gif +0 -0
  31. data/templates/public/images/railstat/19.gif +0 -0
  32. data/templates/public/images/railstat/1pxtr.gif +0 -0
  33. data/templates/public/images/railstat/2.gif +0 -0
  34. data/templates/public/images/railstat/20.gif +0 -0
  35. data/templates/public/images/railstat/21.gif +0 -0
  36. data/templates/public/images/railstat/22.gif +0 -0
  37. data/templates/public/images/railstat/23.gif +0 -0
  38. data/templates/public/images/railstat/24.gif +0 -0
  39. data/templates/public/images/railstat/25.gif +0 -0
  40. data/templates/public/images/railstat/26.gif +0 -0
  41. data/templates/public/images/railstat/27.gif +0 -0
  42. data/templates/public/images/railstat/28.gif +0 -0
  43. data/templates/public/images/railstat/29.gif +0 -0
  44. data/templates/public/images/railstat/3.gif +0 -0
  45. data/templates/public/images/railstat/30.gif +0 -0
  46. data/templates/public/images/railstat/31.gif +0 -0
  47. data/templates/public/images/railstat/32.gif +0 -0
  48. data/templates/public/images/railstat/33.gif +0 -0
  49. data/templates/public/images/railstat/34.gif +0 -0
  50. data/templates/public/images/railstat/35.gif +0 -0
  51. data/templates/public/images/railstat/36.gif +0 -0
  52. data/templates/public/images/railstat/37.gif +0 -0
  53. data/templates/public/images/railstat/38.gif +0 -0
  54. data/templates/public/images/railstat/39.gif +0 -0
  55. data/templates/public/images/railstat/4.gif +0 -0
  56. data/templates/public/images/railstat/40.gif +0 -0
  57. data/templates/public/images/railstat/5.gif +0 -0
  58. data/templates/public/images/railstat/6.gif +0 -0
  59. data/templates/public/images/railstat/7.gif +0 -0
  60. data/templates/public/images/railstat/8.gif +0 -0
  61. data/templates/public/images/railstat/9.gif +0 -0
  62. data/templates/public/images/railstat/other.gif +0 -0
  63. data/templates/public/javascripts/railstat.js +10 -0
  64. data/templates/public/stylesheets/railstat.css +54 -0
  65. data/templates/public/stylesheets/tabs.css +110 -0
  66. metadata +116 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2005 littlegreen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/USAGE ADDED
@@ -0,0 +1,21 @@
1
+ NAME
2
+ RailStat - creates a real-time web site statistics system
3
+
4
+ DESCRIPTION
5
+ This generator creates a real-time web site statistics system.
6
+
7
+ Features:
8
+ - Page views paths on each session
9
+ - Number of total hits / unique hits
10
+ - Operating system and browser
11
+ - Countries and languages
12
+ - Referrers and search strings
13
+ - Flash / JavaVM / Javascript / ScreenWidth / ColorDepth
14
+
15
+ EXAMPLE
16
+ ./script/generate rail_stat
17
+
18
+ This will generate controller, models, helper, views, db scripts,
19
+ images and stylesheets needed for the web statistics system.
20
+
21
+ Installation instructions are provided in README_RAILSTAT
@@ -0,0 +1,40 @@
1
+ class RailStatGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.template "lib/path_tracker.rb", "lib/path_tracker.rb"
5
+
6
+ m.template "app/controllers/rail_stat_controller.rb", "app/controllers/rail_stat_controller.rb"
7
+
8
+ m.template "app/helpers/rail_stat_helper.rb", "app/helpers/rail_stat_helper.rb"
9
+
10
+ m.template "app/models/iptoc.rb", "app/models/iptoc.rb"
11
+ m.template "app/models/rail_stat.rb", "app/models/rail_stat.rb"
12
+ m.template "app/models/search_term.rb", "app/models/search_term.rb"
13
+
14
+ m.template "app/views/layouts/rail_stat.rhtml", "app/views/layouts/rail_stat.rhtml"
15
+
16
+ m.directory "app/views/rail_stat"
17
+ m.template "app/views/rail_stat/_menu.rhtml", "app/views/rail_stat/_menu.rhtml"
18
+ m.template "app/views/rail_stat/hits.rhtml", "app/views/rail_stat/hits.rhtml"
19
+ m.template "app/views/rail_stat/lang.rhtml", "app/views/rail_stat/lang.rhtml"
20
+ m.template "app/views/rail_stat/other.rhtml", "app/views/rail_stat/other.rhtml"
21
+ m.template "app/views/rail_stat/path.rhtml", "app/views/rail_stat/path.rhtml"
22
+ m.template "app/views/rail_stat/platform.rhtml", "app/views/rail_stat/platform.rhtml"
23
+ m.template "app/views/rail_stat/refs.rhtml", "app/views/rail_stat/refs.rhtml"
24
+
25
+ m.template "public/stylesheets/railstat.css", "public/stylesheets/railstat.css"
26
+ m.template "public/stylesheets/tabs.css", "public/stylesheets/tabs.css"
27
+ m.template "public/javascripts/railstat.js", "public/javascripts/railstat.js"
28
+
29
+ m.directory "public/images/railstat"
30
+ 1.upto(40) { |i| m.template "public/images/railstat/#{i}.gif", "public/images/railstat/#{i}.gif" }
31
+ m.template "public/images/railstat/other.gif", "public/images/railstat/other.gif"
32
+ m.template "public/images/railstat/1pxtr.gif", "public/images/railstat/1pxtr.gif"
33
+
34
+ m.template "db/ip-to-country.mysql.sql", "db/ip-to-country.mysql.sql"
35
+ m.template "db/railstat.mysql.sql", "db/railstat.mysql.sql"
36
+
37
+ m.template "README", "README_RAILSTAT"
38
+ end
39
+ end
40
+ end
data/templates/README ADDED
@@ -0,0 +1,21 @@
1
+ == Installation
2
+
3
+ 1. Create database
4
+ 2. Create the database schema using db/railstat.mysql.sql
5
+ 3. Download the IP-to-Country database from http://ip-to-country.webhosting.info/ :
6
+
7
+ wget http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip
8
+ unzip ip-to-country.csv.zip
9
+
10
+ 4. Copy ip-to-country.csv in db/ directory
11
+ 5. Upload the data using ip-to-country.mysql.sql:
12
+
13
+ mysql -u _user_ _dbname_ < ip-to-country.mysql.sql
14
+
15
+ 6. Add your web site name at the end of config/environment.rb (assign it to a constant SITE_NAME)
16
+
17
+ Example:
18
+ SITE_NAME = 'www.mydomain.com'
19
+
20
+ Notes: Steps 3 - 5 are optional. If you don't want to download ip-to-country database
21
+ the system will work but the visitor country will not be tracked.
@@ -0,0 +1,111 @@
1
+ require_dependency 'path_tracker'
2
+
3
+ class RailStatController < ApplicationController
4
+ include PathTracker
5
+
6
+ before_filter :extract_subdomain
7
+
8
+ def path
9
+ @ordered_resources, @orarr = RailStat.get_ordered40resources(@subdomain)
10
+
11
+ @number_hits = (@params['nh'] or not @params['nh'].nil?) ? @params['nh'].to_i : 50
12
+ @include_search_engines = ((@params['ise'] == '' or @params['ise'] == "1") ? 1 : 0)
13
+
14
+ @paths = RailStat.find_all_by_flag(@include_search_engines == 0, @number_hits, @subdomain)
15
+ end
16
+
17
+ def index
18
+ redirect_to(:action=>'path')
19
+ end
20
+
21
+ def hits
22
+ @lastweek = RailStat.get_last_week_stats(@subdomain)
23
+ @first_hit = RailStat.find_first_hit(@subdomain)
24
+ @total_hits = RailStat.total_count(@subdomain)
25
+ @unique_hits = RailStat.unique_count(@subdomain)
26
+ n = Time.now
27
+ d = Date.new(n.year, n.month, n.day)
28
+ @today_total = RailStat.total_day_count(@subdomain, d)
29
+ @today_unique = RailStat.unique_day_count(@subdomain, d)
30
+ if @first_hit.nil?
31
+ render_text "No hits"
32
+ end
33
+ end
34
+
35
+ def platform
36
+ hits = RailStat.total_count(@subdomain)
37
+ @platforms = RailStat.platform_stats(@subdomain, hits)
38
+ @browsers = RailStat.browser_stats(@subdomain, hits)
39
+ end
40
+
41
+ def lang
42
+ hits = RailStat.total_count(@subdomain)
43
+ @languages = RailStat.language_stats(@subdomain, hits)
44
+ @countries = RailStat.country_stats(@subdomain, hits)
45
+ end
46
+
47
+ def refs
48
+ @refs = RailStat.domain_stats(@subdomain)
49
+ @searchterms = SearchTerm.find_grouped(@subdomain)
50
+ end
51
+
52
+ def other
53
+ hits = RailStat.total_count(@subdomain)
54
+ @flashes = RailStat.stats_dyn("flash", @subdomain, hits)
55
+ @jes = RailStat.stats_dyn("java_enabled", @subdomain, hits)
56
+ @javas = RailStat.stats_dyn("java", @subdomain, hits)
57
+ @widths = RailStat.stats_dyn("screen_size", @subdomain, hits)
58
+ @colors = RailStat.stats_dyn("colors", @subdomain, hits)
59
+ end
60
+
61
+ def tracker_js
62
+ if @request.env['HTTP_REFERER'] and @request.env['HTTP_REFERER'].include?(@request.env['HTTP_HOST'])
63
+ str = <<-JSDATA
64
+ c=0;
65
+ s=0;
66
+ n=navigator;
67
+ d=document;
68
+ plugin=(n.mimeTypes&&n.mimeTypes["application/x-shockwave-flash"])?n.mimeTypes["application/x-shockwave-flash"].enabledPlugin:0;
69
+ if(plugin) {
70
+ w=n.plugins["Shockwave Flash"].description.split("");
71
+ for(i=0;i<w.length;++i) { if(!isNaN(parseInt(w[i]))) { f=w[i];break; } }
72
+ } else if(n.userAgent&&n.userAgent.indexOf("MSIE")>=0&&(n.appVersion.indexOf("Win")!=-1)) {
73
+ d.write('<script language="VBScript">On Error Resume Next\\nFor f=10 To 1 Step-1\\nv=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash."&f))\\nIf v Then Exit For\\nNext\\n</script>');
74
+ } if(typeof(top.document)=="object"){
75
+ t=top.document;
76
+ rf=escape(t.referrer);
77
+ pd=escape(t.URL);
78
+ } else {
79
+ x=window;
80
+ for(i=0;i<20&&typeof(x.document)=="object";i++) { rf=escape(x.document.referrer); x=x.parent; }
81
+ pd=0;
82
+ }
83
+ d.write('<script language="JavaScript1.2">c=screen.colorDepth;s=screen.width;</script>');
84
+ if(typeof(f)=='undefined') f=0;
85
+ d.write('<a href="/" target="_blank"><img src="/rail_stat/track?size='+s+'&colors='+c+'&referer='+rf+'&java=1&je='+(n.javaEnabled()?1:0)+'&doc='+escape(d.URL)+'&flash='+f+'" border="0" width="1" height="1"></a><br>');
86
+ JSDATA
87
+ else
88
+ str = ""
89
+ end
90
+ render_text(str)
91
+ end
92
+
93
+ def track
94
+ track_path
95
+ @response.headers['Pragma'] = ' '
96
+ @response.headers['Cache-Control'] = ' '
97
+ @response.headers['Content-Length'] = 68
98
+ @response.headers['Accept-Ranges'] = 'bytes'
99
+ @response.headers['Content-type'] = 'image/gif'
100
+ @response.headers['Content-Disposition'] = 'inline'
101
+ File.open("#{RAILS_ROOT}/public/images/railstat/1pxtr.gif", 'rb') { |file| render :text => file.read }
102
+ end
103
+
104
+
105
+ private
106
+ def extract_subdomain
107
+ @subdomain = ((@request.subdomains and @request.subdomains.first) ? @request.subdomains.first : nil)
108
+ end
109
+
110
+ end
111
+
@@ -0,0 +1,184 @@
1
+ require 'socket'
2
+
3
+ module RailStatHelper
4
+
5
+ @@languages = {"af" => "Afrikaans",
6
+ "sq" => "Albanian",
7
+ "eu" => "Basque",
8
+ "bg" => "Bulgarian",
9
+ "be" => "Byelorussian",
10
+ "ca" => "Catalan",
11
+ "zh" => "Chinese",
12
+ "zh-cn" => "Chinese/China",
13
+ "zh-tw" => "Chinese/Taiwan",
14
+ "zh-hk" => "Chinese/Hong Kong",
15
+ "zh-sg" => "Chinese/singapore",
16
+ "hr" => "Croatian",
17
+ "cs" => "Czech",
18
+ "da" => "Danish",
19
+ "nl" => "Dutch",
20
+ "nl-nl" => "Dutch/Netherlands",
21
+ "nl-be" => "Dutch/Belgium",
22
+ "en" => "English",
23
+ "en-gb" => "English/United Kingdom",
24
+ "en-us" => "English/United States",
25
+ "en-au" => "English/Australian",
26
+ "en-ca" => "English/Canada",
27
+ "en-nz" => "English/New Zealand",
28
+ "en-ie" => "English/Ireland",
29
+ "en-za" => "English/South Africa",
30
+ "en-jm" => "English/Jamaica",
31
+ "en-bz" => "English/Belize",
32
+ "en-tt" => "English/Trinidad",
33
+ "et" => "Estonian",
34
+ "fo" => "Faeroese",
35
+ "fa" => "Farsi",
36
+ "fi" => "Finnish",
37
+ "fr" => "French",
38
+ "fr-be" => "French/Belgium",
39
+ "fr-fr" => "French/France",
40
+ "fr-ch" => "French/Switzerland",
41
+ "fr-ca" => "French/Canada",
42
+ "fr-lu" => "French/Luxembourg",
43
+ "gd" => "Gaelic",
44
+ "gl" => "Galician",
45
+ "de" => "German",
46
+ "de-at" => "German/Austria",
47
+ "de-de" => "German/Germany",
48
+ "de-ch" => "German/Switzerland",
49
+ "de-lu" => "German/Luxembourg",
50
+ "de-li" => "German/Liechtenstein",
51
+ "el" => "Greek",
52
+ "he" => "Hebrew",
53
+ "he-il" => "Hebrew/Israel",
54
+ "hi" => "Hindi",
55
+ "hu" => "Hungarian",
56
+ "ie-ee" => "Internet Explorer/Easter Egg",
57
+ "is" => "Icelandic",
58
+ "id" => "Indonesian",
59
+ "in" => "Indonesian",
60
+ "ga" => "Irish",
61
+ "it" => "Italian",
62
+ "it-ch" => "Italian/ Switzerland",
63
+ "ja" => "Japanese",
64
+ "ko" => "Korean",
65
+ "lv" => "Latvian",
66
+ "lt" => "Lithuanian",
67
+ "mk" => "Macedonian",
68
+ "ms" => "Malaysian",
69
+ "mt" => "Maltese",
70
+ "no" => "Norwegian",
71
+ "pl" => "Polish",
72
+ "pt" => "Portuguese",
73
+ "pt-br" => "Portuguese/Brazil",
74
+ "rm" => "Rhaeto-Romanic",
75
+ "ro" => "Romanian",
76
+ "ro-mo" => "Romanian/Moldavia",
77
+ "ru" => "Russian",
78
+ "ru-mo" => "Russian /Moldavia",
79
+ "gd" => "Scots Gaelic",
80
+ "sr" => "Serbian",
81
+ "sk" => "Slovack",
82
+ "sl" => "Slovenian",
83
+ "sb" => "Sorbian",
84
+ "es" => "Spanish",
85
+ "es-do" => "Spanish",
86
+ "es-ar" => "Spanish/Argentina",
87
+ "es-co" => "Spanish/Colombia",
88
+ "es-mx" => "Spanish/Mexico",
89
+ "es-es" => "Spanish/Spain",
90
+ "es-gt" => "Spanish/Guatemala",
91
+ "es-cr" => "Spanish/Costa Rica",
92
+ "es-pa" => "Spanish/Panama",
93
+ "es-ve" => "Spanish/Venezuela",
94
+ "es-pe" => "Spanish/Peru",
95
+ "es-ec" => "Spanish/Ecuador",
96
+ "es-cl" => "Spanish/Chile",
97
+ "es-uy" => "Spanish/Uruguay",
98
+ "es-py" => "Spanish/Paraguay",
99
+ "es-bo" => "Spanish/Bolivia",
100
+ "es-sv" => "Spanish/El salvador",
101
+ "es-hn" => "Spanish/Honduras",
102
+ "es-ni" => "Spanish/Nicaragua",
103
+ "es-pr" => "Spanish/Puerto Rico",
104
+ "sx" => "Sutu",
105
+ "sv" => "Swedish",
106
+ "sv-se" => "Swedish/Sweden",
107
+ "sv-fi" => "Swedish/Finland",
108
+ "ts" => "Thai",
109
+ "tn" => "Tswana",
110
+ "tr" => "Turkish",
111
+ "uk" => "Ukrainian",
112
+ "ur" => "Urdu",
113
+ "vi" => "Vietnamese",
114
+ "xh" => "Xshosa",
115
+ "ji" => "Yiddish",
116
+ "zu" => "Zulu"}
117
+
118
+ def initListClass
119
+ @itClass = 1
120
+ end
121
+
122
+ def popListClass
123
+ ret = getListClass
124
+ @itClass = @itClass + 1
125
+ return ret
126
+ end
127
+
128
+ def getListClass
129
+ return "even" if @itClass%2 == 0
130
+ return "odd" if @itClass%2 == 1
131
+ end
132
+
133
+ def get_path_image(path, ors)
134
+ "<a href='#{path}' title='#{path}'><img src='/images/railstat/#{get_index(path, ors)}.gif' alt='#{path}'/></a>"
135
+ end
136
+
137
+ def get_index(resource, ordered_resource)
138
+ ordered_resource.fetch(resource, 'other')
139
+ end
140
+
141
+ def get_domain_name(ipaddr)
142
+ return @domain_names[ipaddr] if @domain_names and @domain_names.has_key?(ipaddr)
143
+ dname = ipaddr.to_s
144
+ begin
145
+ a = Socket.gethostbyname(ipaddr)
146
+ dname = Socket.gethostbyaddr(a[3], a[2])[0]
147
+ if @domain_names
148
+ @domain_names[ipaddr] = dname
149
+ else
150
+ @domain_names = {ipaddr => dname}
151
+ end
152
+ rescue
153
+ end
154
+ return dname
155
+ end
156
+
157
+ def get_lang(lang)
158
+ @@languages.fetch(lang, lang)
159
+ end
160
+
161
+ def local_time(date_obj)
162
+ return "" if date_obj.nil?
163
+ "<span class='LOCAL_TIME'>#{date_obj.gmtime.strftime("%m/%d/%Y %H:%M:%S")}</span>"
164
+ end
165
+
166
+ def get_time(t)
167
+ ts = Time.at(t)
168
+ tn = Time.now
169
+ if ts.day == tn.day and tn.year == ts.year and tn.month == ts.month
170
+ # ts.strftime("%I:%M&nbsp;%p")
171
+ local_time(ts)
172
+ else
173
+ ts.strftime("%d&nbsp;%b")
174
+ end
175
+ end
176
+
177
+ def path_version_data(value)
178
+ return 'Unkown' if value.nil?
179
+ return 'Unsupported' if value == "0"
180
+ return 'Supports' if value == "1"
181
+ return value
182
+ end
183
+
184
+ end
@@ -0,0 +1,5 @@
1
+ class Iptoc < ActiveRecord::Base
2
+ def self.find_by_ip_address(ip_address)
3
+ find (:first, :conditions => ["IP_FROM <= inet_aton(?) AND IP_TO >= inet_aton(?)", ip_address, ip_address])
4
+ end
5
+ end
@@ -0,0 +1,130 @@
1
+ class RailStat < ActiveRecord::Base
2
+
3
+ # Method returns paths hash with to 40 paths whith value = top index (1..40)
4
+ def RailStat.get_ordered40resources(subdomain)
5
+ ordered_resources = []
6
+ find_by_sql("SELECT resource, COUNT(resource) AS requests, max(dt) as dt " +
7
+ "FROM rail_stats WHERE subdomain = '#{subdomain}' " +
8
+ "GROUP BY resource ORDER BY dt DESC LIMIT 0,40").each { |row|
9
+ ordered_resources << row
10
+ }
11
+ i = 1
12
+ orh = {}
13
+ ordered_resources = ordered_resources.sort {|x,y| y['requests'].to_i <=> x['requests'].to_i }
14
+ ordered_resources.each { |row|
15
+ orh[row['resource']] = i
16
+ i = i + 1
17
+ } unless ordered_resources.nil? or ordered_resources.size == 0
18
+ return orh, ordered_resources
19
+ end
20
+
21
+ def RailStat.find_all_by_flag(include_search_engine, number_hits, subdomain)
22
+ if include_search_engine
23
+ find_all(["subdomain = ? and browser <> 'Crawler/Search Engine'", subdomain], ["dt desc"], [number_hits,0])
24
+ else
25
+ find_all(["subdomain = ?", subdomain], ["dt desc"], [number_hits,0])
26
+ end
27
+ end
28
+
29
+ def marked?
30
+ (@marked and @marked == true)
31
+ end
32
+
33
+ def mark
34
+ @marked = true
35
+ end
36
+
37
+ def RailStat.get_last_week_stats(subdomain)
38
+ find_by_sql("select date_format(from_unixtime(dt), '%Y-%m-%d' ) as hdate, count(*) as hits " +
39
+ "from rail_stats " +
40
+ "where to_days(now()) - to_days(from_unixtime(dt)) <= 7 and subdomain = '#{subdomain}' " +
41
+ "group by hdate order by 1 desc;")
42
+ end
43
+
44
+ def RailStat.find_first_hit(subdomain)
45
+ find(:first, :conditions => ["subdomain = ?", subdomain], :order => "dt ASC")
46
+ end
47
+
48
+ def datetime
49
+ Time.at(self.dt)
50
+ end
51
+
52
+ def RailStat.total_count(subdomain)
53
+ RailStat.count(["subdomain = ?", subdomain])
54
+ end
55
+
56
+ def RailStat.unique_count(subdomain)
57
+ uniques = 0
58
+ connection.select_all("SELECT COUNT(DISTINCT remote_ip) AS 'hits' from rail_stats " <<
59
+ "where subdomain = '#{subdomain}';").each { |row|
60
+ uniques = row['hits'].to_i
61
+ }
62
+ uniques
63
+ end
64
+
65
+ def RailStat.total_day_count(subdomain, date)
66
+ totals = 0
67
+ connection.select_all("SELECT COUNT(*) AS 'hits' from rail_stats " <<
68
+ "where subdomain = '#{subdomain}' and to_days('#{date}') - to_days(from_unixtime(dt)) = 0").each { |row|
69
+ totals = row['hits'].to_i
70
+ }
71
+ totals
72
+ end
73
+
74
+ def RailStat.unique_day_count(subdomain, date)
75
+ uniques = 0
76
+ connection.select_all("SELECT COUNT(DISTINCT remote_ip) AS 'hits' from rail_stats " <<
77
+ "where subdomain = '#{subdomain}' and to_days('#{date}') - to_days(from_unixtime(dt)) = 0").each { |row|
78
+ uniques = row['hits'].to_i
79
+ }
80
+ uniques
81
+ end
82
+
83
+ def RailStat.platform_stats(subdomain, hits)
84
+ find_by_sql("SELECT platform, COUNT(platform) AS 'total', (COUNT(platform)/#{hits})*100 as percent " +
85
+ "FROM rail_stats " +
86
+ "WHERE subdomain = '#{subdomain}' " +
87
+ "GROUP BY platform " +
88
+ "ORDER BY total DESC; ")
89
+ end
90
+
91
+ def RailStat.browser_stats(subdomain, hits)
92
+ find_by_sql("SELECT browser, version, COUNT(*) AS 'total', (COUNT(*)/#{hits})*100 as percent " +
93
+ "FROM rail_stats " +
94
+ "WHERE browser != 'unknown' and subdomain = '#{subdomain}'" +
95
+ "GROUP BY browser, version " +
96
+ "ORDER BY total DESC; ")
97
+ end
98
+
99
+ def RailStat.language_stats(subdomain, hits)
100
+ find_by_sql("SELECT language, COUNT(*) AS 'total', (COUNT(*)/#{hits})*100 as percent " +
101
+ "FROM rail_stats " +
102
+ "WHERE language != '' and language is not null and language != 'empty' and subdomain = '#{subdomain}'" +
103
+ "GROUP BY language " +
104
+ "ORDER BY total DESC; ")
105
+ end
106
+
107
+ def RailStat.country_stats(subdomain, hits)
108
+ find_by_sql("SELECT country, COUNT(*) AS 'total', (COUNT(*)/#{hits})*100 as percent " +
109
+ "FROM rail_stats " +
110
+ "WHERE country != '' and country is not null and subdomain = '#{subdomain}'" +
111
+ "GROUP BY country " +
112
+ "ORDER BY total DESC; ")
113
+ end
114
+
115
+ def RailStat.domain_stats(subdomain)
116
+ find_by_sql("SELECT domain, referer, resource, COUNT(domain) AS 'total' " +
117
+ "FROM rail_stats " +
118
+ "WHERE domain != '' and subdomain = '#{subdomain}'" +
119
+ "GROUP BY domain " +
120
+ "ORDER BY total DESC, dt DESC; ")
121
+ end
122
+
123
+ def RailStat.stats_dyn(column, subdomain, hits)
124
+ find_by_sql("SELECT #{column}, COUNT(*) AS 'total', (COUNT(*)/#{hits})*100 as percent " +
125
+ "FROM rail_stats " +
126
+ "WHERE subdomain = '#{subdomain}'" +
127
+ "GROUP BY #{column} " +
128
+ "ORDER BY total DESC; ")
129
+ end
130
+ end
@@ -0,0 +1,19 @@
1
+ class SearchTerm < ActiveRecord::Base
2
+ def self.register(searchterms, domain, subdomain)
3
+ terms = find_all(["domain = ? and subdomain = ? and searchterms = ?", domain, subdomain, searchterms.to_s])
4
+ if terms and terms.size > 0
5
+ terms.each {|term|
6
+ term.count = term.count + 1
7
+ term.save
8
+ }
9
+ else
10
+ self.create("count"=>1, "searchterms" => searchterms.to_s, "subdomain" => subdomain, 'domain' => domain)
11
+ end
12
+ end
13
+
14
+ def self.find_grouped(subdomain)
15
+ sts = []
16
+ connection.select_all("select searchterms, sum(count) as count from search_terms where subdomain = '#{subdomain}' group by domain order by 2 desc;").each { |row| sts << row }
17
+ sts
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml">
4
+ <head>
5
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
6
+ <meta http-equiv="imagetoolbar" content="no" />
7
+ <%%= stylesheet_link_tag "railstat.css" %>
8
+ <%%= stylesheet_link_tag "tabs.css" %>
9
+ <%%= javascript_include_tag "railstat" %>
10
+ </head>
11
+
12
+ <body onload="show_date_as_local_time()">
13
+ <%%= @content_for_layout %>
14
+
15
+ <script type="text/javascript" src="/rail_stat/tracker_js"></script>
16
+ <noscript>
17
+ <a href="/"><img src="/rail_stat/track" border="0" width="1px" height="1px" alt=""/></a>
18
+ </noscript>
19
+
20
+ </body>
21
+ </html>
22
+
@@ -0,0 +1,17 @@
1
+ <div id="header">
2
+ <ul id="primary">
3
+ <li><span>Statistics</span>
4
+ <ul id="secondary">
5
+ <li><%%= link_to('Paths', :controller => 'rail_stat', :action => 'path') %></li>
6
+ <li><%%= link_to('Hits', :controller => 'rail_stat', :action => 'hits') %></li>
7
+ <li><%%= link_to('Platform and Browser', :controller => 'rail_stat', :action => 'platform') %></li>
8
+ <li><%%= link_to('Languages', :controller => 'rail_stat', :action => 'lang') %></li>
9
+ <li><%%= link_to('Referrers', :controller => 'rail_stat', :action => 'refs') %></li>
10
+ <li><%%= link_to('Other', :controller => 'rail_stat', :action => 'other') %></li>
11
+ </ul>
12
+ </li>
13
+ </ul>
14
+ </div>
15
+ <div id="tab_main">
16
+ <div id="tab_content">
17
+
@@ -0,0 +1,28 @@
1
+ <h1> Statistic on web hits for <%%= @subdomain %> </h1>
2
+ <%%=render_partial 'menu'%>
3
+ <h2>Last week hits</h2>
4
+ <div id="path_stats_lw">
5
+ <table class="list">
6
+ <tr><th>Date</th><th>Hits</th></tr>
7
+ <%% initListClass
8
+ for day in @lastweek %>
9
+ <tr class="<%%=popListClass%>">
10
+ <td><%%=day['hdate']%></td>
11
+ <td><%%=day['hits']%></td>
12
+ </tr>
13
+ <%% end %>
14
+ </table>
15
+ </div>
16
+
17
+ <h2>Hits summary</h2>
18
+ <div id="path_stats_hs">
19
+ <table class="list">
20
+ <tr><th>Totals</th><th>Uniques</th></tr>
21
+ <tr class="even"><td colspan="2">From <%%=@first_hit.datetime%></td></tr>
22
+ <tr class="odd"><td><%%=@total_hits%></td><td><%%=@unique_hits%></td></tr>
23
+ <tr class="even"><td colspan="2">Only on <%%=Time.now%></td></tr>
24
+ <tr class="odd"><td><%%=@today_total%></td><td><%%=@today_unique%></td></tr>
25
+ </table>
26
+ </div>
27
+ </div>
28
+ </div>
@@ -0,0 +1,25 @@
1
+ <h1>Language and countries statistics for <%%= @subdomain %> </h1>
2
+ <%%=render_partial 'menu'%>
3
+ <h2>Languages</h2>
4
+ <div id="path_stats_lang">
5
+ <table class="list">
6
+ <tr><th>Language</th><th>%</th></tr>
7
+ <%% initListClass
8
+ for row in @languages %>
9
+ <tr class="<%%=popListClass%>"><td><%%=get_lang(row['language'])%></td><td><%%=row['percent']%>%</td></tr>
10
+ <%% end %>
11
+ </table>
12
+ </div>
13
+
14
+ <h2>Countries</h2>
15
+ <div id="path_stats_country">
16
+ <table class="list">
17
+ <tr><th>Country</th><th>%</th></tr>
18
+ <%% initListClass
19
+ for row in @countries %>
20
+ <tr class="<%%=popListClass%>"><td><%%=row['country']%></td><td><%%=row['percent']%>%</td></tr>
21
+ <%% end %>
22
+ </table>
23
+ </div>
24
+ </div>
25
+ </div>