Browse code

New class for database communication

Cinan Rakosnik authored on 16/12/2012 at 13:35:29
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ require_relative "rubysermon/version"
2 2
 require_relative "rubysermon/mod_loader"
3 3
 require_relative "rubysermon/mod_template"
4 4
 require_relative "rubysermon/configurator"
5
+require_relative "rubysermon/db"
5 6
 
6 7
 require "date"
7 8
 
... ...
@@ -11,13 +12,15 @@ module Rubysermon
11 11
 	MOD_PATH = "#{LIB_PATH}/rubysermon/mod"
12 12
 
13 13
 	class App
14
-		attr_accessor :config_path
14
+		attr_accessor :config_path, :db_path
15 15
 
16 16
 		def initialize
17 17
 			@config 		= {repeat: 60}
18 18
 			@running_mods	= []
19 19
 			@config_path	= "#{LIB_PATH}/rubysermon/config.json"
20 20
 			@enabled_mods	= []
21
+			@db				= nil
22
+			@db_path		= "/tmp/rubysermon_history.db"
21 23
 		end
22 24
 
23 25
 		def run
... ...
@@ -28,6 +31,7 @@ module Rubysermon
28 28
 				abort(msg)
29 29
 			end
30 30
 
31
+			start_db()
31 32
 			start_fetch_process_sleep_cycle()
32 33
 		end
33 34
 
... ...
@@ -63,6 +67,10 @@ module Rubysermon
63 63
 			false
64 64
 		end
65 65
 
66
+		def start_db
67
+			@db = DB.new(@db_path)
68
+		end
69
+
66 70
 		def start_fetch_process_sleep_cycle
67 71
 			while true
68 72
 				current_time = DateTime.now()
69 73
new file mode 100644
... ...
@@ -0,0 +1,26 @@
0
+require "sqlite3"
1
+
2
+module Rubysermon
3
+	class DB
4
+
5
+		def initialize(db_path)
6
+			@db = SQLite3::Database.new(db_path)
7
+			@db.execute(self.initialize_db)
8
+		end
9
+
10
+		private
11
+
12
+		def self.initialize_db
13
+			<<-SQL
14
+				DROP TABLE IF EXISTS `history`;
15
+				CREATE TABLE `history` (
16
+					`date` INTEGER NOT NULL,
17
+					`mod_hash` VARCHAR(20) NOT NULL,
18
+					`value` REAL NOT NULL
19
+				);
20
+				CREATE INDEX date_index ON `history` (`date`);
21
+			SQL
22
+		end
23
+
24
+	end
25
+end