git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Commit 2a234d40 authored by Jonas Haraldsson's avatar Jonas Haraldsson

User_Stats table and scrobble count triggers

User_Stats table for storing user's scrobble count.
Triggers and functions for both postgresql and mysql for updating the
User_Stats table whenever a new scrobble gets inserted into Scrobbles
table.
Changed getTotalTracks related functions to use new User_Stats table
parent d6e377f3
......@@ -335,7 +335,12 @@ if (isset($_POST['install'])) {
'INSERT INTO Relationship_Flags VALUES (\'muse\')',
'INSERT INTO Relationship_Flags VALUES (\'crush\')',
'INSERT INTO Relationship_Flags VALUES (\'date\')',
'INSERT INTO Relationship_Flags VALUES (\'sweetheart\')'
'INSERT INTO Relationship_Flags VALUES (\'sweetheart\')',
'CREATE TABLE User_Stats (
userid INTEGER REFERENCES Users(uniqueid) ON DELETE CASCADE,
scrobble_count INTEGER NOT NULL,
PRIMARY KEY (userid))'
);
foreach ($stage_one_queries as $query) {
......@@ -384,6 +389,43 @@ if (isset($_POST['install'])) {
$adodb->Execute("CREATE INDEX scrobbles_artist_idx on Scrobbles(lower(artist))");
$adodb->Execute("CREATE INDEX scrobbles_track_idx on Scrobbles(lower(track))");
$adodb->Execute("CREATE INDEX groups_groupname_idx ON Groups(lower(groupname))");
// PostgreSQL stored functions
$adodb->Execute("CREATE LANGUAGE plpgsql;");
$adodb->Execute("CREATE FUNCTION update_user_stats_scrobble_count() RETURNS TRIGGER AS $$
DECLARE s_count int;
BEGIN
UPDATE User_Stats SET scrobble_count = scrobble_count + 1 WHERE userid = NEW.userid;
IF found THEN
RETURN NULL;
END IF;
BEGIN
-- userid not in User_Stats table, get current scrobble count from Scrobbles
-- and insert userid into User_Stats
SELECT COUNT(userid) into s_count FROM Scrobbles WHERE userid = NEW.userid;
INSERT INTO User_Stats(userid, scrobble_count) VALUES(NEW.userid, s_count);
RETURN NULL;
END;
END;
$$ LANGUAGE plpgsql;");
$adodb->Execute("CREATE TRIGGER update_user_stats_scrobble_count
AFTER INSERT ON Scrobbles
FOR EACH ROW EXECUTE PROCEDURE update_user_stats_scrobble_count();");
} elseif (substr($dbms, 0, 5) == 'mysql') {
$adodb->Execute("CREATE PROCEDURE update_user_stats_scrobble_count(uid INT)
main: BEGIN
DECLARE s_count INT;
UPDATE User_Stats SET scrobble_count = (scrobble_count + 1) WHERE userid = uid;
IF ROW_COUNT() > 0 THEN
LEAVE main;
END IF;
SELECT COUNT(userid) INTO s_count FROM Scrobbles WHERE userid = uid;
INSERT INTO User_Stats(userid, scrobble_count) VALUES(uid, s_count);
END main;");
$adodb->Execute("CREATE TRIGGER update_user_stats_scrobble_count
AFTER INSERT ON Scrobbles
FOR EACH ROW CALL update_user_stats_scrobble_count(NEW.userid);");
}
$adodb->Close();
......
......@@ -38,7 +38,7 @@ require_once('temp-utils.php');
<?php
$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
$total = $adodb->CacheGetOne(500, 'SELECT COUNT(*) as total from Scrobbles');
$total = $adodb->CacheGetOne(500, 'SELECT SUM(scrobble_count) as total from User_Stats');
if (!$total) {
die('sql error');
}
......
......@@ -125,8 +125,8 @@ class UserXML {
return(XML::error('error', '7', 'Invalid resource specified'));
}
$totalPages = $adodb->GetOne('SELECT COUNT(track) FROM Scrobbles WHERE userid = ' . $user->uniqueid);
$totalPages = ceil($totalPages / $limit);
$totalTracks = $user->getTotalTracks();
$totalPages = ceil($totalTracks / $limit);
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
$root = $xml->addChild('recenttracks', null);
......
......@@ -291,7 +291,7 @@ class User {
if ($since) {
$query = 'SELECT COUNT(*) FROM Scrobbles WHERE userid = ' . ($this->uniqueid) . ' AND time > ' . (int)($since);
} else {
$query = 'SELECT COUNT(*) FROM Scrobbles WHERE userid = ' . ($this->uniqueid);
$query = 'SELECT scrobble_count FROM User_Stats WHERE userid = ' . ($this->uniqueid);
}
try {
$tracks = $adodb->CacheGetOne(200, $query);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment