Skip to content

Commit

Permalink
auto label unlabelled actions
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Jun 18, 2023
1 parent b4644f4 commit 91d4604
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Release Notes for Activity Log

## 1.5.0 - 2023-06-18
### Fixed
- Do not load natively labeled actions on Actions page

### Improved
- Auto label unlabeled actions on Actions filter and Actions widget

## 1.4.2 - 2023-06-18
### Fixed
- Unicode for column url throws error when title is in Chinese [#9](https://github.com/matfish2/craft-activity-log/issues/9)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ return [
The `$this` object in this context will be an instance of the request class (`craft\web\Request`).
Only requests satisfying the condition (returning `true`) will be recorded.

The user can also give labels to all recorded actions for ease of search.
Actions are automatically labelled using a naming convention. E.g ["fields","save-group"] will become "Fields Save Group".
This is relevant for the "Action" search dropdown on the Logs page and for the Actions widget on the Statistics page.
In addition the user can optionally override this convention by giving explicit labels to recorded actions under the Actions page.
![craft4 test_adminos_settings_activity-logs_actions_site=default](https://user-images.githubusercontent.com/1510460/190848960-05dc091f-fe01-4e96-ade1-aba8600b00a9.png)

Requests can be viewed and filtered under the Activity Log page.
Expand Down
17 changes: 13 additions & 4 deletions src/controllers/ActionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class ActionsController extends Controller
{
public function actionIndex()
{
// 1. Get all actions (not-native) from actions table
// 1. Get all natively labelled actions in order to exclude from page
$native = ActivityLogAction::find()->where('native=1')->all();

// 2. Get all non-native actions from actions table in order to allow editing
$actions1 = ActivityLogAction::find()->where('native=0')->all();
$actions1 = array_map(static function ($action) {
return [
Expand All @@ -23,12 +26,18 @@ public function actionIndex()
];
}, $actions1);

$existingActions = array_map(static function ($action) {
// 3. Merge native and non-native actions in order to exclude from search in logs
$nonnative = array_map(static function ($action) {
return "'" . $action['action'] . "'";
}, $actions1);

$native = array_map(static function ($action) {
return "'" . $action->action . "'";
}, $native);

$existingActions = array_merge($nonnative, $native);

// 2. Get all unlabeled actions from activity logs table
// 4. Get all unlabeled actions from activity logs table
$existingActionsExp = count($existingActions) > 0 ? implode(',', $existingActions) : "'xxx'";
$actions2 = ActivityLog::find()->select('actionSegments')
->where('actionSegments is not null AND actionSegments not in (' . $existingActionsExp . ')')
Expand All @@ -42,7 +51,7 @@ public function actionIndex()
];
}, $actions2);

// 3. merge 1 and 2
// 5. merge actions from actions table with actions from logs table
$actions = array_merge($actions1, $actions2);

return $this->renderTemplate('activity-logs/actions', ['actions' => $actions]);
Expand Down
30 changes: 29 additions & 1 deletion src/controllers/ActivityLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use craft\records\Site;
use craft\web\assets\dashboard\DashboardAsset;
use matfish\ActivityLog\ActivityLogAssetBundle;
use matfish\ActivityLog\helpers\ActionSegmentsToLabel;
use matfish\ActivityLog\records\ActivityLog;
use matfish\ActivityLog\records\ActivityLogAction;
use matfish\ActivityLog\records\ActivityLogWidget;
use matfish\ActivityLog\services\Stats\WidgetsHandler;
Expand Down Expand Up @@ -138,12 +140,38 @@ public function actionInitialData(): Response
$res = [
'sites' => Site::find()->select(['id', 'name'])->all(),
'svgPath' => $this->getSvgPath(),
'actions' => ActivityLogAction::find()->all()
'actions' => $this->getActionsList()
];

return $this->asJson($res);
}

protected function getActionsList(): array
{
$labeled = ActivityLogAction::find()->all();
$labeledArr = array_map(static function ($action) {
return "'" . $action['action'] . "'";
}, $labeled);

$labeledExp = count($labeledArr) > 0 ? implode(',', $labeledArr) : "'xxx'";

$unlabeled = ActivityLog::find()->select('actionSegments')
->where('actionSegments is not null AND actionSegments not in (' . $labeledExp . ')')
->groupBy('actionSegments')->all();

$unlabeled = array_map(/**
* @throws \JsonException
*/ static function ($action) {
return [
'id' => null,
'action' => $action->actionSegments,
'label' => ActionSegmentsToLabel::convert($action->actionSegments)
];
}, $unlabeled);

return array_merge($labeled, $unlabeled);
}

public function actionData(): Response
{
$res = (new VueTablesActivityLogRetriever())->retrieve();
Expand Down
24 changes: 24 additions & 0 deletions src/helpers/ActionSegmentsToLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace matfish\ActivityLog\helpers;

class ActionSegmentsToLabel
{
public static function convert(string $segments): string
{
try {
$x = array_map(static function ($segment) {
$p = explode('-', $segment);
$r = array_map(static function ($s) {
return ucfirst($s);
}, $p);

return implode(' ', $r);
}, json_decode($segments, true, 512, JSON_THROW_ON_ERROR));

return implode(' ', $x);
} catch (\Throwable $e) {
return $segments;
}
}
}
13 changes: 11 additions & 2 deletions src/services/Stats/Retrievers/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace matfish\ActivityLog\services\Stats\Retrievers;

use matfish\ActivityLog\helpers\ActionSegmentsToLabel;
use matfish\ActivityLog\services\Stats\OthersTrait;
use matfish\ActivityLog\services\Stats\Stats;

Expand All @@ -14,13 +15,21 @@ class Actions extends Stats
protected function getData(): array
{
$res = $this->query()
->select(['{{%activitylog_actions}}.[[label]] name', 'count(*) n'])
->select(['IF({{%activitylog_actions}}.[[label]] IS NULL,0,1) has_label','IF({{%activitylog_actions}}.[[label]] IS NOT NULL,{{%activitylog_actions}}.[[label]],{{%activitylog}}.[[actionSegments]]) name', 'count(*) n'])
->andWhere('{{%activitylog}}.[[isAction]]=1')
->leftJoin('{{%users}}', '{{%users}}.[[id]]={{%activitylog}}.[[userId]]')
->innerJoin('{{%activitylog_actions}}', '{{%activitylog_actions}}.[[action]]={{%activitylog}}.[[actionSegments]]')
->leftJoin('{{%activitylog_actions}}', '{{%activitylog_actions}}.[[action]]={{%activitylog}}.[[actionSegments]]')
->orderBy('count(*) DESC')
->groupBy('[[actionSegments]]')->all();

$res = array_map(static function($x) {
if ($x['has_label']==='0') {
$x['name'] = ActionSegmentsToLabel::convert($x['name']);
}

return $x;
},$res);

$resWithOther = $this->groupOthers($res);

return $this->toKeyValuePairs($resWithOther, 'name', 'n');
Expand Down

0 comments on commit 91d4604

Please sign in to comment.