2016-12-25 20:13:30 +00:00
|
|
|
local Widget = require("ui/widget/widget")
|
2015-09-07 17:06:17 +00:00
|
|
|
local MultiInputDialog = require("ui/widget/multiinputdialog")
|
2016-02-14 21:47:36 +00:00
|
|
|
local KeyValuePage = require("ui/widget/keyvaluepage")
|
2015-09-07 17:06:17 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
local TimeVal = require("ui/timeval")
|
2015-11-23 19:37:36 +00:00
|
|
|
local DataStorage = require("datastorage")
|
2015-09-07 17:06:17 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2015-09-16 09:12:31 +00:00
|
|
|
local DEBUG = require("dbg")
|
2015-09-15 19:36:43 +00:00
|
|
|
local T = require("ffi/util").template
|
2016-02-14 21:47:36 +00:00
|
|
|
local joinPath = require("ffi/util").joinPath
|
2015-09-16 09:12:31 +00:00
|
|
|
local _ = require("gettext")
|
2015-11-27 15:13:01 +00:00
|
|
|
local util = require("util")
|
2015-09-16 09:12:31 +00:00
|
|
|
local tableutil = require("tableutil")
|
2016-10-24 17:15:56 +00:00
|
|
|
local ReadHistory = require("readhistory")
|
|
|
|
local DocSettings = require("docsettings")
|
2016-11-04 21:35:20 +00:00
|
|
|
local ReaderProgress = require("readerprogress")
|
2015-11-23 19:37:36 +00:00
|
|
|
local statistics_dir = DataStorage:getDataDir() .. "/statistics/"
|
2016-11-04 21:35:20 +00:00
|
|
|
|
2016-10-24 17:15:56 +00:00
|
|
|
-- a copy of page_max_read_sec
|
|
|
|
local page_max_time
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2016-12-25 20:13:30 +00:00
|
|
|
local ReaderStatistics = Widget:extend{
|
2015-09-07 17:06:17 +00:00
|
|
|
last_time = nil,
|
|
|
|
page_min_read_sec = 5,
|
|
|
|
page_max_read_sec = 90,
|
|
|
|
current_period = 0,
|
2016-10-31 18:47:57 +00:00
|
|
|
pages_current_period = 0,
|
2015-09-07 17:06:17 +00:00
|
|
|
is_enabled = nil,
|
|
|
|
data = {
|
|
|
|
title = "",
|
|
|
|
authors = "",
|
|
|
|
language = "",
|
|
|
|
series = "",
|
2015-11-23 19:37:36 +00:00
|
|
|
performance_in_pages = {},
|
|
|
|
total_time_in_sec = 0,
|
2015-09-07 17:06:17 +00:00
|
|
|
highlights = 0,
|
|
|
|
notes = 0,
|
|
|
|
pages = 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-19 21:12:45 +00:00
|
|
|
local shortDayOfWeekTranslation = {
|
|
|
|
["Mon"] = _("Mon"),
|
|
|
|
["Tue"] = _("Tue"),
|
|
|
|
["Wed"] = _("Wed"),
|
|
|
|
["Thu"] = _("Thu"),
|
|
|
|
["Fri"] = _("Fri"),
|
|
|
|
["Sat"] = _("Sat"),
|
|
|
|
["Sun"] = _("Sun"),
|
|
|
|
}
|
|
|
|
|
|
|
|
local monthTranslation = {
|
|
|
|
["January"] = _("January"),
|
|
|
|
["February"] = _("February"),
|
|
|
|
["March"] = _("March"),
|
|
|
|
["April"] = _("April"),
|
|
|
|
["May"] = _("May"),
|
|
|
|
["June"] = _("June"),
|
|
|
|
["July"] = _("July"),
|
|
|
|
["August"] = _("August"),
|
|
|
|
["September"] = _("September"),
|
|
|
|
["October"] = _("October"),
|
|
|
|
["November"] = _("November"),
|
|
|
|
["December"] = _("December"),
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:33:57 +00:00
|
|
|
function ReaderStatistics:isDocless()
|
|
|
|
return self.ui == nil or self.ui.document == nil
|
|
|
|
end
|
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:init()
|
2017-01-06 09:33:57 +00:00
|
|
|
if not self:isDocless() and self.ui.document.is_pic then
|
2015-09-07 17:06:17 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
self.current_period = 0
|
2016-10-31 18:47:57 +00:00
|
|
|
self.pages_current_period = 0
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
local settings = G_reader_settings:readSetting("statistics") or {}
|
2015-09-07 17:06:17 +00:00
|
|
|
self.page_min_read_sec = tonumber(settings.min_sec)
|
|
|
|
self.page_max_read_sec = tonumber(settings.max_sec)
|
2016-10-24 17:15:56 +00:00
|
|
|
-- use later in getDatesFromBook
|
|
|
|
page_max_time = self.page_max_read_sec
|
2015-09-07 17:06:17 +00:00
|
|
|
self.is_enabled = not (settings.is_enabled == false)
|
|
|
|
self.last_time = TimeVal:now()
|
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
function ReaderStatistics:getBookProperties()
|
|
|
|
local props = self.view.document:getProps()
|
|
|
|
if props.title == "No document" or props.title == "" then
|
|
|
|
-- FIXME: sometimes crengine returns "No document", try one more time
|
|
|
|
props = self.view.document:getProps()
|
|
|
|
end
|
|
|
|
return props
|
|
|
|
end
|
|
|
|
|
2015-11-23 19:37:36 +00:00
|
|
|
function ReaderStatistics:initData(config)
|
2017-01-06 09:33:57 +00:00
|
|
|
if self:isDocless() or not self.is_enabled then
|
|
|
|
return
|
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
-- first execution
|
2017-01-06 09:33:57 +00:00
|
|
|
if not self.data then
|
|
|
|
self.data = { performance_in_pages= {} }
|
|
|
|
self:inplaceMigration(); -- first time merge data
|
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
|
2017-01-06 09:33:57 +00:00
|
|
|
local book_properties = self:getBookProperties()
|
|
|
|
self.data.title = book_properties.title
|
|
|
|
self.data.authors = book_properties.authors
|
|
|
|
self.data.language = book_properties.language
|
|
|
|
self.data.series = book_properties.series
|
2016-02-14 21:47:36 +00:00
|
|
|
|
2017-01-06 09:33:57 +00:00
|
|
|
self.data.pages = self.view.document:getPageCount()
|
|
|
|
return
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2016-11-04 21:35:20 +00:00
|
|
|
local function generateReadBooksTable(title, dates)
|
|
|
|
local result = {}
|
|
|
|
for k, v in tableutil.spairs(dates, function(t, a, b) return t[b].date < t[a].date end) do
|
|
|
|
table.insert(result, {
|
|
|
|
k,
|
|
|
|
T(_("Pages (%1) Time: %2"), v.count, util.secondsToClock(v.read, false))
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
function ReaderStatistics:getStatisticEnabledMenuItem()
|
2015-09-07 17:06:17 +00:00
|
|
|
return {
|
2016-02-14 21:47:36 +00:00
|
|
|
text = _("Enabled"),
|
2015-09-07 17:06:17 +00:00
|
|
|
checked_func = function() return self.is_enabled end,
|
|
|
|
callback = function()
|
|
|
|
-- if was enabled, have to save data to file
|
2017-01-06 09:33:57 +00:00
|
|
|
if self.last_time and self.is_enabled and not self:isDocless() then
|
2015-11-23 19:37:36 +00:00
|
|
|
self.ui.doc_settings:saveSetting("stats", self.data)
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.is_enabled = not self.is_enabled
|
|
|
|
-- if was disabled have to get data from file
|
2017-01-06 09:33:57 +00:00
|
|
|
if self.is_enabled and not self:isDocless() then
|
2015-11-23 19:37:36 +00:00
|
|
|
self:initData(self.ui.doc_settings)
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
2015-10-26 14:06:52 +00:00
|
|
|
self:saveSettings()
|
2015-09-07 17:06:17 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:updateSettings()
|
2015-09-07 17:06:17 +00:00
|
|
|
self.settings_dialog = MultiInputDialog:new {
|
2015-09-13 19:34:20 +00:00
|
|
|
title = _("Statistics settings"),
|
2015-09-07 17:06:17 +00:00
|
|
|
fields = {
|
|
|
|
{
|
|
|
|
text = "",
|
|
|
|
input_type = "number",
|
2016-04-04 15:39:02 +00:00
|
|
|
hint = T(_("Min seconds, default is 5. Current value: %1"),
|
2016-11-08 05:24:34 +00:00
|
|
|
self.page_min_read_sec),
|
2015-09-07 17:06:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text = "",
|
|
|
|
input_type = "number",
|
2016-04-04 15:39:02 +00:00
|
|
|
hint = T(_("Max seconds, default is 90. Current value: %1"),
|
2016-11-08 05:24:34 +00:00
|
|
|
self.page_max_read_sec),
|
2015-09-07 17:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
2015-09-26 05:00:50 +00:00
|
|
|
text = _("Apply"),
|
2015-09-07 17:06:17 +00:00
|
|
|
callback = function()
|
2016-02-14 21:47:36 +00:00
|
|
|
self:saveSettings(MultiInputDialog:getFields())
|
2015-09-07 17:06:17 +00:00
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width = Screen:getWidth() * 0.95,
|
|
|
|
height = Screen:getHeight() * 0.2,
|
|
|
|
input_type = "number",
|
|
|
|
}
|
|
|
|
self.settings_dialog:onShowKeyboard()
|
|
|
|
UIManager:show(self.settings_dialog)
|
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
function ReaderStatistics:addToMainMenu(tab_item_table)
|
2017-03-03 15:03:12 +00:00
|
|
|
self.ui.menu.menu_items.statistics = {
|
2016-02-14 21:47:36 +00:00
|
|
|
text = _("Statistics"),
|
|
|
|
sub_item_table = {
|
|
|
|
self:getStatisticEnabledMenuItem(),
|
|
|
|
{
|
|
|
|
text = _("Settings"),
|
|
|
|
callback = function() self:updateSettings() end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Current book"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = _("Statistics"),
|
|
|
|
kv_pairs = self:getCurrentStat(),
|
|
|
|
})
|
2017-01-06 09:33:57 +00:00
|
|
|
end,
|
|
|
|
enabled = not self:isDocless()
|
2016-02-14 21:47:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("All books"),
|
|
|
|
callback = function()
|
2016-10-31 18:47:57 +00:00
|
|
|
local total_msg, kv_pairs = self:getTotalStats()
|
2016-02-14 21:47:36 +00:00
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = total_msg,
|
|
|
|
kv_pairs = kv_pairs,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
},
|
2016-11-04 21:35:20 +00:00
|
|
|
{
|
|
|
|
text = _("Reading progress"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(ReaderProgress:new{
|
|
|
|
dates = self:getDatesFromAll(7, "daily_weekday"),
|
|
|
|
current_period = self.current_period,
|
|
|
|
current_pages = self.pages_current_period,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
},
|
2016-10-24 17:15:56 +00:00
|
|
|
{
|
|
|
|
text = _("Time range"),
|
|
|
|
sub_item_table = {
|
|
|
|
{
|
|
|
|
text = _("Last week"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = _("Last week"),
|
2016-11-04 21:35:20 +00:00
|
|
|
kv_pairs = generateReadBooksTable("", self:getDatesFromAll(7, "daily_weekday")),
|
2016-10-24 17:15:56 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Last month by day"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = _("Last month by day"),
|
2016-11-04 21:35:20 +00:00
|
|
|
kv_pairs = generateReadBooksTable("", self:getDatesFromAll(30, "daily_weekday")),
|
2016-10-24 17:15:56 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Last year by day"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = _("Last year by day"),
|
2016-11-04 21:35:20 +00:00
|
|
|
kv_pairs = generateReadBooksTable("", self:getDatesFromAll(365, "daily")),
|
2016-10-24 17:15:56 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Last year by week"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = _("Last year by week"),
|
2016-11-04 21:35:20 +00:00
|
|
|
kv_pairs = generateReadBooksTable("", self:getDatesFromAll(365, "weekly")),
|
2016-10-24 17:15:56 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Last 10 years by month"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = _("Last 10 years by month"),
|
2016-11-04 21:35:20 +00:00
|
|
|
kv_pairs = generateReadBooksTable("", self:getDatesFromAll(3650, "monthly")),
|
2016-10-24 17:15:56 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2016-02-14 21:47:36 +00:00
|
|
|
},
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
function ReaderStatistics:getCurrentStat()
|
|
|
|
local dates = {}
|
2016-10-31 18:47:57 +00:00
|
|
|
local now_stamp = os.time()
|
|
|
|
local now_t = os.date("*t")
|
|
|
|
local from_begin_day = now_t.hour * 3600 + now_t.min * 60 + now_t.sec
|
|
|
|
local start_today_time = now_stamp - from_begin_day
|
|
|
|
local pages_today_period = 0
|
|
|
|
local time_today_period = 0
|
|
|
|
local last_period = 0
|
|
|
|
local sorted_today_performance = {}
|
|
|
|
local diff
|
2016-02-14 21:47:36 +00:00
|
|
|
for k, v in pairs(self.data.performance_in_pages) do
|
2016-04-19 06:50:36 +00:00
|
|
|
dates[os.date("%Y-%m-%d", k)] = true
|
2016-10-31 18:47:57 +00:00
|
|
|
if k >= start_today_time then
|
|
|
|
pages_today_period = pages_today_period + 1
|
|
|
|
table.insert(sorted_today_performance, k)
|
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
local total_days = util.tableSize(dates)
|
|
|
|
local read_pages = util.tableSize(self.data.performance_in_pages)
|
|
|
|
local current_page = self.view.state.page -- get current page from the view
|
|
|
|
local avg_time_per_page = self.data.total_time_in_sec / read_pages
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2016-10-31 18:47:57 +00:00
|
|
|
table.sort(sorted_today_performance)
|
|
|
|
for _, n in pairs(sorted_today_performance) do
|
|
|
|
if last_period == 0 then
|
|
|
|
last_period = n
|
|
|
|
time_today_period = avg_time_per_page
|
|
|
|
else
|
|
|
|
diff = n - last_period
|
|
|
|
if (diff <= page_max_time and diff > 0) then
|
|
|
|
time_today_period = time_today_period + diff
|
|
|
|
else
|
|
|
|
time_today_period = time_today_period + avg_time_per_page
|
|
|
|
end
|
|
|
|
last_period = n
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
return {
|
2016-02-14 21:47:36 +00:00
|
|
|
{ _("Current period"), util.secondsToClock(self.current_period, false) },
|
2016-10-31 18:47:57 +00:00
|
|
|
{ _("Current pages"), self.pages_current_period },
|
|
|
|
{ _("Today period"), util.secondsToClock(time_today_period, false) },
|
|
|
|
{ _("Today pages"), pages_today_period },
|
2016-02-14 21:47:36 +00:00
|
|
|
{ _("Time to read"), util.secondsToClock((self.data.pages - current_page) * avg_time_per_page, false) },
|
|
|
|
{ _("Total time"), util.secondsToClock(self.data.total_time_in_sec, false) },
|
|
|
|
{ _("Total highlights"), self.data.highlights },
|
|
|
|
{ _("Total notes"), self.data.notes },
|
|
|
|
{ _("Total days"), total_days },
|
|
|
|
{ _("Average time per page"), util.secondsToClock(avg_time_per_page, false) },
|
|
|
|
{ _("Read pages/Total pages"), read_pages .. "/" .. self.data.pages },
|
2017-01-18 03:49:12 +00:00
|
|
|
{ _("Percentage completed"), math.floor(read_pages / self.data.pages * 100 + 0.5) .. "%" }, -- adding 0.5 rounds to nearest integer with math.floor
|
2015-09-07 17:06:17 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-11-23 19:37:36 +00:00
|
|
|
-- For backward compatibility
|
2016-11-01 07:31:24 +00:00
|
|
|
local function getDatesForBookOldFormat(book)
|
2015-09-07 17:06:17 +00:00
|
|
|
local dates = {}
|
|
|
|
|
|
|
|
for k, v in pairs(book.details) do
|
|
|
|
local date_text = os.date("%Y-%m-%d", v.time)
|
|
|
|
if not dates[date_text] then
|
|
|
|
dates[date_text] = {
|
|
|
|
date = v.time,
|
|
|
|
read = v.read,
|
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dates[date_text] = {
|
|
|
|
read = dates[date_text].read + v.read,
|
|
|
|
count = dates[date_text].count + 1,
|
|
|
|
date = dates[date_text].date
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
return generateReadBooksTable(book.title, dates)
|
2015-11-23 19:37:36 +00:00
|
|
|
end
|
|
|
|
|
2016-10-24 17:15:56 +00:00
|
|
|
-- sdays -> number of days to show
|
|
|
|
-- ptype -> daily - show daily without weekday name
|
|
|
|
-- daily_weekday - show daily with weekday name
|
|
|
|
-- weekly - show weekly
|
|
|
|
-- monthly - show monthly
|
|
|
|
function ReaderStatistics:getDatesFromAll(sdays, ptype)
|
2015-11-23 19:37:36 +00:00
|
|
|
local dates = {}
|
2016-10-24 17:15:56 +00:00
|
|
|
local sorted_performance_in_pages
|
|
|
|
local diff
|
2016-10-31 18:47:57 +00:00
|
|
|
local now_t = os.date("*t")
|
2016-10-24 17:15:56 +00:00
|
|
|
local from_begin_day = now_t.hour *3600 + now_t.min*60 + now_t.sec
|
|
|
|
local now_stamp = os.time()
|
|
|
|
local one_day = 24 * 3600 -- one day in seconds
|
2016-10-31 18:47:57 +00:00
|
|
|
local avg_time_per_page
|
2016-10-24 17:15:56 +00:00
|
|
|
local period = now_stamp - ((sdays -1) * one_day) - from_begin_day
|
2017-02-19 21:12:45 +00:00
|
|
|
for __, v in pairs(ReadHistory.hist) do
|
2016-10-24 17:15:56 +00:00
|
|
|
local book_stats = DocSettings:open(v.file):readSetting('stats')
|
|
|
|
if book_stats ~= nil then
|
|
|
|
-- if current reading book
|
|
|
|
if book_stats.title == self.data.title then
|
|
|
|
book_stats = self.data
|
2016-10-31 18:47:57 +00:00
|
|
|
local read_pages = util.tableSize(self.data.performance_in_pages)
|
|
|
|
avg_time_per_page = self.data.total_time_in_sec / read_pages
|
|
|
|
else
|
|
|
|
avg_time_per_page = book_stats.total_time_in_sec / book_stats.pages
|
2016-10-24 17:15:56 +00:00
|
|
|
end
|
|
|
|
--zeros table sorted_performance_in_pages
|
|
|
|
sorted_performance_in_pages = {}
|
|
|
|
for k1, v1 in pairs(book_stats.performance_in_pages) do
|
|
|
|
if k1 >= period then
|
|
|
|
table.insert(sorted_performance_in_pages, k1)
|
|
|
|
end --if period
|
|
|
|
end -- for book_performance
|
|
|
|
-- sort table by time (unix timestamp)
|
|
|
|
local date_text
|
|
|
|
table.sort(sorted_performance_in_pages)
|
|
|
|
for i, n in pairs(sorted_performance_in_pages) do
|
|
|
|
if ptype == "daily_weekday" then
|
2017-02-19 21:12:45 +00:00
|
|
|
date_text = string.format("%s (%s)",
|
|
|
|
os.date("%Y-%m-%d", n),
|
|
|
|
shortDayOfWeekTranslation[os.date("%a", n)])
|
2016-10-24 17:15:56 +00:00
|
|
|
elseif ptype == "daily" then
|
2017-02-19 21:12:45 +00:00
|
|
|
date_text = os.date("%Y-%m-%d", n)
|
2016-10-24 17:15:56 +00:00
|
|
|
elseif ptype == "weekly" then
|
2017-02-19 21:12:45 +00:00
|
|
|
date_text = T(_("%1 Week %2"), os.date("%Y "), os.date(" %W", n))
|
2016-10-24 17:15:56 +00:00
|
|
|
elseif ptype == "monthly" then
|
2017-02-19 21:12:45 +00:00
|
|
|
date_text = monthTranslation[os.date("%B", n)] .. os.date(" %Y", n)
|
2016-10-24 17:15:56 +00:00
|
|
|
else
|
2017-02-19 21:12:45 +00:00
|
|
|
date_text = os.date("%Y-%m-%d", n)
|
2016-10-24 17:15:56 +00:00
|
|
|
end --if ptype
|
|
|
|
if not dates[date_text] then
|
|
|
|
dates[date_text] = {
|
|
|
|
-- first pages of day is set to average of all pages
|
2016-10-31 18:47:57 +00:00
|
|
|
read = avg_time_per_page,
|
2016-10-24 17:15:56 +00:00
|
|
|
date = n,
|
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
else
|
|
|
|
local entry = dates[date_text]
|
|
|
|
diff = n - entry.date
|
|
|
|
-- page_max_time
|
|
|
|
if (diff <= page_max_time and diff > 0) then
|
|
|
|
entry.read = entry.read + n - entry.date
|
|
|
|
else
|
|
|
|
--add average time if time > page_max_time
|
2016-10-31 18:47:57 +00:00
|
|
|
entry.read = avg_time_per_page + entry.read
|
2016-10-24 17:15:56 +00:00
|
|
|
end --if diff
|
|
|
|
if diff < 0 then
|
2016-10-31 18:47:57 +00:00
|
|
|
entry.read = avg_time_per_page + entry.read
|
2016-10-24 17:15:56 +00:00
|
|
|
end
|
|
|
|
entry.date = n
|
|
|
|
entry.count = entry.count + 1
|
|
|
|
end --if not dates[]
|
|
|
|
end -- for sorted_performance_in_pages
|
|
|
|
end -- if book_status
|
|
|
|
end --for pairs(ReadHistory.hist)
|
2016-11-04 21:35:20 +00:00
|
|
|
return dates
|
2016-10-24 17:15:56 +00:00
|
|
|
end
|
2015-11-23 19:37:36 +00:00
|
|
|
|
2016-11-01 07:31:24 +00:00
|
|
|
local function getDatesForBook(book)
|
2016-10-24 17:15:56 +00:00
|
|
|
local dates = {}
|
|
|
|
local sorted_performance_in_pages = {}
|
|
|
|
local diff
|
2016-10-31 18:47:57 +00:00
|
|
|
local read_pages = util.tableSize(book.performance_in_pages)
|
2015-11-23 19:37:36 +00:00
|
|
|
for k, v in pairs(book.performance_in_pages) do
|
2016-10-24 17:15:56 +00:00
|
|
|
table.insert(sorted_performance_in_pages, k)
|
|
|
|
end
|
|
|
|
-- sort table by time (unix timestamp)
|
|
|
|
table.sort(sorted_performance_in_pages)
|
|
|
|
for i, n in pairs(sorted_performance_in_pages) do
|
|
|
|
local date_text = os.date("%Y-%m-%d", n)
|
2015-11-23 19:37:36 +00:00
|
|
|
if not dates[date_text] then
|
|
|
|
dates[date_text] = {
|
2016-10-24 17:15:56 +00:00
|
|
|
-- first pages of day is set to average of all pages
|
2016-10-31 18:47:57 +00:00
|
|
|
read = book.total_time_in_sec / read_pages,
|
2016-10-24 17:15:56 +00:00
|
|
|
date = n,
|
2015-11-23 19:37:36 +00:00
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
else
|
2016-02-14 21:47:36 +00:00
|
|
|
local entry = dates[date_text]
|
2016-10-24 17:15:56 +00:00
|
|
|
diff = n - entry.date
|
|
|
|
if diff <= page_max_time then
|
|
|
|
entry.read = entry.read + n - entry.date
|
|
|
|
else
|
|
|
|
--add average time if time > page_max_time e.g longer break while reading
|
2016-10-31 18:47:57 +00:00
|
|
|
entry.read = book.total_time_in_sec / read_pages + entry.read
|
2016-10-24 17:15:56 +00:00
|
|
|
end
|
|
|
|
entry.date = n
|
2016-02-14 21:47:36 +00:00
|
|
|
entry.count = entry.count + 1
|
2015-11-23 19:37:36 +00:00
|
|
|
end
|
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
return generateReadBooksTable(book.title, dates)
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2016-02-15 10:18:10 +00:00
|
|
|
function ReaderStatistics:getTotalStats()
|
2017-01-06 09:33:57 +00:00
|
|
|
local total_stats = {}
|
|
|
|
if not self:isDocless() then
|
2017-01-24 17:51:42 +00:00
|
|
|
-- empty title
|
|
|
|
if self.data.title == "" then
|
|
|
|
self.data.title = self.document.file:match("^.+/(.+)$")
|
|
|
|
end
|
2017-01-06 09:33:57 +00:00
|
|
|
total_stats = {
|
|
|
|
{
|
|
|
|
self.data.title,
|
|
|
|
util.secondsToClock(self.data.total_time_in_sec, false),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = self.data.title,
|
|
|
|
kv_pairs = getDatesForBook(self.data),
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
}
|
2016-02-14 21:47:36 +00:00
|
|
|
}
|
2017-01-06 09:33:57 +00:00
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
-- find stats for all other books in history
|
|
|
|
local proceded_titles, total_books_time = self:getStatisticsFromHistory(total_stats)
|
|
|
|
total_books_time = total_books_time + self:getOldStatisticsFromDirectory(proceded_titles, total_stats)
|
2015-11-23 19:37:36 +00:00
|
|
|
total_books_time = total_books_time + tonumber(self.data.total_time_in_sec)
|
2015-10-02 20:18:18 +00:00
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
return T(_("Total hours read %1"),
|
|
|
|
util.secondsToClock(total_books_time, false)),
|
|
|
|
total_stats
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2016-02-14 21:47:36 +00:00
|
|
|
function ReaderStatistics:getStatisticsFromHistory(total_stats)
|
2015-11-23 19:37:36 +00:00
|
|
|
local titles = {}
|
2016-02-14 21:47:36 +00:00
|
|
|
local total_books_time = 0
|
2016-10-24 17:15:56 +00:00
|
|
|
for _, v in pairs(ReadHistory.hist) do
|
|
|
|
local book_stats = DocSettings:open(v.file):readSetting('stats')
|
2017-01-24 17:51:42 +00:00
|
|
|
-- empty title
|
|
|
|
if book_stats and book_stats.title == "" then
|
|
|
|
book_stats.title = v.file:match("^.+/(.+)$")
|
|
|
|
end
|
2016-10-24 17:15:56 +00:00
|
|
|
if book_stats and book_stats.total_time_in_sec > 0
|
|
|
|
and book_stats.title ~= self.data.title then
|
|
|
|
titles[book_stats.title] = true
|
|
|
|
table.insert(total_stats, {
|
|
|
|
book_stats.title,
|
|
|
|
util.secondsToClock(book_stats.total_time_in_sec, false),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = book_stats.title,
|
|
|
|
kv_pairs = getDatesForBook(book_stats),
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
total_books_time = total_books_time + tonumber(book_stats.total_time_in_sec)
|
|
|
|
end --if book_stats
|
|
|
|
end --for pairs(ReadHistory.hist)
|
2016-02-14 21:47:36 +00:00
|
|
|
return titles, total_books_time
|
2015-11-23 19:37:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- For backward compatibility
|
2016-02-14 21:47:36 +00:00
|
|
|
function ReaderStatistics:getOldStatisticsFromDirectory(exlude_titles, total_stats)
|
2015-11-23 19:37:36 +00:00
|
|
|
if lfs.attributes(statistics_dir, "mode") ~= "directory" then
|
2016-02-14 21:47:36 +00:00
|
|
|
return 0
|
2015-11-23 19:37:36 +00:00
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
local total_books_time = 0
|
2015-11-23 19:37:36 +00:00
|
|
|
for curr_file in lfs.dir(statistics_dir) do
|
|
|
|
local path = statistics_dir .. curr_file
|
|
|
|
if lfs.attributes(path, "mode") == "file" then
|
|
|
|
local book_result = self:importFromFile(statistics_dir, curr_file)
|
2016-02-15 13:58:46 +00:00
|
|
|
if book_result and book_result.total_time > 0
|
2016-02-15 10:18:10 +00:00
|
|
|
and book_result.title ~= self.data.title
|
|
|
|
and not exlude_titles[book_result.title] then
|
2015-11-23 19:37:36 +00:00
|
|
|
table.insert(total_stats, {
|
2016-02-14 21:47:36 +00:00
|
|
|
book_result.title,
|
|
|
|
util.secondsToClock(book_result.total_time, false),
|
2015-11-23 19:37:36 +00:00
|
|
|
callback = function()
|
2016-02-14 21:47:36 +00:00
|
|
|
UIManager:show(KeyValuePage:new{
|
|
|
|
title = book_result.title,
|
|
|
|
kv_pairs = getDatesForBookOldFormat(book_result),
|
|
|
|
})
|
2015-11-23 19:37:36 +00:00
|
|
|
end,
|
|
|
|
})
|
2015-11-27 15:13:01 +00:00
|
|
|
total_books_time = total_books_time + tonumber(book_result.total_time)
|
2015-11-23 19:37:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-14 21:47:36 +00:00
|
|
|
return total_books_time
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:onPageUpdate(pageno)
|
2017-01-06 09:33:57 +00:00
|
|
|
if self:isDocless() or not self.is_enabled then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local curr_time = TimeVal:now()
|
|
|
|
local diff_time = curr_time.sec - self.last_time.sec
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2017-01-06 09:33:57 +00:00
|
|
|
-- if last update was more then 10 minutes then current period set to 0
|
|
|
|
if (diff_time > 600) then
|
|
|
|
self.current_period = 0
|
|
|
|
self.pages_current_period = 0
|
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2017-01-06 09:33:57 +00:00
|
|
|
if diff_time >= self.page_min_read_sec and diff_time <= self.page_max_read_sec then
|
|
|
|
self.current_period = self.current_period + diff_time
|
|
|
|
self.pages_current_period = self.pages_current_period + 1
|
|
|
|
self.data.total_time_in_sec = self.data.total_time_in_sec + diff_time
|
|
|
|
self.data.performance_in_pages[curr_time.sec] = pageno
|
|
|
|
-- we cannot save stats each time this is a page update event,
|
|
|
|
-- because the self.data may not even be initialized when such a event
|
|
|
|
-- comes, which will render a blank stats written into doc settings
|
|
|
|
-- and all previous stats are totally wiped out.
|
|
|
|
self.ui.doc_settings:saveSetting("stats", self.data)
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
2017-01-06 09:33:57 +00:00
|
|
|
|
|
|
|
self.last_time = curr_time
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2015-11-23 19:37:36 +00:00
|
|
|
-- For backward compatibility
|
|
|
|
function ReaderStatistics:inplaceMigration()
|
|
|
|
local oldData = self:importFromFile(statistics_dir, self.data.title .. ".stat")
|
|
|
|
if oldData then
|
|
|
|
for k, v in pairs(oldData.details) do
|
|
|
|
self.data.performance_in_pages[v.time] = v.page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- For backward compatibility
|
|
|
|
function ReaderStatistics:importFromFile(base_path, item)
|
2016-02-14 21:47:36 +00:00
|
|
|
item = string.gsub(item, "^%s*(.-)%s*$", "%1") -- trim
|
2016-02-15 13:58:46 +00:00
|
|
|
if item ~= ".stat" then
|
|
|
|
local statistic_file = joinPath(base_path, item)
|
|
|
|
if lfs.attributes(statistic_file, "mode") == "directory" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local ok, stored = pcall(dofile, statistic_file)
|
|
|
|
if ok then
|
|
|
|
return stored
|
|
|
|
else
|
|
|
|
DEBUG(stored)
|
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:onCloseDocument()
|
2017-01-06 09:33:57 +00:00
|
|
|
if not self:isDocless() and self.last_time and self.is_enabled then
|
2015-11-23 19:37:36 +00:00
|
|
|
self.ui.doc_settings:saveSetting("stats", self.data)
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:onAddHighlight()
|
2015-09-07 17:06:17 +00:00
|
|
|
self.data.highlights = self.data.highlights + 1
|
|
|
|
end
|
|
|
|
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:onAddNote()
|
2015-09-07 17:06:17 +00:00
|
|
|
self.data.notes = self.data.notes + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- in case when screensaver starts
|
2015-10-26 14:06:52 +00:00
|
|
|
function ReaderStatistics:onSaveSettings()
|
|
|
|
self:saveSettings()
|
2017-01-06 09:33:57 +00:00
|
|
|
if not self:isDocless() then
|
|
|
|
self.ui.doc_settings:saveSetting("stats", self.data)
|
|
|
|
self.current_period = 0
|
|
|
|
self.pages_current_period = 0
|
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- screensaver off
|
2015-09-13 19:34:20 +00:00
|
|
|
function ReaderStatistics:onResume()
|
2015-09-07 17:06:17 +00:00
|
|
|
self.current_period = 0
|
2016-10-31 18:47:57 +00:00
|
|
|
self.pages_current_period = 0
|
|
|
|
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2015-10-26 14:06:52 +00:00
|
|
|
function ReaderStatistics:saveSettings(fields)
|
2015-09-07 17:06:17 +00:00
|
|
|
if fields then
|
|
|
|
self.page_min_read_sec = tonumber(fields[1])
|
|
|
|
self.page_max_read_sec = tonumber(fields[2])
|
|
|
|
end
|
|
|
|
|
|
|
|
local settings = {
|
|
|
|
min_sec = self.page_min_read_sec,
|
|
|
|
max_sec = self.page_max_read_sec,
|
|
|
|
is_enabled = self.is_enabled,
|
|
|
|
}
|
2015-09-13 19:34:20 +00:00
|
|
|
G_reader_settings:saveSetting("statistics", settings)
|
2015-09-07 17:06:17 +00:00
|
|
|
end
|
|
|
|
|
2015-11-23 19:37:36 +00:00
|
|
|
function ReaderStatistics:onReadSettings(config)
|
2016-02-07 22:03:53 +00:00
|
|
|
self.data = config.data.stats
|
2015-11-23 19:37:36 +00:00
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2016-02-12 14:55:02 +00:00
|
|
|
function ReaderStatistics:onReaderReady()
|
2016-02-07 22:03:53 +00:00
|
|
|
-- we have correct page count now, do the actual initialization work
|
|
|
|
self:initData()
|
|
|
|
end
|
2015-09-07 17:06:17 +00:00
|
|
|
|
2016-02-07 22:03:53 +00:00
|
|
|
return ReaderStatistics
|