This repository has been archived by the owner on Dec 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.rb
79 lines (70 loc) · 2.8 KB
/
Stats.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
class Stats
@db = SQLite3::Database.new './curry_house.sqlite'
@date = Time.now.strftime("%d/%m/%Y")
def self.increment type, city
has_day = @db.get_first_value('SELECT COUNT(*) FROM daily_stats WHERE date = ? AND city = ?',@date,city)
if has_day==0
@db.execute("INSERT INTO daily_stats(city,date,#{type}) VALUES(?,?,1)" ,[city,@date])
else
new_value = @db.get_first_value("SELECT #{type} FROM daily_stats WHERE date = ? AND city = ?",[@date,city]).to_i + 1
@db.execute("UPDATE daily_stats SET #{type} = ? WHERE date = ? AND city = ?",[new_value,@date,city])
end
end
def self.get type, city
if city.eql? 'all'
return Stats.get(type, 'sheffield') + Stats.get(type,'birmingham')
end
has_day = @db.get_first_value('SELECT COUNT(*) FROM daily_stats WHERE date = ? AND city=?',[@date,city])
if has_day==0
@db.execute('INSERT INTO daily_stats(city,date) VALUES(?,?)' ,[city,@date])
return 0
else
return @db.get_first_value("SELECT #{type} FROM daily_stats WHERE date = ? AND city=?",[@date,city]).to_i
end
end
def self.get_avg type, city
if city.eql? 'all'
count = @db.get_first_value('SELECT COUNT(*) FROM daily_stats')
sum = @db.get_first_value("SELECT SUM(#{type}) FROM daily_stats")
return count!=0 ? sum/count : 0
end
count = @db.get_first_value('SELECT COUNT(*) FROM daily_stats WHERE city = ? ',city)
sum = @db.get_first_value("SELECT SUM(#{type}) FROM daily_stats WHERE city = ?",city)
return count!=0 ? sum/count : 0
end
def self.get_popular_item city
if city.eql? 'all'
return [Stats.get_popular_item('sheffield'),Stats.get_popular_item('birmingham')].max
end
orders = @db.execute('SELECT text FROM tweets WHERE city = ?',city)
max_id = @db.get_first_value('SELECT max(id) FROM menu')
max_id = 0 if max_id.nil?
count = Array.new(max_id+1, 0)
orders.each do |order_text|
items = order_text[0].gsub(/^\s+|\s+$/,'').split(/\s+/)
items.each do |item|
if item.to_i<=max_id
count[item.to_i]+=1
end
end
end
return count.find_index(count.max)
end
def self.get_total_cost city
if city.eql? 'all'
sum = @db.get_first_value('SELECT SUM(sum) FROM tweets WHERE status = "Completed"')
return !sum.nil? ? sum : 0
end
sum = @db.get_first_value('SELECT SUM(sum) FROM tweets WHERE (city = ?) AND (status = "Completed")',city)
return !sum.nil? ? sum : 0
end
def self.get_avg_cost city
if city.eql? 'all'
count = @db.get_first_value('SELECT COUNT(*) FROM tweets WHERE status = "Completed"')
else
count = @db.get_first_value('SELECT COUNT(*) FROM tweets WHERE (city = ?) AND (status = "Completed")',city)
end
sum = Stats.get_total_cost city
return count!=0 ? sum/count : 0
end
end