From b8a82d561917a4336214225f65f4488d977c5fb1 Mon Sep 17 00:00:00 2001 From: colly_wyx <wangyixiong_007@163.com> Date: Thu, 03 May 2018 09:28:00 +0800 Subject: [PATCH] 调整平均值 --- Yfs/Domain/DataDaily.php | 41 ++++++++++++++++++++++++++++++++--------- 1 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Yfs/Domain/DataDaily.php b/Yfs/Domain/DataDaily.php index 9bcfc23..8586c3b 100644 --- a/Yfs/Domain/DataDaily.php +++ b/Yfs/Domain/DataDaily.php @@ -7,7 +7,8 @@ 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(); + $this->data_hourly_model = new Model_DataHourly(); } /** @@ -31,7 +32,17 @@ $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' => date('Y-m-d', $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; @@ -46,17 +57,29 @@ */ public function getUserDataAvg($user_id, $start_time, $end_time){ - // if($start_time == $end_time){ - // $result = $this->model->get(array('date' => $start_time)); - // } - // else{ - $result = $this->model->aggregate( + // if($start_time == $end_time){ + // $result = $this->data_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->data_hourly_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')) + '$group' => array('_id' => '$user_id', 'day_avg' => array('$avg' => '$value')) ), array( '$project' => array('_id' => 0, 'day_avg' => 1) @@ -65,7 +88,7 @@ ); //} - return isset($result[0]['day_avg'])?$result[0]['day_avg']:0; + return isset($result[0]['day_avg'])?floatval(substr($result[0]['day_avg'], 0, 5)):0; } /** -- Gitblit v1.8.0