$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$appId = $_REQUEST['appId'];
$urlId = $_REQUEST['urlId'];
$startDate = $_REQUEST['startDate'];
$endDate = $_REQUEST['endDate'];
if (!$_REQUEST['startDate'] || !$_REQUEST['endDate']) {
    $startDate = $endDate = date('Y-m-d', time() - 86400);
}
$startTime = $startDate . ' 00:00:00';
$endTime = $endDate . ' 23:59:59';
$lastEvenYear = date('Y') % 2 === 0 ? date('Y') : date('Y') - 1;
$lastEvenYearTimestamp = mktime(0, 0, 0, 1, 1, $lastEvenYear);
$startHoursElapsedSinceLastEvenYear = floor((strtotime($startTime) - $lastEvenYearTimestamp) / 3600);
$endHoursElapsedSinceLastEvenYear = floor((strtotime($endTime) - $lastEvenYearTimestamp) / 3600);
$dbNames = DbUtil::getDbNames($appId);
$response = array();
try {
    $sql = 'SELECT ROUND(AVG(avg_network_time), 2) AS networkTime, ROUND(AVG(avg_backend_time), 2) AS backendTime, ROUND(AVG(avg_frontend_time), 2) AS frontendTime, ROUND(AVG(avg_dom_ready_time), 2) AS domReadyTime, ROUND(AVG(avg_done_time), 2) AS doneTime
        FROM ' . $dbNames['summary'] . '.load_time_summary
        WHERE hours_elapsed_since_last_even_year >= :start_hours_elapsed_since_last_even_year
        AND hours_elapsed_since_last_even_year <= :end_hours_elapsed_since_last_even_year
        AND page_id = :page_id';
    $st = $db->prepare($sql);
    $st->bindValue(':start_hours_elapsed_since_last_even_year', $startHoursElapsedSinceLastEvenYear, PDO::PARAM_INT);
    $st->bindValue(':end_hours_elapsed_since_last_even_year', $endHoursElapsedSinceLastEvenYear, PDO::PARAM_INT);
    $st->bindValue(':page_id', $urlId, PDO::PARAM_INT);
    $st->execute();
    $row = $st->fetchAll(PDO::FETCH_ASSOC);
    $response = $row[0];
    $st->closeCursor();
    require_once __DIR__ . '/../config/config.php';
    $lastHourTimestamp = time() - 3600;
    $cronStartTimestamp = strtotime(date('Y-m-d H', $lastHourTimestamp) . ':00:00');
    $cronEndTimestamp = strtotime(date('Y-m-d H', $lastHourTimestamp) . ':59:59');
    $db = ncDatabaseManager::getInstance()->getDatabase('nLogger')->getConnection();
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    $loggingConfig = ncYaml::load(__DIR__ . '/../config/logging.yml');
    $boomWhitelistedAppIds = $loggingConfig['boomerang'];
    foreach ($boomWhitelistedAppIds['appIds'] as $appId) {
        for ($time = $cronStartTimestamp; $time < $cronEndTimestamp; $time += 3600) {
            $date = date('Y-m-d', $time);
            $hour = date('H', $time);
            $startTime = $date . ' ' . $hour . ':00:00';
            $endTime = $date . ' ' . $hour . ':59:59';
            $dbNames = DbUtil::getDbNames($appId, $date);
            echo 'Processing ' . implode(', ', $dbNames) . ' from ' . $startTime . ' to ' . $endTime . "\n";
            summarizeDb($db, $dbNames, $startTime, $endTime);
            echo "\n----------------\n";
        }
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . "<br />\n";
    echo 'Trace: ';
    print_r($e->getTrace());
    echo "<br />\n";
    exit($e->getCode());
}
echo 'Cron ended @ ' . date('Y-m-d H:i:s') . "\n";
function summarizeDb($db, $dbNames, $startTime, $endTime)
{
 public function logBoomr($boomData, $logTime, $ip, $userAgent)
 {
     if ($this->loadTimeOutOfBounds($boomData)) {
         return false;
     }
     $boomData = $this->fixMissingData($boomData);
     if (!$logTime) {
         $logTime = date('Y-m-d H:i:s');
     }
     if (!$ip) {
         $ip = ip2long(\CDNHeaders::getInstance()->getRemoteIP());
     }
     if (!$userAgent) {
         $userAgent = $_SERVER['HTTP_USER_AGENT'];
     }
     $deviceDetails = $this->deviceDetector->getDeviceDetails($userAgent);
     $osName = $deviceDetails['osDetails']["name"] . " " . $deviceDetails['osDetails']["version"];
     $browserName = $deviceDetails['browserDetails']["name"] ? $deviceDetails['browserDetails']["name"] : "Unknown";
     $browserVersion = $deviceDetails['browserDetails']["version"] ? $deviceDetails['browserDetails']["version"] : 0;
     $deviceType = $deviceDetails['type'];
     try {
         $dbNames = \DbUtil::getDbNames($boomData['appId']);
         $db = \ncDatabaseManager::getInstance()->getDatabase('nLogger')->getConnection();
         $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
         $db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
         $sql = 'SELECT os_id FROM ' . $dbNames['common'] . '.os WHERE name = :name and device_type = :device_type';
         $st = $db->prepare($sql);
         $st->bindValue(':name', $osName, \PDO::PARAM_STR);
         $st->bindValue(':device_type', $deviceType, \PDO::PARAM_STR);
         $st->execute();
         $result = $st->fetch(\PDO::FETCH_ASSOC);
         $st->closeCursor();
         if ($result['os_id']) {
             $osId = $result['os_id'];
         } else {
             $sql = 'INSERT INTO ' . $dbNames['common'] . '.os (os_id, name,device_type) VALUES(NULL, :name, :device_type)';
             $st = $db->prepare($sql);
             $st->bindValue(':name', $osName, \PDO::PARAM_STR);
             $st->bindValue(':device_type', $deviceType, \PDO::PARAM_STR);
             $st->execute();
             $osId = $db->lastInsertId();
             $st->closeCursor();
         }
         $sql = 'SELECT browser_id FROM ' . $dbNames['common'] . '.browser WHERE name = :name AND version = :version';
         $st = $db->prepare($sql);
         $st->bindValue(':name', $browserName, \PDO::PARAM_STR);
         $st->bindValue(':version', $browserVersion, \PDO::PARAM_STR);
         $st->execute();
         $result = $st->fetch(\PDO::FETCH_ASSOC);
         $st->closeCursor();
         if ($result['browser_id']) {
             $browserId = $result['browser_id'];
         } else {
             $sql = 'INSERT INTO ' . $dbNames['common'] . '.browser (browser_id, name, version) VALUES(NULL, :name, :version)';
             $st = $db->prepare($sql);
             $st->bindValue(':name', $browserName, \PDO::PARAM_STR);
             $st->bindValue(':version', $browserVersion, \PDO::PARAM_STR);
             $st->execute();
             $browserId = $db->lastInsertId();
             $st->closeCursor();
         }
         $sql = 'SELECT env_id FROM ' . $dbNames['main'] . '.env WHERE os_id = :os_id AND browser_id = :browser_id';
         $st = $db->prepare($sql);
         $st->bindValue(':os_id', $osId, \PDO::PARAM_INT);
         $st->bindValue(':browser_id', $browserId, \PDO::PARAM_INT);
         $st->execute();
         $result = $st->fetch(\PDO::FETCH_ASSOC);
         $st->closeCursor();
         if ($result['env_id']) {
             $envId = $result['env_id'];
         } else {
             $sql = 'INSERT INTO ' . $dbNames['main'] . '.env (env_id, os_id, browser_id) VALUES(NULL, :os_id, :browser_id)';
             $st = $db->prepare($sql);
             $st->bindValue(':os_id', $osId, \PDO::PARAM_INT);
             $st->bindValue(':browser_id', $browserId, \PDO::PARAM_INT);
             $st->execute();
             $envId = $db->lastInsertId();
             $st->closeCursor();
         }
         $isUrlATag = !empty($boomData['tag']);
         if ($isUrlATag) {
             $boomData['u'] = $boomData['tag'];
         }
         $urlIds = $this->insertUrl($dbNames, $db, $boomData['u'], $isUrlATag);
         $urlStaticId = $urlIds['urlStaticId'];
         $urlDynamicId = $urlIds['urlDynamicId'];
         $sql = 'SELECT url_id FROM ' . $dbNames['main'] . '.url WHERE url_static_id = :url_static_id AND url_dynamic_id = :url_dynamic_id';
         $st = $db->prepare($sql);
         $st->bindValue(':url_static_id', $urlStaticId, \PDO::PARAM_INT);
         $st->bindValue(':url_dynamic_id', $urlDynamicId, \PDO::PARAM_INT);
         // $st->bindValue(':referrer_static_id', $referrerStaticId, \PDO::PARAM_INT);
         // $st->bindValue(':referrer_dynamic_id', $referrerDynamicId, \PDO::PARAM_INT);
         $st->execute();
         $result = $st->fetch(\PDO::FETCH_ASSOC);
         $st->closeCursor();
         if ($result['url_id']) {
             $urlId = $result['url_id'];
         } else {
             $sql = 'INSERT INTO ' . $dbNames['main'] . '.url (url_id, url_static_id, url_dynamic_id) VALUES(NULL, :url_static_id, :url_dynamic_id)';
             $st = $db->prepare($sql);
             $st->bindValue(':url_static_id', $urlStaticId, \PDO::PARAM_INT);
             $st->bindValue(':url_dynamic_id', $urlDynamicId, \PDO::PARAM_INT);
             $st->execute();
             $urlId = $db->lastInsertId();
             $st->closeCursor();
         }
         $sql = 'INSERT INTO ' . $dbNames['main'] . '.main (main_id, app_id, log_time, ip_address, env_id, url_id) VALUES(NULL, :app_id, :log_time, :ip_address, :env_id, :url_id)';
         $st = $db->prepare($sql);
         $st->bindValue(':app_id', $boomData['appId'], \PDO::PARAM_INT);
         $st->bindValue(':log_time', $logTime, \PDO::PARAM_STR);
         $st->bindValue(':ip_address', $ip, \PDO::PARAM_INT);
         $st->bindValue(':env_id', $envId, \PDO::PARAM_INT);
         $st->bindValue(':url_id', $urlId, \PDO::PARAM_INT);
         $st->execute();
         $mainId = $db->lastInsertId();
         $st->closeCursor();
         $sql = 'INSERT INTO ' . $dbNames['main'] . '.load_time (main_id, dns, response, page, done, ready) VALUES(:main_id, :dns, :response, :page, :done, :ready)';
         $st = $db->prepare($sql);
         $st->bindValue(':main_id', $mainId, \PDO::PARAM_INT);
         $st->bindValue(':dns', $boomData['nt_dns'], \PDO::PARAM_STR);
         $st->bindValue(':response', $boomData['t_resp'], \PDO::PARAM_STR);
         $st->bindValue(':page', $boomData['t_page'], \PDO::PARAM_STR);
         $st->bindValue(':done', $boomData['t_done'], \PDO::PARAM_STR);
         $st->bindValue(':ready', $boomData['t_resr']['t_domloaded'] ? $boomData['t_resr']['t_domloaded'] : $boomData['t_done'], \PDO::PARAM_STR);
         $st->execute();
         $st->closeCursor();
         $sql = 'INSERT INTO ' . $dbNames['main'] . '.bandwidth_latency (main_id, bandwidth, bandwidth_error, latency, latency_error, round_trip_type) VALUES(:main_id, :bandwidth, :bandwidth_error, :latency, :latency_error, :round_trip_type)';
         $st = $db->prepare($sql);
         $st->bindValue(':main_id', $mainId, \PDO::PARAM_INT);
         $st->bindValue(':bandwidth', $boomData['bw'], \PDO::PARAM_STR);
         $st->bindValue(':bandwidth_error', $boomData['bw_err'], \PDO::PARAM_STR);
         $st->bindValue(':latency', $boomData['lat'], \PDO::PARAM_STR);
         $st->bindValue(':latency_error', $boomData['lat_err'], \PDO::PARAM_STR);
         $st->bindValue(':round_trip_type', $boomData['rt.start'], \PDO::PARAM_STR);
         $st->execute();
         $st->closeCursor();
         foreach ($boomData['t_resr'] as $customTimerName => $customTimerTime) {
             if ($customTimerName == 't_domloaded') {
                 continue;
             }
             $sql = 'SELECT custom_timer_id FROM ' . $dbNames['common'] . '.custom_timer WHERE name = :name';
             $st = $db->prepare($sql);
             $st->bindValue(':name', $customTimerName, \PDO::PARAM_STR);
             $st->execute();
             $result = $st->fetch(\PDO::FETCH_ASSOC);
             $st->closeCursor();
             if ($result['custom_timer_id']) {
                 $customTimerId = $result['custom_timer_id'];
             } else {
                 $sql = 'INSERT INTO ' . $dbNames['common'] . '.custom_timer (custom_timer_id, name) VALUES(NULL, :name)';
                 $st = $db->prepare($sql);
                 $st->bindValue(':name', $customTimerName, \PDO::PARAM_STR);
                 $st->execute();
                 $customTimerId = $db->lastInsertId();
                 $st->closeCursor();
             }
             $sql = 'INSERT INTO ' . $dbNames['main'] . '.custom_time (main_id, custom_timer_id, time) VALUE (:main_id, :custom_timer_id, :time)';
             $st = $db->prepare($sql);
             $st->bindValue(':main_id', $mainId, \PDO::PARAM_INT);
             $st->bindValue(':custom_timer_id', $customTimerId, \PDO::PARAM_INT);
             $st->bindValue(':time', $customTimerTime, \PDO::PARAM_STR);
             $st->execute();
             $st->closeCursor();
         }
     } catch (Exception $e) {
         die('Error: ' . $e->getMessage() . "<br />\n");
     }
     return true;
 }
<?php

echo 'Cron started @ ' . date('Y-m-d H:i:s') . "\n";
try {
    require_once __DIR__ . '/../config/config.php';
    $tomorrowsDate = date('Y-m-d', time() + 86400);
    $db = ncDatabaseManager::getInstance()->getDatabase('nLogger_cleanup')->getConnection();
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    $loggingConfig = ncYaml::load(__DIR__ . '/../config/logging.yml');
    $boomWhitelistedAppIds = $loggingConfig['boomerang'];
    foreach ($boomWhitelistedAppIds['appIds'] as $appId) {
        $dbNames = DbUtil::getDbNames($appId, $tomorrowsDate);
        echo 'Processing ' . implode(', ', $dbNames) . ' for ' . $tomorrowsDate . "\n";
        cleanupMainDatabase($db, $dbNames);
        echo "\n----------------\n";
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . "<br />\n";
    echo 'Trace: ';
    print_r($e->getTrace());
    echo "<br />\n";
    exit($e->getCode());
}
echo 'Cron ended @ ' . date('Y-m-d H:i:s') . "\n";
function cleanupMainDatabase($db, $dbNames)
{
    $tablesToTruncate = array('bandwidth_latency', 'custom_time', 'env', 'load_time', 'main', 'url', 'url_dynamic', 'url_static');
    foreach ($tablesToTruncate as $tableToTruncate) {
        echo 'Truncating ' . $dbNames['main'] . '.' . $tableToTruncate . "\n";
        $sql = 'TRUNCATE TABLE ' . $dbNames['main'] . '.' . $tableToTruncate;