· 8 years ago · Feb 28, 2018, 07:50 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 auth_required
65 dataset = DB[:records].order(:log_time)
66 @row = dataset.first
67 haml :record_detail
68end
69
70get '/login' do
71 haml :login
72end
73
74post '/login' do
75 if authenticate(params[:login], params[:password])
76 session[:authenticated] = true
77 continue
78 else
79 redirect '/login'
80 end
81end
82
83get '/logout' do
84 auth_required
85 session[:authenticated] = nil
86 redirect '/'
87end
88
89get '/stylesheets/style.css' do
90 header 'Content-Type' => 'text/css; charset=utf-8'
91 sass :style
92end
93
94private
95
96def auth_required
97 return session[:authenticated] if session[:authenticated]
98 session[:requested] = request.fullpath
99 redirect '/login'
100end
101
102def continue
103 redirect session[:requested] if session[:requested]
104 redirect '/'
105end
106
107def authenticate(login,password)
108 login == 'admin' && password == 'admin'
109end
110
111__END__
112
113@@layout
114!!!
115%html
116 %head
117 %title App
118 %link{:rel=>"stylesheet", :href=>"/stylesheets/style.css", :type => "text/css"}
119 %body
120 #banner App
121 #nav
122 %a{:href=>("/")}= "home"
123 %a{:href=>("/record")}= "browse"
124 %a{:href=>("/logout" )}= "logout"
125 #content
126 = yield
127
128@@index
129#content
130 This is the index page.
131
132@@login
133#login-form
134%form{:method => "post", :action => "/login"}
135 .label Login:
136 %input{:id=>"login", :name=>"login", :size=>"30", :type=>"text"}
137 .label Password
138 %input{:id=>"password", :name=>"password", :size=>30, :type=>"password"}
139 .button
140 %input{:type=>"submit", :value=>"login"}
141
142@@record_detail
143#record-detail
144%table.display
145 %caption= 'Log Record'
146 %tbody
147 - @row.each_pair do |k,v|
148 %tr
149 %th= k
150 %td= v
151
152@@style
153#banner
154 :height 20px
155 :text-align left
156
157#content
158 :font-family arial
159 :font-size 10px
160
161table.display
162 :border-collapse collapse
163 :width 100%
164 th
165 :border 1px solid gray
166 :text-align left
167 :background #aaf
168 :width 20%
169
170 td
171 :border 1px solid gray
172 :text-align left
173 :background #ffa
174 :width 80%