Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

WIP New devices stats report #316

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion ajax/getGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@
case 'devicesPerOSVersion':
echo $graph->showDevicesPerOSVersion();
break;
}
case 'agentOnlineStats':
echo $graph->showAgentOnlineStats();
break;
}
156 changes: 155 additions & 1 deletion inc/graph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,158 @@ private function checkEmptyLabels(&$labels) {
}
}
}
}

public function showAgentOnlineStats() {
global $DB;

$domain = $range = $serie = [];
$FlyvemdmAgent = PluginFlyvemdmAgent::getTable();
$logTable = Log::getTable();
$query = "SELECT l.`new_value` AS online_date, f.`name` AS agent_name, f.`id` AS agent_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you introduce queries un the plugin, use the query builder. RAW SQL queries is deprecated.

http://glpi-developer-documentation.readthedocs.io/en/master/devapi/database/index.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done it on new commit

FROM " . $logTable . " AS l
INNER JOIN " . $FlyvemdmAgent . " AS f ON l.`items_id` = f.`id`
WHERE l.`itemtype`='PluginFlyvemdmAgent' AND l.`id_search_option`='8'
ORDER BY agent_id ASC, online_date ASC";
$request = [
'FIELDS' => [$logTable => ['new_value'], $FlyvemdmAgent => ['id', 'name']],
'FROM' => $logTable,
'INNER JOIN' => [
$FlyvemdmAgent => [
'FKEY' => [
$FlyvemdmAgent => 'id',
$logTable => 'items_id',
],
],
],
'WHERE' => ['itemtype' => 'PluginFlyvemdmAgent', 'id_search_option' => 8],
'ORDER' => ['id ASC', 'new_value ASC'],
];
$currentId = $currentDate = null;
foreach ($DB->request($request) as $data) {
list($date, $time) = explode(' ', $data['new_value']);
//$domain[] = $date;
//$serie[] = strtotime($time);
if ($currentId != $data['id']) {
$currentId = $data['id'];
$range[$currentId]['name'] = $data['name'] . ' (ID:' . $currentId . ')';
$range[$currentId]['data'] = [];
}
$jsTime = strtotime($data['new_value']) * 1000;
$range[$currentId]['data'][] = "{x:$jsTime, y:$jsTime}";
}
//$domain = array_unique($domain);

$out = $this->displayLineGraph(
__('Online Agents', 'flyvemdm'),
$domain,
$range,
[
'width' => '100%',
],
false
);

echo $out;
echo '<script type="text/javascript" src="/lib/jqueryplugins/fullcalendar/lib/moment.min.js"></script>'; //TODO: fix this bad call
}

/**
* Display stacked bar graph
*
* @param string $title Graph title
* @param string[] $labels Labels to display
* @param array $series Series data. An array of the form:
* [
* ['name' => 'a name', 'data' => []],
* ['name' => 'another name', 'data' => []]
* ]
* @param string[] $options array of options
* @param boolean $display Whether to display directly; defauts to true
*
* @return void
*/
public function displayLineGraph($title, $labels, $series, $options = null, $display = true) {
$param = [
'width' => 900,
'height' => 400,
'tooltip' => true,
'legend' => true,
'animate' => false,
];

if (is_array($options) && count($options)) {
foreach ($options as $key => $val) {
$param[$key] = $val;
}
}

$first = true;
$serieString = '';
foreach ($series as $serie) {
if ($first === true) {
$first = false;
} else {
$serieString .= ",\n";
}
if (isset($serie['name'])) {
$serieString .= "{name: '{$serie['name']}', data: [" . implode(', ',
$serie['data']) . "]}";
} else {
$serieString .= "[" . implode(', ', $serie['data']) . "]";
}
}

$this->checkEmptyLabels($labels);
$jsData = "{
/*labels: ['" . implode('\', \'', Toolbox::addslashes_deep($labels)) . "'],*/
series: [" . $serieString . "]
}";

$jsOptions = "{
width: '{$param['width']}',
height: '{$param['height']}',
showLine: false,
fullWidth: true,
axisX: {
labelInterpolationFnc: function(value, index) {
return moment(value).format('L');
}
},
axisY: {
type: Chartist.FixedScaleAxis,
labelInterpolationFnc: function(value, index) {
return moment(value).format('LT');
}
}
";
if ($param['legend'] === true || $param['tooltip'] === true) {
$jsOptions .= ", plugins: [";
if ($param['legend'] === true) {
$jsOptions .= "Chartist.plugins.legend()";
}
if ($param['tooltip'] === true) {
$jsOptions .= ($param['legend'] === true ? ',' : '') . "Chartist.plugins.tooltip()";
}
$jsOptions .= "]";
}
$jsOptions .= "}";

$slug = str_replace('-', '_', Toolbox::slugify($title));
$out = "<h2 class='center'>$title</h2>";
$out .= "<div id='$slug' class='chart'></div>";
$out .= "<script type='text/javascript'>
$(function() {
var chart_$slug = new Chartist.Line('#$slug', " . $jsData . ", " . $jsOptions . ");";

if ($param['animate'] === true) {
$out .= "";
}
$out .= "});</script>";

if ($display) {
echo $out;
return;
}
return $out;
}
}
6 changes: 6 additions & 0 deletions tpl/menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@
});
</script>
<div id="plugin_flyvemdm_graph_devicesPerOSVersion"></div>
<script type="text/javascript">
$(function() {
$('#plugin_flyvemdm_graph_agentOnlineStats').load('../ajax/getGraph.php', {'graph': 'agentOnlineStats'})
});
</script>
<div id="plugin_flyvemdm_graph_agentOnlineStats"></div>
</div>
{% endif %}