Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature: generate usage report #112

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/df.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@
// list of allowed lookup modifying functions like urlencode, trim, etc.
'allowed_modifiers' => explode(',', env('DF_LOOKUP_MODIFIERS',
'strtoupper,strtolower,ucfirst,lcfirst,ucwords,urlencode,urldecode,rawurlencode,rawurldecode,base64_encode,base64_decode,trim')),
]
],
'generate_report' => env('DF_GENERATE_REPORT', false),
];
4 changes: 3 additions & 1 deletion routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
Route::prefix(config('df.api_route_prefix', 'api'))
->middleware('df.api')
->group(function () {
Route::post('report', [\DreamFactory\Core\Http\Controllers\ReportController::class, 'generate'])->name('report:generate');

$versionPattern = 'v[0-9.]+';
$servicePattern = '[_0-9a-zA-Z-.]+';
$resourcePathPattern = '[0-9a-zA-ZÀ-ÿ-_@&\#\!=,:;\/\^\$\.\|\{\}\[\]\(\)\*\+\?\' ]+';
Expand All @@ -29,7 +31,7 @@
['service' => $servicePattern, 'resource' => $resourcePathPattern]
);
}
);
);

/*
|--------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions src/Events/GenerateReportEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DreamFactory\Core\Events;

class GenerateReportEvent extends Event
{
public function __construct($name)
{
parent::__construct($name);
}

}
71 changes: 71 additions & 0 deletions src/Handlers/Events/GenerateReportHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace DreamFactory\Core\Handlers\Events;

use DreamFactory\Core\Utility\Environment as EnvUtilities;
use DreamFactory\Core\Utility\Session as SessionUtilities;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GenerateReportHandler
{
public function handle()
{
$result = [];
$result['platform'] = [
'version' => \Config::get('app.version'),
'bitnami_demo' => EnvUtilities::isDemoApplication(),
'is_hosted' => to_bool(env('DF_MANAGED', false)),
'is_trial' => to_bool(env('DF_IS_TRIAL', false)),
];

// administrator-only information
$dbDriver = \Config::get('database.default');
$result['platform']['db_driver'] = $dbDriver;
if ($dbDriver === 'sqlite') {
$result['platform']['sqlite_storage'] = \Config::get('df.db.sqlite_storage');
}
$result['platform']['install_path'] = base_path() . DIRECTORY_SEPARATOR;
$result['platform']['log_path'] = env('DF_MANAGED_LOG_PATH',
storage_path('logs')) . DIRECTORY_SEPARATOR;
$result['platform']['app_debug'] = env('APP_DEBUG', false);
$result['platform']['log_mode'] = \Config::get('logging.log');
$result['platform']['log_level'] = \Config::get('logging.log_level');
$result['platform']['cache_driver'] = \Config::get('cache.default');

if ($result['platform']['cache_driver'] === 'file') {
$result['platform']['cache_path'] = \Config::get('cache.stores.file.path') . DIRECTORY_SEPARATOR;
}

// including information that helps users use the API or debug
$result['server'] = php_uname();

/*
* Most API calls return a resource array or a single resource,
* If an array, shall we wrap it?, With what shall we wrap it?
*/
$result['config'] = [
'always_wrap_resources' => \Config::get('df.always_wrap_resources'),
'resources_wrapper' => \Config::get('df.resources_wrapper'),
'db' => [
/** The default number of records to return at once for database queries */
'max_records_returned' => \Config::get('database.max_records_returned'),
'time_format' => \Config::get('df.db.time_format'),
'date_format' => \Config::get('df.db.date_format'),
'datetime_format' => \Config::get('df.db.datetime_format'),
'timestamp_format' => \Config::get('df.db.timestamp_format'),
],
];

$connectors = DB::table('service')->whereNotNull('created_by_id')
->get(['name', 'type', 'created_date', 'last_modified_date']);
$result['connectors'] = $connectors->toArray();
// $result['php'] = EnvUtilities::getPhpInfo();
// // Remove environment variables being kicked back to the client
// unset($result['php']['environment']);
// unset($result['php']['php_variables']);


Log::channel('report')->debug('environment', $result);
}
}
7 changes: 7 additions & 0 deletions src/Handlers/Events/ServiceEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DreamFactory\Core\Events\ApiEvent;
use DreamFactory\Core\Events\BaseServiceEvent;
use DreamFactory\Core\Events\GenerateReportEvent;
use DreamFactory\Core\Events\ServiceDeletedEvent;
use DreamFactory\Core\Events\ServiceAssignedEvent;
use DreamFactory\Core\Events\ServiceModifiedEvent;
Expand Down Expand Up @@ -48,6 +49,12 @@ public function subscribe($events)
static::class . '@handleQueryExecutedEvent'
);
}
if (config('df.generate_report')) {
$events->listen(
[GenerateReportEvent::class],
GenerateReportHandler::class
);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DreamFactory\Core\Http\Controllers;

use DreamFactory\Core\Components\DfResponse;
use DreamFactory\Core\Events\GenerateReportEvent;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class ReportController extends Controller
{
public function generate()
{
$message = 'Option is not active! Please enable it first';

if (config('df.generate_report')) {
Event::dispatch(GenerateReportEvent::class);
$message = 'Generating report! Please check log folder in after a few minutes';

}

return DfResponse::create($message);
}
}