From 4b9bdbdab8d33a9197e12cac65477018e8bdc20e Mon Sep 17 00:00:00 2001 From: colly_wyx <wangyixiong_007@163.com> Date: Sat, 28 Apr 2018 17:08:52 +0800 Subject: [PATCH] 优化了平均值 --- Yfs/Domain/DataDaily.php | 61 ++++++++++++++++++++++++++++-- 1 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Yfs/Domain/DataDaily.php b/Yfs/Domain/DataDaily.php index d952076..de8fc57 100644 --- a/Yfs/Domain/DataDaily.php +++ b/Yfs/Domain/DataDaily.php @@ -7,7 +7,7 @@ public function __construct(){ //DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers')); $this->model = new Model_DataDaily(); - $this->hourly_model = new Model_DataHourly(); + $this->data_three_hourly_model = new Model_DataThreeHourly(); } /** @@ -23,21 +23,74 @@ if($end_time > $start_time){ $diff_days = ($end_time - $start_time) / 86400; $total_days = $diff_days + 1;//��������������������� - $step = ($total_days - $total_days % 7) / 7; + //$step = ($total_days - $total_days % 7) / 7; $days_arr= array(); - for($i = 0; $i < 7*$step ; $i+=$step){ + for($i = 0; $i < $total_days ; $i+=1){ $days_arr[] = date('Y-m-d', strtotime('+'.$i.' day', $start_time)); } $result = $this->model->getList(array('user_id' => $user_id, 'date' => array('$in' => $days_arr))); } else{ - $result = $this->hourly_model->getList(array('user_id' => $user_id, 'date' => $start_time), array(), array('time_level' => 1)) + $result = $this->data_three_hourly_model->get(array('user_id' => $user_id, 'date' => date('Y-m-d', $start_time))); + $_result = array(); + if($result){ + for($i = 1; $i <=8 ; $i++){ + array_push($_result, array('hour_avg' => $result['time_slot_'.$i], 'time_level' => $i)); + } + } + else{ + $_result = null; + } + return $_result; } return $result; } /** + * ������������������������������������������ + * @param [type] $user_id [description] + * @param [type] $start_time [description] + * @param [type] $end_time [description] + * @return [type] [description] + */ + public function getUserDataAvg($user_id, $start_time, $end_time){ + + if($start_time == $end_time){ + $result = $this->data_three_hourly_model->get(array('date' => $start_time)); + if($result){ + $sum = 0; + for($i = 1; $i <= 8; $i++){ + $sum += $result['time_slot_'.$i]; + } + + $result[0]['day_avg'] = $sum/8; + } + else{ + $result[0]['day_avg'] = 0; + } + + } + else{ + $result = $this->model->aggregate( + array( + array( + '$match' => array('date' => array('$gte' => $start_time, '$lte' => $end_time), 'user_id' => $user_id) + ), + array( + '$group' => array('_id' => '$user_id', 'day_avg' => array('$avg' => '$day_avg')) + ), + array( + '$project' => array('_id' => 0, 'day_avg' => 1) + ) + ) + ); + } + + return isset($result[0]['day_avg'])?$result[0]['day_avg']:0; + } + + /** * ������������ * @param [type] $start_time [description] * @param [type] $end_time [description] -- Gitblit v1.8.0