Skip to content

Commit 417ddc1

Browse files
committed
feat: add config for how long to store usage
Signed-off-by: Lukas Schaefer <[email protected]>
1 parent 021c281 commit 417ddc1

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/Cron/CleanupQuotaDb.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ public function __construct(
3030
protected function run($argument) {
3131
$this->logger->debug('Run cleanup job for OpenAI quota db');
3232
$quota = $this->openAiSettingsService->getQuotaPeriod();
33-
$days = $quota['length'];
33+
$quotaDays = $quota['length'];
3434
if ($quota['unit'] == 'month') {
35-
$days *= 30;
35+
$quotaDays *= 30;
3636
}
37+
$days = $this->openAiSettingsService->getUsageStorageTime();
3738
$this->quotaUsageMapper->cleanupQuotaUsages(
3839
// The mimimum period is limited to DEFAULT_QUOTA_PERIOD to not lose
3940
// the stored quota usage data below this limit.
40-
max($days, Application::DEFAULT_QUOTA_PERIOD)
41+
max($quotaDays, $days, Application::DEFAULT_QUOTA_PERIOD)
4142
);
4243

4344
}

lib/Service/OpenAiSettingsService.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class OpenAiSettingsService {
3737
'llm_extra_params' => 'string',
3838
'quota_period' => 'array',
3939
'quotas' => 'array',
40+
'usage_storage_time' => 'integer',
4041
'translation_provider_enabled' => 'boolean',
4142
'llm_provider_enabled' => 'boolean',
4243
't2i_provider_enabled' => 'boolean',
@@ -310,6 +311,10 @@ public function getQuotas(): array {
310311
return $quotas;
311312
}
312313

314+
public function getUsageStorageTime() : int {
315+
return $this->appConfig->getValueInt(Application::APP_ID, 'usage_storage_time', Application::DEFAULT_QUOTA_PERIOD, lazy: true);
316+
}
317+
313318
/**
314319
* @return boolean
315320
*/
@@ -397,6 +402,7 @@ public function getAdminConfig(): array {
397402
// Updated to get quota period
398403
'quotas' => $this->getQuotas(),
399404
// Get quotas from the config value and return it
405+
'usage_storage_time' => $this->getUsageStorageTime(),
400406
'translation_provider_enabled' => $this->getTranslationProviderEnabled(),
401407
'llm_provider_enabled' => $this->getLlmProviderEnabled(),
402408
't2i_provider_enabled' => $this->getT2iProviderEnabled(),
@@ -524,6 +530,15 @@ public function setQuotas(array $quotas): void {
524530
$cache->clear(Application::QUOTA_RULES_CACHE_PREFIX);
525531
}
526532

533+
/**
534+
* @param int $usageStorageTime
535+
* @return void
536+
*/
537+
public function setUsageStorageTime(int $usageStorageTime): void {
538+
$usageStorageTime = max(1, $usageStorageTime);
539+
$this->appConfig->setValueInt(Application::APP_ID, 'usage_storage_time', $usageStorageTime, lazy: true);
540+
}
541+
527542
/**
528543
* @param string $apiKey
529544
* @return void
@@ -833,6 +848,9 @@ public function setAdminConfig(array $adminConfig): void {
833848
if (isset($adminConfig['quotas'])) {
834849
$this->setQuotas($adminConfig['quotas']);
835850
}
851+
if (isset($adminConfig['usage_storage_time'])) {
852+
$this->setUsageStorageTime(intval($adminConfig['usage_storage_time']));
853+
}
836854
if (isset($adminConfig['use_max_completion_tokens_param'])) {
837855
$this->setUseMaxCompletionParam($adminConfig['use_max_completion_tokens_param']);
838856
}

src/components/AdminSettings.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,15 @@
546546
</tr>
547547
</tbody>
548548
</table>
549+
<div class="line">
550+
<NcInputField
551+
id="openai-api-usage-storage-time"
552+
v-model="state.usage_storage_time"
553+
class="input"
554+
type="number"
555+
:label="t('integration_openai', 'Time period (days) for usage storage')"
556+
@update:model-value="onInput()" />
557+
</div>
549558
<div class="line-gap">
550559
<NcDateTimePickerNative
551560
v-model="quota_usage.start_date"
@@ -907,6 +916,7 @@ export default {
907916
quotas: this.state.quotas,
908917
tts_voices: this.state.tts_voices,
909918
default_tts_voice: this.state.default_tts_voice,
919+
usage_storage_time: this.state.usage_storage_time,
910920
}
911921
await this.saveOptions(values, false)
912922
}, 2000),

0 commit comments

Comments
 (0)