try to calculate counters conditionally based on feed ids

This commit is contained in:
Andrew Dolgov 2021-02-24 09:47:26 +03:00
parent a42e8aad97
commit d6203bf350
7 changed files with 168 additions and 60 deletions

View File

@ -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;
}
}

View File

@ -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()) {

View File

@ -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();

View File

@ -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() {

View File

@ -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)

View File

@ -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();
});

View File

@ -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) {