コード例 #1
0
 /**
  * esporta csv con dati storici della rilevanza dei tag, con limite custom
  *
  * @return void
  * @author Guglielmo Celata
  */
 public function executeRilevanzaTagExport()
 {
     sfLoader::loadHelpers(array('Number'));
     $this->session = $this->getUser();
     $this->forward404Unless($this->session->hasCredential('adhoc'));
     $data_inizio = OppLegislaturaPeer::$legislature[OppLegislaturaPeer::getCurrent()]['data_inizio'];
     $data_fine = OppTagHistoryCachePeer::fetchLastData();
     $date = Util::buildCacheDatesArray($data_inizio, $data_fine);
     $limit = $this->getRequestParameter('limit', 50);
     $c = new Criteria();
     $c->addJoin(TagPeer::ID, OppTagHistoryCachePeer::CHI_ID);
     $c->add(OppTagHistoryCachePeer::CHI_TIPO, 'S');
     $c->add(OppTagHistoryCachePeer::DATA, $data_fine);
     $c->addDescendingOrderByColumn(OppTagHistoryCachePeer::INDICE);
     $c->setLimit($limit);
     $tags = TagPeer::doSelect($c);
     $csv_rows = array();
     // costruzione delle serie storiche per ogni politico (da inizio legislatura)
     foreach ($tags as $cnt => $tag) {
         $tag_id = $tag->getId();
         // genero il label
         $label = sprintf("%s(%s)", $tag->getTripleValue(), $tag->getTripleNamespace());
         // arrotonda l'array dei valori storici a due cifre decimali (max)
         $storico = OppTagHistoryCachePeer::getHistory('S', $tag->getId());
         // primi campi (dati fissi)
         $csv_row = "{$tag_id},\"{$label}\",";
         foreach ($date as $cnt => $data) {
             if (array_key_exists($data, $storico)) {
                 $storico[$data] = format_number(round($storico[$data], 2), 'it_IT');
             } else {
                 $storico[$data] = 0;
             }
             $csv_row .= '"' . $storico[$data] . '"';
             if ($cnt < count($date) - 1) {
                 $csv_row .= ",";
             }
         }
         $csv_rows[] = $csv_row;
     }
     $this->csv_header = "tag_id,tag(namespace)," . implode(",", $date);
     $this->csv_rows = $csv_rows;
     $this->setLayout(false);
     $this->response->setContentType('text/plain');
 }
コード例 #2
0
function run_opp_compute_delta_tag($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 = OppTagHistoryCachePeer::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 = OppTagHistoryCachePeer::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 = OppTagHistoryCachePeer::retrieveByDataChiTipoChiId($last_month_end, $r['chi_tipo'], $r['chi_id']);
        // salta record per cui non c'è abbastanza storia
        if (!$r_1 instanceof OppTagHistoryCache) {
            printf(" NA \n");
            continue;
        }
        $rilevanza_delta = indiceDelta($data_lookup, $r, $r_1);
        printf("d_rilevanza: %7.2f,  ", $rilevanza_delta);
        if (!$dry_run) {
            $r = OppTagHistoryCachePeer::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;
}
コード例 #3
0
/**
 * Calcola o ri-calcola la rilevanza degli argomenti, come somma della rilevanza degli atti taggati
 * Si può specificare il una data fino alla quale calcolare la rilevanza
 * Se sono passati degli ID (argomenti), sono interpretati come ID di argomenti e il calcolo è fatto solo per loro
 */
function run_opp_calcola_rilevanza_tag($task, $args, $options)
{
    static $loaded;
    // load application context
    if (!$loaded) {
        _loader();
    }
    echo "memory usage: " . memory_get_usage() . "\n";
    $data = '';
    $verbose = false;
    $offset = null;
    $limit = null;
    $start_time = time();
    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 = OppTagHistoryCachePeer::fetchLastData();
    }
    $msg = sprintf("calcolo rilevanza tag - fino a: %10s\n", $data);
    echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true));
    if (count($args) > 0) {
        $tags_ids = $args;
    } else {
        $tags_ids = TaggingPeer::getActiveTagsIds('OppAtto');
    }
    $n_tags = count($tags_ids);
    echo "memory usage: " . memory_get_usage() . "\n";
    foreach ($tags_ids as $cnt => $tag_id) {
        printf("%5d/%6d) %40s %6d ... ", $cnt + 1, $n_tags, TagPeer::retrieveByPK($tag_id), $tag_id);
        $indice = OppIndiceRilevanzaPeer::calcola_rilevanza_tag($tag_id, $data, $verbose);
        // inserimento o aggiornamento del valore in opp_tag_history_cache
        $cache_record = OppTagHistoryCachePeer::retrieveByDataChiTipoChiId($data_lookup, 'S', $tag_id);
        if (!$cache_record) {
            $cache_record = new OppTagHistoryCache();
        }
        $cache_record->setLegislatura($legislatura_corrente);
        $cache_record->setChiTipo('S');
        $cache_record->setChiId($tag_id);
        $cache_record->setIndice($indice);
        $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 tag elaborati\n", $cnt + 1);
    echo pakeColor::colorize($msg, array('fg' => 'cyan', 'bold' => true));
}