public function cronVisitorDataType($type) { if (!isset($this->config[$type])) { return; } $config = $this->config[$type]; $fields = 'visitor_datatype_' . $type; $update = 'updated_datatype_' . $type; $where = $config['where'] ? $config['where'] : '1=1'; $order = $config['order'] ? $config['order'] : 'created_at asc'; $limit = $config['limit'] ? $config['limit'] : 100; $models = ApiVisitorDetail::find()->where($where)->orderBy($order)->limit($limit)->all(); //连续5次失败则不更新 $i = 0; foreach ($models as $k => $model) { $userName = $model->visitor_username; $ref = $model->visitor_referrer; $params = ['userName' => $userName]; $return = $this->getByType($ref, $type, $params); // $return = [ // 'code' =>200 , // 'data' =>'a' // ]; if ($return['code'] == 200) { $model->{$fields} = $return['data']; $model->{$update} = CURRENT_TIMESTAMP; if (!$model->update()) { print_r($model->errors); } else { \yii::$app->controller->stdout(sprintf("%s - %s:%s Id:%d \n", $userName, $config['name'], $return['data'], $model->id), Console::BOLD); } } else { $i++; if ($i > 5) { break; } } } }
public function getUpdate($limit = 100) { $array = ApiVisitorDetail::find()->where(['month_cron' => $this->time])->orderBy('visitor_regtime asc')->limit($limit)->all(); $fromTime = date('Y-m-d H:i:s', $this->time); $y = date('Y', $this->time); $m = date('m', $this->time); $toTimeStamp = mktime(0, 0, 0, $m + 1, 1, $y); $toTime = date('Y-m-d H:i:s', $toTimeStamp); foreach ($array as $k => $v) { //查找月表是否有该数据,有则不存储 //逐条获取api 获取完后 数据更新完毕 $model = ApiMonthDetail::find()->where(['idvisit' => $v['idvisit'], 'mtime' => $this->time])->one(); if ($model == null) { $model = new ApiMonthDetail(); } $model->idvisit = $v->idvisit; $model->mtime = $this->time; $model->visitor_username = $v->visitor_username; $model->visitor_referrer = $v->visitor_referrer; $model->created_at = CURRENT_TIMESTAMP; $params = ['userName' => $v->visitor_username, 'fromTime' => $fromTime, 'toTime' => $toTime]; foreach ($this->config as $type => $config) { $fieldName = 'visitor_datatype_' . $type; $timeName = 'updated_datatype_' . $type; $fieldReturn = $this->getByType($v['visitor_referrer'], $type, $params); if ($fieldReturn['code'] == 200) { $model->{$fieldName} = $fieldReturn['data']; $model->{$timeName} = CURRENT_TIMESTAMP; \yii::$app->controller->stdout(sprintf("%s - %s:%s Id:%d \n", $v->visitor_username, $config['name'], $fieldReturn['data'], $model->id), \yii\helpers\Console::BOLD); } } if ($model->save()) { $v->month_cron = 0; $v->save(); } else { print_r($model->errors); } } if (!ApiVisitorDetail::getMonthCronData($this->time)) { $model = new ApiMonthSetting(); $data = $model->getUpdating(); $model->updateTheMonth($data->id, false); } }
public function actionCron($type, $num) { \app\models\ApiVisitorDetail::cronUpdateVisitorDataType($type, $num, 5); return $this->render('cron'); }
public function getAllCount() { $fromTime = intval($this->time); $y = date('Y', $this->time); $m = date('m', $this->time); $toTime = mktime(0, 0, 0, $m + 1, 1, $y); return ApiVisitorDetail::find()->where(['between', 'visitor_regtime', $fromTime, $toTime])->count(); }
public function getDb($apiData) { $idvisits = []; $data = []; if ($apiData) { foreach ($apiData as $k => $v) { $data[$v['idVisit']] = $v; $idvisits[] = $v['idVisit']; } } $idvisits = array_unique($idvisits); $find = ApiVisitorDetail::find()->where(["in", "idvisit", $idvisits])->asArray()->all(); //格式化username $find = ArrayHelper::index($find, 'idvisit'); foreach ($data as $k => $v) { $array = isset($find[$k]) ? $find[$k] : false; $data[$k]['ip'] = $array ? $array['ip'] : ''; $data[$k]['iptype'] = $array ? $array['iptype'] : ''; $data[$k]['iptext'] = $array ? $array['iptext'] : ''; $data[$k]['visitor_referrer'] = $array ? $array['visitor_referrer'] : ''; for ($i = 0; $i < 10; $i++) { $data[$k]['visitor_datatype_' . $i] = $array ? $array['visitor_datatype_' . $i] : ''; } } return $data; }