/** * 检查日志类型是否唯一 */ public function testAppname() { $stt = ApplicateName::find()->where('appname=:appname AND logtype=:logtype', [':appname' => $this->appname, ':logtype' => $this->logtype])->count(1); if ($stt > 0) { $this->addError('appname', '该类型已经存在'); } }
/** * 按ApplicationId统计错误的数量 * @return array */ public static function countErrorByApplicationId() { //查找Error的appname $application_query = new Query(); $application_query->select("id,appname,lastupdatetime,logtotal")->from("ApplicateName")->where("logtype=0"); //1-Error类型 0-trace $application_list = $application_query->all(); $appname_list = array(); foreach ($application_list as $value) { $appname_list[] = $value['appname']; } #更新统计数据 if ($application_list) { foreach ($application_list as $oneAppValue) { if (empty($oneAppValue['lastupdatetime'])) { $oneAppValue['lateupdatetime'] = date('Y-m-d H:i:s'); } #获得最新统计数据 $nums = ErrorLog::find()->where('ApplicationId=:appid AND AddDate>=:addDate', [':appid' => $oneAppValue['appname'], ':addDate' => $oneAppValue['lastupdatetime']])->count(); #更新当前最新统计 $updatenums = $oneAppValue['logtotal'] + $nums; $lastupdatetime = date('Y-m-d H:i:s'); ApplicateName::updateAll(['logtotal' => $updatenums, 'lastupdatetime' => $lastupdatetime], 'id=:id', [':id' => $oneAppValue['id']]); } } #获得没有统计在ApplicateName里的错误信息 #由于有自动任务的处理所以每次更新的时候以前两天的数据再统计一次不存在的数据就可以了,以前两天为了有个时间宽度作保证 $lastvaluetime = date('Y-m-d H:i:s', strtotime('-2 day')); if ($appname_list) { $listString = implode("','", $appname_list); $listString = "'" . $listString . "'"; $sql = "insert into ApplicateName(`appname`,`newname`,`logtotal`,`logtype`,`lastupdatetime`) select ApplicationId,ApplicationId,count(*),0,now() from ErrorLog where ApplicationId not in({$listString}) AND AddDate>='{$lastvaluetime}' group by ApplicationId"; \Yii::$app->db->createCommand($sql)->execute(); } //重新获取ErrorLog的统计数据 $error_query = new Query(); $error_query->select("logtotal as total,appname as ApplicationId")->from("ApplicateName")->where(["in", "appname", $appname_list])->where("logtype=0")->groupBy("appname"); $error_count_list = $error_query->all(); return $error_count_list; }
/** * 获得限制的数据记录 * @param type $condition * @param type $conPrams * @param type $limit * @return type */ public static function findAppliName($condition, $conPrams, $limit, $order) { return ApplicateName::find()->asArray()->where($condition, $conPrams)->orderBy([$order => SORT_DESC])->limit($limit)->all(); }
/** * * @return type */ public static function findAll() { return ApplicateName::findAll($condition); }
/** * 错误类型 * @param bool $isReturnData * @return array */ public static function getTraceCategory($isReturnData = true) { if ($isReturnData) { //统计表数据 $appNameTotal = ApplicateName::find()->select("id,appname,logtotal,lastupdatetime")->where(['logtype' => 1])->asArray()->all(); if ($appNameTotal) { foreach ($appNameTotal as $oneAppValue) { if (empty($oneAppValue['lastupdatetime'])) { $oneAppValue['lateupdatetime'] = date('Y-m-d H:i:s'); } #获得最新统计数据 $nums = TraceLog::find()->where('ApplicationId=:appid AND AddDate>=:addDate', [':appid' => $oneAppValue['appname'], ':addDate' => $oneAppValue['lastupdatetime']])->count(); #更新当前最新统计 $updatenums = $oneAppValue['logtotal'] + $nums; $lastupdatetime = date('Y-m-d H:i:s'); ApplicateName::updateAll(['logtotal' => $updatenums, 'lastupdatetime' => $lastupdatetime], 'id=:id', [':id' => $oneAppValue['id']]); } $appname_list = ArrayHelper::map($appNameTotal, 'logtotal', 'appname'); } #获得没有统计在ApplicateName里的错误信息 #由于有自动任务的处理所以每次更新的时候以前两天的数据再统计一次不存在的数据就可以了,以前两天为了有个时间宽度作保证 $lastvaluetime = date('Y-m-d H:i:s', strtotime('-1 day')); if ($appname_list) { $listString = implode("','", $appname_list); $listString = "'" . $listString . "'"; $sql = "insert into ApplicateName(`appname`,`newname`,`logtotal`,`logtype`,`lastupdatetime`) select ApplicationId,ApplicationId,count(*),1,now() from TraceLog where ApplicationId not in({$listString}) AND AddDate>='{$lastvaluetime}' group by ApplicationId"; \Yii::$app->db->createCommand($sql)->execute(); } //重新获取TraceLog的统计数据 $error_query = new Query(); $error_query->select("logtotal as total,appname as ApplicationId")->from("ApplicateName")->where("logtype=1")->groupBy("appname"); $error_count_lists = $error_query->all(); $error_count_list = ArrayHelper::map($error_count_lists, 'ApplicationId', 'total'); //$total['name'] = '跟踪日志列表'; arsort($error_count_list); foreach ($error_count_list as $k => $v) { $category[] = trim($k); $total['data'][] = floatval($v); } return ['category' => $category, 'series' => [$total]]; } else { $ApplicationIds = ApplicateName::find()->select('appname as ApplicationId')->where(['logtype' => 1])->asArray()->all(); foreach ($ApplicationIds as $k => $v) { $category[] = trim($v['ApplicationId']); } if (!$isReturnData) { return $category; } } }
use backend\models\TraceLogSearch; use backend\models\ErrorLogSearch; use yii\grid\GridView; use yii\widgets\Pjax; use yii\helpers\Html; use yii\widgets\Breadcrumbs; #获得具体日志统计记录 $p_get = \Yii::$app->request->get(); $params = \Yii::$app->request->queryParams; ?> <div class="content-wrapper"> <section class="content-header"> <h1> <?php $typename = \common\models\ApplicateName::find("id=:id", [':id' => $p_get['id']])->one(); ?> <?php echo $typename->newname; ?> </h1> <?php echo Breadcrumbs::widget(['tag' => 'ol', 'itemTemplate' => "<li>{link}</li>\n", 'links' => [['label' => '错误信息', 'url' => ['/ajax/error/index']], ['label' => $typename->newname]]]); ?> </section> <!-- Main content --> <section class="content"> <div class="row"> <div class="col-xs-12"> <div class="box"> <div class="box-header">
public static function findAllLineEcharts() { #获得所有错误类型 $allErrorsType = \common\models\ApplicateName::find()->asArray()->select('appname')->where('logtype=0')->all(); foreach ($allErrorsType as $oneDate) { $fitAllErrorsType[] = $oneDate['appname']; } $whereString = implode(',', $fitAllErrorsType); #获得最近五天的数据 $fitDate = date('Y-m-d 00:00:00'); $after5Date = date('Y-m-d 00:00:00', strtotime('-5 day', strtotime($fitDate))); $fiveDateLists = \common\models\ErrorLogDay::find()->where(["ApplicationId" => $fitAllErrorsType])->andWhere('Date>:sd AND Date<=:ed', [':sd' => $after5Date, ':ed' => $fitDate])->asArray()->orderBy("Date desc")->All(); //如果为空,就取最后一个时间,以最后一个时间点为准 //标记是否为最新的数据 $isToday = true; if (empty($fiveDateLists)) { $lastDay = \common\models\ErrorLogDay::find()->orderBy("Id desc")->one(); if (empty($lastDay)) { return []; } $fitDate = date('Y-m-d 00:00:00', strtotime($lastDay->Date)); $after5Date = date('Y-m-d 00:00:00', strtotime('-5 day', strtotime($fitDate))); $fiveDateLists = \common\models\ErrorLogDay::find()->where(["ApplicationId" => $fitAllErrorsType])->andWhere('Date>:sd AND Date<=:ed', [':sd' => $after5Date, ':ed' => $fitDate])->asArray()->orderBy("Date desc")->All(); if (empty($fiveDateLists)) { return []; } $isToday = false; } #//开始处理这五天的数据 $fitdates = []; foreach ($fiveDateLists as $key => $oneDate) { //处理成2016-5-6形式的日期 $toIntDay = date("Y-m-d", strtotime($oneDate['Date'])); $fitdates[$oneDate['ApplicationId']][$toIntDay] = floatval($oneDate['Number']); } #对处理了的数据再处理一遍,不存在的数据加上0 $outCharts = []; $outCharts['categories'] = []; $outCharts['series'] = []; foreach ($fitdates as $key => $oneDate) { $start_date = date('Y-m-d', strtotime($fitDate)); for ($i = 1; $i <= 5; $i++) { if (!in_array($start_date, $outCharts['categories'])) { $outCharts['categories'][] = $start_date; } if (!isset($oneDate[$start_date])) { $fitdates[$key][$start_date] = 0; } $start_date = date('Y-m-d', strtotime("-1 day", strtotime($start_date))); } ksort($fitdates[$key]); } //处理一个上面显示的数据 foreach ($fitdates as $key => $oneDate) { $outCharts['series'][] = ['name' => $key, 'type' => 'line', 'smooth' => true, 'data' => array_values($oneDate)]; $outCharts['toptip'][] = $key; } $outCharts['categories'] = array_reverse($outCharts['categories']); //标记是否为今天的数据,如果是今天的数据,那么对今天的数据进行实时统计处理 if ($isToday) { $fitDate = date('Y-m-d 00:00:00'); $after1Date = date('Y-m-d 00:00:00', strtotime('+1 day', strtotime($fitDate))); //因为上面已经做了判断,所以今天的数据肯定会存在 //获得今天最新的数据 $errorstatusLists = \common\models\ErrorLog::find()->select("count(*) nums,ApplicationId")->where('AddDate>:sd AND AddDate<=:ed', [':sd' => $fitDate, ':ed' => $after1Date])->groupBy('ApplicationId')->indexBy('ApplicationId')->asArray()->all(); //对上面的数组进行处理 foreach ($outCharts['series'] as $key => $oneVaue) { //把最近一个元素推出 array_pop($outCharts['series'][$key]['data']); //推入一个新元素 //如果没有这个数组就推入一个0的数据 if (!isset($errorstatusLists[$oneVaue['name']]['nums'])) { array_push($outCharts['series'][$key]['data'], 0); } else { array_push($outCharts['series'][$key]['data'], floatval($errorstatusLists[$oneVaue['name']]['nums'])); } } } return $outCharts; }