Example #1
0
 /**
  * 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];
     }
 }