set Cache file and Min Max

This commit is contained in:
Michael RICOIS 2015-01-13 13:15:33 +00:00
parent 1f2e47ba42
commit be0c4cea80
2 changed files with 216 additions and 151 deletions

216
scripts/jobs/setCache.php Normal file
View File

@ -0,0 +1,216 @@
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
define('APPLICATION_ENV', 'production');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
//require_once 'Zend/Application.php';
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
),
'fallback_autoloader' => true
)
));
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Aide.",
'minmax' => "Défini les minimums et maximum",
'table' => "Créer le cache des tables",
'overwrite' => "Ecrase les fichiers présents",
'key' => "Agit uniquement sur une clé",
'verbose' => "Mode manuel",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(isset($opts->help))
{
echo $opts->getUsageMessage();
exit;
}
$config = new Zend_Config($application->getOptions());
try {
$db = Zend_Db::factory($config->profil->db->metier);
} catch ( Exception $e ) {
exit ( $e->getMessage() );
}
Zend_Db_Table::setDefaultAdapter($db);
$minmaxSqlLimit = array(
'dateCrea_ent' => array(
'dateCrea_ent>17000101',
'dateCrea_ent<DATE_FORMAT(NOW() + INTERVAL 5 YEAR, "%Y%m%d")',
),
'dateCrea_etab' => array(
'dateCrea_etab>17000101',
'dateCrea_etab<DATE_FORMAT(NOW() + INTERVAL 5 YEAR, "%Y%m%d")',
),
'nbActio' => array(),
'nbPart' => array(),
'age_etab' => array(),
'age_entrep' => array(),
'nbEtab' => array(),
'eff_entrep' => array(),
'eff_etab' => array(),
'capital' => array(),
'dateImmat' => array(
'dateImmat>17000101',
'dateImmat<DATE_FORMAT(NOW() + INTERVAL 5 YEAR, "%Y%m%d")',
),
'bilAnnee' => array(),
'bilCloture' => array(),
'bilEE' => array(),
'bilFL' => array(),
'bilFK' => array(),
'bilFR' => array(),
'bilGF' => array(),
'bilGP' => array(),
'bilGW' => array(),
'bilHD' => array(),
'bilHH' => array(),
'bilHL' => array(),
'bilHM' => array(),
'bilHN' => array(),
'bilYP' => array(),
);
echo "Début\n";
if ($opts->minmax && $opts->key === null) {
$sql = 'TRUNCATE TABLE ciblage.fields_minmax';
if ( !$db->query($sql) ) {
echo "Impossible de vider la table fields_minmax\n";
}
}
$fields = new Scores_Ciblage_FieldList();
$list = $fields->getItemsStruct();
foreach ( $list as $key => $item ) {
if ( $opts->key !== null && $opts->key != $key) continue;
// --- Gestion des tables
if ( $opts->table && array_key_exists('sql', $item) ) {
$file = APPLICATION_PATH . '/../library/Scores/Table/'.$key.'.php';
if ( $opts->overwrite && file_exists($file) ) {
unlink($file);
}
if ( file_exists($file) ) continue;
echo "Create cache for ".$key." => ".$file;
$values = $fields->getItemValues($key);
file_put_contents($file, "<?php\n");
file_put_contents($file, "return array(\n", FILE_APPEND);
foreach ( $values as $k => $v ) {
file_put_contents($file, "\t'".$k."' => \"".$v."\",\n", FILE_APPEND);
}
file_put_contents($file, ");", FILE_APPEND);
echo " - OK\n";
}
// --- Gestion des minimums et maximum
elseif ( $opts->minmax && array_key_exists('minmax', $item) ) {
if ( $opts->key ) {
$sql = "DELETE FROM ciblage.fields_minmax WHERE cle='".$key."'";
if ( !$db->query($sql) ) {
echo "Impossible de vider la table fields_minmax\n";
}
}
// --- Détermination des minimums et maximums
$sql = 'SELECT MIN('.$key.') AS min, MAX('.$key.') AS max FROM jo.etablissements_act';
$where = array();
if ( array_key_exists($key, $minmaxSqlLimit) ) {
$where = $minmaxSqlLimit[$key];
}
if ( count($where)>0 ) {
$sql.= ' WHERE ';
$i = 0;
foreach ( $where as $w ) {
if ( $i>0 ) {
$sql.= ' AND ';
}
$sql.= $w;
$i++;
}
}
if ($opts->verbose) echo $sql."\n";
try {
$stmt = $db->query($sql);
$result = $stmt->fetchObject();
$data = array(
'cle' => $key,
'min' => $result->min,
'max' => $result->max,
);
$db->insert('ciblage.fields_minmax', $data);
if ($opts->verbose) echo $key.' => min:'.$result->min.' max:'.$result->max."\n";
} catch ( Zend_Db_Exception $e ) {
if ($opts->verbose) {
echo $key." => Erreur\n";
}
continue;
}
// --- Cache fichier
$file = APPLICATION_PATH . '/../library/Scores/Table/'.$key.'.php';
if ( $opts->overwrite && file_exists($file) ) {
unlink($file);
}
if ( file_exists($file) ) continue;
echo "Create cache for ".$key." => ".$file;
$values = $fields->getItemValues($key);
file_put_contents($file, "<?php\n");
file_put_contents($file, "return array(\n", FILE_APPEND);
file_put_contents($file, "\t'min'=> ".$values['min'].",\n", FILE_APPEND);
file_put_contents($file, "\t'max'=> ".$values['max'].",\n", FILE_APPEND);
file_put_contents($file, ");", FILE_APPEND);
echo " - OK\n";
}
}
echo "Fin\n";

View File

@ -1,151 +0,0 @@
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
define('APPLICATION_ENV', 'production');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
//require_once 'Zend/Application.php';
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
),
'fallback_autoloader' => true
)
));
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Aide.",
'cron' => "Mode automatique",
'manuel' => "Mode manuel",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(isset($opts->help))
{
echo $opts->getUsageMessage();
exit;
}
$config = new Zend_Config($application->getOptions());
try {
$db = Zend_Db::factory($config->profil->db->metier);
} catch ( Exception $e ) {
exit ( $e->getMessage() );
}
$keys = array(
'dateCrea_ent' => array(
'dateCrea_ent>17000101',
'dateCrea_ent<DATE_FORMAT(NOW() + INTERVAL 5 YEAR, "%Y%m%d")',
),
'dateCrea_etab' => array(
'dateCrea_etab>17000101',
'dateCrea_etab<DATE_FORMAT(NOW() + INTERVAL 5 YEAR, "%Y%m%d")',
),
'nbActio' => array(),
'nbPart' => array(),
'age_etab' => array(),
'age_entrep' => array(),
'nbEtab' => array(),
'eff_entrep' => array(),
'eff_etab' => array(),
'capital' => array(),
'dateImmat' => array(
'dateImmat>17000101',
'dateImmat<DATE_FORMAT(NOW() + INTERVAL 5 YEAR, "%Y%m%d")',
),
'bilAnnee' => array(),
'bilCloture' => array(),
'bilEE' => array(),
'bilFL' => array(),
'bilFK' => array(),
'bilFR' => array(),
'bilGF' => array(),
'bilGP' => array(),
'bilGW' => array(),
'bilHD' => array(),
'bilHH' => array(),
'bilHL' => array(),
'bilHM' => array(),
'bilHN' => array(),
'bilYP' => array(),
);
$sql = 'TRUNCATE TABLE ciblage.fields_minmax';
if ( !$db->query($sql) ) {
die ('Impossible de vider la table fields_minmax');
}
foreach($keys as $key => $where) {
if ($opts->manuel) echo $key.PHP_EOL;
//Lecture
$sql = 'SELECT MIN('.$key.') AS min, MAX('.$key.') AS max FROM jo.etablissements_act';
if ( count($where)>0 ) {
$sql.= ' WHERE ';
$i = 0;
foreach ( $where as $w ) {
if ( $i>0 ) {
$sql.= ' AND ';
}
$sql.= $w;
$i++;
}
}
if ($opts->manuel) echo $sql.PHP_EOL;
try {
$stmt = $db->query($sql);
$result = $stmt->fetchObject();
//Insertion
$data = array(
'cle' => $key,
'min' => $result->min,
'max' => $result->max,
);
$db->insert('ciblage.fields_minmax', $data);
if ($opts->manuel) echo $key.' => min:'.$result->min.' max:'.$result->max."\n";
} catch ( Zend_Db_Exception $e ) {
if ($opts->manuel) echo $key." => Erreur\n";
}
}
if ($opts->manuel) echo "Fin\n";