Browse Source

php - add a simple shift perfomance graph

master
kanoi 10 years ago
parent
commit
e355d93bed
  1. 54
      html/can.js
  2. 55
      pool/db.php
  3. 8
      pool/page_blocks.php
  4. 26
      pool/page_usperf.php
  5. 1
      pool/prime.php

54
html/can.js

@ -0,0 +1,54 @@
function hasCan(){var c=document.getElementById('can');return !!(c.getContext&&c.getContext('2d'));}
function sep(d){ans={};var ar=d.split("\t");var l=ar.length;for(var i=0;i<l;i++){var e=ar[i].indexOf('=');ans[ar[i].substr(0,e)]=ar[i].substr(e+1)};return ans}
function gch(z,zm,zo){if(z<0){return 0}if(z>(zm-1)){return(zm-1)}return z}
function gchx(c,x){return gch(x*c['xm']+c['xo'],c['ctx'].canvas.width)}
function gchy(c,y){return gch((1-y)*c['ym']+c['yo'],c['ctx'].canvas.height)}
function gx0(c){return -c['xo']/c['xm']};
function gy0(c){return -c['yo']/c['ym']};
function gto(c,xo,yo){c['xo']+=xo;c['yo']+=yo}
function gts(c,xs,ys){c['xm']*=xs;c['ym']*=ys}
function gtso(c,xs,ys){gto(c,c['xm']*(1.0-xs)/2.0,c['ym']*(1.0-ys)/2.0);gts(c,xs,ys)}
function gfs(c,bg){c['ctx'].fillStyle=bg}
function gss(c,fg){c['ctx'].strokeStyle=fg}
function glw(c,pct){c['ctx'].lineWidth=pct*c['ym']/100.0}
function glwr(c,rat){c['ctx'].lineWidth*=rat}
function gfz(c,x,y,ox,oy,t,co,a){gfs(c,co);c['ctx'].textAlign=a;c['ctx'].fillText(t,gchx(c,x)+ox,gchy(c,y)-oy)}
function gbe(c,x,y){c['ctx'].beginPath();c['ctx'].moveTo(gchx(c,x),gchy(c,y))}
function gln(c,x,y){c['ctx'].lineTo(gchx(c,x),gchy(c,y))}
function glm(c,x,y){c['ctx'].moveTo(gchx(c,x),gchy(c,y))}
function gle(c){c['ctx'].closePath()}
function gfl(c){c['ctx'].fill()}
function gst(c){c['ctx'].stroke()}
function gfi(c){gle(c);gst(c)}
function gbd(c){gbe(c,0,0);gln(c,1,0);gln(c,1,1);gln(c,0,1);gle(c);gfl(c);gst(c)}
function ggr(c,xs,ys,yt,xn,x0,x1,y0,y1,ar,nx,vx,vy,av){
gtso(c,xs,ys);
gss(c,'black');glw(c,0.25);
gbe(c,0,1);gln(c,0,0);gln(c,1,0);gst(c);
glwr(c,0.1);
var hi=c['ctx'].measureText('M').width;
var wi=c['ctx'].measureText('1').width;
for(var i=0;i<11;i++){var y=i/10.0;gbe(c,-0.01,y);gln(c,1,y);gst(c);var t=''+(((y1-y0)*i/10+y0).toFixed(2));gfz(c,0,y,-wi,0,t,'black','end')}
gfz(c,gx0(c),0.55,wi,0,yt,'#0080ff','left');
var m=Math.round(0.5+xn/20.0);
var f=1;
for(var i=0;i<xn;i++){var n=ar[nx+i];var x=ar[vx+i];var xo=(x-x0)/(x1-x0);if((i<(xn-1))&&(i%m)==0){gbe(c,xo,0);gln(c,xo,-0.01);gst(c);gfz(c,xo,0,0,-hi*1.5,n,'#00a050','center')}}
glw(c,0.1);
for(var i=0;i<xn;i++){var x=ar[vx+i];var y=ar[vy+i];var xo=(x-x0)/(x1-x0);var yo=(y-y0)/(y1-y0);if(f==1){gbe(c,xo,yo);f=0}else{gln(c,xo,yo)}}gst(c);
glw(c,0.2);
gss(c,'red');
var y=(av-y0)/(y1-y0);
gbe(c,0,y);gln(c,1,y);gst(c);
}
function sn(i,shi){if(shi.indexOf(' Shift ')<0){return ''+(i%10)}else{return shi.replace(/.* ([a-z])[a-z]*$/,'$1')}}
function gc(c){c['can']=document.getElementById('can');c['ctx']=c['can'].getContext('2d');c['wx']=window.innerWidth;c['wy']=window.innerHeight;c['xm']=c['wx']*0.9;c['ym']=c['wy']*0.8;if(c['ym']>c['xm']){c['ym']=c['xm']}c['xo']=0.0;c['yo']=0.0;c['ctx'].canvas.width=c['xm'];c['ctx'].canvas.height=c['ym']}
function gdrw(d){var c={};gc(c);
gfs(c,'white');gss(c,'black');glw(c,1);gbd(c);
var rows=d['rows'];var ymin=-1;var ymax=0;var xmin=-1;var xmax=0;
var tda=0;
for(var i=0;i<rows;i++){var s=parseFloat(d['start:'+i]);var e=parseFloat(d['end:'+i]);var da=parseFloat(d['diffacc:'+i]);tda+=da;var ths=(da/(e-s))*Math.pow(2,32)/Math.pow(10,12);d['ths:'+i]=ths;if(ymin==-1||ymin>ths){ymin=ths}if(ths>ymax)ymax=ths;d['nx:'+i]=sn(i,d['shift:'+i]);if(xmin==-1||xmin>s){xmin=s}if(xmax<e){xmax=e}d['vx:'+i]=(s+e)/2.0};
var tav=(tda/(xmax-xmin))*Math.pow(2,32)/Math.pow(10,12);
var p5=(ymax-ymin)*0.05;ymax+=p5;ymin-=p5;if(ymin<0){ymin=0}
ggr(c,0.8,0.9,'THs',rows,xmin,xmax,0,ymax,d,'nx:','vx:','ths:',tav);
}
function dodrw(d){if(hasCan()){gdrw(sep(d))}}

55
pool/db.php

@ -5,13 +5,20 @@ include_once('base.php');
#
# List of db functions to call and get the results back from ckdb
# From homeInfo() and the rest after that
# The result is an array of all ckdb result field names and their values
# The repDecode() result is an array of all ckdb result field names and
# their values
# Also included:
# ['ID'] the id sent
# ['STAMP'] the ckdb reply timestamp
# ['STATUS'] the ckdb reply status (!'ok' = error)
# ['ERROR'] if status not 'ok' the error message reply
# ['ERROR'] if status != 'ok', the error message reply
# The reply is false if the ckdb return data was corrupt
# The repData() result is:
# ['ID'] the id sent
# ['STAMP'] the ckdb reply timestamp
# ['STATUS'] the ckdb reply status (!'ok' = error)
# ['DATA'] the rest of the ckdb reply, or '' on error
# ['ERROR'] if status not 'ok', the error message reply
#
global $send_sep, $fld_sep, $val_sep;
$send_sep = '.';
@ -65,6 +72,38 @@ function repDecode($rep)
return $ans;
}
#
function repData($rep)
{
global $send_sep;
$fix = preg_replace("/[\n\r]*$/",'',$rep);
$major = explode($send_sep, $fix, 4);
if (count($major) < 3)
return false;
$ans = array();
$ans['ID'] = $major[0];
$ans['STAMP'] = $major[1];
$ans['STATUS'] = $major[2];
$ans['DATA'] = '';
if ($major[2] == 'ok')
{
$ans['ERROR'] = null;
if (isset($major[3]))
$ans['DATA'] = $major[3];
}
else
{
if (isset($major[3]))
$ans['ERROR'] = $major[3];
else
$ans['ERROR'] = 'system error';
}
return $ans;
}
#
# Convenience function
function zeip()
{
@ -282,6 +321,18 @@ function getShifts($user)
return repDecode($rep);
}
#
function getShiftData($user)
{
if ($user == false)
showIndex();
$flds = array('username' => $user);
$msg = msgEncode('shifts', 'shift', $flds, $user);
$rep = sendsockreply('getShifts', $msg);
if (!$rep)
dbdown();
return repData($rep);
}
#
function getBlocks($user)
{
if ($user == false)

8
pool/page_blocks.php

@ -182,11 +182,15 @@ function doblocks($data, $user)
if ($stat == 'Orphan')
$stara = '<span class=st1>*</span>';
$statsconf = $ans['statsconf:'.$i];
if ($statsconf == 'Y')
if (isset($ans['statsconf:'.$i]))
{
if ($ans['statsconf:'.$i] == 'Y')
$approx = '';
else
$approx = '~';
}
else
$approx = '';
$diffacc = $ans['diffacc:'.$i];
$acc = number_format($diffacc, 0);

26
pool/page_usperf.php

@ -0,0 +1,26 @@
<?php
#
function dousperf($data, $user)
{
$ans = getShiftData($user);
$pg = '<h1>User Shift Performance</h1><br>';
if ($ans['STATUS'] == 'ok' and $ans['DATA'] != '')
{
$pg .= "<canvas id=can width=1 height=1>";
$pg .= "A graph will show here if your browser supports html5/canvas";
$pg .= "</canvas>\n";
$data = str_replace(array("\\","'"), array("\\\\","\\'"), $ans['DATA']);
$pg .= "<script src='/can.js'></script>\n";
$pg .= "<script type='text/javascript'>dodrw('$data');</script>\n";
}
return $pg;
}
#
function show_usperf($info, $page, $menu, $name, $user)
{
gopage($info, NULL, 'dousperf', $page, $menu, $name, $user);
}
#
?>

1
pool/prime.php

@ -77,6 +77,7 @@ function check()
),
'Workers' => array(
'Shifts' => 'shifts',
'Shift Graph' => 'usperf',
'Workers' => 'workers',
'Management' => 'workmgt',
),

Loading…
Cancel
Save