From c446fe7d37ab26b2d3888880e48807b21db15d27 Mon Sep 17 00:00:00 2001 From: Ken Irwin Date: Thu, 29 Mar 2018 09:42:47 -0400 Subject: [PATCH] add more nightly report options (#75) * 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 --- analysis/reports/lib/php/NightlyData.php | 62 ++++++++++- analysis/reports/lib/php/nightly.php | 130 +++++++++++++++++------ analysis/src/lib/php/NightlyData.php | 62 ++++++++++- analysis/src/lib/php/nightly.php | 130 +++++++++++++++++------ docs/docs/installation.md | 14 ++- 5 files changed, 320 insertions(+), 78 deletions(-) diff --git a/analysis/reports/lib/php/NightlyData.php b/analysis/reports/lib/php/NightlyData.php index 461075b07..d9d427bc7 100644 --- a/analysis/reports/lib/php/NightlyData.php +++ b/analysis/reports/lib/php/NightlyData.php @@ -1,4 +1,4 @@ - $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 @@ -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; } @@ -409,10 +459,14 @@ public function formatTable($rows, $printFormat = "text") if ($printFormat == "html") { $output .= "" . join("\n", $row) . "" . 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") diff --git a/analysis/reports/lib/php/nightly.php b/analysis/reports/lib/php/nightly.php index 6ba8ac1b2..10c25f59f 100644 --- a/analysis/reports/lib/php/nightly.php +++ b/analysis/reports/lib/php/nightly.php @@ -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) { @@ -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'])) @@ -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 @@ -50,8 +82,11 @@ print ''; } @@ -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 "

" . $key . "

\n"; - # print link to timeseries report only if analysisBaseUrl is set and there is data - if (isset($config['analysisBaseUrl'])) + if ($omitHeader) { - print "Time Series Report\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 "

" . $key . "

\n"; + } + # print link to timeseries report only if analysisBaseUrl is set and there is data + if (isset($config['analysisBaseUrl'])) + { + print "Time Series Report\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)); } } } diff --git a/analysis/src/lib/php/NightlyData.php b/analysis/src/lib/php/NightlyData.php index 461075b07..d9d427bc7 100644 --- a/analysis/src/lib/php/NightlyData.php +++ b/analysis/src/lib/php/NightlyData.php @@ -1,4 +1,4 @@ - $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 @@ -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; } @@ -409,10 +459,14 @@ public function formatTable($rows, $printFormat = "text") if ($printFormat == "html") { $output .= "" . join("\n", $row) . "" . 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") diff --git a/analysis/src/lib/php/nightly.php b/analysis/src/lib/php/nightly.php index 6ba8ac1b2..10c25f59f 100644 --- a/analysis/src/lib/php/nightly.php +++ b/analysis/src/lib/php/nightly.php @@ -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) { @@ -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'])) @@ -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 @@ -50,8 +82,11 @@ print ''; } @@ -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 "

" . $key . "

\n"; - # print link to timeseries report only if analysisBaseUrl is set and there is data - if (isset($config['analysisBaseUrl'])) + if ($omitHeader) { - print "Time Series Report\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 "

" . $key . "

\n"; + } + # print link to timeseries report only if analysisBaseUrl is set and there is data + if (isset($config['analysisBaseUrl'])) + { + print "Time Series Report\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)); } } } diff --git a/docs/docs/installation.md b/docs/docs/installation.md index cd9cbb603..6a0f8e0e7 100644 --- a/docs/docs/installation.md +++ b/docs/docs/installation.md @@ -185,6 +185,12 @@ Suma Analysis Tools Configuration * **--hide-zeros**: do not display data for hours or locations with no activity * **--hours-across**: display hours from left-to-right instead of the default top-to-bottom in the report * **--html**: formats the report as in HTML rather than plain text (strongly recommended for use when using the 'hours-across' and/or 'locations' options + * **--tab**: formats the report as tab-delimited text. If both --html and --tab flags are set, the --html flag will be ignored and the --tab flag will be respected + * **--omit-header**: do not display column or section headers in report; only display hourly counts; best when used for only one initiative at a time, to avoid confusion +* **--prepend-date**: include a column giving the report date at the beginning of each line; ideal for use with the --omit-header flag +* **--report-inits**: limit reporting to one or more initiative(s); identify the initiative(s) by name, comma separated, e.g.: **--report-inits="Head Counts"*** or **--report-inits="Reference Transactions","Head Counts"** +* **--report-date**: select the date for which to report; default is "yesterday"; any machine readable date format is acceptable, e.g. **--report-date=2018-03-11** or **--report-date="March 11, 2018"** +* **--start-hour**: choose the hour with which a reporting period starts. Default is 0000 hours. Examples of nightlyEmail.php configuration include: @@ -194,9 +200,13 @@ Examples of nightlyEmail.php configuration include: `YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightlyEmail.php --hide-zeros` -`YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightlyEmail.php --hours-across --hide-zeros --html` +`YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightlyEmail.php --hours-across --hide-zeros --html --start-hour=0400` -Alternatively, `YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightly.php` may be run from the command line for quick reporting through stdout. +`YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightlyEmail.php --tab --omit-header --prepend-date --report-date=2018-03-11 --report-inits="Head Counts"` + +Alternatively, `YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightly.php` may be run from the command line for quick reporting through stdout, using the same flags, e.g.: +`php YOUR_WEB_DIR/suma/analysis/reports/lib/php/nightly.php --tab --omit-header --prepend-date --report-date=2018-03-11 --report-inits="Head Counts"` +This approach is especially useful if you want to use a shell script to report on multiple consecutive dates. Other Things You Can Configure -------------------------------