From e5eb015f1b157be28ce94ab2388b8b8dd78752d2 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 14 Sep 2017 22:29:09 +0200 Subject: [PATCH] Statistics in bookstatuswidget (#3218) --- frontend/ui/widget/bookstatuswidget.lua | 85 +++++++++++-------------- plugins/statistics.koplugin/main.lua | 43 +++++++++++++ 2 files changed, 79 insertions(+), 49 deletions(-) diff --git a/frontend/ui/widget/bookstatuswidget.lua b/frontend/ui/widget/bookstatuswidget.lua index 417d39d52..ddc16dfd9 100644 --- a/frontend/ui/widget/bookstatuswidget.lua +++ b/frontend/ui/widget/bookstatuswidget.lua @@ -27,6 +27,8 @@ local template = require("ffi/util").template local util = require("util") local _ = require("gettext") +local stats_book = {} + --[[ --Save into sdr folder addtional section ["summary"] = { @@ -47,16 +49,9 @@ local BookStatusWidget = InputContainer:new{ status = "", modified = "", }, - stats = nil } function BookStatusWidget:init() - self.stats = { - total_time_in_sec = 0, - performance_in_pages = {}, - total_pages = self.document:getPageCount(), - } - self:getStatisticsSettings() if self.settings then self.summary = self.settings:readSetting("summary") or { rating = nil, @@ -65,6 +60,8 @@ function BookStatusWidget:init() modified = "", } end + self.total_pages = self.view.document:getPageCount() + stats_book = self:getStats() self.small_font_face = Font:getFace("smallffont") self.medium_font_face = Font:getFace("ffont") @@ -89,6 +86,34 @@ function BookStatusWidget:init() } end +function BookStatusWidget:getStats() + return {} +end + +function BookStatusWidget:getStatDays() + if stats_book.days then + return stats_book.days + else + return _("N/A") + end +end + +function BookStatusWidget:getStatHours() + if stats_book.time then + return util.secondsToClock(stats_book.time, false) + else + return _("N/A") + end +end + +function BookStatusWidget:getStatReadPages() + if stats_book.pages then + return string.format("%s/%s",stats_book.pages, self.total_pages) + else + return _("N/A") + end +end + function BookStatusWidget:getStatusContent(width) return VerticalGroup:new{ align = "left", @@ -106,31 +131,6 @@ function BookStatusWidget:getStatusContent(width) } end -function BookStatusWidget:getStatDays(stats) - if stats and stats.performance_in_pages then - local dates = {} - for k, v in pairs(stats.performance_in_pages) do - dates[os.date("%Y-%m-%d", k)] = "" - end - return util.tableSize(dates) - end - return "none" -end - -function BookStatusWidget:getStatHours(stats) - if stats and stats.total_time_in_sec then - return util.secondsToClock(stats.total_time_in_sec, false) - end - return "none" -end - -function BookStatusWidget:getReadPages(stats) - if stats and stats.performance_in_pages and stats.total_pages then - return util.tableSize(stats.performance_in_pages) .. "/" .. stats.total_pages - end - return "none" -end - function BookStatusWidget:genHeader(title) local width, height = Screen:getWidth(), Screen:scaleBySize(35) @@ -255,8 +255,7 @@ function BookStatusWidget:genBookInfoGroup() } ) -- progress bar - local total_pages = self.stats.total_pages - local read_percentage = self.view.state.page / total_pages + local read_percentage = self.view.state.page / self.total_pages local progress_bar = ProgressWidget:new{ width = width * 0.7, height = Screen:scaleBySize(10), @@ -357,21 +356,21 @@ function BookStatusWidget:genStatisticsGroup(width) CenterContainer:new{ dimen = Geom:new{ w = tile_width, h = tile_height }, TextWidget:new{ - text = self:getStatDays(self.stats), + text = self:getStatDays(), face = self.medium_font_face, }, }, CenterContainer:new{ dimen = Geom:new{ w = tile_width, h = tile_height }, TextWidget:new{ - text = self:getStatHours(self.stats), + text = self:getStatHours(), face = self.medium_font_face, }, }, CenterContainer:new{ dimen = Geom:new{ w = tile_width, h = tile_height }, TextWidget:new{ - text = self:getReadPages(self.stats), + text = self:getStatReadPages(), face = self.medium_font_face, } } @@ -507,18 +506,6 @@ function BookStatusWidget:onClose() return true end -function BookStatusWidget:getStatisticsSettings() - if self.settings then - local stats = self.settings:readSetting("stats") - if stats then - self.stats.total_time_in_sec = self.stats.total_time_in_sec + stats.total_time_in_sec - for k, v in pairs(stats.performance_in_pages) do - self.stats.performance_in_pages[k] = v - end - end - end -end - function BookStatusWidget:onSwitchFocus(inputbox) self.note_dialog = InputDialog:new{ title = "Note", diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index ec1d5e250..db68c3423 100755 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -1,3 +1,4 @@ +local BookStatusWidget = require("ui/widget/bookstatuswidget") local ConfirmBox = require("ui/widget/confirmbox") local DataStorage = require("datastorage") local DocSettings = require("docsettings") @@ -95,6 +96,9 @@ function ReaderStatistics:init() self.convert_to_db = settings.convert_to_db self.ui.menu:registerToMainMenu(self) self:checkInitDatabase() + BookStatusWidget.getStats = function() + return self:getStatsBookStatus(self.id_curr_book, self.is_enabled) + end end function ReaderStatistics:initData() @@ -128,6 +132,45 @@ function ReaderStatistics:initData() end end +function ReaderStatistics:getStatsBookStatus(id_curr_book, stat_enable) + if not stat_enable or id_curr_book == nil then + return {} + end + + self:insertDB(self.id_curr_book) + local conn = SQ3.open(db_location) + local sql_stmt = [[ + SELECT count(*) + FROM ( + SELECT strftime('%%Y-%%m-%%d', start_time, 'unixepoch', 'localtime') AS dates + FROM page_stat + WHERE id_book = '%s' + GROUP BY dates + ) + ]] + local total_days = conn:rowexec(string.format(sql_stmt, id_curr_book)) + sql_stmt = [[ + SELECT sum(period), + count(DISTINCT page) + FROM page_stat + WHERE id_book = '%s' + ]] + local total_time_book, total_read_pages = conn:rowexec(string.format(sql_stmt, id_curr_book)) + conn:close() + + if total_time_book == nil then + total_time_book = 0 + end + if total_read_pages == nil then + total_read_pages = 0 + end + return { + days = tonumber(total_days), + time = tonumber(total_time_book), + pages = tonumber(total_read_pages), + } +end + function ReaderStatistics:checkInitDatabase() local conn = SQ3.open(db_location) if self.convert_to_db then -- if conversion to sqlite was doing earlier