Пример #1
0
 function _get_latest($start_date, $debug = FALSE)
 {
     $this->load->library("google_php_client");
     $end_date = $start_date;
     $start_time = microtime(TRUE);
     $companies = $this->company->get_all();
     foreach ($companies as $company) {
         if ($company->ga_token && $company->view_id) {
             $posts = $this->post->where('company_id', $company->company_id)->as_array()->get_all();
             $posts = array_column($posts, 'post_id', 'url');
             $this->google_php_client->set_user_company($company);
             $rows = $this->google_php_client->get_posts_stats($start_date, $end_date);
             foreach ($rows as $row) {
                 $url = $row[0];
                 if (array_key_exists($url, $posts)) {
                     $post_id = $posts[$url];
                 } else {
                     if ($debug) {
                         echo "Post is not found: adding " . $url . PHP_EOL;
                     }
                     $data = Post_model::get_post($url);
                     $data['company_id'] = $company->company_id;
                     $post_id = $this->post->insert($data);
                 }
                 $this->db->query("INSERT INTO post_stats VALUES (?, ?, ?, ?, NOW()) ON DUPLICATE KEY UPDATE sessions = ?, pageviews = ?, date_updated = NOW()", array($post_id, $start_date, $row[1], $row[2], $row[1], $row[2]));
                 if ($debug) {
                     echo "Updated post_id #" . $post_id . " for " . $start_date . " (" . $row[1] . ", " . $row[2] . ")" . PHP_EOL;
                 }
             }
             usleep(500000);
         } elseif ($debug) {
             echo "Skipping company ID #" . $company->company_id . " - no GA data" . PHP_EOL;
         }
     }
     if ($debug) {
         echo 'Done: ' . round(microtime(TRUE) - $start_time, 2) . " secs, " . $this->google_php_client->queries . " queries." . PHP_EOL . PHP_EOL;
     }
 }