-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.rb
299 lines (232 loc) · 7.17 KB
/
models.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
require 'data_mapper'
require 'email'
require 'my_rollbar'
require 'hashie'
DB_STRING_LENGTH=255
DataMapper::Property::String.length(DB_STRING_LENGTH)
module Models
class Base
def self.timestamps!
property :created_at, DateTime,
:required => true,
:default => lambda { |r, p| Time.now.utc.to_datetime }
property :updated_at, DateTime,
:required => true,
:default => lambda { |r, p| Time.now.utc.to_datetime }
end
def self.first_or_create(opts)
first(opts) || create(opts)
end
end
class Unparseable < Base
include DataMapper::Resource
property :id, Serial, :key => true
property :data, String, :required => true
property :status_code, Integer
property :resolved, String, :required => false
timestamps!
after :create do |unparseable|
#Keen.publish("create_unparseable", {})
Rollbar.error("New unparseable created: #{unparseable.data}")
end
end
# TODO: an elevator may have more than one name
class Elevator < Base
include DataMapper::Resource
property :id, Serial, :key => true
property :name, String, :index => true, :unique => true
property :alias_id, Integer, :required => false
#has n, :systems, :through => :stations
has n, :outages, :through => Resource
belongs_to :station, :required => false
has n, :users, :through => :station
after :create do |elevator|
#Keen.publish("create_elevator", :name => elevator.name)
Rollbar.error("New elevator created: #{elevator.name}, #{elevator.id}") unless ENV['ADDING_ELEVATORS']
end
timestamps!
def self.stationless
all(:station => nil)
end
def to_h
{
:name => name
}
end
end
class Station < Base
include DataMapper::Resource
property :id, Serial, :key => true
# TODO a station might have more than one name, e.g. BART/MUNI
property :name, String, :index => true, :unique => true
has n, :elevators
has n, :systems, :through => Resource
has n, :users, :through => Resource
timestamps!
def to_h
{
:name => name,
:elevators => elevators.map(&:to_h)
}
end
end
class System < Base
include DataMapper::Resource
property :id, Serial, :key => true
property :name, String, :index => true, :unique => true
has n, :stations, :through => Resource
has n, :elevators, :through => :stations, :constraint => :destroy
def visible?
self.class.visible.include?(self)
end
def self.visible
all
end
def to_h
{
:name => name,
:stations => stations.map(&:to_h)
}
end
timestamps!
end
class Outage < Base
include DataMapper::Resource
property :id, Serial, :key => true
belongs_to :elevator
#has n, :systems, :through => :elevator
property :started_at, DateTime,
:required => true,
:default => lambda { |r, p| Time.now.utc.to_datetime }
property :ended_at, DateTime,
:required => false,
:index => true
def end!
self.ended_at = DateTime.now
self.save
end
def length
((ended_at || DateTime.now) - started_at).to_f*24
end
def self.all_open(opts={})
all(opts.merge(:ended_at => nil))
end
def self.all_closed(opts={})
all(opts.merge(:ended_at.not => nil))
end
# Gets all now-closed outages that were open at the specified time
# Doesn't include any currently-open outages
def self.all_at_time(time)
all(:started_at.lte => time,
:ended_at.gte => time)
end
end
class Metric < Base
include DataMapper::Resource
property :id, Serial, :key => true
property :name, String, :required => true, :index => true
property :counter, Integer, :default => 0
# This is *wildly* un-threadsafe
def self.incr(name)
m = Models::Metric.first_or_create(:name => name)
m.counter += 1
m.save
end
end
class Report < Base
include DataMapper::Resource
property :id, Serial, :key => true
property :problem_type, String
property :problem, Text
property :elevator_id, Integer
property :station_id, Integer # optional, only needed if faregate report
belongs_to :user
timestamps!
def elevator
if elevator_id.zero?
0
else
Models::Elevator.first(id: elevator_id)
end
end
def elevator=(e)
case e
when Integer, String
elevator_id = e
when Models::Elevator
elevator_id = e.id
end
end
end
class User < Base
include DataMapper::Resource
property :id, Serial, :key => true
property :email, String, :index => true
property :name, String, :required => false
property :can_see_invisible_systems, Boolean, :default => false
property :phone_number, String, :required => false
property :phone_number_verified, Boolean, :default => false
property :can_submit_reports, Boolean, :default => false
timestamps!
has n, :stations, :through => Resource
has n, :elevators, :through => :stations
def can_submit_reports
true
end
def can_see_invisible_systems?
!!can_see_invisible_systems
end
def use_phone_number?
self.phone_number && self.phone_number_verified
end
def current_notification_address
if self.use_phone_number?
_, area_code, exchange, extension = self.phone_number.match(/(...)(...)(....)/).to_a
"(#{area_code}) #{exchange}-#{extension}"
else
self.email
end
end
end
def self.setup
if ENV['RACK_ENV'] == 'testing'
ENV['DATABASE_URL'] = 'sqlite::memory:'
end
DataMapper.setup(:default, ENV['DATABASE_URL'])
DataMapper::Model.raise_on_save_failure = true
DataMapper.repository.adapter.resource_naming_convention =
DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
DataMapper.finalize
DataMapper.auto_upgrade! unless ENV['RACK_ENV'] == 'testing'
end
def self.seed_hash
{
:systems => Models::System.all.map(&:to_h),
:date => DateTime.now.iso8601
}
end
def self.from_seed_hash(seed_hash)
Hashie.symbolize_keys!(seed_hash)
seed_hash[:systems].each do |system|
puts "Importing system: #{system[:name]}..."
system_model = Models::System.first_or_create(:name => system[:name])
system[:stations].map do |station|
puts "Importing station: #{station[:name]}..."
station_model = Models::Station.first_or_create(:name => station[:name])
station_model.systems = [system_model]
station_model.save
station[:elevators].map do |elevator|
puts "Importing elevator: #{elevator[:name]}..."
Models::Elevator.first_or_create(:name => elevator[:name],
:station => station_model)
end
end
end
puts "And adding one 'faregate' dummy elevator, since that's used by the /report form ..."
DataMapper.repository.adapter.execute(
"INSERT INTO elevators (id, name, created_at, updated_at)
VALUES (0, 'faregate', NOW(), NOW())"
)
puts "DONE IMPORTING DATA."
end
end