mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Limit some collection features.
This commit is contained in:
parent
e379bbab4c
commit
40bbed2a8b
@ -51,7 +51,6 @@ class CronController extends Controller
|
||||
$return = [];
|
||||
$return['recurring_transactions'] = $this->runRecurring($config['force'], $config['date']);
|
||||
$return['auto_budgets'] = $this->runAutoBudget($config['force'], $config['date']);
|
||||
$return['telemetry'] = $this->runTelemetry($config['force'], $config['date']);
|
||||
|
||||
return response()->json($return);
|
||||
}
|
||||
|
@ -132,11 +132,11 @@ class ExportData extends Command
|
||||
if (0 !== count($data)) {
|
||||
try {
|
||||
$this->exportData($options, $data);
|
||||
app('telemetry')->feature('system.command.executed', $this->signature);
|
||||
app('telemetry')->feature('system.command.executed', 'firefly-iii:export-data');
|
||||
} catch (FireflyException $e) {
|
||||
$this->error(sprintf('Could not store data: %s', $e->getMessage()));
|
||||
|
||||
app('telemetry')->feature('system.command.errored', $this->signature);
|
||||
app('telemetry')->feature('system.command.errored', 'firefly-iii:export-data');
|
||||
$returnCode = 1;
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ class SetLatestVersion extends Command
|
||||
app('fireflyconfig')->set('ff3_version', config('firefly.version'));
|
||||
$this->line('Updated version.');
|
||||
|
||||
app('telemetry')->feature('system.command.executed', $this->signature);
|
||||
app('telemetry')->feature('system.command.executed', 'firefly-iii:set-latest-version');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ class ApplyRules extends Command
|
||||
|
||||
$result = $this->verifyInput();
|
||||
if (false === $result) {
|
||||
app('telemetry')->feature('system.command.errored', $this->signature);
|
||||
app('telemetry')->feature('system.command.errored', 'firefly-iii:apply-rules');
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -119,7 +119,7 @@ class ApplyRules extends Command
|
||||
$this->warn(' --rule_groups=1,2,...');
|
||||
$this->warn(' --all_rules');
|
||||
|
||||
app('telemetry')->feature('system.command.errored', $this->signature);
|
||||
app('telemetry')->feature('system.command.errored', 'firefly-iii:apply-rules');
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -148,7 +148,7 @@ class ApplyRules extends Command
|
||||
// file the rule(s)
|
||||
$ruleEngine->fire();
|
||||
|
||||
app('telemetry')->feature('system.command.executed', $this->signature);
|
||||
app('telemetry')->feature('system.command.executed', 'firefly-iii:apply-rules');
|
||||
|
||||
$this->line('');
|
||||
$end = round(microtime(true) - $start, 2);
|
||||
|
@ -25,11 +25,9 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Console\Commands\Tools;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Support\Cronjobs\AutoBudgetCronjob;
|
||||
use FireflyIII\Support\Cronjobs\RecurringCronjob;
|
||||
use FireflyIII\Support\Cronjobs\TelemetryCronjob;
|
||||
use Illuminate\Console\Command;
|
||||
use InvalidArgumentException;
|
||||
use Log;
|
||||
@ -92,20 +90,9 @@ class Cron extends Command
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
/*
|
||||
* Fire telemetry cron job
|
||||
*/
|
||||
try {
|
||||
$this->telemetryCronJob($force, $date);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$this->info('More feedback on the cron jobs can be found in the log files.');
|
||||
|
||||
app('telemetry')->feature('system.command.executed', $this->signature);
|
||||
app('telemetry')->feature('system.command.executed', 'firefly-iii:cron');
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -127,13 +114,13 @@ class Cron extends Command
|
||||
}
|
||||
|
||||
$recurring->fire();
|
||||
if($recurring->jobErrored) {
|
||||
if ($recurring->jobErrored) {
|
||||
$this->error(sprintf('Error in "create recurring transactions" cron: %s', $recurring->message));
|
||||
}
|
||||
if($recurring->jobFired) {
|
||||
if ($recurring->jobFired) {
|
||||
$this->error(sprintf('"Create recurring transactions" cron fired: %s', $recurring->message));
|
||||
}
|
||||
if($recurring->jobSucceeded) {
|
||||
if ($recurring->jobSucceeded) {
|
||||
$this->error(sprintf('"Create recurring transactions" cron ran with success: %s', $recurring->message));
|
||||
}
|
||||
}
|
||||
@ -154,50 +141,15 @@ class Cron extends Command
|
||||
|
||||
$autoBudget->fire();
|
||||
|
||||
if($autoBudget->jobErrored) {
|
||||
if ($autoBudget->jobErrored) {
|
||||
$this->error(sprintf('Error in "create auto budgets" cron: %s', $autoBudget->message));
|
||||
}
|
||||
if($autoBudget->jobFired) {
|
||||
if ($autoBudget->jobFired) {
|
||||
$this->error(sprintf('"Create auto budgets" cron fired: %s', $autoBudget->message));
|
||||
}
|
||||
if($autoBudget->jobSucceeded) {
|
||||
if ($autoBudget->jobSucceeded) {
|
||||
$this->error(sprintf('"Create auto budgets" cron ran with success: %s', $autoBudget->message));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
* @param Carbon|null $date
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function telemetryCronJob(bool $force, ?Carbon $date): void
|
||||
{
|
||||
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
|
||||
// if not configured to do anything with telemetry, do nothing.
|
||||
return;
|
||||
}
|
||||
$telemetry = new TelemetryCronjob;
|
||||
$telemetry->setForce($force);
|
||||
|
||||
// set date in cron job:
|
||||
if (null !== $date) {
|
||||
$telemetry->setDate($date);
|
||||
}
|
||||
|
||||
$telemetry->fire();
|
||||
|
||||
if($telemetry->jobErrored) {
|
||||
$this->error(sprintf('Error in "send telemetry" cron: %s', $telemetry->message));
|
||||
}
|
||||
if($telemetry->jobFired) {
|
||||
$this->error(sprintf('"Send telemetry" cron fired: %s', $telemetry->message));
|
||||
}
|
||||
if($telemetry->jobSucceeded) {
|
||||
$this->error(sprintf('"Send telemetry" cron ran with success: %s', $telemetry->message));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -69,7 +69,6 @@ class UpgradeFireflyInstructions extends Command
|
||||
app('telemetry')->feature('system.os.version', PHP_OS);
|
||||
app('telemetry')->feature('system.database.driver', env('DB_CONNECTION', '(unknown)'));
|
||||
app('telemetry')->feature('system.os.is_docker', $isDocker);
|
||||
app('telemetry')->feature('system.command.executed', $this->signature);
|
||||
try {
|
||||
app('telemetry')->feature('system.users.count', (string)User::count());
|
||||
} catch (QueryException $e) {
|
||||
|
@ -25,21 +25,15 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Http\Controllers\Admin;
|
||||
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Jobs\SubmitTelemetryData;
|
||||
use FireflyIII\Repositories\Telemetry\TelemetryRepositoryInterface;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Illuminate\View\View;
|
||||
|
||||
/**
|
||||
* Class TelemetryController
|
||||
*/
|
||||
class TelemetryController extends Controller
|
||||
{
|
||||
/** @var TelemetryRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* TelemetryController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if (false === config('firefly.feature_flags.telemetry')) {
|
||||
@ -51,37 +45,12 @@ class TelemetryController extends Controller
|
||||
function ($request, $next) {
|
||||
app('view')->share('title', (string)trans('firefly.administration'));
|
||||
app('view')->share('mainTitleIcon', 'fa-hand-spock-o');
|
||||
$this->repository = app(TelemetryRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RedirectResponse|Redirector
|
||||
*/
|
||||
public function deleteAll()
|
||||
{
|
||||
$this->repository->deleteAll();
|
||||
|
||||
session()->flash('success', trans('firefly.telemetry_all_deleted'));
|
||||
|
||||
return redirect(url()->previous(route('admin.telemetry.index')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RedirectResponse|Redirector
|
||||
*/
|
||||
public function deleteSubmitted()
|
||||
{
|
||||
$this->repository->deleteSubmitted();
|
||||
|
||||
session()->flash('success', trans('firefly.telemetry_submitted_deleted'));
|
||||
|
||||
return redirect(url()->previous(route('admin.telemetry.index')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Index
|
||||
*/
|
||||
@ -91,37 +60,7 @@ class TelemetryController extends Controller
|
||||
app('view')->share('subTitle', (string)trans('firefly.telemetry_admin_index'));
|
||||
$version = config('firefly.version');
|
||||
$enabled = config('firefly.send_telemetry', false) && config('firefly.feature_flags.telemetry');
|
||||
|
||||
$count = $this->repository->count();
|
||||
|
||||
return prefixView('admin.telemetry.index', compact('version', 'enabled', 'count'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run job to submit telemetry.
|
||||
*/
|
||||
public function submit()
|
||||
{
|
||||
$job = app(SubmitTelemetryData::class);
|
||||
$job->setDate(today(config('app.timezone')));
|
||||
$job->setForce(true);
|
||||
$job->handle();
|
||||
session()->flash('info', trans('firefly.telemetry_submission_executed'));
|
||||
|
||||
return redirect(url()->previous(route('admin.telemetry.index')));
|
||||
}
|
||||
|
||||
/**
|
||||
* View telemetry. Paginated because you never know how much will be collected.
|
||||
*
|
||||
* @return Factory|View
|
||||
*/
|
||||
public function view()
|
||||
{
|
||||
$format = trans('config.date_time');
|
||||
$size = 100;
|
||||
$records = $this->repository->paginated($size);
|
||||
|
||||
return prefixView('admin.telemetry.view', compact('records', 'format'));
|
||||
}
|
||||
}
|
||||
|
@ -1,200 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* SubmitTelemetryData.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\Jobs;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Telemetry;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Collection;
|
||||
use JsonException;
|
||||
use Log;
|
||||
use Sentry\State\Scope;
|
||||
use function Sentry\captureMessage;
|
||||
use function Sentry\configureScope;
|
||||
|
||||
/**
|
||||
* Class SubmitTelemetryData
|
||||
*/
|
||||
class SubmitTelemetryData implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
private Carbon $date;
|
||||
private bool $force;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Carbon|null $date
|
||||
*/
|
||||
public function __construct(?Carbon $date)
|
||||
{
|
||||
$this->date = $date;
|
||||
Log::debug('Created new SubmitTelemetryData');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$url = sprintf('%s/submit', config('firefly.telemetry_endpoint'));
|
||||
Log::debug(sprintf('Will submit telemetry to endpoint: %s', $url));
|
||||
|
||||
$telemetry = $this->collectTelemetry();
|
||||
|
||||
if (0 === $telemetry->count()) {
|
||||
Log::debug('Nothing to submit.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// submit to Sentry one by one:
|
||||
/** @var Telemetry $entry */
|
||||
foreach($telemetry as $entry) {
|
||||
|
||||
configureScope(function (Scope $scope) use ($entry): void {
|
||||
$scope->setContext('telemetry', [
|
||||
'installation_id' => $entry->installation_id,
|
||||
'collected_at' => $entry->created_at->format('r'),
|
||||
'type' => $entry->type,
|
||||
'key' => $entry->key,
|
||||
'value' => $entry->value,
|
||||
]);
|
||||
});
|
||||
|
||||
captureMessage('Telemetry submission');
|
||||
}
|
||||
$this->markAsSubmitted($telemetry);
|
||||
|
||||
// $json = $this->parseJson($telemetry);
|
||||
// try {
|
||||
// $body = json_encode($json, JSON_THROW_ON_ERROR);
|
||||
// } catch (JsonException $e) {
|
||||
// Log::error($e->getMessage());
|
||||
// Log::error('Could not parse JSON.');
|
||||
// throw new FireflyException(sprintf('Could not parse telemetry JSON: %s', $e->getMessage()), 0, $e);
|
||||
// }
|
||||
//
|
||||
// $client = new Client;
|
||||
// $options = [
|
||||
// 'body' => $body,
|
||||
// 'headers' => [
|
||||
// 'Content-Type' => 'application/json',
|
||||
// 'Accept' => 'application/json',
|
||||
// 'connect_timeout' => 3.14,
|
||||
// 'User-Agent' => sprintf('FireflyIII/%s', config('firefly.version')),
|
||||
// ],
|
||||
// ];
|
||||
// try {
|
||||
// $result = $client->post($url, $options);
|
||||
// } catch (GuzzleException | Exception $e) {
|
||||
// Log::error($e->getMessage());
|
||||
// Log::error($e->getTraceAsString());
|
||||
// Log::error('Could not submit telemetry.');
|
||||
// throw new FireflyException(sprintf('Could not submit telemetry: %s', $e->getMessage()), 0, $e);
|
||||
// }
|
||||
// $body = (string)$result->getBody();
|
||||
// $statusCode = $result->getStatusCode();
|
||||
// Log::info(sprintf('Result of submission [%d]: %s', $statusCode, $body));
|
||||
// if (200 === $statusCode) {
|
||||
// // mark as submitted:
|
||||
// $this->markAsSubmitted($telemetry);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
private function collectTelemetry(): Collection
|
||||
{
|
||||
$collection = Telemetry::whereNull('submitted')->take(50)->get();
|
||||
Log::debug(sprintf('Found %d entry(s) to submit', $collection->count()));
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $telemetry
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parseJson(Collection $telemetry): array
|
||||
{
|
||||
$array = [];
|
||||
/** @var Telemetry $entry */
|
||||
foreach ($telemetry as $entry) {
|
||||
$array[] = [
|
||||
'installation_id' => $entry->installation_id,
|
||||
'collected_at' => $entry->created_at->format('r'),
|
||||
'type' => $entry->type,
|
||||
'key' => $entry->key,
|
||||
'value' => $entry->value,
|
||||
];
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $telemetry
|
||||
*/
|
||||
private function markAsSubmitted(Collection $telemetry): void
|
||||
{
|
||||
$telemetry->each(
|
||||
static function (Telemetry $entry) {
|
||||
$entry->submitted = today(config('app.timezone'));
|
||||
$entry->save();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function setDate(Carbon $date): void
|
||||
{
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $force
|
||||
*/
|
||||
public function setForce(bool $force): void
|
||||
{
|
||||
$this->force = $force;
|
||||
}
|
||||
|
||||
}
|
@ -42,8 +42,6 @@ use FireflyIII\Helpers\Webhook\Sha3SignatureGenerator;
|
||||
use FireflyIII\Helpers\Webhook\SignatureGeneratorInterface;
|
||||
use FireflyIII\Repositories\ObjectGroup\ObjectGroupRepository;
|
||||
use FireflyIII\Repositories\ObjectGroup\ObjectGroupRepositoryInterface;
|
||||
use FireflyIII\Repositories\Telemetry\TelemetryRepository;
|
||||
use FireflyIII\Repositories\Telemetry\TelemetryRepositoryInterface;
|
||||
use FireflyIII\Repositories\TransactionType\TransactionTypeRepository;
|
||||
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
|
||||
use FireflyIII\Repositories\User\UserRepository;
|
||||
@ -226,7 +224,6 @@ class FireflyServiceProvider extends ServiceProvider
|
||||
$this->app->bind(ReportHelperInterface::class, ReportHelper::class);
|
||||
$this->app->bind(FiscalHelperInterface::class, FiscalHelper::class);
|
||||
$this->app->bind(UpdateRequestInterface::class, UpdateRequest::class);
|
||||
$this->app->bind(TelemetryRepositoryInterface::class, TelemetryRepository::class);
|
||||
|
||||
// webhooks:
|
||||
$this->app->bind(MessageGeneratorInterface::class, StandardMessageGenerator::class);
|
||||
|
@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* TelemetryRepository.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\Repositories\Telemetry;
|
||||
|
||||
use FireflyIII\Models\Telemetry;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Class TelemetryRepository
|
||||
*/
|
||||
class TelemetryRepository implements TelemetryRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return Telemetry::count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function deleteAll(): void
|
||||
{
|
||||
// created_at is never NULL.
|
||||
Telemetry::whereNotNull('created_at')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function deleteSubmitted(): void
|
||||
{
|
||||
// created_at is never NULL.
|
||||
Telemetry::whereNotNull('submitted')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function paginated(int $pageSize): LengthAwarePaginator
|
||||
{
|
||||
return Telemetry::orderBy('created_at', 'DESC')->paginate($pageSize);
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* TelemetryRepositoryInterface.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\Repositories\Telemetry;
|
||||
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Interface TelemetryRepositoryInterface
|
||||
*/
|
||||
interface TelemetryRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Return the number of stored telemetry records.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int;
|
||||
|
||||
/**
|
||||
* Delete all records.
|
||||
*/
|
||||
public function deleteAll(): void;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function deleteSubmitted(): void;
|
||||
|
||||
/**
|
||||
* Return paginated result of telemetry records.
|
||||
*
|
||||
* @param int $pageSize
|
||||
*
|
||||
* @return LengthAwarePaginator
|
||||
*/
|
||||
public function paginated(int $pageSize): LengthAwarePaginator;
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -37,36 +37,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
|
||||
<div class="box box-default">
|
||||
<div class="box-header with-border">
|
||||
<h3 class="box-title">{{ 'telemetry_collected_info'|_ }}</h3>
|
||||
</div>
|
||||
<div class="box-body">
|
||||
{% if count == 0 %}
|
||||
<p>
|
||||
{{ 'no_telemetry_present'|_ }}
|
||||
</p>
|
||||
{% else %}
|
||||
<p>
|
||||
{{ trans_choice('firefly.records_telemetry_present', count) }}
|
||||
</p>
|
||||
<div class="btn-group btn-group-sm">
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.view') }}" title="{{ 'telemetry_button_view'|_ }}">
|
||||
<i class="fa fa-eye"></i>
|
||||
{{ 'telemetry_button_view'|_ }}
|
||||
</a>
|
||||
<a class="btn btn-danger" href="{{ route('admin.telemetry.delete') }}" title="{{ 'telemetry_button_delete'|_ }}">
|
||||
<i class="fa fa-trash"></i>
|
||||
{{ 'telemetry_button_delete'|_ }}
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
{% block scripts %}
|
||||
{% endblock %}
|
||||
|
@ -1,101 +0,0 @@
|
||||
{% extends './v1/layout/default' %}
|
||||
|
||||
{% block breadcrumbs %}
|
||||
{{ Breadcrumbs.render }}
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
|
||||
<div class="box box-default">
|
||||
<div class="box-header with-border">
|
||||
<h3 class="box-title">{{ 'telemetry_admin_overview'|_ }}</h3>
|
||||
</div>
|
||||
<div class="box-body">
|
||||
<div class="btn-group" style="margin-bottom: 1em;">
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.index') }}"><i class="fa fa-fw fa-arrow-left"></i> {{ 'telemetry_back_to_index'|_ }}</a>
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.submit') }}"><i class="fa fa-fw fa-upload"></i> {{ 'telemetry_submit_all'|_ }}</a>
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.delete') }}"><i class="fa fa-fw fa-trash"></i> {{ 'telemetry_button_delete'|_ }}</a>
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.delete-submitted') }}"><i class="fa fa-fw fa-trash"></i> {{ 'telemetry_delete_submitted_records'|_ }}</a>
|
||||
</div>
|
||||
<div>
|
||||
{{ records.links | raw }}
|
||||
</div>
|
||||
<table class="table table-striped table-condensed">
|
||||
<tr>
|
||||
<th>{{ trans('form.collected') }}</th>
|
||||
<th>{{ trans('form.submitted') }}</th>
|
||||
<th>{{ trans('form.type') }}</th>
|
||||
<th>{{ trans('form.key') }}</th>
|
||||
<th>{{ trans('form.value') }}</th>
|
||||
</tr>
|
||||
{% for record in records %}
|
||||
<tr>
|
||||
<td style="width:15%;">
|
||||
<small>{{ record.created_at.formatLocalized(format) }}</small>
|
||||
</td>
|
||||
<td style="width:15%;">
|
||||
<small>
|
||||
{% if record.submitted %}
|
||||
{{ record.submitted.formatLocalized(format) }}
|
||||
{% else %}
|
||||
<em>{{ 'not_yet_submitted'|_ }}</em>
|
||||
{% endif %}
|
||||
</small>
|
||||
</td>
|
||||
<td style="width:15%;">
|
||||
{{ trans('firefly.telemetry_type_'~record.type) }}
|
||||
</td>
|
||||
<td style="width:15%;">
|
||||
<code>{{ record.key }}</code>
|
||||
</td>
|
||||
<td style="width:40%;">
|
||||
{% if record.value|json_encode|length > 100 %}
|
||||
<code data-state="partial" data-partial="{{ record.value|json_encode|slice(0, 30)|escape }}..."
|
||||
data-full="{{ record.value|json_encode }}|escape" class="expando">
|
||||
{{ record.value|json_encode|slice(0, 30) }}...
|
||||
</code>
|
||||
{% else %}
|
||||
<code>{{ record.value|json_encode }}</code>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<div>
|
||||
{{ records.links | raw }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="box-footer">
|
||||
<div class="btn-group">
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.index') }}"><i class="fa fa-fw fa-arrow-left"></i> {{ 'telemetry_back_to_index'|_ }}</a>
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.submit') }}"><i class="fa fa-fw fa-upload"></i> {{ 'telemetry_submit_all'|_ }}</a>
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.delete') }}"><i class="fa fa-fw fa-trash"></i> {{ 'telemetry_button_delete'|_ }}</a>
|
||||
<a class="btn btn-default" href="{{ route('admin.telemetry.delete-submitted') }}"><i class="fa fa-fw fa-trash"></i> {{ 'telemetry_delete_submitted_records'|_ }}</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
{% block styles %}
|
||||
<style type="text/css" nonce="{{ JS_NONCE }}">
|
||||
.expando {
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
{% block scripts %}
|
||||
<script type="text/javascript" nonce="{{ JS_NONCE }}">
|
||||
$('.expando').click(function (e) {
|
||||
var codeBlock = $(e.currentTarget);
|
||||
if ('partial' === codeBlock.data('state')) {
|
||||
codeBlock.text(codeBlock.data('full'));
|
||||
codeBlock.data('state', 'full');
|
||||
} else {
|
||||
codeBlock.text(codeBlock.data('partial'));
|
||||
codeBlock.data('state', 'partial');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
@ -1103,10 +1103,6 @@ Route::group(
|
||||
|
||||
// telemetry manager:
|
||||
Route::get('telemetry', ['uses' => 'TelemetryController@index', 'as' => 'telemetry.index']);
|
||||
Route::get('telemetry/view', ['uses' => 'TelemetryController@view', 'as' => 'telemetry.view']);
|
||||
Route::get('telemetry/delete', ['uses' => 'TelemetryController@deleteAll', 'as' => 'telemetry.delete']);
|
||||
Route::get('telemetry/delete-submitted', ['uses' => 'TelemetryController@deleteSubmitted', 'as' => 'telemetry.delete-submitted']);
|
||||
Route::get('telemetry/submit', ['uses' => 'TelemetryController@submit', 'as' => 'telemetry.submit']);
|
||||
|
||||
// journal links manager
|
||||
Route::get('links', ['uses' => 'LinkController@index', 'as' => 'links.index']);
|
||||
|
Loading…
Reference in New Issue
Block a user