Commit 0612c8fdcea69591559f9183f3d1e2e87b52eb80

Kano 2014-03-18T21:07:10

miner.php custom formatting and row counter '#'

diff --git a/API-README b/API-README
index 5643e80..a39d24f 100644
--- a/API-README
+++ b/API-README
@@ -1470,20 +1470,25 @@ $autorefresh = default value, 0 means dont auto-refresh
 ---------
 
 Default:
- $placebuttons = 'top';
-
-Where to place the Refresh, Summary, Custom Pages, Quit, etc. buttons
+ $miner_font_family = 'verdana,arial,sans';
+ $miner_font_size = '13pt';
 
-Valid values are: 'top' 'bot' 'both'
- anything else means don't show them - case sensitive
+Change these to set the font and font size used on the web page
 
 ---------
 
 Default:
- $miner_font_family = 'verdana,arial,sans';
- $miner_font_size = '13pt';
+ $add_css_names = array();
 
-Change these to set the font and font size used on the web page
+List of CSS names to add to the CSS style object
+	e.g. array('td.cool' => false);
+true/false to not include the default $miner_font
+The CSS name/value pairs must be defined in $colouroverride below
+
+This allows you to create multiple complete CSS styles, optionally
+using a different font to the default used/specified for all other
+styles, and then when using the class name in a custom formatting
+function (fmt) in a customsummarypage, it can use this style
 
 ---------
 
@@ -1504,6 +1509,20 @@ you could do the following:
 	'td.h background'	=> 'blue'
  );
 
+You can also add your own CSS styles to be used by a customsummarypage
+custom format function, if you specify the class name in $add_css_names
+and put the class styles in $colouroverride
+
+---------
+
+Default:
+ $placebuttons = 'top';
+
+Where to place the Refresh, Summary, Custom Pages, Quit, etc. buttons
+
+Valid values are: 'top' 'bot' 'both'
+ anything else means don't show them - case sensitive
+
 ---------
 
 Default:
@@ -1522,16 +1541,22 @@ The section defines what data you want in the summary table and the Fields
 define what data you want shown from that section
 
 Standard sections are:
- SUMMARY, POOL, PGA, GPU, NOTIFY, CONFIG, DEVDETAILS, DEVS, STATS, COIN
+ SUMMARY, POOL, PGA, GPU, NOTIFY, CONFIG, DEVDETAILS, DEVS, EDEVS, STATS,
+ ESTATS, COIN
 
 Fields are the names as shown on the headers on the normal pages
 
+There is a special field name '#' that will total to the number of rows
+displayed in the custom summary page
+In the actual row output it is a row counter per rig
+
 Fields can be 'name=new name' to display 'name' with a different heading
 'new name'
 
 There are also now joined sections:
- SUMMARY+POOL, SUMMARY+DEVS, SUMMARY+CONFIG, DEVS+NOTIFY, DEVS+DEVDETAILS
- SUMMARY+COIN
+ SUMMARY+POOL, SUMMARY+DEVS, SUMMARY+EDEVS, DEVS+STATS, EDEVS+ESTATS,
+ POOL+STATS plus many more
+See the miner.php function joinsections() for the full list
 
 These sections are an SQL join of the two sections and the fields in them
 are named section.field where section. is the section the field comes from
@@ -1648,10 +1673,10 @@ The example given:
 With cgminer 2.10.2 and later, miner.php includes an extension to
 the custom pages that allows you to apply SQL style commands to
 the data: where, group, and having
-cgminer 3.4.2 also includes another option 'gen'
+cgminer 3.4.2 and later also includes another option 'gen'
+cgminer 4.1.1 and later also includes another option 'fmt'
 
-As an example, miner.php includes a more complex custom page called 'Pools'
-this includes the extension:
+An example of an 'ext' section in a more complex custom summary page:
 
 $poolsext = array(
  'POOL+STATS' => array(
@@ -1665,8 +1690,30 @@ $poolsext = array(
                         'STATS.Times Recv' => 'sum',
                         'STATS.Bytes Recv' => 'sum'),
         'gen' => array('AvShr', 'POOL.Difficulty Accepted/max(POOL.Accepted,1)),
-        'having' => array(array('STATS.Bytes Recv', '>', 0)))
-);
+        'having' => array(array('STATS.Bytes Recv', '>', 0)),
+	'fmt' => 'myfmtfunc'));
+
+function myfmtfunc($section, $name, $value, $when, $alldata,
+		   $warnclass, $errorclass, $hiclass, $loclass, $totclass);
+{
+ $ret = '';
+ $class = '';
+ switch ($section.'.'.$name)
+ {
+ case 'GEN.AvShr':
+	$ret = number_format((float)$value, 2);
+	if ($value == 0)
+		$class = $errorclass;
+	break;
+ // Nonsence example :) since total would show the sum of the averages
+ case 'total.AvShr':
+	$ret = $value;
+	if ($value == 0)
+		$class = $warnclass;
+	break;
+ }
+ return array($ret, $class);
+}
 
 This allows you to group records together from one or more rigs
 In the example, you'll get each Pool (with the same URL+Stratum+GBT settings)
@@ -1717,6 +1764,34 @@ The first 4 are as expected - the numerical sum, average, minimum or maximum
 'any' is effectively random: the field value in the 1st row of the grouped data
 An unrecognised 'function' uses 'any'
 
+
+A 'fmt' allows you to specify a function to be called by miner.php to format
+data to be displayed in the output html
+If the function doesn't exist in miner.php or myminer.php, then it will be
+ignored
+If the function returns a $ret value (see the example 'myfmtfunc' above) then
+that will be displayed, however if $ret is empty, then the normal formatting
+code will process the data to be displayed
+Thus, if there is no formatting code in miner.php for the field value, then it
+will be displayed as it was received from the API
+i.e. this allows you to either supply some php code to format field values
+that are not formatted by miner.php, or you can also override the formatting
+done by miner.php itself for your chosen list of field data
+You can return an ' ' if you wish to force it to display as blank
+Use the example 'myfmtfunc' above as a template to write your own
+Note that your provided function will be called for all data being displayed,
+so you should use the 'case' layout as in the example to select the data fields
+you wish to format, but return '' for fields you don't wish to change the way
+they are formatted
+The 2nd return field is the name of a CSS class in $colourtable or created in
+your own $add_css_names and $colouroverride
+The value you return can stay in effect even if you return an empty $ret, if
+the default formatting function for the field doesn't set the $class variable
+The fields passed to your function by miner.php:
+ $warnclass, $errorclass, $hiclass, $loclass, $totclass
+contain the default class names used for formatting
+
+
 A 'gen' allows you to generate new fields from any php valid function of any
 of the other fields
  e.g. 'gen' => array('AvShr', 'POOL.Difficulty Accepted/max(POOL.Accepted,1)),
diff --git a/miner.php b/miner.php
index 3d9fe78..beda63f 100644
--- a/miner.php
+++ b/miner.php
@@ -11,7 +11,7 @@ global $checklastshare, $poolinputs, $hidefields;
 global $ignorerefresh, $changerefresh, $autorefresh;
 global $allowcustompages, $customsummarypages;
 global $miner_font_family, $miner_font_size;
-global $bad_font_family, $bad_font_size;
+global $bad_font_family, $bad_font_size, $add_css_names;
 global $colouroverride, $placebuttons, $userlist;
 #
 $doctype = "<!DOCTYPE html>\n";
@@ -208,6 +208,12 @@ $miner_font_size = '13pt';
 $bad_font_family = '"Times New Roman", Times, serif';
 $bad_font_size = '18pt';
 #
+# List of css names to add to the css style object
+#	e.g. array('td.cool' => false);
+# true/false to not include the default $miner_font
+# The css name/value pairs must be defined in $colouroverride below
+$add_css_names = array();
+#
 # Edit this or redefine it in myminer.php to change the colour scheme
 # See $colourtable below for the list of names
 $colouroverride = array();
@@ -314,7 +320,7 @@ function php_pr($cmd)
 function htmlhead($mcerr, $checkapi, $rig, $pg = null, $noscript = false)
 {
  global $doctype, $title, $miner_font_family, $miner_font_size;
- global $bad_font_family, $bad_font_size;
+ global $bad_font_family, $bad_font_size, $add_css_names;
  global $error, $readonly, $poolinputs, $here;
  global $ignorerefresh, $autorefresh;
 
@@ -358,8 +364,16 @@ td.sta { $miner_font ".getcss('td.sta')."}
 td.tot { $miner_font ".getcss('td.tot')."}
 td.lst { $miner_font ".getcss('td.lst')."}
 td.hi { $miner_font ".getcss('td.hi')."}
-td.lo { $miner_font ".getcss('td.lo')."}
-</style>
+td.lo { $miner_font ".getcss('td.lo')."}\n";
+ if (isset($add_css_names))
+	foreach ($add_css_names as $css_name => $no_miner_font)
+	{
+		echo "$css_name { ";
+		if ($no_miner_font !== true)
+			echo "$miner_font ";
+		echo getcss("$css_name")."}\n";
+	}
+ echo "</style>
 </head><body".getdom('body').">\n";
 if ($noscript === false)
 {
@@ -796,32 +810,46 @@ function endzero($num)
  return $rep;
 }
 #
-function fmt($section, $name, $value, $when, $alldata)
+function fmt($section, $name, $value, $when, $alldata, $cf = NULL)
 {
  global $dfmt, $rownum;
 
  if ($alldata == null)
 	$alldata = array();
 
- $errorclass = ' class=err';
- $warnclass = ' class=warn';
- $lstclass = ' class=lst';
- $hiclass = ' class=hi';
- $loclass = ' class=lo';
- $c2class = ' class=two';
- $totclass = ' class=tot';
+ $errorclass = 'err';
+ $warnclass = 'warn';
+ $lstclass = 'lst';
+ $hiclass = 'hi';
+ $loclass = 'lo';
+ $c2class = 'two';
+ $totclass = 'tot';
  $b = '&nbsp;';
 
- $ret = $value;
  $class = '';
 
  $nams = explode('.', $name);
  if (count($nams) > 1)
 	$name = $nams[count($nams)-1];
 
+ $done = false;
  if ($value === null)
+ {
 	$ret = $b;
+	$done = true;
+ }
  else
+	if ($cf != NULL and function_exists($cf))
+	{
+		list($ret, $class) = $cf($section, $name, $value, $when, $alldata,
+					   $warnclass, $errorclass, $hiclass, $loclass, $totclass);
+		if ($ret !== '')
+			$done = true;
+	}
+
+ if ($done === false)
+ {
+	$ret = $value;
 	switch ($section.'.'.$name)
 	{
 	case 'GPU.Last Share Time':
@@ -1229,6 +1257,7 @@ function fmt($section, $name, $value, $when, $alldata)
 			$ret = number_format((float)$value);
 		break;
 	}
+ }
 
  if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
 	$class = $errorclass;
@@ -1245,6 +1274,9 @@ function fmt($section, $name, $value, $when, $alldata)
  if ($ret === '')
 	$ret = $b;
 
+ if ($class !== '')
+	$class = " class=$class";
+
  return array($ret, $class);
 }
 #
@@ -2291,10 +2323,11 @@ function secmatch($section, $field)
  return false;
 }
 #
-function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total)
+function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total, $cf = NULL)
 {
  global $rigbuttons;
 
+ $rn = 0;
  foreach ($result as $sec => $row)
  {
 	$secname = preg_replace('/\d/', '', $sec);
@@ -2314,13 +2347,22 @@ function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total
 		echo rigbutton($rig, $rig, $when, $row, $rigbuttons);
 	else
 	{
-		list($ignore, $class) = fmt('total', '', '', $when, $row);
+		list($ignore, $class) = fmt('total', '', '', $when, $row, $cf);
 		echo "<td align=middle$class>$rig</td>";
 	}
 
 	foreach ($showfields as $name => $one)
 	{
-		if (isset($row[$name]))
+		if ($name === '#' and $sec != 'total')
+		{
+			$rn++;
+			$value = $rn;
+			if (isset($total[$name]))
+				$total[$name]++;
+			else
+				$total[$name] = 1;
+		}
+		elseif (isset($row[$name]))
 		{
 			$value = $row[$name];
 
@@ -2341,11 +2383,14 @@ function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total
 		}
 
 		if (strpos($secname, '+') === false)
-			list($showvalue, $class) = fmt($secname, $name, $value, $when, $row);
+			list($showvalue, $class) = fmt($secname, $name, $value, $when, $row, $cf);
 		else
 		{
-			$parts = explode('.', $name, 2);
-			list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row);
+			if ($name != '#')
+				$parts = explode('.', $name, 2);
+			else
+				$parts[0] = $parts[1] = '#';
+			list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row, $cf);
 		}
 
 		echo "<td$class align=right>$showvalue</td>";
@@ -2710,6 +2755,11 @@ function processcustompage($pagename, $sections, $sum, $ext, $namemap)
 
 		if (isset($results[$sectionmap[$section]]))
 		{
+			if (isset($ext[$section]['fmt']))
+				$cf = $ext[$section]['fmt'];
+			else
+				$cf = NULL;
+
 			$rigresults = processext($ext, $section, $results[$sectionmap[$section]], $fields);
 
 			$showfields = array();
@@ -2733,6 +2783,11 @@ function processcustompage($pagename, $sections, $sum, $ext, $namemap)
 										$showhead[$f] = 1;
 								}
 							}
+							elseif ($field === '#')
+							{
+								$showfields[$field] = 1;
+								$showhead[$field] = 1;
+							}
 							elseif (isset($row[$field]))
 							{
 								$showfields[$field] = 1;
@@ -2761,10 +2816,10 @@ function processcustompage($pagename, $sections, $sum, $ext, $namemap)
 				$add = array('total' => array());
 
 				foreach ($rigresults as $num => $result)
-					$total = customset($showfields, $sum, $section, $num, true, $result, $total);
+					$total = customset($showfields, $sum, $section, $num, true, $result, $total, $cf);
 
 				if (count($total) > 0)
-					customset($showfields, $sum, $section, '&Sigma;', false, $add, $total);
+					customset($showfields, $sum, $section, '&Sigma;', false, $add, $total, $cf);
 
 				$first = false;