-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmaildata.rb
105 lines (90 loc) · 3.31 KB
/
dmaildata.rb
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
class DMailDB
def initialize
@db = SQLite3::Database.new($DMAIL_DB)
end
def insert(record)
delete_if_exist({:timestamp=>record[:timestamp], :user_id=>record[:user_id],
:tag=>record[:tag]})
@db.execute("insert into dmail_data values (:id, :timestamp, :user_id, :tag, :value)",
record)
end
def delete_if_exist(cond)
sql = "delete from dmail_data where " + build_where_condition(cond)
@db.execute(sql)
end
def sum_values(cond)
sql = "select sum(value) from dmail_data where " + build_where_condition(cond)
result = @db.get_first_value(sql)
return result
end
def count_values(cond)
sql = "select count(value) from dmail_data where " + build_where_condition(cond)
result = @db.get_first_value(sql)
return result
end
def avg_values(cond)
sql = "select avg(value) from dmail_data where " + build_where_condition(cond)
result = @db.get_first_value(sql)
return result
end
def load_historical_data(user_id, data, tag)
data.each do |d|
@db.execute("insert into dmail_data values (:id, :timestamp, :user_id, :tag, :value)",
{:timestamp=> d[0], :value=> d[1], :user_id=> user_id, :tag=> tag})
end
end
def dump_historical_data(cond)
sql = "select timestamp, value from dmail_data where " + build_where_condition(cond)
@db.query(sql)
end
def build_where_condition(cond)
sql = ""
sep = ""
if cond.key?(:timestamp_begin) && cond.key?(:timestamp_end) then
sql = sql + "timestamp between '#{cond[:timestamp_begin]}' and '#{cond[:timestamp_end]}' "
cond.delete(:timestamp_begin)
cond.delete(:timestamp_end)
sep = "and "
end
if cond.key?(:timestamp) then
sql = sql + sep + "timestamp='#{cond[:timestamp]}' "
sep = "and "
end
if cond.key?(:user_id) then
sql = sql + sep + "user_id='#{cond[:user_id]}' "
sep = "and "
end
if cond.key?(:tag) then
sql = sql + sep + "tag='#{cond[:tag]}' "
sep = "and "
end
return sql
end
end
if __FILE__ == $0 then
require 'config.rb'
require 'rubygems'
require 'sqlite3'
db = DMailDB.new
db.insert({:timestamp => '2011-06-20 00:00:00',
:user_id => 'test-01', :tag => '売上', :value => 234})
db.insert({:timestamp => '2011-06-21 00:00:00',
:user_id => 'test-01', :tag => '売上', :value => 301})
db.insert({:timestamp => '2011-06-22 00:00:00',
:user_id => 'test-01', :tag => '売上', :value => 338})
db.insert({:timestamp => '2011-06-23 00:00:00',
:user_id => 'test-01', :tag => '売上', :value => 184})
db.insert({:timestamp => '2011-06-24 00:00:00',
:user_id => 'test-01', :tag => '売上', :value => 495})
db.insert({:timestamp => '2011-06-24 00:00:00',
:user_id => 'test-01', :tag => '売上', :value => 666})
p db.sum_values({:timestamp_begin => '2011-06-01 00:00:00',
:timestamp_end => '2011-06-30 00:00:00',
:user_id => 'test-01', :tag => '売上'})
p db.count_values({:timestamp_begin => '2011-06-01 00:00:00',
:timestamp_end => '2011-06-30 00:00:00',
:user_id => 'test-01', :tag => '売上'})
db.delete_if_exist({:timestamp_begin => '2011-06-01 00:00:00',
:timestamp_end => '2011-06-30 00:00:00',
:user_id => 'test-01', :tag => '売上'})
end