@@ -116,15 +116,24 @@ function StatusStats:getStatisticsPlugin()
116116 return nil
117117end
118118
119- function StatusStats :getStatisticsPair (method_name )
120- local statistics = self :getStatisticsPlugin ()
119+ function StatusStats :getNowTimestamp ()
120+ return os.time ()
121+ end
122+
123+ function StatusStats :getBaseStatisticsPair (statistics , method_name )
121124 if not (statistics and statistics [method_name ]) then
122- return nil
125+ return {
126+ time = 0 ,
127+ pages = 0 ,
128+ }
123129 end
124130
125131 local ok , duration , pages = pcall (statistics [method_name ], statistics )
126132 if not ok then
127- return nil
133+ return {
134+ time = 0 ,
135+ pages = 0 ,
136+ }
128137 end
129138
130139 return {
@@ -133,12 +142,111 @@ function StatusStats:getStatisticsPair(method_name)
133142 }
134143end
135144
145+ function StatusStats :getLiveTupleDurationSince (statistics , page , data_list , tuple_index , boundary_time )
146+ local tuple = data_list and data_list [tuple_index ]
147+ if type (tuple ) ~= " table" then
148+ return 0
149+ end
150+
151+ local start_time = tonumber (tuple [1 ])
152+ local stored_duration = tonumber (tuple [2 ]) or 0
153+ if not start_time then
154+ return 0
155+ end
156+
157+ local effective_duration = math.max (stored_duration , 0 )
158+ local is_active_tuple = statistics .curr_page == page
159+ and tuple_index == # data_list
160+ and not statistics ._reading_paused_ts
161+
162+ if is_active_tuple then
163+ local settings = statistics .settings or {}
164+ local min_sec = tonumber (settings .min_sec ) or 0
165+ local max_sec = tonumber (settings .max_sec ) or math.huge
166+ local elapsed = math.max (self :getNowTimestamp () - start_time , 0 )
167+
168+ if elapsed >= min_sec then
169+ effective_duration = math.max (effective_duration , math.min (elapsed , max_sec ))
170+ end
171+ end
172+
173+ if effective_duration <= 0 then
174+ return 0
175+ end
176+
177+ boundary_time = tonumber (boundary_time ) or 0
178+ if start_time >= boundary_time then
179+ return effective_duration
180+ end
181+
182+ local end_time = start_time + effective_duration
183+ if end_time <= boundary_time then
184+ return 0
185+ end
186+
187+ return end_time - boundary_time
188+ end
189+
190+ function StatusStats :getLiveStatisticsPair (statistics , boundary_time )
191+ if not (statistics and type (statistics .page_stat ) == " table" ) then
192+ return {
193+ time = 0 ,
194+ pages = 0 ,
195+ }
196+ end
197+
198+ local live_duration = 0
199+ local live_pages = 0
200+
201+ for page , data_list in pairs (statistics .page_stat ) do
202+ if type (data_list ) == " table" then
203+ local page_duration = 0
204+
205+ for tuple_index = 1 , # data_list do
206+ page_duration = page_duration
207+ + self :getLiveTupleDurationSince (statistics , page , data_list , tuple_index , boundary_time )
208+ end
209+
210+ if page_duration > 0 then
211+ live_duration = live_duration + page_duration
212+ live_pages = live_pages + 1
213+ end
214+ end
215+ end
216+
217+ return {
218+ time = live_duration ,
219+ pages = live_pages ,
220+ }
221+ end
222+
223+ function StatusStats :getStatisticsPair (method_name , boundary_time )
224+ local statistics = self :getStatisticsPlugin ()
225+ if not statistics then
226+ return nil
227+ end
228+
229+ local persisted = self :getBaseStatisticsPair (statistics , method_name )
230+ local live = self :getLiveStatisticsPair (statistics , boundary_time )
231+
232+ return {
233+ time = persisted .time + live .time ,
234+ pages = persisted .pages + live .pages ,
235+ }
236+ end
237+
136238function StatusStats :getTodayStats ()
137- return self :getStatisticsPair (" getTodayBookStats" )
239+ local now_stamp = self :getNowTimestamp ()
240+ local now_t = os.date (" *t" , now_stamp )
241+ local from_begin_day = now_t .hour * 3600 + now_t .min * 60 + now_t .sec
242+ local start_today_time = now_stamp - from_begin_day
243+ return self :getStatisticsPair (" getTodayBookStats" , start_today_time )
138244end
139245
140246function StatusStats :getSessionStats ()
141- return self :getStatisticsPair (" getCurrentBookStats" )
247+ local statistics = self :getStatisticsPlugin ()
248+ local boundary_time = statistics and statistics .start_current_period or self :getNowTimestamp ()
249+ return self :getStatisticsPair (" getCurrentBookStats" , boundary_time )
142250end
143251
144252function StatusStats :formatDuration (seconds )
0 commit comments