public function executeGetLastDateForPoliticianHistoryCache() { $ramo = $this->getRequestParameter('ramo', null); if (!is_null($ramo)) { $this->forward404Unless(in_array(strtoupper($ramo), array('C', 'S'))); } $last_date = OppPoliticianHistoryCachePeer::fetchLastData('P', $ramo); $this->_send_json_output(json_encode(array('last_date' => $last_date))); return sfView::NONE; }
/** * tabella indici e presenze * * @return void * @author Guglielmo Celata */ public function executeIndicePresenze() { sfLoader::loadHelpers(array('Number')); $this->session = $this->getUser(); $this->forward404Unless($this->session->hasCredential('adhoc')); $limit = $this->getRequestParameter('limit', 50); $last_date = OppPoliticianHistoryCachePeer::fetchLastData(); /* deppFiltersAndSortVariablesManager::resetVars($this->session, 'action', 'storici_action', array('sf_admin/opp_storici/filter', 'sf_admin/opp_storici/sort')); */ // estrae tutte le date per cui esistono dati di tipo P (presenze) $this->all_dates = OppPoliticianHistoryCachePeer::extractDates('P'); // reset dei filtri, se richiesto esplicitamente if ($this->getRequestParameter('reset_filters', 'false') == 'true') { $this->getRequest()->getParameterHolder()->set('filter_ramo', '0'); $this->getRequest()->getParameterHolder()->set('filter_data', $last_date); } // se si arriva dalla rule parlamentari_nuovo_indice, è specificato il ramo if ($this->hasRequestParameter('ramo')) { $ramo = $this->getRequestParameter('ramo'); if ($ramo == 'camera') { $ramo = 'c'; } elseif ($ramo == 'senato') { $ramo = 's'; } $this->session->setAttribute('ramo', $ramo, 'sf_admin/opp_storici/filter'); } $this->processFilters(array('ramo', 'data'), $last_date); $this->date = $this->filters['data']; // if all filters were reset, then restart if ($this->getRequestParameter('filter_ramo') == '0' && $this->getRequestParameter('filter_data') == $last_date) { $this->redirect('datiStorici/indicePresenze'); } $this->processListSort('indice', 'presenze'); if ($this->hasRequestParameter('itemsperpage')) { $this->getUser()->setAttribute('itemsperpage', $this->getRequestParameter('itemsperpage')); } $itemsperpage = $this->getUser()->getAttribute('itemsperpage', sfConfig::get('app_pagination_limit')); $this->pager = new sfPropelPager('OppPoliticianHistoryCache', $itemsperpage); $c = new Criteria(); $this->addFiltersCriteria($c); $this->addListSortCriteria($c); $c->addDescendingOrderByColumn(OppPoliticianHistoryCachePeer::CHI_ID); $c->add(OppPoliticianHistoryCachePeer::CHI_TIPO, 'P'); $this->pager->setCriteria($c); $this->pager->setPage($this->getRequestParameter('page', 1)); $this->pager->setPeerMethod('doSelect'); $this->pager->init(); }
/** * API (protetta da una API key) * torna flusso xml di tutti i parlamentari (divisi per ramo) * per ciascun parlamentare, visualizza i dati anagrafici e di carica * nonché informazioni variabili, a seconda del parametro info_type * - presenze: presenze, assenza, missione * - attivita: indice di attività parlamentare (nuovo) * - ribelli: numero di voti ribelli * progetto op_kw * * <opkw xmlns="http://www.openpolis.it/2010/opkw" * xmlns:op="http://www.openpolis.it/2010/op" * xmlns:op_location="http://www.openpolis.it/2010/op_location" * xmlns:op_politician="http://www.openpolis.it/2010/op_politician" * xmlns:xlink="http://www.w3.org/1999/xlink"> * <op:content> * <camera n_rappresentanti="600"> * <parlamentare xlink:href="/parlamentari/12345.xml"> * <thumbnail xlink:href="/parlamentari/thumb/332104.jpeg" width="40" height="53"/> * <nome>NOME</nome> * <cognome>COGNOME</cognome> * <gruppo> * <nome>NOME GRUPPO</nome> * <acronimo>NOME GRUPPO</acronimo> * </gruppo> * <data_inizio_carica>2008-04-12</data_inizio_carica> ** solo se successiva a data inizio leg. * <circoscrizione>Lombardia 1</circoscrizione> * * ...* informazioni *... * * </parlamentare> * ... * </camera> * <senato> * <parlamentare> ... </parlamentare> * </camera> * </op:content> * </opkw> * * Informazioni possono essere: * <presenze> * <numero>3898</numero> * <percentuale>84,59</percentuale> * </presenze> * <assenze> * <numero>710</numero> * <percentuale>15,41</percentuale> * </assenze> * <missioni> * <numero>0</numero> * <percentuale>0,00</percentuale> * </missioni> * <mese_precedente>+1</mese_precedente> * * <indice> * <numero>389,8</numero> * </indice> * <mese_precedente>+-1</mese_precedente> * * <voti_ribelli> * <numero>15</numero> * </indice> * <mese_precedente>-1</mese_precedente> * * Return error in case something's wrong * <opkw xlmns="http://www.openpolis.it/2010/opkw" * xmlns:op="http://www.openpolis.it/2010/op" * xlmns:op_location="http://www.openpolis.it/2010/op_location" * xmlns:op_politician="http://www.openpolis.it/2010/op_politician"> * <op:error>Messaggio di errore</op:error> * </opkw> * @return String * @author Guglielmo Celata **/ public function executeElencoParlamentari() { $key = $this->getRequestParameter('key'); $ramo = $this->getRequestParameter('ramo'); $infotype = $this->getRequestParameter('infotype'); $order_by = $this->getRequestParameter('orderby'); $order_type = $this->getRequestParameter('ordertype'); $con = Propel::getConnection('propel'); $current_data = OppPoliticianHistoryCachePeer::fetchLastData('P', null, $con); $is_valid_key = deppApiKeysPeer::isValidKey($key); $resp_node = new SimpleXMLElement('<opkw xmlns="' . $this->opkw_ns . '" ' . ' xmlns:op="' . $this->op_ns . '" ' . ' xmlns:xlink="' . $this->xlink_ns . '" >' . '</opkw>'); if ($is_valid_key) { // start producing xml $content_node = $resp_node->addChild('op:content', null, $this->op_ns); // camera o senato if ($ramo == 'tutti') { $rami = array('camera' => 'C', 'senato' => 'S'); } else { $rami = array($ramo => strtoupper(substr($ramo, 0, 1))); } foreach ($rami as $ramo_name => $ramo_code) { $ramo_node = $content_node->addChild($ramo_name, null, $this->opkw_ns); // dato storico aggregato per tutto il ramo $ramo_storico_node = $ramo_node->addChild('ramo', null, $this->opkw_ns); $rs = OppPoliticianHistoryCachePeer::getKWRamoRS($ramo_code, $con); $cnt = 0; while ($rs->next()) { $cnt++; $r = $rs->getRow(); $this->addRamoNode($ramo_storico_node, $r, $infotype); } // gruppi $gruppi_node = $ramo_node->addChild('gruppi', null, $this->opkw_ns); $n_gruppi = OppPoliticianHistoryCachePeer::countByDataRamoChiTipo($current_data, $ramo_code, 'G', $con); $rs = OppPoliticianHistoryCachePeer::getKWGruppoRSByDataRamo($current_data, $ramo_code, null, null, $con); $cnt = 0; while ($rs->next()) { $cnt++; $g = $rs->getRow(); $this->addGruppoNode($gruppi_node, $g, $infotype, $cnt); } $gruppi_node->addAttribute('n_gruppi', $n_gruppi); // parlamentari $parlamentari_node = $ramo_node->addChild('parlamentari', null, $this->opkw_ns); $n_parlamentari = OppPoliticianHistoryCachePeer::countByDataRamoChiTipo($current_data, $ramo_code, 'P', $con); $rs = OppPoliticianHistoryCachePeer::getKWPoliticiRSByDataRamo($current_data, $ramo_code, $order_by, $order_type, $con); $cnt = 0; while ($rs->next()) { $cnt++; $p = $rs->getRow(); $this->addParlamentareNode($parlamentari_node, $p, $infotype, $cnt); } $parlamentari_node->addAttribute('n_rappresentanti', $n_parlamentari); } } else { $resp_node->addChild('op:error', 'Chiave di accesso non valida', $this->op_ns); } $xmlContent = $resp_node->asXML(); $this->_send_output($xmlContent); return sfView::NONE; }
function run_opp_compute_delta_politici($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 = OppPoliticianHistoryCachePeer::fetchLastData(); } $msg = sprintf("start time: %s\n", date('H:i:s')); echo $msg; // calcolo date inizio e fine mese scorso list($last_month_start, $last_month_end) = Util::getLastMonthDates($data_lookup); // calcolo delta per dati di presenza, indice e ribellioni $rs = OppPoliticianHistoryCachePeer::getRSByDataRamoChiTipo($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 alla fine del mese scorso $r_1 = OppPoliticianHistoryCachePeer::retrieveByDataChiTipoChiIdRamo($last_month_end, $r['chi_tipo'], $r['chi_id'], $r['ramo']); // salta record per cui non c'è abbastanza storia if (!$r_1 instanceof OppPoliticianHistoryCache) { printf(" NA \n"); continue; } list($presenze_delta, $assenze_delta, $missioni_delta) = presenzeDelta($data_lookup, $r, $r_1); printf("presenze: %7.2f%%, assenze: %7.2f%%, missioni: %7.2f%%, ", $presenze_delta, $assenze_delta, $missioni_delta); $indice_delta = indiceDelta($data_lookup, $r, $r_1); printf("indice: %8.4f, ", $indice_delta); $ribellioni_delta = ribellioniDelta($data_lookup, $r, $r_1); printf("ribellioni: %7.2f%%", $ribellioni_delta); if (!$dry_run) { $r = OppPoliticianHistoryCachePeer::retrieveByDataChiTipoChiIdRamo($data_lookup, $r['chi_tipo'], $r['chi_id'], $r['ramo']); $r->setPresenzeDelta($presenze_delta); $r->setAssenzeDelta($assenze_delta); $r->setMissioniDelta($missioni_delta); $r->setIndiceDelta($indice_delta); $r->setRibellioniDelta($ribellioni_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 l'indice di attività. * Si può specificare il ramo (camera, senato, governo, tutti) e la data * Se sono passati degli ID (argomenti), sono interpretati come ID di politici e il calcolo è fatto solo per loro */ function run_opp_calcola_nuovo_indice($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"; $start_time = time(); $data = ''; $ramo = ''; $verbose = false; $offset = null; $limit = null; if (array_key_exists('data', $options)) { $data = $options['data']; } if (array_key_exists('ramo', $options)) { $ramo = strtolower($options['ramo']); } 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']; } $atti_ids = array(); if (array_key_exists('atti', $options)) { $atti_ids = explode(",", $options['atti']); } $emendamenti_ids = array(); if (array_key_exists('emendamenti', $options)) { $emendamenti_ids = explode(",", $options['emendamenti']); } // definisce la data fino alla quale vanno fatti i calcoli // data_lookup serve per controllare se i record già esistono if ($data != '') { $legislatura_corrente = OppLegislaturaPeer::getCurrent($data); $data_lookup = $data; } else { $legislatura_corrente = OppLegislaturaPeer::getCurrent(); $data = date('Y-m-d'); $data_lookup = OppPoliticianHistoryCachePeer::fetchLastData('N'); } $msg = sprintf("calcolo indice di attività - data: %10s, ramo: %10s\n", $data ? $data : '-', $ramo); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); if (count($args) > 0) { try { $parlamentari_rs = OppCaricaPeer::getRSFromIDArray($args); } catch (Exception $e) { throw new Exception("Specificare degli ID validi. \n" . $e); } } else { $parlamentari_rs = OppCaricaPeer::getParlamentariRamoDataRS($ramo, $legislatura_corrente, $data, $offset, $limit); } echo "memory usage: " . memory_get_usage() . "\n"; $cnt = 0; while ($parlamentari_rs->next()) { $cnt++; $p = $parlamentari_rs->getRow(); $nome = $p['nome']; $cognome = $p['cognome']; $tipo_carica_id = $p['tipo_carica_id']; $id = $p['id']; switch ($tipo_carica_id) { case 1: $ramo = 'C'; $prefisso = 'On.'; break; case 4: case 5: $ramo = 'S'; $prefisso = 'Sen.'; break; case 2: case 3: case 6: case 7: $ramo = 'G'; $prefisso = ''; break; default: break; } $politico_stringa = sprintf("%s %s %s", $prefisso, $nome, strtoupper($cognome)); printf("%4d) %40s [%06d] ... ", $cnt, $politico_stringa, $id); $indice = OppIndiceAttivitaPeer::calcola_indice_politico($id, $legislatura_corrente, $data, $verbose, $atti_ids, $emendamenti_ids); // inserimento o aggiornamento del valore in opp_politician_history_cache // uso N come ChiTipo, perché P è preso per il vecchio indice. // una volta fatta la sostituzione, la scrittura qui è ridondante $cache_record = OppPoliticianHistoryCachePeer::retrieveByDataChiTipoChiIdRamo($data_lookup, 'N', $id, $ramo); if ($cache_record === null) { $cache_record = new OppPoliticianHistoryCache(); } $cache_record->setLegislatura($legislatura_corrente); $cache_record->setChiTipo('N'); $cache_record->setChiId($id); $cache_record->setRamo($ramo); $cache_record->setIndice($indice); $cache_record->setData($data); $cache_record->setNumero(1); $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 parlamentari elaborati\n", $cnt); echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true)); }