Skip to content

Commit

Permalink
Merge pull request Aspen-Discovery#2213 from LeoStoyanovByWater/DIS-2…
Browse files Browse the repository at this point in the history
…50-usage-tables-aggregating-data

DIS 250: Fixed Usage Tables Not Aggregating Data Properly
  • Loading branch information
mdnoble73 authored and kylemhall committed Jan 31, 2025
1 parent 23c5173 commit f97cf9a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion code/web/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected function forbidAPIAccess()
$aspenUsage->blockedApiRequests++;
$aspenUsage->update();
global $usageByIPAddress;
try{
try {
$usageByIPAddress->numBlockedApiRequests++;
if (SystemVariables::getSystemVariables()->trackIpAddresses) {
$usageByIPAddress->update();
Expand Down
2 changes: 2 additions & 0 deletions code/web/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
$usageByUserAgent->month = date('n');
global $aspenUsage;
$usageByUserAgent->instance = $aspenUsage->getInstance();
// Attempts to load an existing row from usage_by_user_agent that matches the index userAgentId.
$foundExisting = $usageByUserAgent->find(true);

if ($userAgent->blockAccess) {
$usageByUserAgent->numBlockedRequests++;
Expand Down
49 changes: 49 additions & 0 deletions code/web/sys/DBMaintenance/version_updates/25.01.02.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

function getUpdates25_01_02(): array {
$curTime = time();
return [
/*'name' => [
'title' => '',
'description' => '',
'continueOnError' => false,
'sql' => [
''
]
], //name*/

// Leo Stoyanov - BWS
'aggregate_usage_by_user_agent' => [
'title' => 'Aggregate usage_by_user_agent & add unique key',
'description' => 'Combine multiple rows per (userAgentId, year, month, instance) into one row before adding unique key',
'continueOnError' => true,
'sql' => [
// 1) Create a temporary table with the same structure.
"CREATE TABLE usage_by_user_agent_temp LIKE usage_by_user_agent",

// 2) Insert aggregated data into temp table.
"INSERT INTO usage_by_user_agent_temp (userAgentId, year, month, instance, numRequests, numBlockedRequests)
SELECT userAgentId, year, month, instance,
SUM(numRequests) AS totalRequests,
SUM(numBlockedRequests) AS totalBlocked
FROM usage_by_user_agent
GROUP BY userAgentId, year, month, instance",

// 3) Clear out original table.
"TRUNCATE TABLE usage_by_user_agent",

// 4) Re-insert aggregated rows into original table.
"INSERT INTO usage_by_user_agent
SELECT * FROM usage_by_user_agent_temp",

// 5) Drop the temp table.
"DROP TABLE usage_by_user_agent_temp",

// 6) Finally, drop the old index and add the unique index.
"ALTER TABLE usage_by_user_agent
DROP INDEX userAgentId,
ADD UNIQUE KEY userAgentId (userAgentId, year, month, instance)"
],
], //aggregate_usage_by_user_agent
];
}
6 changes: 3 additions & 3 deletions install/aspen.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5732,7 +5732,7 @@ CREATE TABLE `usage_by_user_agent` (
`numRequests` int(11) NOT NULL DEFAULT 0,
`numBlockedRequests` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `userAgentId` (`userAgentId`,`year`,`instance`,`month`)
UNIQUE KEY `userAgentId` (`userAgentId`,`year`,`instance`,`month`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
DROP TABLE IF EXISTS usage_tracking;
CREATE TABLE `usage_tracking` (
Expand Down Expand Up @@ -6856,8 +6856,8 @@ Thank you for your purchase suggestion!', 0, 0, -1, 0, 1, 0, 0),
Thanks', 0, 0, -1, 0, 0, 1, 0),
(23, 'Hold Not Needed', 0, 0, '', 0, 0, -1, 0, 0, 0, 1);
UPDATE modules set enabled=0;UPDATE modules set enabled=1 where name in ('Side Loads', 'User Lists');
INSERT INTO system_variables (currencyCode, storeRecordDetailsInSolr, storeRecordDetailsInDatabase, indexVersion, searchVersion, appScheme, trackIpAddresses) VALUES ('USD', 0, 1, 2, 2, 'aspen-lida', 0);
INSERT INTO themes
INSERT INTO system_variables (currencyCode, storeRecordDetailsInSolr, storeRecordDetailsInDatabase, indexVersion, searchVersion, appScheme, trackIpAddresses) VALUES ('USD', 0, 1, 2, 2, 'aspen-lida', 0);
INSERT INTO themes
(id,themeName,logoName,headerBackgroundColor,headerBackgroundColorDefault,headerForegroundColor,headerForegroundColorDefault,
generatedCss,
pageBackgroundColor,pageBackgroundColorDefault,primaryBackgroundColor,primaryBackgroundColorDefault,primaryForegroundColor,primaryForegroundColorDefault,
Expand Down

0 comments on commit f97cf9a

Please sign in to comment.