<?php 
$pass = "spotway";
if($_GET[pass] == $pass)
{
include("app_needed/config.php");
// GENERAL SYNC

$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_tag`, `unique_publisher`,COALESCE(SUM(`income`),0)  as `total`, `date`,`url`  FROM `app_count_imp` GROUP BY `unique_tag`,`unique_publisher`,`date` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){
	
	$add = mysql_query("INSERT INTO `app_daily_imp` (`unique_user`, `unique_type`, `user_type`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today['unique_publisher']."','".$today['unique_tag']."','2','".$today['impressions']."','".$today['clicks']."','".$today['total']."','".$today['date']."')");
	}

$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_ad`, `unique_advertiser`,COALESCE(SUM(`outcome`),0)  as `total`, `date`  FROM `app_count_imp` GROUP BY `unique_ad`,`unique_advertiser`,`date` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){
	
	$add = mysql_query("INSERT INTO `app_daily_imp` (`unique_user`, `unique_type`, `user_type`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today['unique_advertiser']."','".$today['unique_ad']."','1','".$today['impressions']."','".$today['clicks']."','".$today['total']."','".$today['date']."')");
	}
// COUNTRY SYNC
$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_tag`, `country`, `unique_publisher`,COALESCE(SUM(`income`),0)  as `total`, `date`  FROM `app_count_imp` GROUP BY `unique_tag`,`unique_publisher`,`date`,`country` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){
	
	$add = mysql_query("INSERT INTO `app_daily_country` (`unique_user`, `unique_type`, `user_type`, `country`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today[unique_publisher]."','".$today[unique_tag]."','2','".$today[country]."','".$today[impressions]."','".$today[clicks]."','".$today[total]."','".$today[date]."')");	
	}
	
$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_ad`, `country`, `unique_advertiser`,COALESCE(SUM(`outcome`),0)  as `total`, `date`  FROM `app_count_imp` GROUP BY `unique_ad`,`unique_advertiser`,`date`,`country` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){
	
	$add = mysql_query("INSERT INTO `app_daily_country` (`unique_user`, `unique_type`, `user_type`, `country`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today[unique_advertiser]."','".$today[unique_ad]."','1','".$today[country]."','".$today[impressions]."','".$today[clicks]."','".$today[total]."','".$today[date]."')");	
	}
// PAGES SYNC
$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_tag`, `url`, `unique_publisher`,COALESCE(SUM(`income`),0)  as `total`, `date`  FROM `app_count_imp` GROUP BY `unique_tag`,`unique_publisher`,`date`,`url` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){

    $add = mysql_query("INSERT INTO `app_daily_pages` (`unique_user`, `unique_type`, `user_type`, `url`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today[unique_publisher]."','".$today[unique_tag]."','2','".$today[url]."','".$today[impressions]."','".$today[clicks]."','".$today[total]."','".$today[date]."')");
}

$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_ad`, `url`, `unique_advertiser`,COALESCE(SUM(`outcome`),0)  as `total`, `date`  FROM `app_count_imp` GROUP BY `unique_ad`,`unique_advertiser`,`date`,`url` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){

    $add = mysql_query("INSERT INTO `app_daily_pages` (`unique_user`, `unique_type`, `user_type`, `url`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today[unique_advertiser]."','".$today[unique_ad]."','1','".$today[url]."','".$today[impressions]."','".$today[clicks]."','".$today[total]."','".$today[date]."')");
}
// AGENTS SYNC
$sql = "SELECT COUNT(*) as `impressions` , COALESCE(SUM(`click`),0)  as `clicks`, `unique_tag`, `browser`, `unique_publisher`,COALESCE(SUM(`income`),0)  as `total`, `date`  FROM `app_count_imp` GROUP BY `unique_tag`,`unique_publisher`,`date`,`browser` ";
$result = mysql_query ($sql);
while ($today = mysql_fetch_array($result)){
	
	$add = mysql_query("INSERT INTO `app_daily_agents` (`unique_user`, `unique_type`, `user_type`, `browser`, `impressions`,`clicks`,`money`,`date`) VALUES ('".$today[unique_publisher]."','".$today[unique_tag]."','2','".$today[browser]."','".$today[impressions]."','".$today[clicks]."','".$today[total]."','".$today[date]."')");	
	}
	
mysql_query("DELETE FROM `app_count_imp` ");
        $result = mysql_query('SHOW TABLES') or die('Cannot get tables');
        while($table = mysql_fetch_row($result)) {
            mysql_query('OPTIMIZE TABLE '.$table[0]) or die('Cannot optimize '.$table[0]);
            echo $table[0]." - Optimized <br /> ";
        }
echo "Database cleaned. <br>";
echo "End.";
}
?>
<?php mysql_close($conn); ?>