Skip to content

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Nov 5, 2023
2 parents 859cff8 + 0272009 commit f69d9e5
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 650 deletions.
133 changes: 33 additions & 100 deletions app/Controllers/Reference.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace GaletteTelemetry\Controllers;

use GaletteTelemetry\Models\Reference as ReferenceModel;
use GaletteTelemetry\Models\DynamicReference;
use PHPMailer\PHPMailer\PHPMailer;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
Expand All @@ -24,78 +23,46 @@ public function view(Request $request, Response $response): Response
$order_field = $_SESSION['reference']['orderby'];
$order_sort = $_SESSION['reference']['sort'];

try {
$join_table = '';
//prepare model and common queries
$ref = new ReferenceModel();
$model = $ref->newInstance();
$where = [
['is_displayed', '=', true]
];
//prepare model and common queries
$ref = new ReferenceModel();
$model = $ref->newInstance();
$where = [
['is_displayed', '=', true]
];

//check for references presence
$dyn_refs = $this->container->get('project')->getDynamicReferences();
if (false !== $dyn_refs) {
$join_table = $this->container->get('project')->getSlug() . '_reference';
$order_table = (isset($dyn_refs[$order_field]) ? $join_table : 'reference');
$order_field = $order_table . '.' . $order_field;

// retrieve data from model
$model = call_user_func_array(
[
$model,
'select'
],
array_merge(
['reference.*'],
array_map(
function ($key) use ($join_table) {
return $join_table . '.' . $key;
},
array_keys($dyn_refs)
)
)
);
$model->leftJoin($join_table, 'reference.id', '=', $join_table . '.reference_id');
}
$model = call_user_func_array(
[
$model,
'select'
],
['reference.*']
);

$current_filters = [];
if (isset($_SESSION['reference']['filters'])) {
if (!empty($_SESSION['reference']['filters']['name'])) {
$current_filters['name'] = $_SESSION['reference']['filters']['name'];
$where[] = ['name', 'like', "%{$_SESSION['reference']['filters']['name']}%"];
}
if (!empty($_SESSION['reference']['filters']['country'])) {
$current_filters['country'] = $_SESSION['reference']['filters']['country'];
$where[] = ['country', '=', strtolower($_SESSION['reference']['filters']['country'])];
}
$current_filters = [];
if (isset($_SESSION['reference']['filters'])) {
if (!empty($_SESSION['reference']['filters']['name'])) {
$current_filters['name'] = $_SESSION['reference']['filters']['name'];
$where[] = ['name', 'like', "%{$_SESSION['reference']['filters']['name']}%"];
}

$model->where($where);
if (count($where) > 1) {
//calculate filtered number of references
$current_filters['count'] = $model->count('reference.id');
if (!empty($_SESSION['reference']['filters']['country'])) {
$current_filters['country'] = $_SESSION['reference']['filters']['country'];
$where[] = ['country', '=', strtolower($_SESSION['reference']['filters']['country'])];
}
}

$model->orderBy(
$order_field,
$order_sort
);

$references = $model->paginate($_SESSION['reference']['pagination']);
} catch (\Illuminate\Database\QueryException $e) {
if ($e->getCode() == '42P01') {
//relation does not exists
throw new \RuntimeException(
'You have configured dynamic references for your project; but table ' .
$join_table . ' is missing!',
0,
$e
);
}
throw $e;
$model->where($where);
if (count($where) > 1) {
//calculate filtered number of references
$current_filters['count'] = $model->count('reference.id');
}

$model->orderBy(
'reference.' . $order_field,
$order_sort
);

$references = $model->paginate($_SESSION['reference']['pagination']);

$references->setPath($this->routeparser->urlFor('reference'));

$ref_countries = [];
Expand All @@ -116,7 +83,6 @@ function ($key) use ($join_table) {
'references' => $references,
'orderby' => $_SESSION['reference']['orderby'],
'sort' => $_SESSION['reference']['sort'],
'dyn_refs' => $dyn_refs,
'filters' => $current_filters,
'ref_countries' => $ref_countries
]
Expand All @@ -134,17 +100,6 @@ public function register(Request $request, Response $response): Response
unset($post['csrf_value']);

$ref_data = $post;
$dyn_data = [];

$dyn_ref = $this->container->get('project')->getDynamicReferences();
if (false !== $dyn_ref) {
foreach (array_keys($dyn_ref) as $ref) {
if (isset($post[$ref])) {
$dyn_data[$ref] = (int)$post[$ref];
unset($ref_data[$ref]);
}
}
}

// alter data
$ref_data['country'] = strtolower($ref_data['country']);
Expand All @@ -161,28 +116,6 @@ public function register(Request $request, Response $response): Response
);
}

if (false !== $dyn_ref) {
$dref = new DynamicReference();
$dynamics = $dref->newInstance();
$dynamics->setTable($this->container->get('project')->getSlug() . '_reference');

/** @phpstan-ignore-next-line */
$exists = $dynamics->where('reference_id', $reference['id'])->get();

if (0 === $exists->count()) {
$dyn_data['reference_id'] = $reference['id'];
/** @phpstan-ignore-next-line */
$dynamics->insert(
$dyn_data
);
} else {
/** @phpstan-ignore-next-line */
$dynamics
->where('reference_id', '=', $reference['id'])
->update($dyn_data);
}
}

// send a mail to admin
$mail = new PHPMailer;
$mail->setFrom($this->container->get('mail_from'));
Expand Down
80 changes: 39 additions & 41 deletions app/Controllers/Telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Illuminate\Database\Capsule\Manager as DB;

use GaletteTelemetry\Models\Telemetry as TelemetryModel;
use GaletteTelemetry\Models\GlpiPlugin as GlpiPluginModel;
use GaletteTelemetry\Models\Plugin as PluginModel;
use GaletteTelemetry\Models\Reference as ReferenceModel;
use GaletteTelemetry\Models\TelemetryGlpiPlugin;
use GaletteTelemetry\Models\PluginTelemetry;

class Telemetry extends ControllerAbstract
{
Expand All @@ -31,7 +31,7 @@ public function view(Request $request, Response $response): Response
'created_at',
'>=',
DB::raw("NOW() - INTERVAL '$years YEAR'")
)->count(DB::raw('DISTINCT glpi_uuid'));
)->count(DB::raw('DISTINCT instance_uuid'));
$nb_tel_entries = [
'raw' => $raw_nb_tel_entries,
'nb' => (string)$raw_nb_tel_entries
Expand Down Expand Up @@ -60,7 +60,7 @@ public function view(Request $request, Response $response): Response
// retrieve php versions -- bar
$raw_php_versions = TelemetryModel::query()->select(
DB::raw("split_part(php_version, '.', 1) || '.' || split_part(php_version, '.', 2) as version,
count(DISTINCT(glpi_uuid)) as total")
count(DISTINCT(instance_uuid)) as total")
)
->where('created_at', '>=', DB::raw("NOW() - INTERVAL '$years YEAR'"))
->groupBy(DB::raw("version"))
Expand Down Expand Up @@ -102,51 +102,51 @@ public function view(Request $request, Response $response): Response
}
$dashboard['references_countries'] = json_encode($references_countries);

// retrieve glpi versions
$glpi_versions = TelemetryModel::query()->select(
DB::raw("TRIM(trailing '-dev' FROM glpi_version) as version,
count(DISTINCT(glpi_uuid)) as total")
// retrieve galette versions
$galette_versions = TelemetryModel::query()->select(
DB::raw("TRIM(trailing '-dev' FROM galette_version) as version,
count(DISTINCT(instance_uuid)) as total")
)
->where('created_at', '>=', DB::raw("NOW() - INTERVAL '$years YEAR'"))
->groupBy('version')
->get()
->toArray();
$dashboard['glpi_versions'] = json_encode([[
$dashboard['galette_versions'] = json_encode([[
'type' => 'pie',
'hole' => .4,
'palette' => 'belize11',
'labels' => array_column($glpi_versions, 'version'),
'values' => array_column($glpi_versions, 'total')
'labels' => array_column($galette_versions, 'version'),
'values' => array_column($galette_versions, 'total')
]]);

// retrieve top 5 plugins
$top_plugins = GlpiPluginModel::query()->join(
'telemetry_glpi_plugin',
'glpi_plugin.id',
$top_plugins = PluginModel::query()->join(
'plugins_telemetry',
'plugins.id',
'=',
'telemetry_glpi_plugin.glpi_plugin_id'
'plugins_telemetry.plugin_id'
)
->select(DB::raw("glpi_plugin.pkey, count(telemetry_glpi_plugin.*) as total"))
->select(DB::raw("plugins.name, count(plugins_telemetry.*) as total"))
->where(
'telemetry_glpi_plugin.created_at',
'plugins_telemetry.created_at',
'>=',
DB::raw("NOW() - INTERVAL '$years YEAR'")
)
->orderBy('total', 'desc')
->limit(5)
->groupBy(DB::raw("glpi_plugin.pkey"))
->groupBy(DB::raw("plugins.name"))
->get()
->toArray();
$dashboard['top_plugins'] = json_encode([[
'type' => 'bar',
'marker' => ['color' => "#22727B"],
'x' => array_column($top_plugins, 'pkey'),
'x' => array_column($top_plugins, 'name'),
'y' => array_column($top_plugins, 'total')
]]);

// retrieve os
$os_family = TelemetryModel::query()->select(
DB::raw("os_family, count(DISTINCT(glpi_uuid)) as total")
DB::raw("os_family, count(DISTINCT(instance_uuid)) as total")
)
->where('created_at', '>=', DB::raw("NOW() - INTERVAL '$years YEAR'"))
->groupBy(DB::raw("os_family"))
Expand All @@ -162,17 +162,17 @@ public function view(Request $request, Response $response): Response

// retrieve languages
$languages = TelemetryModel::query()->select(
DB::raw("glpi_default_language, count(DISTINCT(glpi_uuid)) as total")
DB::raw("instance_default_language, count(DISTINCT(instance_uuid)) as total")
)
->where('created_at', '>=', DB::raw("NOW() - INTERVAL '$years YEAR'"))
->groupBy(DB::raw("glpi_default_language"))
->groupBy(DB::raw("instance_default_language"))
->get()
->toArray();
$dashboard['default_languages'] = json_encode([[
'type' => 'pie',
'hole' => .4,
'palette' => 'combo',
'labels' => array_column($languages, 'glpi_default_language'),
'labels' => array_column($languages, 'instance_default_language'),
'values' => array_column($languages, 'total')
]]);

Expand All @@ -184,7 +184,7 @@ public function view(Request $request, Response $response): Response
WHEN UPPER(db_engine) LIKE '%PERCONA%' THEN 'Percona'
ELSE 'MySQL'
END as reduced_db_engine,
count(DISTINCT(glpi_uuid)) as total")
count(DISTINCT(instance_uuid)) as total")
)
->where('created_at', '>=', DB::raw("NOW() - INTERVAL '$years YEAR'"))
->groupBy('reduced_db_engine')
Expand All @@ -200,7 +200,7 @@ public function view(Request $request, Response $response): Response

// retrieve web engine
$web_engines = TelemetryModel::query()->select(
DB::raw("web_engine, count(DISTINCT(glpi_uuid)) as total")
DB::raw("web_engine, count(DISTINCT(instance_uuid)) as total")
)
->where([
['created_at', '>=', DB::raw("NOW() - INTERVAL '$years YEAR'")],
Expand Down Expand Up @@ -262,8 +262,7 @@ public function send(Request $request, Response $response): Response

// check json structure
$project = $this->container->get('project');
$cache = $this->container->get('is_debug') ? null : $this->container->get('cache');
$schema = json_decode($project->getSchema($cache));
$schema = $project->getSchema();

$storage = new SchemaStorage();
$storage->addSchema('file://mySchema', $schema);
Expand Down Expand Up @@ -293,12 +292,12 @@ public function send(Request $request, Response $response): Response

// manage plugins
foreach ($json[$project->getSlug()]['plugins'] as $plugin) {
/** @var GlpiPluginModel $plugin_m */
$plugin_m = GlpiPluginModel::query()->firstOrCreate(['pkey' => $plugin['key']]);
/** @var PluginModel $plugin_m */
$plugin_m = PluginModel::query()->firstOrCreate(['name' => $plugin['key']]);

TelemetryGlpiPlugin::query()->create([
PluginTelemetry::query()->create([
'telemetry_entry_id' => $telemetry_m->id,
'glpi_plugin_id' => $plugin_m->id,
'plugin_id' => $plugin_m->id,
'version' => $plugin['version']
]);
}
Expand Down Expand Up @@ -357,8 +356,7 @@ public function geojson(Request $request, Response $response): Response

public function schema(Request $request, Response $response): Response
{
$cache = $this->container->get('is_debug') ? null : $this->container->get('cache');
$schema = $this->container->get('project')->getSchema($cache);
$schema = $this->container->get('project')->getSchema();
return $this->withJson($response, $schema);
}

Expand All @@ -370,20 +368,20 @@ public function allPlugins(Request $request, Response $response): Response
$years = $get['years'];
}

$top_plugins = GlpiPluginModel::query()->join(
'telemetry_glpi_plugin',
'glpi_plugin.id',
$top_plugins = PluginModel::query()->join(
'plugins_telemetry',
'plugins.id',
'=',
'telemetry_glpi_plugin.glpi_plugin_id'
'plugins_telemetry.plugin_id'
)
->selectRaw(DB::raw("glpi_plugin.pkey, count(telemetry_glpi_plugin.*) as total"))
->selectRaw(DB::raw("plugins.name, count(plugins_telemetry.*) as total"))
->where(
'telemetry_glpi_plugin.created_at',
'plugins_telemetry.created_at',
'>=',
DB::raw("NOW() - INTERVAL '$years YEAR'")
)
->orderBy('total', 'desc')
->groupBy(DB::raw("glpi_plugin.pkey"))
->groupBy(DB::raw("plugins.name"))
->limit(30)
->get()
->toArray();
Expand All @@ -394,7 +392,7 @@ public function allPlugins(Request $request, Response $response): Response
[
'type' => 'bar',
'marker' => ['color' => "#22727B"],
'x' => array_column($top_plugins, 'pkey'),
'x' => array_column($top_plugins, 'name'),
'y' => array_column($top_plugins, 'total')
]
]
Expand Down
Loading

0 comments on commit f69d9e5

Please sign in to comment.