Skip to content

Commit

Permalink
[FEATURE] Determine severity based on quota usage
Browse files Browse the repository at this point in the history
Calculate the message severity based on the API quota usage rate.
Matching popular quota warning levels if will notify above 90% usage,
and shown an error when the limit is exceeded.

Hide the flashmessage after a translation if the severity is low.

Refs #343
  • Loading branch information
pixelbrackets committed Sep 9, 2024
1 parent 6c4b545 commit a06e537
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
27 changes: 26 additions & 1 deletion Classes/Event/Listener/UsageToolBarEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public function __invoke(SystemInformationToolbarCollectorEvent $systemInformati
'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.toolbar.message'
);

$severity = $this->determineSeverity($usage->character->count, $usage->character->limit);

$systemInformation->getToolbarItem()->addSystemInformation(
$title,
sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)),
'actions-localize-deepl',
InformationStatus::STATUS_NOTICE,
$severity
);
}

Expand All @@ -72,4 +74,27 @@ private function formatNumber(int $number): string
{
return number_format($number, 0, ',', '.');
}


/**
* Calculate the message severity based on the quota usage rate
*
* @param int $characterCount Already translated characters in the current billing period
* @param int $characterLimit Total character limit in the current billing period
* @return string Severity level
*/
private function determineSeverity(int $characterCount, int $characterLimit): string
{
$quotaUtilization = ($characterCount / $characterLimit) * 100;
if ($quotaUtilization >= 100) {
return InformationStatus::STATUS_ERROR;
}
if ($quotaUtilization >= 98) {
return InformationStatus::STATUS_WARNING;
}
if ($quotaUtilization >= 90) {
return InformationStatus::STATUS_INFO;
}
return InformationStatus::STATUS_NOTICE;
}
}
39 changes: 34 additions & 5 deletions Classes/Hooks/UsageProcessAfterFinishHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ public function processCmdmap_afterFinish(DataHandler $dataHandler): void
'LLL:EXT:wv_deepltranslate/Resources/Private/Language/locallang.xlf:usages.flashmassage.limit.description'
);

$severity = $this->determineSeverity($usage->character->count, $usage->character->limit);
// Reduce noise - Don't bother editors with low quota usage messages
if ($severity === FlashMessage::NOTICE) {
return;
}

$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$notificationQueue = $flashMessageService->getMessageQueueByIdentifier();

$severity = FlashMessage::INFO;
if ($this->usageService->isTranslateLimitExceeded()) {
$severity = FlashMessage::WARNING;
}

$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
sprintf($message, $this->formatNumber($usage->character->count), $this->formatNumber($usage->character->limit)),
Expand All @@ -70,8 +71,36 @@ private function getLanguageService(): LanguageService
return $GLOBALS['LANG'];
}

/**
* Make large API limits easier to read
*
* @param int $number Any large integer - 5000000
* @return string Formated, better readable string variant of the integer - 5.000.000
*/
private function formatNumber(int $number): string
{
return number_format($number, 0, ',', '.');
}

/**
* Calculate the message severity based on the quota usage rate
*
* @param int $characterCount Already translated characters in the current billing period
* @param int $characterLimit Total character limit in the current billing period
* @return int Severity level
*/
private function determineSeverity(int $characterCount, int $characterLimit): int
{
$quotaUtilization = ($characterCount / $characterLimit) * 100;
if ($quotaUtilization >= 100) {
return FlashMessage::ERROR;
}
if ($quotaUtilization >= 98) {
return FlashMessage::WARNING;
}
if ($quotaUtilization >= 90) {
return FlashMessage::INFO;
}
return FlashMessage::NOTICE;
}
}

0 comments on commit a06e537

Please sign in to comment.