From d6203bf3508ba77e3943ddc412eb562edb327f22 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 24 Feb 2021 09:47:26 +0300 Subject: [PATCH] try to calculate counters conditionally based on feed ids --- classes/article.php | 16 ++++++ classes/counters.php | 122 ++++++++++++++++++++++++++++++------------- classes/feeds.php | 26 ++++++++- classes/rpc.php | 32 +++++++++--- js/App.js | 4 +- js/Feeds.js | 10 ++-- js/Headlines.js | 18 +++---- 7 files changed, 168 insertions(+), 60 deletions(-) diff --git a/classes/article.php b/classes/article.php index a2a38118b..4bf563c4b 100755 --- a/classes/article.php +++ b/classes/article.php @@ -640,4 +640,20 @@ class Article extends Handler_Protected { return [$article_image, $article_stream, $article_kind]; } + static function _feeds_of(array $article_ids) { + $id_qmarks = arr_qmarks($article_ids); + + $sth = DB::pdo()->prepare("SELECT DISTINCT feed_id FROM ttrss_entries e, ttrss_user_entries ue + WHERE ue.ref_id = e.id AND id IN ($id_qmarks)"); + + $sth->execute($article_ids); + + $rv = []; + + while ($row = $sth->fetch()) { + array_push($rv, $row["feed_id"]); + } + + return $rv; + } } diff --git a/classes/counters.php b/classes/counters.php index fb2553ec2..b4309cf7b 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -11,6 +11,15 @@ class Counters { ); } + static function get_for_feeds($feed_ids) { + return array_merge( + self::get_global(), + self::get_virt(), + self::get_labels(), + self::get_feeds($feed_ids), + self::get_cats(Feeds::_cats_of($feed_ids, $_SESSION["uid"], true))); + } + static private function get_cat_children($cat_id, $owner_uid) { $pdo = Db::pdo(); @@ -31,7 +40,7 @@ class Counters { return [$unread, $marked]; } - private static function get_cats() { + private static function get_cats(array $cat_ids = []) { $ret = []; /* Labels category */ @@ -43,27 +52,57 @@ class Counters { $pdo = Db::pdo(); - $sth = $pdo->prepare("SELECT fc.id, - SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, - SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked, - (SELECT COUNT(id) FROM ttrss_feed_categories fcc - WHERE fcc.parent_cat = fc.id) AS num_children - FROM ttrss_feed_categories fc - LEFT JOIN ttrss_feeds f ON (f.cat_id = fc.id) - LEFT JOIN ttrss_user_entries ue ON (ue.feed_id = f.id) - WHERE fc.owner_uid = :uid - GROUP BY fc.id - UNION - SELECT 0, - SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, - SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked, - 0 - FROM ttrss_feeds f, ttrss_user_entries ue - WHERE f.cat_id IS NULL AND - ue.feed_id = f.id AND - ue.owner_uid = :uid"); + if (count($cat_ids) == 0) { + $sth = $pdo->prepare("SELECT fc.id, + SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, + SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked, + (SELECT COUNT(id) FROM ttrss_feed_categories fcc + WHERE fcc.parent_cat = fc.id) AS num_children + FROM ttrss_feed_categories fc + LEFT JOIN ttrss_feeds f ON (f.cat_id = fc.id) + LEFT JOIN ttrss_user_entries ue ON (ue.feed_id = f.id) + WHERE fc.owner_uid = :uid + GROUP BY fc.id + UNION + SELECT 0, + SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, + SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked, + 0 + FROM ttrss_feeds f, ttrss_user_entries ue + WHERE f.cat_id IS NULL AND + ue.feed_id = f.id AND + ue.owner_uid = :uid"); - $sth->execute(["uid" => $_SESSION['uid']]); + $sth->execute(["uid" => $_SESSION['uid']]); + } else { + $cat_ids_qmarks = arr_qmarks($cat_ids); + + $sth = $pdo->prepare("SELECT fc.id, + SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, + SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked, + (SELECT COUNT(id) FROM ttrss_feed_categories fcc + WHERE fcc.parent_cat = fc.id) AS num_children + FROM ttrss_feed_categories fc + LEFT JOIN ttrss_feeds f ON (f.cat_id = fc.id) + LEFT JOIN ttrss_user_entries ue ON (ue.feed_id = f.id) + WHERE fc.owner_uid = ? AND fc.id IN ($cat_ids_qmarks) + GROUP BY fc.id + UNION + SELECT 0, + SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, + SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked, + 0 + FROM ttrss_feeds f, ttrss_user_entries ue + WHERE f.cat_id IS NULL AND + ue.feed_id = f.id AND + ue.owner_uid = ?"); + + $sth->execute(array_merge( + [$_SESSION['uid']], + $cat_ids, + [$_SESSION['uid']] + )); + } while ($line = $sth->fetch()) { if ($line["num_children"] > 0) { @@ -83,29 +122,42 @@ class Counters { array_push($ret, $cv); } - array_push($ret, $cv); - return $ret; } - - private static function get_feeds($active_feed = false) { + private static function get_feeds(array $feed_ids = []) { $ret = []; $pdo = Db::pdo(); - $sth = $pdo->prepare("SELECT f.id, - f.title, - ".SUBSTRING_FOR_DATE."(f.last_updated,1,19) AS last_updated, - f.last_error, - SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, - SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked - FROM ttrss_feeds f, ttrss_user_entries ue - WHERE f.id = ue.feed_id AND ue.owner_uid = :uid - GROUP BY f.id"); + if (count($feed_ids) == 0) { + $sth = $pdo->prepare("SELECT f.id, + f.title, + ".SUBSTRING_FOR_DATE."(f.last_updated,1,19) AS last_updated, + f.last_error, + SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, + SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked + FROM ttrss_feeds f, ttrss_user_entries ue + WHERE f.id = ue.feed_id AND ue.owner_uid = :uid + GROUP BY f.id"); - $sth->execute(["uid" => $_SESSION['uid']]); + $sth->execute(["uid" => $_SESSION['uid']]); + } else { + $feed_ids_qmarks = arr_qmarks($feed_ids); + + $sth = $pdo->prepare("SELECT f.id, + f.title, + ".SUBSTRING_FOR_DATE."(f.last_updated,1,19) AS last_updated, + f.last_error, + SUM(CASE WHEN unread THEN 1 ELSE 0 END) AS count, + SUM(CASE WHEN marked THEN 1 ELSE 0 END) AS count_marked + FROM ttrss_feeds f, ttrss_user_entries ue + WHERE f.id = ue.feed_id AND ue.owner_uid = ? AND f.id IN ($feed_ids_qmarks) + GROUP BY f.id"); + + $sth->execute(array_merge([$_SESSION['uid']], $feed_ids)); + } while ($line = $sth->fetch()) { diff --git a/classes/feeds.php b/classes/feeds.php index a38cbae97..453c8aa0f 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -1765,7 +1765,7 @@ class Feeds extends Handler_Protected { $sth->execute([$cat, $owner_uid]); while ($line = $sth->fetch()) { - array_push($rv, $line["parent_cat"]); + array_push($rv, (int)$line["parent_cat"]); $rv = array_merge($rv, self::_get_parent_cats($line["parent_cat"], $owner_uid)); } @@ -1789,6 +1789,30 @@ class Feeds extends Handler_Protected { return $rv; } + static function _cats_of(array $feeds, int $owner_uid, bool $with_parents = false) { + $pdo = Db::pdo(); + + $feeds_qmarks = arr_qmarks($feeds); + + $sth = $pdo->prepare("SELECT DISTINCT cat_id FROM ttrss_feeds + WHERE id IN ($feeds_qmarks)"); + $sth->execute($feeds); + + $rv = []; + + if ($row = $sth->fetch()) { + array_push($rv, (int)$row["cat_id"]); + + if ($with_parents) + $rv = array_merge($rv, + self::_get_parent_cats($row["cat_id"], $owner_uid)); + } + + $rv = array_unique($rv); + + return $rv; + } + static function _cat_of_feed($feed) { $pdo = Db::pdo(); diff --git a/classes/rpc.php b/classes/rpc.php index d0388a066..7caf37cf0 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -73,14 +73,21 @@ class RPC extends Handler_Protected { } function getAllCounters() { + $feed_ids = array_map("intval", + explode(",", + clean($_REQUEST["feed_ids"]))); + @$seq = (int) $_REQUEST['seq']; + // @phpstan-ignore-next-line + $counters = count($feed_ids) > 0 ? Counters::get_for_feeds($feed_ids) : Counters::get_all(); + $reply = [ - 'counters' => Counters::get_all(), + 'counters' => $counters, 'seq' => $seq ]; - if ($seq % 2 == 0) + if ($seq % 2) $reply['runtime-info'] = $this->make_runtime_info(); print json_encode($reply); @@ -88,30 +95,39 @@ class RPC extends Handler_Protected { /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */ function catchupSelected() { - $ids = explode(",", clean($_REQUEST["ids"])); + $ids = array_map("intval", + explode(",", + clean($_REQUEST["ids"]))); + $cmode = (int)clean($_REQUEST["cmode"]); Article::_catchup_by_id($ids, $cmode); - print json_encode(array("message" => "UPDATE_COUNTERS", "ids" => $ids)); + print json_encode(["message" => "UPDATE_COUNTERS", "feeds" => Article::_feeds_of($ids)]); } function markSelected() { - $ids = explode(",", clean($_REQUEST["ids"])); + $ids = array_map("intval", + explode(",", + clean($_REQUEST["ids"]))); + $cmode = (int)clean($_REQUEST["cmode"]); $this->markArticlesById($ids, $cmode); - print json_encode(array("message" => "UPDATE_COUNTERS")); + print json_encode(["message" => "UPDATE_COUNTERS", "feeds" => Article::_feeds_of($ids)]); } function publishSelected() { - $ids = explode(",", clean($_REQUEST["ids"])); + $ids = array_map("intval", + explode(",", + clean($_REQUEST["ids"]))); + $cmode = (int)clean($_REQUEST["cmode"]); $this->publishArticlesById($ids, $cmode); - print json_encode(array("message" => "UPDATE_COUNTERS")); + print json_encode(["message" => "UPDATE_COUNTERS", "feeds" => Article::_feeds_of($ids)]); } function sanityCheck() { diff --git a/js/App.js b/js/App.js index 6cad062f0..3c940d8a6 100644 --- a/js/App.js +++ b/js/App.js @@ -421,8 +421,8 @@ const App = { // not in preferences if (typeof Feeds != "undefined") { if (message == "UPDATE_COUNTERS") { - console.log("need to refresh counters..."); - Feeds.requestCounters(true); + console.log("need to refresh counters for", reply.feeds); + Feeds.requestCounters(reply.feeds); } if (counters) diff --git a/js/Feeds.js b/js/Feeds.js index a8f08e623..e40d31087 100644 --- a/js/Feeds.js +++ b/js/Feeds.js @@ -133,8 +133,8 @@ const Feeds = { this._search_query = ""; this.reloadCurrent(); }, - requestCounters: function() { - xhr.json("backend.php", {op: "rpc", method: "getAllCounters", seq: App.next_seq()}, () => { + requestCounters: function(feed_ids = null) { + xhr.json("backend.php", {op: "rpc", method: "getAllCounters", feed_ids: feed_ids, seq: App.next_seq()}, () => { // }); }, @@ -264,10 +264,10 @@ const Feeds = { // bw_limit disables timeout() so we request initial counters separately if (App.getInitParam("bw_limit")) { - this.requestCounters(true); + this.requestCounters(); } else { setTimeout(() => { - this.requestCounters(true); + this.requestCounters(); setInterval(() => { this.requestCounters(); }, 60 * 1000) }, 250); } @@ -396,7 +396,7 @@ const Feeds = { Notify.progress("Marking all feeds as read..."); xhr.post("backend.php", {op: "feeds", method: "catchupAll"}, () => { - this.requestCounters(true); + this.requestCounters(); this.reloadCurrent(); }); diff --git a/js/Headlines.js b/js/Headlines.js index abb96d4ce..1ba52a6b8 100755 --- a/js/Headlines.js +++ b/js/Headlines.js @@ -7,7 +7,7 @@ const Headlines = { vgroup_last_feed: undefined, _headlines_scroll_timeout: 0, - _observer_counters_timeout: 0, + //_observer_counters_timeout: 0, headlines: [], current_first_id: 0, _scroll_reset_timeout: false, @@ -149,38 +149,38 @@ const Headlines = { const promises = []; if (ops.tmark.length != 0) - promises.push(xhr.post("backend.php", + promises.push(xhr.json("backend.php", {op: "rpc", method: "markSelected", ids: ops.tmark.toString(), cmode: 2})); if (ops.tpub.length != 0) - promises.push(xhr.post("backend.php", + promises.push(xhr.json("backend.php", {op: "rpc", method: "publishSelected", ids: ops.tpub.toString(), cmode: 2})); if (ops.read.length != 0) - promises.push(xhr.post("backend.php", + promises.push(xhr.json("backend.php", {op: "rpc", method: "catchupSelected", ids: ops.read.toString(), cmode: 0})); if (ops.unread.length != 0) - promises.push(xhr.post("backend.php", + promises.push(xhr.json("backend.php", {op: "rpc", method: "catchupSelected", ids: ops.unread.toString(), cmode: 1})); const scores = Object.keys(ops.rescore); if (scores.length != 0) { scores.forEach((score) => { - promises.push(xhr.post("backend.php", + promises.push(xhr.json("backend.php", {op: "article", method: "setScore", id: ops.rescore[score].toString(), score: score})); }); } - if (promises.length > 0) + /*if (promises.length > 0) Promise.all([promises]).then(() => { window.clearTimeout(this._observer_counters_timeout); this._observer_counters_timeout = setTimeout(() => { - Feeds.requestCounters(true); + Feeds.requestCounters(); }, 1000); - }); + });*/ }, click: function (event, id, in_body) {