Index: www/sites/all/modules/custom/np_scan/np_scan.analytics.inc =================================================================== --- www/sites/all/modules/custom/np_scan/np_scan.analytics.inc (revision 34171) +++ www/sites/all/modules/custom/np_scan/np_scan.analytics.inc (working copy) @@ -400,56 +400,44 @@ $max = 0; $data = array(); - $interval = 'minutes'; - $multiplier = 12; - do { - $velocity_field = $interval . '.velocity'; - $order_field = ($order == 'trending') ? 'trending' : $velocity_field; - $ordering_data = array(); - foreach ($scan_ids as $scan_id) { - $fields = array($velocity_field => 1, 'trending' => 1, 'word' => 1); - $query = array( - 'scan_id' => $scan_id, - 'word' => array('$exists' => TRUE), - ); - if ($cursor = scan_api_get_mongo('scan_stats', $table_type)) { // keyword, hashtag - try { - $results = $cursor - ->find($query, $fields) - ->sort(array($order_field => -1)) - ->limit($count + 1) - ->timeout(scan_api_get_mongo_timeout()); - foreach ($results as $row) { - if (!isset($row['word'])) { - // bug in workers. they write empty word records... bad - continue; - } - $velocity = isset($row[$interval]['velocity']) ? $row[$interval]['velocity'] * $multiplier : 0; - $order_data = $order != 'trending' ? $velocity : $row['trending']; - if (!isset($ordering_data[$row['word']]) || ($ordering_data[$row['word']] < $order_data)) { - $data[$row['word']] = array( - 'velocity' => $velocity, - 'trending' => $row['trending'], - 'keyword' => ($table_type == 'hashtag') ? '#' . $row['word'] : $row['word'], - ); - $ordering_data[$row['word']] = $order_data; - } + $order_field = ($order == 'trending') ? 'trending' : 'minutes.velocity'; + $ordering_data = array(); + foreach ($scan_ids as $scan_id) { + $fields = array('minutes.velocity' => 1, 'trending' => 1, 'word' => 1); + $query = array( + 'scan_id' => $scan_id, + 'word' => array('$exists' => TRUE), + ); + if ($cursor = scan_api_get_mongo('scan_stats', $table_type)) { // keyword, hashtag + try { + $results = $cursor + ->find($query, $fields) + ->sort(array($order_field => -1)) + ->limit($count + 1) + ->timeout(scan_api_get_mongo_timeout()); + foreach ($results as $row) { + if (!isset($row['word'])) { + // bug in workers. they write empty word records... bad + continue; } + + $velocity = isset($row['minutes']['velocity']) ? $row['minutes']['velocity'] * 12 : 0; + $order_data = $order != 'trending' ? $velocity : $row['trending']; + if (!isset($ordering_data[$row['word']]) || ($ordering_data[$row['word']] < $order_data)) { + $data[$row['word']] = array( + 'velocity' => $velocity, + 'trending' => $row['trending'], + 'keyword' => ($table_type == 'hashtag') ? '#' . $row['word'] : $row['word'], + ); + $ordering_data[$row['word']] = $order_data; + } } - catch (MongoCursorTimeoutException $e) { - } } + catch (MongoCursorTimeoutException $e) { + } } - if (!array_filter($ordering_data) && $interval == 'minutes' && $order != 'trending') { - $interval = 'hours'; - $multiplier = 1; - $fallback = TRUE; - } - else { - $fallback = FALSE; - } - } while ($fallback); + } if ($data) { $words = array_keys($data); @@ -496,13 +484,13 @@ if ($cursor = scan_api_get_mongo('scan_stats', $table_type)) { // keyword, hashtag try { $results = $cursor - ->find($query, array($velocity_field => 1)) - ->sort(array($velocity_field => -1)) + ->find($query, array('minutes.velocity' => 1)) + ->sort(array('minutes.velocity' => -1)) ->limit(1) ->timeout(scan_api_get_mongo_timeout()); if ($results->hasNext()) { $row = $results->getNext(); - $max = isset($row[$interval]['velocity']) ? $row[$interval]['velocity'] * $multiplier : 0; + $max = isset($row['minutes']['velocity']) ? $row['minutes']['velocity'] * 12 : 0; } } catch (MongoCursorTimeoutException $e) { @@ -595,40 +583,15 @@ } // max velocity - $fallback = FALSE; - $interval = 'minutes'; - $multiplier = 12; - do { - $velocity_field = $interval . '.velocity'; - $results = $cursor - ->find($query, array($velocity_field => 1)) - ->sort(array($velocity_field => -1)) - ->limit(1) - ->timeout(scan_api_get_mongo_timeout()); - if ($results->hasNext()) { - $row = $results->getNext(); - if (!isset($row[$interval]['velocity']) || !$row[$interval]['velocity']) { - $fallback = TRUE; - } - else { - $result[$collection_name]['velocity'] = $row[$interval]['velocity'] * $multiplier; - } - } - - if ($fallback) { - if ($interval == 'minutes') { - $interval = 'hours'; - $multiplier = 1; - } - /*elseif ($interval == 'hours') { - $interval = 'days'; - }*/ - else { - // no more fallbacks. - $fallback = FALSE; - } - } - } while ($fallback); + $results = $cursor + ->find($query, array('minutes.velocity' => 1)) + ->sort(array('minutes.velocity' => -1)) + ->limit(1) + ->timeout(scan_api_get_mongo_timeout()); + if ($results->hasNext()) { + $row = $results->getNext(); + $result[$collection_name]['velocity'] = isset($row['minutes']['velocity']) ? $row['minutes']['velocity'] * 12 : 0; + } } catch (MongoCursorTimeoutException $e) { } @@ -739,50 +702,25 @@ ); $pagenumber = $full ? _np_scan_analytics_get_page_number('scan_stats', $collection_name, $limit, $query) : 0; - $interval = 'minutes'; - $multiplier = 12; - do { - $velocity_field = $interval . '.velocity'; - $fields = array('scan_id' => 1, $velocity_field => 1, 'word' => 1); - try { - $results = $cursor - ->find($query, $fields) - ->sort(array($velocity_field => -1)) - ->skip($pagenumber * $limit) - ->limit($limit) - ->timeout(scan_api_get_mongo_timeout()); + $fields = array('scan_id' => 1, 'minutes.velocity' => 1, 'word' => 1); + try { + $results = $cursor + ->find($query, $fields) + ->sort(array('minutes.velocity' => -1)) + ->skip($pagenumber * $limit) + ->limit($limit) + ->timeout(scan_api_get_mongo_timeout()); - $fallback = FALSE; - $index = 0; - foreach ($results as $row) { - if (!$index && (!isset($row[$interval]['velocity']) || !$row[$interval]['velocity'])) { - $fallback = TRUE; - break; - } - $tweets[$row['word']] = array( - 'scan_id' => $row['scan_id'], - 'velocity' => isset($row[$interval]['velocity']) ? $row[$interval]['velocity'] * $multiplier : 0, - 'word' => $row['word'], - ); - $index++; - } + foreach ($results as $row) { + $tweets[$row['word']] = array( + 'scan_id' => $row['scan_id'], + 'velocity' => isset($row['minutes']['velocity']) ? $row['minutes']['velocity'] * 12 : 0, + 'word' => $row['word'], + ); } - catch (MongoCursorTimeoutException $e) { - } - if ($fallback) { - if ($interval == 'minutes') { - $interval = 'hours'; - $multiplier = 1; - } - /*elseif ($interval == 'hours') { - $interval = 'days'; - }*/ - else { - // no more fallbacks. - $fallback = FALSE; - } - } - } while ($fallback); + } + catch (MongoCursorTimeoutException $e) { + } } $stats = array();