/**
 * Calcola o ri-calcola la cache dell'indice di 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_build_cache_atti($task, $args, $options)
{
    static $loaded;
    // load application context
    if (!$loaded) {
        task_loader();
        $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'];
        $cnt++;
        $atto = OppAttoPeer::retrieveByPK($atto_id);
        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->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));
}