batch/scripts/scoring.php

168 lines
5.4 KiB
PHP
Raw Normal View History

<?php
/**
* Lister les événéments, table => faire une requête SQL
* Lundi au Vendredi : à H du jour, prendre de -24h à H-2
* Samedi : Reprendre les événements de la semaine, du mois ?
* Dimanche : Reprendre les événements du mois
*
* Processus :
* Une fois la liste des sirens obtenues, vérifier dans la table des surveillances l'horodatage du dernier calcul
* Si plus récent faire le calcul
* Si plus vieux suivant
*
*/
2016-02-01 20:09:11 +00:00
error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
// --- Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(__DIR__ . '/../application'));
// --- Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// --- Composer autoload
require_once realpath(__DIR__ . '/../vendor/autoload.php');
// --- Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
// --- Options
$displayUsage = false;
try {
$opts = new Zend_Console_Getopt(array(
'help|?' => "Displays usage information.",
'verbose|v' => "Mode bavard ou debug",
2016-02-01 20:09:11 +00:00
'source-s' => ".....",
'date-s' => "AAAAMMJJ, re-calcul depuis la date",
));
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
$displayUsage = true;
}
// --- Aide / Options
if (count($opts->getOptions())==0 || isset($opts->help)) {
$displayUsage = true;
}
// --- Usage
if ($displayUsage) {
echo "Scoring.\n";
echo $opts->getUsageMessage();
exit;
}
if (empty($opts->source)) {
echo date('Y-md-d H:i:s') . " - Aucune source.\n";
exit;
}
// --- Date pour la sélection
$dateEnd = new Zend_Date();
$dateBegin = new Zend_Date();
if ($opts->date) {
$dateBegin->set($opts->date, 'yyyyMMdd');
} else {
2016-02-01 20:09:11 +00:00
$dateBegin->subDay(1);
}
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
// --- Requetes de selection (toujours avec retour date AS dateEven)
$sourcesList = array(
// --- 'declencheur' => "SQL '{DATEBEGIN}' AND '{DATEEND}'",
2016-02-01 20:09:11 +00:00
'ajout' => "SELECT LPAD(insSIREN,9,0) AS siren FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND insVMAJ='C' AND insEVE='CS' AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'",
);
foreach($sourcesList as $declencheur => $sql)
{
if ($opts->source !== null && $declencheur != $opts->source) {
continue;
}
// --- Remplacement des variables dans la requete
$sql = str_replace(
array(
'{DATEBEGIN}',
'{DATEEND}',
),
array(
$dateBegin->toString('yyyyMMddHHmmss'),
$dateEnd->toString('yyyyMMddHHmmss'),
),
$sql
);
// --- Selection
try {
// --- Execution de la requete
2016-02-01 20:09:11 +00:00
$itemResult = $db->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
} catch(Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s') . " - Erreur ".$e->getMessage().".\n";
exit;
}
$total = count($itemResult);
// --- Boucle sur les entités
if ($total > 0) {
$cpt = 0;
foreach($itemResult as $item) {
$cpt++;
echo date('Y-m-d H:i:s')." - Ligne ".str_pad($cpt, strlen($total), ' ', STR_PAD_LEFT)." / ".$total;
// --- Vérification si score déjà calculé
try {
$survM = new Application_Model_JoScoresSurveillance();
2016-02-01 20:09:11 +00:00
$survSql = $survM->select()
->from($survM, array('siren', 'dateUpdate'))
->where('siren=?', $item->siren)
->group('siren');
$survResult = $survM->fetchRow($survSql);
} catch(Zend_Db_Exception $e) {
echo " : ".$item->siren." - Erreur ".$e->getMessage()."\n";
continue;
}
$toCalculate = false;
if ($survResult === null) {
$toCalculate = true;
} else {
$calculDate = new Zend_Date();
$calculDate->set($survResult->dateUpdate, 'yyyyMMddHHmmss');
$evenDate = new Zend_Date();
2016-02-01 20:09:11 +00:00
if (empty($itemResult->dateEven)) {
echo " : dateEven = NULL\n";
continue;
}
$evenDate->set($itemResult->dateEven, 'yyyyMMddHHmmss');
if ($evenDate->compare($calculDate) == 1) {
$toCalculate = true;
}
}
// --- Calcul
if ( $toCalculate ) {
require_once APPLICATION_PATH.'/configs/config.php';
require_once 'framework/fwk.php';
require_once 'framework/common/chiffres.php';
require_once 'framework/common/dates.php';
require_once 'framework/mail/sendMail.php';
require_once 'Metier/Scores/MScores.php';
2016-02-01 20:09:11 +00:00
$tabScore = calculIndiScore($item->siren, 0, false, 0, false, 'scores', $declencheur);
$indiScore = $tabScore['Indiscore'];
$encours = round($tabScore['encours']);
echo " : ".$item->siren." - Calcul $indiScore ($encours)\n";
} else {
echo " : ".$item->siren." - Pas de Calcul\n";
}
}
}
}