diff --git a/lib/Ebook.php b/lib/Ebook.php index ca074164..54c504bf 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -83,7 +83,9 @@ final class Ebook{ /** When the database row was updated. */ public DateTimeImmutable $Updated; public ?int $TextSinglePageByteCount = null; + /** The numer of non-bot downloads in the past 30 days. */ public int $DownloadsPast30Days = 0; + /** The numer of all-time non-bot downloads. */ public int $DownloadsTotal = 0; /** @var array $_GitCommits */ diff --git a/lib/EbookDownloadSummary.php b/lib/EbookDownloadSummary.php index d7679331..6f56577c 100644 --- a/lib/EbookDownloadSummary.php +++ b/lib/EbookDownloadSummary.php @@ -5,7 +5,7 @@ use Safe\DateTimeImmutable; class EbookDownloadSummary{ public int $EbookId; public DateTimeImmutable $Date; - /** The number of downloads by non-bot clients on the given date. */ + /** The total number of downloads, including bot downloads, on the given date. */ public int $DownloadCount = 0; /** The number of downloads by bot clients on the given date. */ public int $BotDownloadCount = 0; diff --git a/scripts/recompute-ebook-downloads b/scripts/recompute-ebook-downloads index 7ad58fef..8b869548 100755 --- a/scripts/recompute-ebook-downloads +++ b/scripts/recompute-ebook-downloads @@ -19,12 +19,11 @@ foreach($downloadDates as $date){ $summaries[$ebookId] = new EbookDownloadSummary($ebookId, $downloadDate); } + $summaries[$ebookId]->DownloadCount++; + if($ebookDownload->IsBot()){ $summaries[$ebookId]->BotDownloadCount++; } - else{ - $summaries[$ebookId]->DownloadCount++; - } } foreach($summaries as $summary){ @@ -35,9 +34,9 @@ foreach($downloadDates as $date){ Db::Query(' UPDATE Ebooks e left join ( - select EbookId, sum(DownloadCount) as DownloadsPast30Days + select EbookId, sum(DownloadCount) - sum(BotDownloadCount) as DownloadsPast30Days from EbookDownloadSummaries - where Date >= date(utc_timetamp()) - interval 30 day + where Date >= date(utc_timestamp()) - interval 30 day group by EbookId ) s on e.EbookId = s.EbookId set e.DownloadsPast30Days = coalesce(s.DownloadsPast30Days, 0) @@ -46,7 +45,7 @@ Db::Query(' Db::Query(' UPDATE Ebooks e left join ( - select EbookId, sum(DownloadCount) as DownloadsTotal + select EbookId, sum(DownloadCount) - sum(BotDownloadCount) as DownloadsTotal from EbookDownloadSummaries group by EbookId ) s on e.EbookId = s.EbookId