Skip to content

Commit

Permalink
add more nightly report options (#75)
Browse files Browse the repository at this point in the history
* add --report-inits option to nightly.php

* add --report-date option to nightly.php

* add --omitHeader option to nightly.php/NightlyData.php

* grunt the nightly/NightlyData updates

* add --prepend-date option to nightly.php and NightlyData.php

* add --tab option to nightly.php and NightlyData.php

* fix PHP Notice for --hide-zeros in nightly.php

* re-add missing lines from scripts.js

* re-add unmodified min.js files from master branch

* update installation.md with new report info

* add note about command-line use of nightly.php in installation.md
  • Loading branch information
Ken Irwin authored and bretdavidson committed Mar 29, 2018
1 parent 297b6c1 commit c446fe7
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 78 deletions.
62 changes: 58 additions & 4 deletions analysis/reports/lib/php/NightlyData.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
require_once 'ServerIO.php';
/**
* Class to create an hourly report on previous
Expand Down Expand Up @@ -292,6 +292,56 @@ public function eliminateLocations($table)
}
return $newTable;
}
/**
* Prepend date to each line of table; early morning hours will have a different date than the pre-midnight hours that immediate precede thm
*
* @param array of table rows, date format optional (default yyyy-mm-dd)
* @return array newTable
* @access public
*/
public function prependDate($table, $date, $format="Y-m-d")
{
$newTable = array();
$year = substr($date,0,4);
$month = substr($date,4,2);
$day = substr($date,6,2);
$use_date = strtotime($year.'-'.$month.'-'.$day);
$display_date = date($format, $use_date);

$i = 0;
$previous_hour = 0;
foreach ($table as $row)
{
$newRow = $row;
if ($i==0)
{
array_unshift($newRow, 'Date');
}
else
{
$hour = strtotime($newRow[0]);
if ($previous_hour > $hour) {
$display_date = date($format, strtotime("+1 day",$use_date));
}
array_unshift($newRow, $display_date);
$previous_hour = $hour;
}
array_push($newTable, $newRow);
$i++;
}
return $newTable;
}
/** Remove table header from the report table
* @param array table
* @return array newTable
* @access public
*/
public function omitHeader($table)
{
$newTable = $table;
array_shift($newTable);
return $newTable;
}
/** Delete a column from a multidimensional array
* @param table (passed by reference), offset of row to delete
* @return array (passed by reference, so inherently returned
Expand Down Expand Up @@ -341,7 +391,7 @@ public function hideZeroColumns($table)
foreach ($rows as $key => $value) {
if (is_numeric($value))
{
if (is_null($columnCounts[$key]))
if (! array_key_exists($key, $columnCounts))
{
$columnCounts[$key] = $value;
}
Expand Down Expand Up @@ -409,10 +459,14 @@ public function formatTable($rows, $printFormat = "text")
if ($printFormat == "html")
{
$output .= "<tr><td>" . join("</td>\n<td>", $row) . "</td></tr>" . PHP_EOL;
}
}
elseif ($printFormat == "tab")
{
$output .= join("\t",$row).PHP_EOL;
}
else
{
$output .= vsprintf($format, $row) . "\n";
$output .= vsprintf($format, $row) . PHP_EOL;
}
}
if ($printFormat == "html")
Expand Down
130 changes: 96 additions & 34 deletions analysis/reports/lib/php/nightly.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@
/* get command-line arguments if present. allowed values:
locations
--hours-across
--html
--html //if both --html and --tab are set, will default to --tab
--tab //if both --html and --tab are set, will default to --tab
--hide-zeros
--omit-header
--prepend-date
--start-hour=****
--report-inits="****","****" //use initiative names e.g. "Head Counts"
--report-date=**** //any date forma; enclose in quotes if includes spaces
*/
$locationBreakdown = (array_search("locations", $argv) ? (array_search("locations", $argv) > 0 ? true : "") : false);
$hoursAcross = (array_search("--hours-across", $argv) ? (array_search("--hours-across", $argv) > 0 ? true : "") : false);
$outputHtml = (array_search("--html", $argv) ? (array_search("--html", $argv) > 0 ? true : "") : false);
$outputTab = (array_search("--tab", $argv) ? (array_search("--tab", $argv) > 0 ? true : "") : false);
$hideZeroHours = (array_search("--hide-zeros", $argv) ? (array_search("--hide-zeros", $argv) > 0 ? true : "") : false);
$omitHeader = (array_search("--omit-header", $argv) ? (array_search("--omit-header", $argv) > 0 ? true: "") : false);
$prependDate = (array_search("--prepend-date", $argv) ? (array_search("--prepend-date", $argv) > 0 ? true: "") : false);

if ($outputTab && $outputHtml)
{
$outputHtml = false;
}
$findStartHour = preg_grep('/start-hour=\d{4}$/', $argv);
$findReportInits = preg_grep('/report-inits=.+/', $argv);
$findReportDate = preg_grep('/report-date=.+/', $argv);

if ($findStartHour)
{
Expand All @@ -27,6 +41,24 @@
$startHour = "0000";
}

if ($findReportInits)
{
$inits = array_values($findReportInits);
$pieces = explode("=", $inits[0]);
$reportInits = explode(",", $pieces[1]);
}

if ($findReportDate)
{
$date = array_values($findReportDate);
$pieces = explode("=", $date[0]);
$reportDate = $pieces[1];
}
else
{
$reportDate = 'yesterday';
}

$config = Spyc::YAMLLoad(realpath(dirname(__FILE__)) . '/../../../config/config.yaml');

if (isset($config['nightly']))
Expand All @@ -36,7 +68,7 @@
date_default_timezone_set($DEFAULT_TIMEZONE);

// Which day to retrieve hourly report
$DAY_PROCESS = date('Ymd', strtotime('yesterday'));
$DAY_PROCESS = date('Ymd', strtotime($reportDate));

// Initialize class and retrieve data
try
Expand All @@ -50,8 +82,11 @@
print '<style>';
print 'table { border-collapse: collapse;}';
print 'td,th { border: 1px solid black; text-align: center;}';
print 'tr:first-child { font-weight: bold }';
print 'td:first-child { font-weight: bold }';
if (! $omitHeader)
{
print 'tr:first-child { font-weight: bold }';
print 'td:first-child { font-weight: bold }';
}
print '</style>';
}

Expand All @@ -62,43 +97,70 @@

foreach ($nightlyData as $key => $init)
{
$table = ($data->buildLocationStatsTable($nightlyData[$key]['counts'], $key));

if (!$locationBreakdown)
{
$table = $data->eliminateLocations($table);
}

if ($hideZeroHours)
if (! isset ($reportInits) || in_array($key, $reportInits))
{
$table = $data->hideZeroHours($table);
$table = $data->hideZeroColumns($table);
}
$table = ($data->buildLocationStatsTable($nightlyData[$key]['counts'], $key));

if (!$locationBreakdown)
{
$table = $data->eliminateLocations($table);
}

if ($hoursAcross)
{
$table = $data->sideways($table);
}
if ($prependDate)
{
$table = $data->prependDate($table, $DAY_PROCESS);
}

if ($hideZeroHours)
{
$table = $data->hideZeroHours($table);
$table = $data->hideZeroColumns($table);
}

if ($outputHtml)
{
print "<h2>" . $key . "</h2>\n";
# print link to timeseries report only if analysisBaseUrl is set and there is data
if (isset($config['analysisBaseUrl']))
if ($omitHeader)
{
print "<a href='" . $config['analysisBaseUrl'] . $nightlyData[$key]['url'] . "'>Time Series Report</a>\n";
$table = $data->omitHeader($table);
}
print($data->formatTable($table, "html"));
}
else
{
print "\n" . $key . "\n";
# print link to timeseries report only if analysisBaseUrl is set there is data
if (isset($config['analysisBaseUrl']))

if ($hoursAcross)
{
$table = $data->sideways($table);
}

if ($outputHtml)
{
if (! $omitHeader)
{
print "<h2>" . $key . "</h2>\n";
}
# print link to timeseries report only if analysisBaseUrl is set and there is data
if (isset($config['analysisBaseUrl']))
{
print "<a href='" . $config['analysisBaseUrl'] . $nightlyData[$key]['url'] . "'>Time Series Report</a>\n";
}
print($data->formatTable($table, "html"));
}
else
{
print $config['analysisBaseUrl'] . $nightlyData[$key]['url'] . "\n\n";
if (! $omitHeader)
{
print "\n" . $key . "\n";
}
# print link to timeseries report only if analysisBaseUrl is set there is data
if (isset($config['analysisBaseUrl']))
{
print $config['analysisBaseUrl'] . $nightlyData[$key]['url'] . "\n\n";
}
if ($outputTab)
{
$textFormat = 'tab';
}
else
{
$textFormat = 'text';
}
print($data->formatTable($table,$textFormat));
}
print($data->formatTable($table));
}
}
}
Expand Down
62 changes: 58 additions & 4 deletions analysis/src/lib/php/NightlyData.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
require_once 'ServerIO.php';
/**
* Class to create an hourly report on previous
Expand Down Expand Up @@ -292,6 +292,56 @@ public function eliminateLocations($table)
}
return $newTable;
}
/**
* Prepend date to each line of table; early morning hours will have a different date than the pre-midnight hours that immediate precede thm
*
* @param array of table rows, date format optional (default yyyy-mm-dd)
* @return array newTable
* @access public
*/
public function prependDate($table, $date, $format="Y-m-d")
{
$newTable = array();
$year = substr($date,0,4);
$month = substr($date,4,2);
$day = substr($date,6,2);
$use_date = strtotime($year.'-'.$month.'-'.$day);
$display_date = date($format, $use_date);

$i = 0;
$previous_hour = 0;
foreach ($table as $row)
{
$newRow = $row;
if ($i==0)
{
array_unshift($newRow, 'Date');
}
else
{
$hour = strtotime($newRow[0]);
if ($previous_hour > $hour) {
$display_date = date($format, strtotime("+1 day",$use_date));
}
array_unshift($newRow, $display_date);
$previous_hour = $hour;
}
array_push($newTable, $newRow);
$i++;
}
return $newTable;
}
/** Remove table header from the report table
* @param array table
* @return array newTable
* @access public
*/
public function omitHeader($table)
{
$newTable = $table;
array_shift($newTable);
return $newTable;
}
/** Delete a column from a multidimensional array
* @param table (passed by reference), offset of row to delete
* @return array (passed by reference, so inherently returned
Expand Down Expand Up @@ -341,7 +391,7 @@ public function hideZeroColumns($table)
foreach ($rows as $key => $value) {
if (is_numeric($value))
{
if (is_null($columnCounts[$key]))
if (! array_key_exists($key, $columnCounts))
{
$columnCounts[$key] = $value;
}
Expand Down Expand Up @@ -409,10 +459,14 @@ public function formatTable($rows, $printFormat = "text")
if ($printFormat == "html")
{
$output .= "<tr><td>" . join("</td>\n<td>", $row) . "</td></tr>" . PHP_EOL;
}
}
elseif ($printFormat == "tab")
{
$output .= join("\t",$row).PHP_EOL;
}
else
{
$output .= vsprintf($format, $row) . "\n";
$output .= vsprintf($format, $row) . PHP_EOL;
}
}
if ($printFormat == "html")
Expand Down
Loading

0 comments on commit c446fe7

Please sign in to comment.