/** * Tong hop du lieu theo thoi gian * * @param string $from_time * @param string $to_time * @param string $dateInterval P1D: từng ngày | P1W: từng tuần | P1M: từng tháng * @throws \yii\mongodb\Exception * * Use: php yii job-stats * php yii job-stats 2015/08/01 * php yii job-stats 2015/08/01 2015/08/30 */ public function actionIndex($from_time = '', $to_time = '', $dateInterval = 'P1D') { JobStats::deleteAll(); // if (empty($from_time) AND empty($to_time)) { // Chay ngay hom qua // $from_time = date('Y/m/d 00:00:00', strtotime("-1 days")); // $to_time = date('Y/m/d 23:59:59', strtotime("-1 days")); // } if (empty($from_time) and empty($to_time)) { // Chay ngay hien tai $from_time = date('Y/m/d 00:00:00'); $to_time = date('Y/m/d 23:59:59'); } elseif (empty($from_time)) { // Chay tu dau den $to_time $from_time = date('Y/m/d 00:00:00', JobStats::find()->min('date_time')->sec); $to_time = date('Y/m/d 23:59:59', strtotime($to_time)); } elseif (empty($to_time)) { // Chay tu $from_time den ngay hien tai $from_time = date('Y/m/d 00:00:00', strtotime($from_time)); $to_time = date('Y/m/d 23:59:59'); } else { // Chay trong khoang $from_time den $to_time $from_time = date('Y/m/d 00:00:00', strtotime($from_time)); $to_time = date('Y/m/d 23:59:59', strtotime($to_time)); } // Create range date $data = []; $interval = new DateInterval($dateInterval); $daterange = new DatePeriod(new DateTime($from_time), $interval, new DateTime($to_time)); foreach ($daterange as $key => $date) { // Update du lieu vao bang job_stats $currentDate = new MongoDate($date->getTimestamp()); $model = JobStats::find()->where(['date_time' => $currentDate])->one(); if (!$model) { $model = new JobStats(); $model->date_time = $currentDate; } // Tu danh sach ma loi, count xem trong ngay co bao nhieu tin nhan $count = Job::find()->where(['created_time' => ['$gte' => new MongoDate(strtotime($date->format("Y-m-d 00:00:00"))), '$lte' => new MongoDate(strtotime($date->format("Y-m-d 23:59:59")))]])->count('_id'); $data['job'] = $count; $model->data = $data; $model->save(); echo 'Tao bao cao thong ke Job ngay ' . $date->format("Y-m-d"); echo "\n"; } }
/** * Build data cho chart */ private function buildData() { // build data default $data = ['collection' => JobStats::getCollection()->name, 'format' => $this->formatDate, 'chartReport' => JobStats::find()->where(['date_time' => ['$gte' => new MongoDate($this->from_time['timestamp']), '$lte' => new MongoDate($this->to_time['timestamp'])]])->orderBy('date_time')->all(), 'query' => ['$group' => ['_id' => null]], 'where' => ['$match' => ['date_time' => ['$gte' => new MongoDate($this->from_time['timestamp']), '$lte' => new MongoDate($this->to_time['timestamp'])]]]]; // Tao data cho chart foreach ($data['chartReport'] as $day_report) { // Build x $x = ['x' => date($this->formatDate, $day_report->date_time->sec)]; foreach ($day_report->data as $key => $value) { // Build ykeys, labels and colors if (!in_array($key, $this->chartData['ykeys'])) { $this->chartData['ykeys'][] = $key; $this->chartData['labels'][] = $key; //parseKey // Build Colors $this->chartData['lineColors'][] = $this->genColor(); // Build query if (isset($data['query'])) { $data['query']['$group'][$key] = ['$sum' => '$data.' . $key]; } } // Build a,b,c $x[$key] = $value; } $this->chartData['data'][] = $x; } $collection = Yii::$app->mongodb->getCollection($data['collection']); $result = $collection->aggregate($data['where'], $data['query']); if (isset($result[0]) and is_array($result[0])) { unset($result[0]['_id']); $this->overall_statistics = $result[0]; } }