Skip to content

Add static image charts #138

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

Open
wants to merge 2 commits into
base: master
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
52 changes: 52 additions & 0 deletions classes/report_formats/ImagechartReportFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
class ImagechartReportFormat extends ReportFormatBase {
public static function display(&$report, &$request) {
if(!$report->options['has_charts']) return;

$cachekey = FileSystemCache::generateCacheKey($report->getCacheKeyParameters(),'imagecharts');

$result = FileSystemCache::retrieve($cachekey);
if(!$result) {
// Generate phantomjs command
$pwd = trim(shell_exec('pwd'));
$cmd = "phantomjs ".$pwd."/lib/phantomjs/chart.js";
$cmd .= " ".escapeshellarg(PhpReports::$request->base);
$cmd .= " ".escapeshellarg($report->report);
$cmd .= " ".escapeshellarg($_SERVER['QUERY_STRING']);

// Determine viewport width/height
$fullwidth = isset($_REQUEST['width'])? $_REQUEST['width'] : 1024;
$width = 0;
$height = 0;
foreach($report->options['Charts'] as $chart) {
if(preg_match('/\%$/',$chart['width'])) $width = max($width,substr($chart['width'],0,-1)/100*$fullwidth);
else $width = max($width,preg_replace('/^([0-9]+)([^0-9].*)?$/','$1',$chart['width']));
$height += preg_replace('/^([0-9]+)([^0-9].*)?$/','$1',$chart['height'])*1;
}
$cmd .= " ".floor($width)." ".floor($height);

// Generate temporary filename in cache directory
if(!file_exists($pwd.'/cache/phatomjs/')) mkdir($pwd.'/cache/phantomjs/',0777,true);
$filename = $pwd.'/cache/phantomjs/'.preg_replace('/\//','.',$report->report).'.'.date('Y-m-d_H-i-s').'.png';
$cmd .= " ".escapeshellarg($filename);

// Take the screenshot with phantomjs
shell_exec($cmd);

// Trim transparent border and whitespace
shell_exec('mogrify -trim +repage -trim +repage '.$filename);

// Cache result and delete image file
$result = file_get_contents($filename);
unlink($filename);
FileSystemCache::store($cachekey, base64_encode($result), 600);
}
else {
$result = base64_decode($result);
}

// Output image
header('Content-Type: image/png');
echo $result;
}
}
16 changes: 14 additions & 2 deletions lib/PhpReports/PhpReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,13 @@ public static function emailReport() {
$csv_link = str_replace('report/html/?','report/csv/?',$link);
$table_link = str_replace('report/html/?','report/table/?',$link);
$text_link = str_replace('report/html/?','report/text/?',$link);
$chart_link = str_replace('report/html/?','report/imagechart/?',$link);

// Get the CSV file attachment and the inline HTML table
// Get the CSV file attachment, the inline HTML table, and any chart images
$csv = self::urlDownload($csv_link);
$table = self::urlDownload($table_link);
$text = self::urlDownload($text_link);
$chart = self::urlDownload($chart_link);

$email_text = $body."\n\n".$text."\n\nView the report online at $link";
$email_html = "<p>$body</p>$table<p>View the report online at <a href=\"".htmlentities($link)."\">".htmlentities($link)."</a></p>";
Expand All @@ -488,14 +490,24 @@ public static function emailReport() {
->addPart($email_html, 'text/html')
;

// CSV
$attachment = Swift_Attachment::newInstance()
->setFilename('report.csv')
->setContentType('text/csv')
->setBody($csv)
;

$message->attach($attachment);

// Charts
if($chart) {
$attachment = Swift_Attachment::newInstance()
->setFilename('charts.png')
->setContentType('image/png')
->setBody($chart)
;
$message->attach($attachment);
}

// Create the Transport
$transport = self::getMailTransport();
$mailer = Swift_Mailer::newInstance($transport);
Expand Down
14 changes: 8 additions & 6 deletions lib/PhpReports/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ public function addMacro($name, $value) {
public function exportHeader($name,$params) {
$this->exported_headers[] = array('name'=>$name,'params'=>$params);
}

public function getCacheKeyParameters() {
return array(
'report'=>$this->report,
'macros'=>$this->macros,
'database'=>$this->options['Environment']
);
}
public function getCacheKey() {
return FileSystemCache::generateCacheKey(array(
'report'=>$this->report,
'macros'=>$this->macros,
'database'=>$this->options['Environment']
),'report_results');
return FileSystemCache::generateCacheKey($this->getCacheKeyParameters(),'report_results');
}
public function getReportTimesCacheKey() {
return FileSystemCache::generateCacheKey($this->report,'report_times');
Expand Down
4 changes: 3 additions & 1 deletion lib/PhpReports/ReportFormatBase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
abstract class ReportFormatBase {
abstract public static function display(&$report, &$request);
public static function display(&$report, &$request) {

}

public static function prepareReport($report) {
$environment = $_SESSION['environment'];
Expand Down
4 changes: 3 additions & 1 deletion lib/PhpReports/ReportTypeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public static function getVariableOptions($params, &$report) {
return array();
}

abstract public static function run(&$report);
public static function run(&$report) {

}
}
17 changes: 17 additions & 0 deletions lib/phantomjs/chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var page = require('webpage').create();
var args = require('system').args;

var host = args[1];
var report = args[2];
var querystring = args[3];
var width = args[4];
var height = args[5];
var filename = args[6];

var url = host.replace(/\/$/,'')+"/report/chart/?report="+report+"&"+querystring;

page.viewportSize = {width: width, height: height};
page.open(url, function() {
page.render(filename);
phantom.exit();
});