· 8 years ago · Feb 28, 2018, 07:44 AM
1require 'rubygems'
2require 'sinatra'
3require 'haml'
4require 'sequel'
5require 'fastercsv'
6
7DB = Sequel.sqlite('log.db') unless defined?(DB)
8
9DB << %{
10 CREATE TABLE IF NOT EXISTS records
11 (
12 log_time timestamp,
13 user_name text,
14 database_name text,
15 process_id integer,
16 connection_from text,
17 session_id text,
18 session_line_num bigint,
19 command_tag text,
20 session_start_time timestamp,
21 virtual_transaction_id text,
22 transaction_id bigint,
23 error_severity text,
24 sql_state_code text,
25 message text,
26 detail text,
27 hint text,
28 internal_query text,
29 internal_query_pos integer,
30 context text,
31 query text,
32 query_pos integer,
33 location text,
34 PRIMARY KEY (session_id, session_line_num)
35 );
36}
37
38DB << "DELETE FROM records"
39
40records = DB[:records]
41
42header =
43[
44 'log_time','user_name','database_name','process_id','connection_from',
45 'session_id','session_line_num','command_tag','session_start_time',
46 'virtual_transaction_id','transaction_id','error_severity','sql_state_code',
47 'message','detail','hint','internal_query','internal_query_pos','context',
48 'query','query_pos','location'
49]
50
51FasterCSV.foreach('example_log.csv', options = { :headers => header }) do |row|
52 records << row.to_hash
53end
54
55enable :sessions
56use_in_file_templates!
57
58get '/' do
59 auth_required
60 haml :index
61end
62
63get '/record' do
64 dataset = DB[:records].order(:log_time)
65 @row = dataset.first
66 haml :record_detail
67end
68
69get '/login' do
70 haml :login
71end
72
73post '/login' do
74 if authenticate(params[:login], params[:password])
75 session[:authenticated] = true
76 continue
77 else
78 redirect '/login'
79 end
80end
81
82get '/logout' do
83 auth_required
84 session[:authenticated] = nil
85 redirect '/'
86end
87
88get '/stylesheets/style.css' do
89 header 'Content-Type' => 'text/css; charset=utf-8'
90 sass :style
91end
92
93private
94
95def auth_required
96 return session[:authenticated] if session[:authenticated]
97 session[:requested] = request.fullpath
98 redirect '/login'
99end
100
101def continue
102 redirect session[:requested] if session[:requested]
103 redirect '/'
104end
105
106def authenticate(login,password)
107 login == 'admin' && password == 'admin'
108end
109
110__END__
111
112@@layout
113!!!
114%html
115 %head
116 %title App
117 %link{:rel=>"stylesheet", :href=>"/stylesheets/style.css", :type => "text/css"}
118 %body
119 #banner App
120 #nav
121 %a{:href=>("/")}= "home"
122 %a{:href=>("/record")}= "browse"
123 %a{:href=>("/logout" )}= "logout"
124 #content
125 = yield
126
127@@index
128#content
129 This is the index page.
130
131@@login
132#login-form
133%form{:method => "post", :action => "/login"}
134 .label Login:
135 %input{:id=>"login", :name=>"login", :size=>"30", :type=>"text"}
136 .label Password
137 %input{:id=>"password", :name=>"password", :size=>30, :type=>"password"}
138 .button
139 %input{:type=>"submit", :value=>"login"}
140
141@@record_detail
142#record-detail
143%table.display
144 %caption= 'Log Record'
145 %tbody
146 - @row.each_pair do |k,v|
147 %tr
148 %th= k
149 %td= v
150
151@@style
152#banner
153 :height 20px
154 :text-align left
155
156#content
157 :font-family arial
158 :font-size 10px
159
160table.display
161 :border-collapse collapse
162 :width 100%
163 th
164 :border 1px solid gray
165 :text-align left
166 :background #aaf
167 :width 20%
168
169 td
170 :border 1px solid gray
171 :text-align left
172 :background #ffa
173 :width 80%