error_log('record_region_interest (f): ' . date('d m Y H:i:s'));
}
/***** END - INTEREST *****/
/***** COMPORTAMIENTO *****/
if ($_SERVER['argv'][1] == 'region_comportamiento') {
    $query = 'SELECT count(*) cantidad FROM `facebook_mobile_os_city_3_1`;';
    $result = db_query($query, array());
    if ($fila = mysql_fetch_assoc($result)) {
        $total_comportamiento = $fila['cantidad'];
    }
    $query = 'SELECT id_region id, region_key code, id_country FROM `facebook_region_3_1` WHERE id_region NOT IN(' . get_data_for_sql_in('comportamiento', $total_comportamiento) . ') AND active_fb_get_data = 1 ORDER BY 1;';
    $que = db_query($query, array());
    $count = 1;
    error_log('record_region_comportamiento (i): ' . date('d m Y H:i:s'));
    while ($region = mysql_fetch_assoc($que)) {
        $query = "SELECT id_comportamiento id, key_comportamiento 'key' FROM `facebook_comportamiento_3_1` WHERE id_comportamiento IN(SELECT id_comportamiento FROM facebook_mobile_os_city_3_1) AND active_fb_get_data = 1 AND id_comportamiento NOT IN(" . get_data_id_secundary_for_sql_in($region['id'], 'comportamiento') . ") ORDER BY 1;";
        $result = db_query($query, array());
        $comportamientos = array();
        while ($fila = mysql_fetch_assoc($result)) {
            $comportamientos[] = $fila;
        }
        $comportamientos = json_encode($comportamientos);
        exec('php-cli ' . $folder_path . 'call_record_region_comportamiento.php ' . $region['id'] . ' ' . $region['code'] . ' ' . $total_region . ' ' . urlencode($comportamientos) . ' ' . $total_comportamiento . ' c45a5f3b2cfa74ac94bd5bbfb2c5d6a5 > ' . $log_path . 'call_record_region_comportamiento.log 2>&1 &');
        usleep(rand(0, 100000));
        if ($count++ % 100 == 0) {
            sleep(20);
        }
    }
    error_log('record_region_comportamiento (f): ' . date('d m Y H:i:s'));
}
/***** END - COMPORTAMIENTO *****/
error_log(date('d m Y H:i:s'));
*/
/****** END - INTEREST *****/
/****** COMPORTAMIENTO *****/
if ($_SERVER['argv'][1] == 'country_comportamiento') {
    $query = 'SELECT count(*) cantidad FROM `facebook_comportamiento_3_1` WHERE active_fb_get_data = 1;';
    $result = db_query($query, array());
    if ($fila = mysql_fetch_assoc($result)) {
        $total_comportamiento = $fila['cantidad'];
    }
    $query = 'SELECT id_country id, code FROM `facebook_country_3_1` WHERE id_country NOT IN(' . get_data_for_sql_in('comportamiento', $total_comportamiento) . ') AND active_fb_get_data = 1 ORDER BY 1;';
    $que = db_query($query, array());
    $count = 1;
    error_log('record_country_comportamiento (i - aux): ' . date('d m Y H:i:s'));
    while ($country = mysql_fetch_assoc($que)) {
        $query = "SELECT id_comportamiento id, key_comportamiento 'key' FROM `facebook_comportamiento_3_1` WHERE active_fb_get_data = 1 AND id_comportamiento NOT IN(" . get_data_id_secundary_for_sql_in($country['id'], 'comportamiento') . ") ORDER BY 1;";
        $result = db_query($query, array());
        $comportamientos = array();
        while ($fila = mysql_fetch_assoc($result)) {
            $comportamientos[] = $fila;
        }
        $comportamientos = json_encode($comportamientos);
        //Audiencia por comportamientos (12:00 min)
        exec('php-cli ' . $folder_path . 'call_record_country_comportamiento.php ' . $country['id'] . ' ' . $country['code'] . ' ' . $total_country . ' ' . urlencode($comportamientos) . ' ' . $total_comportamiento . ' 1 c45a5f3b2cfa74ac94bd5bbfb2c5d6a5 > ' . $log_path . 'call_record_country_comportamiento.log 2>&1 &');
        usleep(rand(0, 100000));
        if ($count++ % 7 == 0) {
            sleep(20);
        }
    }
    error_log('record_country_comportamiento (f - aux): ' . date('d m Y H:i:s'));
}
$table = str_replace('call_', '', str_replace('record_', '', $_SERVER['argv'][3]));
$query = 'SELECT id_city, key_city FROM `facebook_city_3_1` WHERE id_city IN(' . get_data_for_sql_in($_SERVER['argv'][1], $table, $_SERVER['argv'][4]) . ') AND active_fb_get_data = 1 ORDER BY 1;';
$que = db_query($query, array());
$total_city = $_SERVER['argv'][2];
while ($fila = mysql_fetch_assoc($que)) {
    $data = array();
    switch ($table) {
        case 'city_age':
            $query = 'SELECT id_age id, min, max FROM `facebook_age_3_1` WHERE active_fb_get_data = 1 AND id_age NOT IN(' . get_data_id_secundary_for_sql_in($fila['id_city'], 'age') . ') ORDER BY 1;';
            break;
        case 'city_interest':
            $query = "SELECT id_interest id, key_interest 'key' FROM `facebook_interest_3_1` WHERE id_interest IN(SELECT id_interest FROM facebook_interest_city_3_1 WHERE id_country = " . $_SERVER['argv'][1] . ") AND id_interest NOT IN(" . get_data_id_secundary_for_sql_in($fila['id_city'], 'interest') . ") AND active_fb_get_data = 1 ORDER BY 1;";
            break;
        case 'city_comportamiento':
            $query = "SELECT id_comportamiento id, key_comportamiento 'key' FROM `facebook_comportamiento_3_1` WHERE id_comportamiento IN(SELECT id_comportamiento FROM facebook_mobile_os_city_3_1) AND active_fb_get_data = 1 AND id_comportamiento NOT IN(" . get_data_id_secundary_for_sql_in($fila['id_city'], 'comportamiento') . ") ORDER BY 1;";
            break;
        case 'city_relationship':
            $query = "SELECT id_relationship id, key_relationship 'key' FROM `facebook_relationship_3_1` WHERE active_fb_get_data = 1 AND id_relationship NOT IN(" . get_data_id_secundary_for_sql_in($fila['id_city'], 'relationship') . ") ORDER BY 1;";
            break;
    }
    if ($table != 'city') {
        $result = db_query($query, array());
        while ($fila2 = mysql_fetch_assoc($result)) {
            $data[] = $fila2;
        }
    }
    $data = json_encode($data);
    exec('php-cli ' . $folder_path . $_SERVER['argv'][3] . '.php ' . $fila['id_city'] . ' ' . $fila['key_city'] . ' ' . $total_city . ' ' . urlencode($data) . ' c45a5f3b2cfa74ac94bd5bbfb2c5d6a5 > ' . $log_path . $_SERVER['argv'][3] . '.log 2>&1 &');
    usleep(rand(0, 100000));
}
die;