<commit_before><?php

    class Ajax extends AjaxBase {
        
        var $arg_list = array('page' => '\d+',
                              'a' => '\d+(.\d+)?',
                              'b' => '\d+(.\d+)?',
                              'c' => '\d+(.\d+)?',
                              'al' => '\d+(.\d+)?',
                              'be' => '\d+(.\d+)?',
                              'ga' => '\d+(.\d+)?',
                              'tol' => '\d+(.\d+)?',
                              'res' => '\d+(.\d+)?',
                              'sg' => '\w+',
                              'id' => '\d+',
                              'pdb' => '\w+',
                              'title' => '.*',
                              'author' => '.*',
                              'bl' => '.*',
                              'year' => '.*',
                              'iDisplayStart' => '\d+',
                              'iDisplayLength' => '\d+',
                              'iSortCol_0' => '\d+',
                              'sSortDir_0' => '\w+',
                              'sSearch' => '\w+',
                              'pp' => '\d+',
                              'page' => '\d+',
                              );
        
        var $dispatch = array('cells' => '_cells',
                              'analysed' => '_analysed',
                              'dc' => '_lookup_dc',
                              );
        
        var $def = 'cells';
        #var $profile = True;
        #var $debug = True;
        
        # ------------------------------------------------------------------------
        # List data collections / visits for a particular cell
        function _cells() {
            session_write_close();
            
            $tol = $this->has_arg('tol') ? $this->arg('tol') : 0.01;
            
            $args = array();
            foreach (array('a', 'b', 'c', 'al', 'be', 'ga') as $p) {
                if (!$this->has_arg($p)) $this->_error('One or more unit cell parameters are missing');
                
                array_push($args, $this->arg($p)*(1-$tol));
                array_push($args, $this->arg($p)*(1+$tol));
            }
            
            $tot_args = $args;
            foreach (array('a', 'b', 'c', 'al', 'be', 'ga') as $p) array_push($args, $this->arg($p));
            
            if ($this->has_arg('year')) {
                array_push($args, $this->arg('year'));
            } else {
                array_push($args, strftime('%Y-%m-%d'));
            }
            array_push($tot_args, $args[sizeof($args)-1]);
            
            $rest = '';
            $sgt = '';
            
            if ($this->has_arg('res')) {
                $res = 'AND apss.resolutionlimithigh <= :'.(sizeof($args)+1);
                array_push($args, $this->arg('res'));
                $rest = 'AND apss.resolutionlimithigh <= :'.(sizeof($tot_args)+1);
                array_push($tot_args, $this->arg('res'));
            } else $res = '';
            
            if ($this->has_arg('sg')) {
                $sg = 'AND ap.spacegroup LIKE :'.(sizeof($args)+1);
                array_push($args, $this->arg('sg'));
                $sgt = 'AND ap.spacegroup LIKE :'.(sizeof($tot_args)+1);
                array_push($tot_args, $this->arg('sg'));
            } else $sg = '';
            
            //print_r($args);
            
            $nostafft = '';
            if (!$this->staff) {
                $nostaff = "INNER JOIN investigation@<EMAIL> i ON lower(i.visit_id) = p.proposalcode || p.proposalnumber || '-' || s.visit_number INNER JOIN investigationuser@DICAT_RO iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on (u.id = iu.user_id AND u.name=:".(sizeof($args)+1).")";
                array_push($args, phpCAS::getUser());
                
                $nostafft = "INNER JOIN investigation@<EMAIL> i ON lower(i.visit_id) = p.proposalcode || p.proposalnumber || '-' || s.visit_number INNER JOIN investigationuser@<EMAIL> iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on (u.id = iu.user_id AND u.name=:".(sizeof($tot_args)+1).")";
                array_push($tot_args, phpCAS::getUser());
            } else $nostaff = '';
            
            
            $tot = $this->db->pq("SELECT count(ap.refinedcell_a) as tot FROM ispyb4a_db.autoprocintegration api INNER JOIN ispyb4a_db.autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN ispyb4a_db.autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN ispyb4a_db.autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN ispyb4a_db.autoprocscalingstatistics apss ON apss.autoprocscalingid = aph.autoprocscalingid INNER JOIN ispyb4a_db.autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid INNER JOIN ispyb4a_db.datacollection dc ON api.datacollectionid = dc.datacollectionid INNER JOIN ispyb4a_db.blsession s ON s.sessionid = dc.sessionid INNER JOIN ispyb4a_db.proposal p ON s.proposalid = p.proposalid $nostafft WHERE p.proposalcode != 'in' AND apss.scalingstatisticstype LIKE 'overall' AND (ap.refinedcell_a BETWEEN :1 AND :2) AND (ap.refinedcell_b BETWEEN :3 AND :4) AND (ap.refinedcell_c BETWEEN :5 AND :6) AND (ap.refinedcell_alpha BETWEEN :7 AND :8) AND (ap.refinedcell_beta BETWEEN :9 AND :10) AND (ap.refinedcell_gamma BETWEEN :11 AND :12) AND to_date(:13, 'YYYY-MM-DD') >= dc.starttime $rest $sgt", $tot_args);
            
            if (sizeof($tot)) $tot = $tot[0]['TOT'];
            else $tot = 0;
            
            $start = 0;
            $end = 10;
            $pp = $this->has_arg('pp') ? $this->arg('pp') : 15;
            
            if ($this->has_arg('page')) {
                $pg = $this->arg('page') - 1;
                $start = $pg*$pp;
                $end = $pg*$pp+$pp;
            }
            
            $st = sizeof($args)+1;
            $en = $st + 1;
            array_push($args, $start);
            array_push($args, $end);
            
            $pgs = intval($tot/$pp);
            if ($tot % $pp != 0) $pgs++;
            
            $rows = $this->db->pq("SELECT outer.* FROM (SELECT ROWNUM rn, inner.* FROM (SELECT sqrt(power(ap.refinedcell_a-:13,2)+power(ap.refinedcell_b-:14,2)+power(ap.refinedcell_c-:15,2)+power(ap.refinedcell_alpha-:16,2)+power(ap.refinedcell_beta-:17,2)+power(ap.refinedcell_gamma-:18,2)) as dist, s.beamlinename as bl, app.processingcommandline as type, apss.ntotalobservations as ntobs, apss.ntotaluniqueobservations as nuobs, apss.resolutionlimitlow as rlow, apss.resolutionlimithigh as rhigh, apss.scalingstatisticstype as shell, apss.rmerge, apss.completeness, apss.multiplicity, apss.meanioversigi as isigi, ap.spacegroup as sg, ap.refinedcell_a as cell_a, ap.refinedcell_b as cell_b, ap.refinedcell_c as cell_c, ap.refinedcell_alpha as cell_al, ap.refinedcell_beta as cell_be, ap.refinedcell_gamma as cell_ga, dc.datacollectionid as id, TO_CHAR(dc.starttime, 'DD-MM-YYYY HH24:MI:SS') as st, dc.imagedirectory as dir, dc.filetemplate, p.proposalcode || p.proposalnumber || '-' || s.visit_number as visit, dc.numberofimages as numimg, dc.axisrange, dc.axisstart, dc.wavelength, dc.transmission, dc.exposuretime FROM ispyb4a_db.autoprocintegration api INNER JOIN ispyb4a_db.autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN ispyb4a_db.autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN ispyb4a_db.autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN ispyb4a_db.autoprocscalingstatistics apss ON apss.autoprocscalingid = aph.autoprocscalingid INNER JOIN ispyb4a_db.autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid INNER JOIN ispyb4a_db.datacollection dc ON api.datacollectionid = dc.datacollectionid INNER JOIN ispyb4a_db.blsession s ON s.sessionid = dc.sessionid INNER JOIN ispyb4a_db.proposal p ON s.proposalid = p.proposalid $nostaff WHERE p.proposalcode != 'in' AND apss.scalingstatisticstype LIKE 'overall' AND (ap.refinedcell_a BETWEEN :1 AND :2) AND (ap.refinedcell_b BETWEEN :3 AND :4) AND (ap.refinedcell_c BETWEEN :5 AND :6) AND (ap.refinedcell_alpha BETWEEN :7 AND :8) AND (ap.refinedcell_beta BETWEEN :9 AND :10) AND (ap.refinedcell_gamma BETWEEN :11 AND :12) AND to_date(:19, 'YYYY-MM-DD') >= dc.starttime $res $sg ORDER BY dist) inner) outer WHERE outer.rn > :$st AND outer.rn <= :$en", $args);
                        
            $types = array('fast_dp' => 'Fast DP', '-3d' => 'XIA2 3d', '-3dii' => 'XIA2 3dii', '-3da ' => 'XIA2 3da', '-2da ' => 'XIA2 2da', '-2d' => 'XIA2 2d', '-2dr' => 'XIA2 2dr', '-3daii ' => 'XIA2 3daii', '-blend' => 'MultiXIA2');
                                  
            foreach ($rows as &$dc) {
                foreach ($types as $id => $name) {
                    if (strpos($dc['TYPE'], $id) !== false) {
                        $dc['TYPE'] = $name;
                        break;
                    }
                }
                
                $users = $this->db->pq("SELECT u.name,u.fullname FROM investigation<EMAIL> i INNER JOIN investigation<EMAIL> iu on i.id = iu.investigation_id INNER JOIN user_@<EMAIL> u on u.id = iu.user_id WHERE lower(i.visit_id)=:1", array($dc['VISIT']));
                
                $dc['USERS'] = array();
                foreach ($users as $u) {
                    array_push($dc['USERS'], $u['FULLNAME']);
                }
                
                $dc['DIR'] = $this->ads($dc['DIR']);
                $dc['DIR'] = substr($dc['DIR'], strpos($dc['DIR'], $dc['VISIT'])+strlen($dc['VISIT'])+1);
                
                $dc['WAVELENGTH'] = number_format($dc['WAVELENGTH'], 3);
                $dc['TRANSMISSION'] = number_format($dc['TRANSMISSION'], 3);
            }
            
            if ($this->has_arg('pdb')) {
                if (file_exists('tables/pdbs.json')) {
                    $pdbs = json_decode(file_get_contents('tables/pdbs.json'));
                } else $pdbs = new stdClass();<commit_msg>Use an actual time in cell check<commit_after><?php

    class Ajax extends AjaxBase {
        
        var $arg_list = array('page' => '\d+',
                              'a' => '\d+(.\d+)?',
                              'b' => '\d+(.\d+)?',
                              'c' => '\d+(.\d+)?',
                              'al' => '\d+(.\d+)?',
                              'be' => '\d+(.\d+)?',
                              'ga' => '\d+(.\d+)?',
                              'tol' => '\d+(.\d+)?',
                              'res' => '\d+(.\d+)?',
                              'sg' => '\w+',
                              'id' => '\d+',
                              'pdb' => '\w+',
                              'title' => '.*',
                              'author' => '.*',
                              'bl' => '.*',
                              'year' => '.*',
                              'iDisplayStart' => '\d+',
                              'iDisplayLength' => '\d+',
                              'iSortCol_0' => '\d+',
                              'sSortDir_0' => '\w+',
                              'sSearch' => '\w+',
                              'pp' => '\d+',
                              'page' => '\d+',
                              );
        
        var $dispatch = array('cells' => '_cells',
                              'analysed' => '_analysed',
                              'dc' => '_lookup_dc',
                              );
        
        var $def = 'cells';
        #var $profile = True;
        #var $debug = True;
        
        # ------------------------------------------------------------------------
        # List data collections / visits for a particular cell
        function _cells() {
            session_write_close();
            
            $tol = $this->has_arg('tol') ? $this->arg('tol') : 0.01;
            
            $args = array();
            foreach (array('a', 'b', 'c', 'al', 'be', 'ga') as $p) {
                if (!$this->has_arg($p)) $this->_error('One or more unit cell parameters are missing');
                
                array_push($args, $this->arg($p)*(1-$tol));
                array_push($args, $this->arg($p)*(1+$tol));
            }
            
            $tot_args = $args;
            foreach (array('a', 'b', 'c', 'al', 'be', 'ga') as $p) array_push($args, $this->arg($p));
            
            if ($this->has_arg('year')) {
                array_push($args, '00:01 '.$this->arg('year'));
            } else {
                array_push($args, strftime('%H:%M %Y-%m-%d'));
            }
            array_push($tot_args, $args[sizeof($args)-1]);
            
            $rest = '';
            $sgt = '';
            
            if ($this->has_arg('res')) {
                $res = 'AND apss.resolutionlimithigh <= :'.(sizeof($args)+1);
                array_push($args, $this->arg('res'));
                $rest = 'AND apss.resolutionlimithigh <= :'.(sizeof($tot_args)+1);
                array_push($tot_args, $this->arg('res'));
            } else $res = '';
            
            if ($this->has_arg('sg')) {
                $sg = 'AND ap.spacegroup LIKE :'.(sizeof($args)+1);
                array_push($args, $this->arg('sg'));
                $sgt = 'AND ap.spacegroup LIKE :'.(sizeof($tot_args)+1);
                array_push($tot_args, $this->arg('sg'));
            } else $sg = '';
            
            $nostafft = '';
            if (!$this->staff) {
                $nostaff = "INNER JOIN investigation<EMAIL> i ON lower(i.visit_id) = p.proposalcode || p.proposalnumber || '-' || s.visit_number INNER JOIN investigation<EMAIL> iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on (u.id = iu.user_id AND u.name=:".(sizeof($args)+1).")";
                array_push($args, phpCAS::getUser());
                
                $nostafft = "INNER JOIN investigation<EMAIL> i ON lower(i.visit_id) = p.proposalcode || p.proposalnumber || '-' || s.visit_number INNER JOIN investigation<EMAIL>@<EMAIL> iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on (u.id = iu.user_id AND u.name=:".(sizeof($tot_args)+1).")";
                array_push($tot_args, phpCAS::getUser());
            } else $nostaff = '';
            
            
            $tot = $this->db->pq("SELECT count(ap.refinedcell_a) as tot FROM ispyb4a_db.autoprocintegration api INNER JOIN ispyb4a_db.autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN ispyb4a_db.autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN ispyb4a_db.autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN ispyb4a_db.autoprocscalingstatistics apss ON apss.autoprocscalingid = aph.autoprocscalingid INNER JOIN ispyb4a_db.autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid INNER JOIN ispyb4a_db.datacollection dc ON api.datacollectionid = dc.datacollectionid INNER JOIN ispyb4a_db.blsession s ON s.sessionid = dc.sessionid INNER JOIN ispyb4a_db.proposal p ON s.proposalid = p.proposalid $nostafft WHERE p.proposalcode != 'in' AND apss.scalingstatisticstype LIKE 'overall' AND (ap.refinedcell_a BETWEEN :1 AND :2) AND (ap.refinedcell_b BETWEEN :3 AND :4) AND (ap.refinedcell_c BETWEEN :5 AND :6) AND (ap.refinedcell_alpha BETWEEN :7 AND :8) AND (ap.refinedcell_beta BETWEEN :9 AND :10) AND (ap.refinedcell_gamma BETWEEN :11 AND :12) AND to_date(:13, 'HH24:MI YYYY-MM-DD') >= dc.starttime $rest $sgt", $tot_args);
            
            if (sizeof($tot)) $tot = $tot[0]['TOT'];
            else $tot = 0;
            
            $start = 0;
            $end = 10;
            $pp = $this->has_arg('pp') ? $this->arg('pp') : 15;
            
            if ($this->has_arg('page')) {
                $pg = $this->arg('page') - 1;
                $start = $pg*$pp;
                $end = $pg*$pp+$pp;
            }
            
            $st = sizeof($args)+1;
            $en = $st + 1;
            array_push($args, $start);
            array_push($args, $end);
            
            $pgs = intval($tot/$pp);
            if ($tot % $pp != 0) $pgs++;
            
            $rows = $this->db->pq("SELECT outer.* FROM (SELECT ROWNUM rn, inner.* FROM (SELECT sqrt(power(ap.refinedcell_a-:13,2)+power(ap.refinedcell_b-:14,2)+power(ap.refinedcell_c-:15,2)+power(ap.refinedcell_alpha-:16,2)+power(ap.refinedcell_beta-:17,2)+power(ap.refinedcell_gamma-:18,2)) as dist, s.beamlinename as bl, app.processingcommandline as type, apss.ntotalobservations as ntobs, apss.ntotaluniqueobservations as nuobs, apss.resolutionlimitlow as rlow, apss.resolutionlimithigh as rhigh, apss.scalingstatisticstype as shell, apss.rmerge, apss.completeness, apss.multiplicity, apss.meanioversigi as isigi, ap.spacegroup as sg, ap.refinedcell_a as cell_a, ap.refinedcell_b as cell_b, ap.refinedcell_c as cell_c, ap.refinedcell_alpha as cell_al, ap.refinedcell_beta as cell_be, ap.refinedcell_gamma as cell_ga, dc.datacollectionid as id, TO_CHAR(dc.starttime, 'DD-MM-YYYY HH24:MI:SS') as st, dc.imagedirectory as dir, dc.filetemplate, p.proposalcode || p.proposalnumber || '-' || s.visit_number as visit, dc.numberofimages as numimg, dc.axisrange, dc.axisstart, dc.wavelength, dc.transmission, dc.exposuretime FROM ispyb4a_db.autoprocintegration api INNER JOIN ispyb4a_db.autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN ispyb4a_db.autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN ispyb4a_db.autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN ispyb4a_db.autoprocscalingstatistics apss ON apss.autoprocscalingid = aph.autoprocscalingid INNER JOIN ispyb4a_db.autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid INNER JOIN ispyb4a_db.datacollection dc ON api.datacollectionid = dc.datacollectionid INNER JOIN ispyb4a_db.blsession s ON s.sessionid = dc.sessionid INNER JOIN ispyb4a_db.proposal p ON s.proposalid = p.proposalid $nostaff WHERE p.proposalcode != 'in' AND apss.scalingstatisticstype LIKE 'overall' AND (ap.refinedcell_a BETWEEN :1 AND :2) AND (ap.refinedcell_b BETWEEN :3 AND :4) AND (ap.refinedcell_c BETWEEN :5 AND :6) AND (ap.refinedcell_alpha BETWEEN :7 AND :8) AND (ap.refinedcell_beta BETWEEN :9 AND :10) AND (ap.refinedcell_gamma BETWEEN :11 AND :12) AND to_date(:19, 'HH24:MI YYYY-MM-DD') >= dc.starttime $res $sg ORDER BY dist) inner) outer WHERE outer.rn > :$st AND outer.rn <= :$en", $args);
                        
            $types = array('fast_dp' => 'Fast DP', '-3d' => 'XIA2 3d', '-3dii' => 'XIA2 3dii', '-3da ' => 'XIA2 3da', '-2da ' => 'XIA2 2da', '-2d' => 'XIA2 2d', '-2dr' => 'XIA2 2dr', '-3daii ' => 'XIA2 3daii', '-blend' => 'MultiXIA2');
                                  
            foreach ($rows as &$dc) {
                foreach ($types as $id => $name) {
                    if (strpos($dc['TYPE'], $id) !== false) {
                        $dc['TYPE'] = $name;
                        break;
                    }
                }
                
                $users = $this->db->pq("SELECT u.name,u.fullname FROM <EMAIL>@<EMAIL> i INNER JOIN investigationuser@D<EMAIL>RO iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on u.id = iu.user_id WHERE lower(i.visit_id)=:1", array($dc['VISIT']));
                
                $dc['USERS'] = array();
                foreach ($users as $u) {
                    array_push($dc['USERS'], $u['FULLNAME']);
                }
                
                $dc['DIR'] = $this->ads($dc['DIR']);
                $dc['DIR'] = substr($dc['DIR'], strpos($dc['DIR'], $dc['VISIT'])+strlen($dc['VISIT'])+1);
                
                $dc['WAVELENGTH'] = number_format($dc['WAVELENGTH'], 3);
                $dc['TRANSMISSION'] = number_format($dc['TRANSMISSION'], 3);
            }
            
            if ($this->has_arg('pdb')) {
                if (file_exists('tables/pdbs.json')) {
                    $pdbs = json_decode(file_get_contents('tables/pdbs.json'));
                } else $pdbs = new stdClass();
{'fraction_non_alphanumeric': 0.16186978959904724, 'fraction_numerical': 0.012604208019055181, 'mean_word_length': 1.8170254403131116, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3811454', 'n_tokens_mistral': 6886, 'n_tokens_neox': 6547, 'n_words': 1376}