mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Limit some collection features.
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* TelemetryCronjob.php
|
||||
* Copyright (c) 2020 james@firefly-iii.org
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Cronjobs;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Jobs\SubmitTelemetryData;
|
||||
use FireflyIII\Models\Configuration;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class TelemetryCronjob
|
||||
*/
|
||||
class TelemetryCronjob extends AbstractCronjob
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function fire(): void
|
||||
{
|
||||
// do not fire if telemetry is disabled.
|
||||
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
|
||||
$msg = 'Telemetry is disabled. The cron job will do nothing.';
|
||||
$this->message = $msg;
|
||||
Log::warning($msg);
|
||||
|
||||
return;
|
||||
}
|
||||
/** @var Configuration $config */
|
||||
$config = app('fireflyconfig')->get('last_tm_job', 0);
|
||||
$lastTime = (int)$config->data;
|
||||
$diff = time() - $lastTime;
|
||||
$diffForHumans = Carbon::now()->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
|
||||
if (0 === $lastTime) {
|
||||
Log::info('Telemetry cron-job has never fired before.');
|
||||
}
|
||||
// less than a week ago:
|
||||
if ($lastTime > 0 && $diff <= 604800) {
|
||||
Log::info(sprintf('It has been %s since the telemetry cron-job has fired.', $diffForHumans));
|
||||
if (false === $this->force) {
|
||||
Log::info('The cron-job will not fire now.');
|
||||
$this->message = sprintf('It has been %s since the telemetry cron-job has fired. It will not fire now.', $diffForHumans);
|
||||
return;
|
||||
}
|
||||
|
||||
// fire job regardless.
|
||||
if (true === $this->force) {
|
||||
Log::info('Execution of the telemetry cron-job has been FORCED.');
|
||||
}
|
||||
}
|
||||
// more than a week ago.
|
||||
if ($lastTime > 0 && $diff > 604799) {
|
||||
Log::info(sprintf('It has been %s since the telemetry cron-job has fired. It will fire now!', $diffForHumans));
|
||||
}
|
||||
|
||||
$this->fireTelemetry();
|
||||
|
||||
app('preferences')->mark();
|
||||
}
|
||||
/**
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function fireTelemetry(): void
|
||||
{
|
||||
Log::info(sprintf('Will now fire telemetry cron job task for date "%s".', $this->date->format('Y-m-d')));
|
||||
/** @var SubmitTelemetryData $job */
|
||||
$job = app(SubmitTelemetryData::class);
|
||||
$job->setDate($this->date);
|
||||
$job->setForce($this->force);
|
||||
$job->handle();
|
||||
|
||||
$this->jobFired = true;
|
||||
$this->jobErrored = false;
|
||||
$this->jobSucceeded = true;
|
||||
$this->message = 'Telemetry cron job fired successfully.';
|
||||
|
||||
// TODO remove old, submitted telemetry data.
|
||||
|
||||
app('fireflyconfig')->set('last_tm_job', (int)$this->date->format('U'));
|
||||
Log::info('Done with telemetry cron job task.');
|
||||
}
|
||||
}
|
@@ -27,7 +27,6 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Support\Cronjobs\AutoBudgetCronjob;
|
||||
use FireflyIII\Support\Cronjobs\RecurringCronjob;
|
||||
use FireflyIII\Support\Cronjobs\TelemetryCronjob;
|
||||
|
||||
/**
|
||||
* Trait CronRunner
|
||||
@@ -97,35 +96,5 @@ trait CronRunner
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function runTelemetry(bool $force, Carbon $date): array
|
||||
{
|
||||
/** @var TelemetryCronjob $telemetry */
|
||||
$telemetry = app(TelemetryCronjob::class);
|
||||
$telemetry->setForce($force);
|
||||
$telemetry->setDate($date);
|
||||
try {
|
||||
$telemetry->fire();
|
||||
} catch (FireflyException $e) {
|
||||
return [
|
||||
'job_fired' => false,
|
||||
'job_succeeded' => false,
|
||||
'job_errored' => true,
|
||||
'message' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'job_fired' => $telemetry->jobFired,
|
||||
'job_succeeded' => $telemetry->jobSucceeded,
|
||||
'job_errored' => $telemetry->jobErrored,
|
||||
'message' => $telemetry->message,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,11 +25,11 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\Telemetry as TelemetryModel;
|
||||
use FireflyIII\Support\System\GeneratesInstallationId;
|
||||
use Illuminate\Database\QueryException;
|
||||
use JsonException;
|
||||
use Log;
|
||||
use Sentry\Severity;
|
||||
use Sentry\State\Scope;
|
||||
use function Sentry\captureMessage;
|
||||
use function Sentry\configureScope;
|
||||
|
||||
/**
|
||||
* Class Telemetry
|
||||
@@ -37,6 +37,7 @@ use Log;
|
||||
class Telemetry
|
||||
{
|
||||
use GeneratesInstallationId;
|
||||
|
||||
/**
|
||||
* Feature telemetry stores a $value for the given $feature.
|
||||
* Will only store the given $feature / $value combination once.
|
||||
@@ -64,134 +65,25 @@ class Telemetry
|
||||
// do nothing!
|
||||
return;
|
||||
}
|
||||
|
||||
Log::info(sprintf('Logged telemetry feature "%s" with value "%s".', $key, $value));
|
||||
if (!$this->hasEntry('feature', $key, $value)) {
|
||||
$this->storeEntry('feature', $key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param int $days
|
||||
*/
|
||||
public function recurring(string $key, string $value, int $days): void
|
||||
{
|
||||
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
|
||||
// hard stop if not allowed to do telemetry.
|
||||
// do nothing!
|
||||
return;
|
||||
}
|
||||
|
||||
$cutoffDate = Carbon::today()->subDays($days);
|
||||
if (!$this->hasRecentEntry('recurring', $key, $value, $cutoffDate)) {
|
||||
$this->storeEntry('recurring', $key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* String telemetry stores a string value as a telemetry entry. Values could include:
|
||||
*
|
||||
* - "php-version", "php7.3"
|
||||
* - "os-version", "linux"
|
||||
*
|
||||
* Any meta-data stored is strictly non-financial.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*/
|
||||
public function string(string $name, string $value): void
|
||||
{
|
||||
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
|
||||
// hard stop if not allowed to do telemetry.
|
||||
// do nothing!
|
||||
return;
|
||||
}
|
||||
Log::info(sprintf('Logged telemetry string "%s" with value "%s".', $name, $value));
|
||||
|
||||
$this->storeEntry('string', $name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasEntry(string $type, string $key, string $value): bool
|
||||
{
|
||||
try {
|
||||
$jsonEncoded = json_encode($value, JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $e) {
|
||||
Log::error(sprintf('JSON Exception encoding the following value: %s: %s', $value, $e->getMessage()));
|
||||
$jsonEncoded = [];
|
||||
}
|
||||
try {
|
||||
$count = TelemetryModel
|
||||
::where('type', $type)
|
||||
->where('key', $key)
|
||||
->where('value', $jsonEncoded)
|
||||
->count();
|
||||
} catch (QueryException $e) {
|
||||
Log::info(sprintf('Could not execute hasEntry() but this is OK: %s', $e->getMessage()));
|
||||
$count = 0;
|
||||
}
|
||||
|
||||
return $count > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasRecentEntry(string $type, string $key, string $value, Carbon $date): bool
|
||||
{
|
||||
try {
|
||||
$jsonEncoded = json_encode($value, JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException $e) {
|
||||
Log::error(sprintf('JSON Exception encoding the following value: %s: %s', $value, $e->getMessage()));
|
||||
$jsonEncoded = [];
|
||||
}
|
||||
|
||||
return TelemetryModel
|
||||
::where('type', $type)
|
||||
->where('key', $key)
|
||||
->where('created_at', '>=', $date->format('Y-m-d H:i:s'))
|
||||
->where('value', $jsonEncoded)
|
||||
->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store new entry in DB.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
*
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
*/
|
||||
private function storeEntry(string $type, string $name, string $value): void
|
||||
{
|
||||
// send to Sentry.
|
||||
$this->generateInstallationId();
|
||||
$config = app('fireflyconfig')->get('installation_id');
|
||||
$installationId = null !== $config ? $config->data : 'empty';
|
||||
try {
|
||||
TelemetryModel::create(
|
||||
[
|
||||
'installation_id' => $installationId,
|
||||
'key' => $name,
|
||||
'type' => $type,
|
||||
'value' => $value,
|
||||
]
|
||||
);
|
||||
} catch (QueryException $e) {
|
||||
Log::info(sprintf('Could not execute storeEntry() but this is OK: %s', $e->getMessage()));
|
||||
}
|
||||
$installationId = app('fireflyconfig')->get('installation_id');
|
||||
|
||||
// add some context:
|
||||
configureScope(
|
||||
function (Scope $scope) use ($installationId, $key, $value): void {
|
||||
$scope->setContext(
|
||||
'telemetry', [
|
||||
'installation_id' => $installationId->data,
|
||||
'version' => config('firefly.version'),
|
||||
'collected_at' => Carbon::now()->format('r'),
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
]
|
||||
);
|
||||
}
|
||||
);
|
||||
captureMessage(sprintf('FIT: %s/%s', $key, $value), Severity::info());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user