/** * @return array */ public function actionIndex() { Yii::$app->response->format = Response::FORMAT_JSON; $data = Json::decode(Yii::$app->request->post('data')); if (!isset($data['auditEntry'])) { $entry = Audit::getInstance()->getEntry(true); $data['auditEntry'] = $entry->id; } // Convert data into the loggable object $javascript = new models\AuditJavascript(); $map = ['auditEntry' => 'entry_id', 'message' => 'message', 'type' => 'type', 'file' => 'origin', 'line' => function ($value) use($javascript) { $javascript->origin .= ':' . $value; }, 'col' => function ($value) use($javascript) { $javascript->origin .= ':' . $value; }, 'data' => function ($value) use($javascript) { if (count($value)) { $javascript->data = $value; } }]; foreach ($map as $key => $target) { if (isset($data[$key])) { if (is_callable($target)) { $target($data[$key]); } else { $javascript->{$target} = $data[$key]; } } } if ($javascript->save()) { return ['result' => 'ok', 'entry' => $data['auditEntry']]; } return ['result' => 'error', 'errors' => $javascript->getErrors()]; }
public function actionLog() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $data = \yii\helpers\Json::decode(\Yii::$app->request->post('data')); $entry = null; if (isset($data['auditEntry'])) { $entry = models\AuditEntry::findOne($data['auditEntry']); } else { return ['result' => 'error', 'message' => 'No audit entry to attach to']; } // Convert data into the loggable object $javascript = new models\AuditJavascript(); $map = ['auditEntry' => 'audit_id', 'message' => 'message', 'type' => 'type', 'file' => 'origin', 'line' => function ($value) use($javascript) { $javascript->origin .= ':' . $value; }, 'col' => function ($value) use($javascript) { $javascript->origin .= ':' . $value; }, 'data' => function ($value) use($javascript) { if (count($value)) { $javascript->data = $value; } }]; foreach ($map as $key => $target) { if (isset($data[$key])) { if (is_callable($target)) { $target($data[$key]); } else { $javascript->{$target} = $data[$key]; } } } if ($javascript->save()) { return ['result' => 'ok']; } return ['result' => 'error', 'errors' => $javascript->getErrors()]; }
/** * @return array */ public function originFilter() { $origin = AuditJavascript::getDb()->cache(function () { return AuditJavascript::find()->distinct(true)->select('origin')->where(['entry_id' => $this->entry_id])->andWhere(['is not', 'origin', null])->groupBy('origin')->orderBy('origin ASC')->column(); }, 30); return array_combine($origin, $origin); }
/** * Displays a single AuditJavascript model. * @param integer $id * @return mixed * @throws NotFoundHttpException */ public function actionView($id) { $model = AuditJavascript::findOne($id); if (!$model) { throw new NotFoundHttpException('The requested javascript does not exist.'); } return $this->render('view', ['model' => $model]); }
/** * @inheritdoc */ public function cleanup($maxAge = null) { $maxAge = $maxAge !== null ? $maxAge : $this->maxAge; if ($maxAge === null) { return false; } return AuditJavascript::deleteAll(['<=', 'created', date('Y-m-d 23:59:59', strtotime("-{$maxAge} days"))]); }
/** * Clean up the audit data according to the settings. */ public function actionCleanup() { /** @var Audit $audit */ $audit = Yii::$app->getModule('audit'); if ($audit->maxAge === null) { return; } $entry = AuditEntry::tableName(); $errors = AuditError::tableName(); $data = AuditData::tableName(); $javascript = AuditJavascript::tableName(); $threshold = time() - $audit->maxAge * 86400; AuditEntry::getDb()->createCommand(<<<SQL DELETE FROM {$entry}, {$errors}, {$data}, {$javascript} USING {$entry} INNER JOIN {$errors} ON {$errors}.entry_id = {$entry}.id INNER JOIN {$data} ON {$data}.entry_id = {$entry}.id INNER JOIN {$javascript} ON {$javascript}.entry_id = {$entry}.id WHERE {$entry}.created < FROM_UNIXTIME({$threshold}) SQL )->execute(); }
/* @var $this yii\web\View */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('audit', 'Audit Module'); $this->params['breadcrumbs'][] = $this->title; $this->registerCss('canvas {width: 100% !important;height: 400px;}'); $dataSet = ['fillColor' => "rgba(151,187,205,0.5)", 'strokeColor' => "rgba(151,187,205,1)", 'pointColor' => "rgba(151,187,205,1)", 'pointStrokeColor' => "#fff"]; $options = ['height' => 400, 'width' => 400]; $days = []; $count = ['entry' => [], 'trail' => [], 'javascript' => [], 'error' => []]; foreach (range(-6, 0) as $day) { $date = strtotime($day . 'days'); $days[] = date('D: Y-m-d', $date); $count['entry'][] = AuditEntry::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count(); $count['trail'][] = AuditTrail::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count(); $count['mail'][] = AuditMail::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count(); $count['javascript'][] = AuditJavascript::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count(); $count['error'][] = AuditError::find()->where(['between', 'created', date('Y-m-d 00:00:00', $date), date('Y-m-d 23:59:59', $date)])->count(); } //fake data //foreach ($count as $type => $data) { // foreach ($data as $k => $v) { // if (!$v) { // $v = $type == 'entry' ? rand(100, 1000) : rand(0, 100); // $count[$type][$k] = $v; // } // } //} ?> <div class="audit-index"> <h1><?php
/** * Returns all linked AuditJavascript instances * @return AuditJavascript[] */ public function getJavascript() { return static::hasMany(AuditJavascript::className(), ['audit_id' => 'id']); }
/** * Clean up the audit data according to the settings. * Can be handy if you are offloading the data somewhere and want to keep only the most recent entries readily available */ public function truncate() { if ($this->maxAge === null) { return; } $entry = models\AuditEntry::tableName(); $errors = models\AuditError::tableName(); $data = models\AuditData::tableName(); $javascript = models\AuditJavascript::tableName(); $threshold = time() - $this->maxAge * 86400; models\AuditEntry::getDb()->createCommand(<<<SQL DELETE FROM {$entry}, {$errors}, {$data}, {$javascript} USING {$entry} INNER JOIN {$errors} ON {$errors}.audit_id = {$entry}.id INNER JOIN {$data} ON {$data}.audit_id = {$entry}.id INNER JOIN {$javascript} ON {$javascript}.audit_id = {$entry}.id WHERE {$entry}.created < FROM_UNIXTIME({$threshold}) SQL )->execute(); }