1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
require 'sqlite3'
class Datenbank @Counter @db = nil @last_id @last_id_by_date @@datenbanknamen def open begin @db = SQLite3::Database.open (@@datenbanknamen) @db.execute "CREATE TABLE IF NOT EXISTS Counter(Id INTEGER PRIMARY KEY DEFAULT NULL, 'Ip_Adresse' VARCHAR(16), Datum DATETIME)" rescue SQLite3::Exception => e puts "Exception occured" puts e end end def close begin @db.close rescue Exception => e puts e end end def transaction @db.transaction end def create_table @db.execute "CREATE TABLE IF NOT EXISTS Counter(Id INTEGER PRIMARY KEY DEFAULT NULL, 'Ip_Adresse' VARCHAR(16), Datum DATETIME)" end def initialize(datenbankpfad) begin @@datenbanknamen = datenbankpfad self.open self.create_table end def drop_table @db.execute "DROP TABLE Counter" end def last_row_id self.open @db.results_as_hash=true @last_id = @db.execute ("SELECT MAX(id) as id FROM Counter LIMIT 1") @db.results_as_hash=false self.close return @last_id[0][0] end def prepare(params = {}) begin v = @db.prepare "INSERT INTO Counter VALUES(? , ? , ?)" v.bind_param 1, params[:id] v.bind_param 2, params[:ip_adress] v.bind_param 3, params[:datetime] v.execute rescue SQLite3::Exception => e puts e end end def fetchbydatetime(datum) self.open return @db.execute ("SELECT * From Counter WHERE Datum > '#{datum.to_s()}' " ) end def commit begin @db.commit rescue SQLite3::Exception => e puts "Exception occured" puts e @db.rollback end end def fetch() v = @db.prepare "SELECT * FROM Counter" rows = v.execute return rows end end end
class Counter @id @ip_adress @datetime
def initialize(params = {}) @id = params[:id] @ip_adress = params[:ip_adress] @datetime = params[:datetime] end def fetchdata() return hash = { :id => @id, :ip_adress => @ip_adress, :datetime => @datetime } end end
class Session def chkdb(besucher, datenbank) begin wert = besucher.fetchdata if wert[:ip_adress] then begin datenbank.open row = datenbank.fetchbydatetime(1800) row.each do |object| if wert[:ip_adress] == object[1] return false else return true end end rescue Exception =>e puts "Fehler : Datenbankfehler." end end rescue Exception => e puts "Fehler : Kein Objekt vom Typ Counter ��bergeben." end end end
class Master @besucher = [] @datenbank @lastid def gibBesucherAnzahl return @lastid = @datenbank.last_row_id end def initialize @besucher = Array.new @datenbank = Datenbank.new('test.db') gibBesucherAnzahl() end def gibBesucherHeute tag = 84400 @datenbank.open return @datenbank.fetchbydatetime(Time.now-tag) end def gibBesucherWoche woche = 84400*7 @datenbank.open return @datenbank.fetchbydatetime(Time.now-woche) end def gibBesucherMonat tag = 84400 time = Time.new date = Date.new(time.year,time.month,-1) @datenbank.open return @datenbank.fetchbydatetime(Time.now - (date.day*tag)) end def gibBesucherHalbjahr tag = 84400 @datenbank.open return @datenbank.fetchbydatetime(Time.now - (tag*(365/2))) end def neuerBesucher(params = {}) if !params[:id] then id = @lastid.to_i + 1 else id = params[:id] end ip = params[:ip] if params[:ip] if params[:datetime] then datetime = params[:datetime] else datetime = Time.now.to_s end @besucher << Counter.new(p = {:id => id, :ip_adress => ip, :datetime => datetime}) @lastid = @lastid.to_i + 1 return true end def save @datenbank.open @datenbank.transaction @besucher.each do |gast| @datenbank.prepare(gast.fetchdata) end @datenbank.commit end def load @datenbank.open obj = @datenbank.fetch self.reset obj .each do |guest| @besucher << Counter.new(p = {:id => guest[0], :ip_adress => guest[1], :datetime => guest[2]}) end end def reset @besucher = Array.new end def reset_db_table @datenbank.open @datenbank.drop_table @datenbank.create_table end
end
tag = 84400 woche = tag*7 monat = woche*4 time = Time.now-tag*730 master = Master.new
master.neuerBesucher(p = {:ip => '192.168.1.35'}) master.save
puts master.gibBesucherAnzahl
|