-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
263 lines (226 loc) · 6.5 KB
/
app.rb
File metadata and controls
263 lines (226 loc) · 6.5 KB
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
require "dotenv/load"
require "sinatra"
require "sinatra/json"
require "rufus-scheduler"
require "json"
require "logger"
require "date"
require_relative "lib/data_store"
require_relative "lib/library_scraper"
require_relative "lib/item_tracker"
class DeweyApp < Sinatra::Base
configure do
set :public_folder, "public"
set :views, "views"
enable :logging
# Set up logger
logger = Logger.new(STDOUT)
logger.level = case ENV["LOG_LEVEL"]&.upcase
when "DEBUG" then Logger::DEBUG
when "WARN" then Logger::WARN
when "ERROR" then Logger::ERROR
else Logger::INFO
end
set :logger, logger
end
def initialize(app = nil)
super
@data_store = DataStore.new("data")
@item_tracker = ItemTracker.new(@data_store.data_dir)
@scraper = LibraryScraper.new(@data_store, settings.logger)
start_scheduler
run_initial_scrape
end
# Web Dashboard Routes
get "/" do
@data = @data_store.get_all_data
@scrape_failures = @data_store.get_recent_scrape_failures
erb :dashboard
end
get "/patron/:name" do
patron_name = params[:name]
@data = @data_store.get_patron_data(patron_name)
@patron_name = patron_name
@scrape_failures = @data_store.get_recent_scrape_failures
erb :patron
end
# Thumbnail serving
get "/thumbnails/:filename" do
filename = params[:filename]
file_path = File.join("data", "thumbnails", filename)
if File.exist?(file_path)
content_type "image/jpeg"
send_file file_path
else
send_file "public/placeholder.jpg"
end
end
# API Routes for Home Assistant
get "/api/status" do
json @data_store.get_all_data
end
get "/api/patron/:name" do
json @data_store.get_patron_data(params[:name])
end
get "/api/missing-items" do
missing_events = @item_tracker.get_missing_items_report(30)
json({
missing_items_events: missing_events,
total_events: missing_events.length
})
end
get "/api/transitions" do
days_back = params[:days]&.to_i || 7
unexpected_only = params[:unexpected] == "true"
if unexpected_only
transitions = @item_tracker.get_unexpected_transitions(days_back)
else
# Could add a method to get all transitions if needed
transitions = @item_tracker.get_unexpected_transitions(days_back)
end
json({
transitions: transitions,
total: transitions.length,
days_back: days_back
})
end
get "/health" do
json({
status: "ok",
timestamp: Time.now.iso8601,
last_scrape: @data_store.get_last_scrape_time
})
end
# Manual refresh endpoint
post "/refresh" do
Thread.new do
@scraper.scrape_all_patrons
end
redirect "/"
end
# Manual thumbnail cleanup endpoint
post "/cleanup-thumbnails" do
deleted = @data_store.cleanup_stale_thumbnails(@item_tracker)
json({
status: "ok",
thumbnails_deleted: deleted,
timestamp: Time.now.iso8601
})
end
# Helper methods for views
helpers do
def format_due_date(due_date_str)
return "Unknown" unless due_date_str
begin
due_date = Time.parse(due_date_str).to_date
today = Date.today
days_until_due = (due_date - today).to_i
formatted_date = due_date.strftime("%b %d")
if days_until_due < 0
"#{formatted_date} (#{-days_until_due} days overdue)"
elsif days_until_due == 0
"#{formatted_date} (Today!)"
elsif days_until_due == 1
"#{formatted_date} (Tomorrow)"
elsif days_until_due <= 7
"#{formatted_date} (#{days_until_due} days)"
else
formatted_date
end
rescue
due_date_str
end
end
def due_date_class(due_date_str)
return "due-normal" unless due_date_str
begin
due_date = Time.parse(due_date_str).to_date
today = Date.today
days_until_due = (due_date - today).to_i
due_soon_threshold = ENV.fetch("DUE_SOON_DAYS", "5").to_i
if days_until_due < 0
"due-overdue"
elsif days_until_due <= due_soon_threshold
"due-soon"
else
"due-normal"
end
rescue
"due-normal"
end
end
def status_class(status)
return "status-waiting" unless status
status_lower = status.downcase.strip
if status_lower == "ready" || status_lower == "available"
"status-ready"
elsif status_lower == "not ready"
"status-not-ready"
elsif status_lower.include?("transit") || status_lower.include?("shipping")
"status-transit"
else
"status-waiting"
end
end
def format_timestamp(timestamp_str)
return "Unknown" unless timestamp_str
begin
timestamp = Time.parse(timestamp_str)
timestamp.strftime("%B %d, %Y at %I:%M %p")
rescue
timestamp_str
end
end
def relative_time(timestamp_str)
return "never" unless timestamp_str
begin
timestamp = Time.parse(timestamp_str)
seconds_ago = (Time.now - timestamp).to_i
if seconds_ago < 60
"#{seconds_ago} seconds ago"
elsif seconds_ago < 3600
minutes = seconds_ago / 60
"#{minutes} #{minutes == 1 ? 'minute' : 'minutes'} ago"
elsif seconds_ago < 86400
hours = seconds_ago / 3600
"#{hours} #{hours == 1 ? 'hour' : 'hours'} ago"
else
days = seconds_ago / 86400
"#{days} #{days == 1 ? 'day' : 'days'} ago"
end
rescue
"unknown"
end
end
end
private
def start_scheduler
scheduler = Rufus::Scheduler.new
interval = ENV.fetch("SCRAPE_INTERVAL", "1").to_i
scheduler.every "#{interval}h" do
settings.logger.info "Starting scheduled scrape"
@scraper.scrape_all_patrons
end
# Also run every day at 6 AM to catch any overnight changes
scheduler.cron "0 6 * * *" do
settings.logger.info "Starting daily 6 AM scrape"
@scraper.scrape_all_patrons
end
# Clean up stale thumbnails weekly on Sundays at 3 AM
scheduler.cron "0 3 * * 0" do
settings.logger.info "Starting weekly thumbnail cleanup"
deleted = @data_store.cleanup_stale_thumbnails(@item_tracker)
settings.logger.info "Thumbnail cleanup complete: #{deleted} thumbnails deleted"
end
end
def run_initial_scrape
Thread.new do
sleep 5 # Give the app time to start up
settings.logger.info "Running initial scrape on startup"
@scraper.scrape_all_patrons
end
end
end
if __FILE__ == $0
DeweyApp.run!
end