function run_opp_compute_delta_atti($task, $args, $options) { static $loaded; // load application context if (!$loaded) { task_loader(); $loaded = true; } $data = ''; $dry_run = false; if (array_key_exists('data', $options)) { $data = $options['data']; } if (array_key_exists('dry-run', $options)) { $dry_run = true; } // definisce la data fino alla quale vanno fatti i calcoli // data_lookup serve per controllare se i record già esistono if ($data != '') { $data_lookup = $data; } else { $data_lookup = OppActHistoryCachePeer::fetchLastData(); } $msg = sprintf("start time: %s\n", date('H:i:s')); echo $msg; // calcolo date fine mese scorso e precedente list($last_month_start, $last_month_end) = Util::getLastMonthDates($data_lookup); // calcolo delta per dati di presenza, indice e ribellioni $rs = OppActHistoryCachePeer::getRSByData($data_lookup); $cnt = 0; while ($rs->next()) { $cnt++; $r = $rs->getRow(); printf("%6d) %1s %7d ... ", $cnt, $r['chi_tipo'], $r['chi_id']); // estrazione record storico del mese scorso $r_1 = OppActHistoryCachePeer::retrieveByDataChiTipoChiId($last_month_end, $r['chi_tipo'], $r['chi_id']); // salta record per cui non c'è abbastanza storia if (!$r_1 instanceof OppActHistoryCache) { printf(" NA \n"); continue; } $rilevanza_delta = indiceDelta($data_lookup, $r, $r_1); printf("d_rilevanza: %7.2f, ", $rilevanza_delta); if (!$dry_run) { $r = OppActHistoryCachePeer::retrieveByDataChiTipoChiId($data_lookup, $r['chi_tipo'], $r['chi_id']); $r->setIndiceDelta($rilevanza_delta); $r->save(); printf(" OK!\n"); } else { printf("\n"); } } echo "data: {$data_lookup}\n"; $msg = sprintf("end time: %s\n", date('H:i:s')); echo $msg; }
/** * Calcola o ri-calcola la rilevanza degli atti * Si può specificare il una data fino alla quale calcolare la rilevanza * Se sono passati degli ID (argomenti), sono interpretati come ID di atti e il calcolo è fatto solo per loro */ function run_opp_calcola_rilevanza_atti($task, $args, $options) { static $loaded; // load application context if (!$loaded) { define('SF_ROOT_DIR', sfConfig::get('sf_root_dir')); define('SF_APP', 'fe'); define('SF_ENVIRONMENT', 'task'); define('SF_DEBUG', false); require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'; sfContext::getInstance(); sfConfig::set('pake', true); error_reporting(E_ALL); $loaded = true; } echo "memory usage: " . memory_get_usage() . "\n"; $data = ''; $verbose = false; $offset = null; $limit = null; if (array_key_exists('data', $options)) { $data = $options['data']; } if (array_key_exists('verbose', $options)) { $verbose = true; } if (array_key_exists('offset', $options)) { $offset = $options['offset']; } if (array_key_exists('limit', $options)) { $limit = $options['limit']; } if ($data != '') { $legislatura_corrente = OppLegislaturaPeer::getCurrent($data); $data_lookup = $data; } else { $legislatura_corrente = OppLegislaturaPeer::getCurrent(); $data = date('Y-m-d'); $data_lookup = OppActHistoryCachePeer::fetchLastData(); } $msg = sprintf("calcolo rilevanza - fino a: %10s\n", $data); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $start_time = time(); if (count($args) > 0) { try { $atti_rs = OppAttoPeer::getRSFromIDArray($args); } catch (Exception $e) { throw new Exception("Specificare degli ID validi. \n" . $e); } } else { $atti_rs = OppAttoPeer::getAttiDataRS($data, $offset, $limit); } $n_atti = $atti_rs->getRecordCount(); echo "memory usage: " . memory_get_usage() . "\n"; $cnt = 0; while ($atti_rs->next()) { $a = $atti_rs->getRow(); $atto_id = $a['id']; $tipo_atto_id = $a['tipo_atto_id']; $atto = OppAttoPeer::retrieveByPK($atto_id); $priorita = is_null($atto->getPriorityValue()) ? 1 : $atto->getPriorityValue(); $cnt++; if (!array_key_exists($tipo_atto_id, OppTipoAttoPeer::$tipi_per_indice)) { continue; } printf("%5d/%6d) %40s %d ... ", $cnt, $n_atti, OppTipoAttoPeer::$tipi_per_indice[$tipo_atto_id], $atto_id); $indice = OppIndiceRilevanzaPeer::calcola_rilevanza_atto($atto, $tipo_atto_id, $data, $verbose); // inserimento o aggiornamento del valore in opp_politician_history_cache $cache_record = OppActHistoryCachePeer::retrieveByDataChiTipoChiId($data_lookup, 'A', $atto_id); if (!$cache_record) { $cache_record = new OppActHistoryCache(); } $cache_record->setLegislatura($legislatura_corrente); $cache_record->setChiTipo('A'); $cache_record->setChiId($atto_id); $cache_record->setTipoAttoId($tipo_atto_id); $cache_record->setIndice($indice); $cache_record->setPriorita($priorita); $cache_record->setData($data); $cache_record->setUpdatedAt(date('Y-m-d H:i')); // forza riscrittura updated_at, per tenere traccia esecuzioni $cache_record->save(); unset($cache_record); $msg = sprintf("%7.2f", $indice); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); $msg = sprintf(" [%4d sec] [%10d bytes]\n", time() - $start_time, memory_get_usage()); echo pakeColor::colorize($msg, array('fg' => 'red', 'bold' => false)); } $msg = sprintf("%d atti elaborati\n", $cnt); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); }