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

Usage Graphs refactor implementation #128

Open
wants to merge 16 commits into
base: usagegraphs_refactor_implementation
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="dashboardCategory col-sm-6">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h3 class="dashboardCategoryLabel">{$statusStats.label} <a href="/MaterialsRequest/Graph?status={$statusStats.id}&location={$library}" title="{translate text="Show Graph" inAttribute="true" isAdminFacing=true}"> <i class="fas fa-chart-line"></i></a></h3> {* No translation needed *}
<h3 class="dashboardCategoryLabel">{$statusStats.label} <a href="/MaterialsRequest/UsageGraphs?stat={$statusStats.id}&location={$library}" title="{translate text="Show Graph" inAttribute="true" isAdminFacing=true}"> <i class="fas fa-chart-line"></i></a></h3> {* No translation needed *}
</div>
</div>
<div class="row">
Expand Down
Empty file.
98 changes: 11 additions & 87 deletions code/web/services/API/UsageGraphs.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
<?php

require_once ROOT_DIR . '/services/Admin/Admin.php';
require_once ROOT_DIR . '/services/Admin/AbstractUsageGraphs.php';
require_once ROOT_DIR . '/sys/SystemLogging/APIUsage.php';
require_once ROOT_DIR . '/sys/Utils/GraphingUtils.php';

class API_UsageGraphs extends Admin_Admin
{
function launch()
{
global $interface;

$stat = $_REQUEST['stat'];
if (!empty($_REQUEST['instance'])) {
$instanceName = $_REQUEST['instance'];
} else {
$instanceName = '';
}

$title = 'Aspen Discovery API Usage Graph';
$interface->assign('section', 'API');
$interface->assign('showCSVExportButton', true);
$interface->assign('graphTitle', $title);
$this->assignGraphSpecificTitle($stat);
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
$interface->assign('stat', $stat);
$interface->assign('propName', 'exportToCSV');
$title = $interface->getVariable('graphTitle');
$this->display('../Admin/usage-graph.tpl', $title);
class API_UsageGraphs extends Admin_AbstractUsageGraphs {
function launch(): void {
$this->launchGraph('API');
}
function getBreadcrumbs(): array
{

function getBreadcrumbs(): array {
$breadcrumbs = [];
$breadcrumbs[] = new Breadcrumb('/Admin/Home', 'Administration Home');
$breadcrumbs[] = new Breadcrumb('/Admin/Home#system_reports', 'System Reports');
Expand All @@ -37,63 +18,11 @@ function getBreadcrumbs(): array
return $breadcrumbs;
}

function getActiveAdminSection(): string
{
function getActiveAdminSection(): string {
return 'system_reports';
}

function canView(): bool
{
return UserAccount::userHasPermission([
'View Dashboards',
'View System Reports',
]);
}

// note that this will only handle tables with one stat (as is needed for Summon usage data)
// to see a version that handle multpile stats, see the Admin/UsageGraphs.php implementation
public function buildCSV() {
global $interface;
$stat = $_REQUEST['stat'];
if (!empty($_REQUEST['instance'])) {
$instanceName = $_REQUEST['instance'];
} else {
$instanceName = '';
}
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
$dataSeries = $interface->getVariable('dataSeries');

$filename = "AspenAPIUsageData_{$stat}.csv";
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment;filename={$filename}");
$fp = fopen('php://output', 'w');

// builds the first row of the table in the CSV - column headers: Dates, and the title of the graph
fputcsv($fp, ['Dates', $stat]);

// builds each subsequent data row - aka the column value
foreach ($dataSeries as $dataSerie) {
$data = $dataSerie['data'];
$numRows = count($data);
$dates = array_keys($data);

if( empty($numRows)) {
fputcsv($fp, ['no data found!']);
}
for($i = 0; $i < $numRows; $i++) {
$date = $dates[$i];
$value = $data[$date];
$row = [$date, $value];
fputcsv($fp, $row);
}
}
exit();
}
private function getAndSetInterfaceDataSeries($stat, $instanceName) {
protected function getAndSetInterfaceDataSeries($stat, $instanceName): void {
global $interface;

$dataSeries = [];
Expand All @@ -108,11 +37,7 @@ private function getAndSetInterfaceDataSeries($stat, $instanceName) {
$usage->selectAdd('year');
$usage->selectAdd('month');
$usage->orderBy('year, month');
$dataSeries[$stat] = [
'borderColor' => 'rgba(255, 99, 132, 1)',
'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
'data' => [],
];
$dataSeries[$stat] = GraphingUtils::getDataSeriesArray(count($dataSeries));
$usage->selectAdd('SUM(numCalls) as numCalls');

//Collect results
Expand All @@ -130,8 +55,7 @@ private function getAndSetInterfaceDataSeries($stat, $instanceName) {
$interface->assign('translateColumnLabels', false);
}

private function assignGraphSpecificTitle($stat)
{
protected function assignGraphSpecificTitle($stat): void {
global $interface;
$title = 'Aspen Discovery API Usage Graph';
$title .= " - $stat";
Expand Down
1 change: 1 addition & 0 deletions code/web/services/Admin/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -1679,5 +1679,6 @@ public function exportUsageData() {
require_once ROOT_DIR . '/services/Admin/UsageGraphs.php';
$aspenUsageGraph = new Admin_UsageGraphs();
$aspenUsageGraph->buildCSV();

}
}
105 changes: 105 additions & 0 deletions code/web/services/Admin/AbstractUsageGraphs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
require_once ROOT_DIR . '/services/Admin/Admin.php';
abstract class Admin_AbstractUsageGraphs extends Admin_Admin {

// method specific enough to be worth writing an implementation for per section
abstract function getBreadcrumbs(): array;
abstract function getActiveAdminSection(): string;
abstract protected function assignGraphSpecificTitle(string $stat): void;
abstract protected function getAndSetInterfaceDataSeries(string $stat, string $instanceName): void;

// methods shared amongst all usagegraph classes
protected function launchGraph(string $sectionName): void {
global $interface;

$stat = $_REQUEST['stat'];
if (!empty($_REQUEST['instance'])) {
$instanceName = $_REQUEST['instance'];
} else {
$instanceName = '';
}

// includes dashboard subsection name in title if relevant
$subSectionName = $_REQUEST['subSection'];
$sectionTitle = $sectionName;
if (!empty($subSectionName)) {
$sectionTitle .= ': ' . $subSectionName;
}
$sectionTitle .= ' Usage Graph';

$interface->assign('stat', $stat);
$interface->assign('section', $sectionName);
$interface->assign('subSection', $subSectionName);
$interface->assign('graphTitle', $sectionTitle);
$interface->assign('showCSVExportButton', true);
$interface->assign('propName', 'exportToCSV');

$this->assignGraphSpecificTitle($stat);
$this->getAndSetInterfaceDataSeries($stat, $instanceName);

$graphTitle = $interface->getVariable('graphTitle');
$this->display('../Admin/usage-graph.tpl', $graphTitle);
}

public function canView(): bool {
return UserAccount::userHasPermission([
'View Dashboards',
'View System Reports',
]);
}

public function buildCSV(string $section): void {
global $interface;

$stat = $_REQUEST['stat'];
if (!empty($_REQUEST['instance'])) {
$instanceName = $_REQUEST['instance'];
} else {
$instanceName = '';
}
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
$dataSeries = $interface->getVariable('dataSeries');

// ensures csv filename contains dashboard subsection name if relevant
$subSectionName = str_replace(' ', '_', $_REQUEST['subSection']);
$filename = $section . '_';
if (!empty($subSectionName)) {
$filename .= $subSectionName . '_';
}
$filename .= 'UsageData_' . $stat . '.csv';

// sets up the csv file
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment;filename={$filename}");
$fp = fopen('php://output', 'w');

// builds the file content
$graphTitles = array_keys($dataSeries);
$numGraphTitles = count($dataSeries);

for($i = 0; $i < $numGraphTitles; $i++) {
// builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph
$dataSerie = $dataSeries[$graphTitles[$i]];
$numRows = count($dataSerie['data']);
$dates = array_keys($dataSerie['data']);
$header = ['Dates', $graphTitles[$i]];
fputcsv($fp, $header);

// builds each subsequent data row - aka the column value
if (empty($numRows)) {
fputcsv($fp, ['no data found']);
}
for($j = 0; $j < $numRows; $j++) {
$date = $dates[$j];
$value = $dataSerie['data'][$date];
$row = [$date, $value];
fputcsv($fp, $row);
}
}
exit();
}
}
Loading
Loading