From a4fe4c4489a47250ac5d55189f1ab7e7b9a05675 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 23 Dec 2023 10:49:54 +0100 Subject: [PATCH] feat: :sparkles: transition to time-block based access count storage, including referrer storage --- docker_dev/mysql_schema.sql | 18 ++++++++-------- www/mysql_adapter.php | 43 +++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/docker_dev/mysql_schema.sql b/docker_dev/mysql_schema.sql index e3934bc..255920f 100644 --- a/docker_dev/mysql_schema.sql +++ b/docker_dev/mysql_schema.sql @@ -12,6 +12,8 @@ CREATE TABLE posts ( post_create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, post_update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + post_access_count INTEGER DEFAULT 0, + post_metadata JSON NOT NULL, post_settings_cache JSON DEFAULT NULL, @@ -20,24 +22,22 @@ CREATE TABLE posts ( PRIMARY KEY(post_id), CONSTRAINT unique_post_path UNIQUE (post_path), - INDEX(post_path, post_update_time), - INDEX(post_path, post_create_time), - INDEX(post_path_depth, post_path) + INDEX(post_path), + INDEX(post_path_depth, post_path), + INDEX(post_create_time), + INDEX(post_update_time) ); CREATE TABLE path_access_counts ( + access_time DATETIME NOT NULL, post_path VARCHAR(255), agent VARCHAR(255), - - path_last_access_time DATETIME NOT NULL - DEFAULT CURRENT_TIMESTAMP - ON UPDATE CURRENT_TIMESTAMP, + referrer VARCHAR(255), path_access_count INTEGER DEFAULT 0, path_processing_time DOUBLE PRECISION DEFAULT 0, - PRIMARY KEY(post_path, agent), - INDEX(path_last_access_time) + PRIMARY KEY(access_time, post_path, agent, referrer) ); INSERT INTO posts (post_path, post_path_depth, post_metadata, post_content) diff --git a/www/mysql_adapter.php b/www/mysql_adapter.php index 80dc64a..54603e3 100644 --- a/www/mysql_adapter.php +++ b/www/mysql_adapter.php @@ -105,19 +105,27 @@ class MySQLAdapter { } } - function log_post_access($post_path, $agent, $time) { - $qry = " - INSERT INTO path_access_counts - (post_path, agent, + function log_post_access($post_path, $agent, $referrer, $time) { + $post_path = $this->_sanitize_path($post_path); + + $qry = "INSERT INTO path_access_counts + (access_time, + post_path, agent, referrer, path_access_count, path_processing_time) - VALUES ( ?, ?, 1, ? ) AS new + VALUES ( from_unixtime(floor(unix_timestamp(CURRENT_TIMESTAMP) / 300)*300), + ?, ?, ?, 1, ? + ) AS new ON DUPLICATE KEY UPDATE path_access_count=path_access_counts.path_access_count+1, path_processing_time=path_access_counts.path_processing_time+new.path_processing_time; "; - $this->_exec($qry, "ssd", $post_path, $agent, $time); + $this->_exec($qry, "sssd", $post_path, $agent, $referrer, $time); + + if(preg_match('/^user/', $agent)) { + $this->_exec("UPDATE posts SET post_access_count=post_access_count+1 WHERE post_path=?", "s", $post_path); + } } function get_post_access_counters() { @@ -229,27 +237,20 @@ class MySQLAdapter { function get_post_by_path($post_path, $with_subposts = false, $with_settings = true) { - $qry = " - WITH selected_post AS ( - SELECT * - FROM posts WHERE post_path = ? - ), post_count_data AS ( - SELECT pac.post_path AS post_path, sum(path_access_count) AS post_access_counter - FROM path_access_counts AS pac - INNER JOIN selected_post ON pac.post_path = selected_post.post_path - WHERE agent LIKE 'user%' - GROUP BY pac.post_path - ) - SELECT * - FROM selected_post - LEFT JOIN post_count_data ON post_count_data.post_path = selected_post.post_path"; + $qry = "SELECT * + FROM posts WHERE post_path = ? + "; $post_path = $this->_sanitize_path($post_path); $post_data = $this->_exec($qry, "s", $post_path)->fetch_assoc(); + + if(!isset($post_data)) { + $post_data = ['found' => false]; + } + $post_data['post_path'] = $post_path; $post_data = $this->_normalize_post_data($post_data); - $post_data['post_path'] = $post_path; if(!$post_data['found']) { return $post_data;