mirror of
https://github.com/portainer/portainer.git
synced 2025-08-07 06:45:23 +02:00
refactor(activity-logs): migrate activity logs table to react [EE-4714] (#10891)
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
This commit is contained in:
parent
960d18998f
commit
c22d280491
29 changed files with 659 additions and 429 deletions
|
@ -615,24 +615,6 @@ input[type='checkbox'] {
|
|||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* json-tree override */
|
||||
json-tree {
|
||||
font-size: 13px;
|
||||
color: var(--blue-5);
|
||||
}
|
||||
|
||||
json-tree .key {
|
||||
color: var(--blue-3);
|
||||
padding-right: 5px;
|
||||
}
|
||||
|
||||
json-tree .branch-preview {
|
||||
font-style: normal;
|
||||
font-size: 11px;
|
||||
opacity: 0.5;
|
||||
}
|
||||
/* !json-tree override */
|
||||
|
||||
/* uib-progressbar override */
|
||||
.progress-bar {
|
||||
color: var(--text-progress-bar-color);
|
||||
|
|
|
@ -168,17 +168,6 @@ pre {
|
|||
background-color: var(--bg-pre-color);
|
||||
color: var(--text-pre-color);
|
||||
}
|
||||
json-tree .key {
|
||||
color: var(--text-json-tree-color);
|
||||
}
|
||||
|
||||
json-tree .leaf-value {
|
||||
color: var(--text-json-tree-leaf-color);
|
||||
}
|
||||
|
||||
json-tree .branch-preview {
|
||||
color: var(--text-json-tree-branch-preview-color);
|
||||
}
|
||||
|
||||
.progress {
|
||||
background-color: var(--bg-progress-color);
|
||||
|
|
13
app/portainer/react/views/activity-logs.ts
Normal file
13
app/portainer/react/views/activity-logs.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import angular from 'angular';
|
||||
|
||||
import { r2a } from '@/react-tools/react2angular';
|
||||
import { withUIRouter } from '@/react-tools/withUIRouter';
|
||||
import { ActivityLogsView } from '@/react/portainer/logs/ActivityLogsView/ActivityLogsView';
|
||||
import { withCurrentUser } from '@/react-tools/withCurrentUser';
|
||||
|
||||
export const activityLogsModule = angular
|
||||
.module('portainer.app.react.views.activity-logs', [])
|
||||
.component(
|
||||
'activityLogsView',
|
||||
r2a(withUIRouter(withCurrentUser(ActivityLogsView)), [])
|
||||
).name;
|
|
@ -18,6 +18,7 @@ import { teamsModule } from './teams';
|
|||
import { updateSchedulesModule } from './update-schedules';
|
||||
import { environmentGroupModule } from './env-groups';
|
||||
import { registriesModule } from './registries';
|
||||
import { activityLogsModule } from './activity-logs';
|
||||
|
||||
export const viewsModule = angular
|
||||
.module('portainer.app.react.views', [
|
||||
|
@ -26,6 +27,7 @@ export const viewsModule = angular
|
|||
updateSchedulesModule,
|
||||
environmentGroupModule,
|
||||
registriesModule,
|
||||
activityLogsModule,
|
||||
])
|
||||
.component(
|
||||
'homeView',
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
export default class ActivityLogsDatatableController {
|
||||
/* @ngInject */
|
||||
constructor($controller, $scope, PaginationService) {
|
||||
this.PaginationService = PaginationService;
|
||||
|
||||
this.tableKey = 'authLogs';
|
||||
|
||||
const $onInit = this.$onInit;
|
||||
angular.extend(this, $controller('GenericDatatableController', { $scope }));
|
||||
|
||||
this.changeSort = this.changeSort.bind(this);
|
||||
this.handleChangeLimit = this.handleChangeLimit.bind(this);
|
||||
this.$onInit = $onInit.bind(this);
|
||||
}
|
||||
|
||||
changeSort(key) {
|
||||
let desc = false;
|
||||
if (key === this.sort.key) {
|
||||
desc = !this.sort.desc;
|
||||
}
|
||||
|
||||
this.onChangeSort({ key, desc });
|
||||
}
|
||||
|
||||
handleChangeLimit(limit) {
|
||||
this.PaginationService.setPaginationLimit(this.tableKey, limit);
|
||||
this.onChangeLimit(limit);
|
||||
}
|
||||
|
||||
$onInit() {
|
||||
this.$onInitGeneric();
|
||||
|
||||
const limit = this.PaginationService.getPaginationLimit(this.tableKey);
|
||||
if (limit) {
|
||||
this.onChangeLimit(+limit);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
.activity-logs-datatable .small-column {
|
||||
width: 150px;
|
||||
}
|
|
@ -1,95 +0,0 @@
|
|||
<div class="datatable datatable-empty">
|
||||
<rd-widget>
|
||||
<rd-widget-body classes="no-padding">
|
||||
<div class="toolBar vertical-center flex-wrap !gap-x-5 !gap-y-1">
|
||||
<div class="toolBarTitle vertical-center">
|
||||
<div class="widget-icon space-right">
|
||||
<pr-icon icon="'history'"></pr-icon>
|
||||
</div>
|
||||
Activity Logs
|
||||
</div>
|
||||
<div class="vertical-center">
|
||||
<datatable-searchbar on-change="($ctrl.onChangeKeyword)" value="$ctrl.keyword"></datatable-searchbar>
|
||||
</div>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table-hover table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="small-column">
|
||||
<div class="vertical-center">
|
||||
<table-column-header
|
||||
col-title="'Time'"
|
||||
can-sort="true"
|
||||
is-sorted="$ctrl.sort.key === 'Timestamp'"
|
||||
is-sorted-desc="$ctrl.sort.key === 'Timestamp' && $ctrl.sort.desc"
|
||||
ng-click="$ctrl.changeSort('Timestamp')"
|
||||
>
|
||||
</table-column-header>
|
||||
</div>
|
||||
</th>
|
||||
<th class="small-column">
|
||||
<div class="vertical-center">
|
||||
<table-column-header
|
||||
col-title="'User'"
|
||||
can-sort="true"
|
||||
is-sorted="$ctrl.sort.key === 'Username'"
|
||||
is-sorted-desc="$ctrl.sort.key === 'Username' && $ctrl.sort.desc"
|
||||
ng-click="$ctrl.changeSort('Username')"
|
||||
>
|
||||
</table-column-header>
|
||||
</div>
|
||||
</th>
|
||||
<th class="small-column">
|
||||
<div class="vertical-center">
|
||||
<table-column-header
|
||||
col-title="'Environment'"
|
||||
can-sort="true"
|
||||
is-sorted="$ctrl.sort.key === 'Context'"
|
||||
is-sorted-desc="$ctrl.sort.key === 'Context' && $ctrl.sort.desc"
|
||||
ng-click="$ctrl.changeSort('Context')"
|
||||
>
|
||||
</table-column-header>
|
||||
</div>
|
||||
</th>
|
||||
|
||||
<th>
|
||||
<div class="vertical-center">
|
||||
<table-column-header col-title="'Action'" can-sort="false"> </table-column-header>
|
||||
</div>
|
||||
</th>
|
||||
<th>
|
||||
<div class="vertical-center">
|
||||
<table-column-header col-title="'Payload'" can-sort="false"> </table-column-header>
|
||||
</div>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr dir-paginate-start="item in $ctrl.logs | itemsPerPage: $ctrl.limit" total-items="$ctrl.totalItems" current-page="$ctrl.currentPage">
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr dir-paginate-end ng-show="item.Expanded">
|
||||
<td colspan="5">
|
||||
<json-tree object="item.payload" root-name="containerInfo.Id" start-expanded="true"></json-tree>
|
||||
</td>
|
||||
</tr>
|
||||
<tr ng-if="!$ctrl.logs">
|
||||
<td class="text-muted text-center" colspan="5">Loading...</td>
|
||||
</tr>
|
||||
<tr ng-if="$ctrl.logs.length === 0">
|
||||
<td class="text-muted text-center" colspan="8"> No logs available. </td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="footer" ng-if="$ctrl.logs">
|
||||
<datatable-pagination limit="$ctrl.limit" on-change-limit="($ctrl.handleChangeLimit)" on-change-page="($ctrl.onChangePage)"></datatable-pagination>
|
||||
</div>
|
||||
</rd-widget-body>
|
||||
</rd-widget>
|
||||
</div>
|
|
@ -1,24 +0,0 @@
|
|||
import './activity-logs-datatable.css';
|
||||
|
||||
import controller from './activity-logs-datatable.controller.js';
|
||||
|
||||
export const activityLogsDatatable = {
|
||||
templateUrl: './activity-logs-datatable.html',
|
||||
controller,
|
||||
bindings: {
|
||||
logs: '<',
|
||||
keyword: '<',
|
||||
sort: '<',
|
||||
limit: '<',
|
||||
totalItems: '<',
|
||||
currentPage: '<',
|
||||
feature: '@',
|
||||
|
||||
onChangeContextFilter: '<',
|
||||
onChangeKeyword: '<',
|
||||
onChangeSort: '<',
|
||||
|
||||
onChangeLimit: '<',
|
||||
onChangePage: '<',
|
||||
},
|
||||
};
|
|
@ -1,89 +0,0 @@
|
|||
import moment from 'moment';
|
||||
|
||||
import { FeatureId } from '@/react/portainer/feature-flags/enums';
|
||||
export default class ActivityLogsViewController {
|
||||
/* @ngInject */
|
||||
constructor($async, $scope, Notifications) {
|
||||
this.$async = $async;
|
||||
this.$scope = $scope;
|
||||
this.Notifications = Notifications;
|
||||
|
||||
this.limitedFeature = FeatureId.ACTIVITY_AUDIT;
|
||||
|
||||
this.state = {
|
||||
keyword: '',
|
||||
date: {
|
||||
from: 0,
|
||||
to: 0,
|
||||
},
|
||||
sort: {
|
||||
key: 'Timestamp',
|
||||
desc: true,
|
||||
},
|
||||
page: 1,
|
||||
limit: 10,
|
||||
totalItems: 0,
|
||||
logs: null,
|
||||
};
|
||||
|
||||
this.today = moment().endOf('day');
|
||||
this.minValidDate = moment().subtract(7, 'd').startOf('day');
|
||||
|
||||
this.onChangeDate = this.onChangeDate.bind(this);
|
||||
this.onChangeKeyword = this.onChangeKeyword.bind(this);
|
||||
this.onChangeSort = this.onChangeSort.bind(this);
|
||||
this.loadLogs = this.loadLogs.bind(this);
|
||||
this.onChangePage = this.onChangePage.bind(this);
|
||||
this.onChangeLimit = this.onChangeLimit.bind(this);
|
||||
}
|
||||
|
||||
onChangePage(page) {
|
||||
this.state.page = page;
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
onChangeLimit(limit) {
|
||||
this.state.page = 1;
|
||||
this.state.limit = limit;
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
onChangeSort(sort) {
|
||||
this.state.page = 1;
|
||||
this.state.sort = sort;
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
onChangeKeyword(keyword) {
|
||||
return this.$scope.$evalAsync(() => {
|
||||
this.state.page = 1;
|
||||
this.state.keyword = keyword;
|
||||
this.loadLogs();
|
||||
});
|
||||
}
|
||||
|
||||
onChangeDate({ startDate, endDate }) {
|
||||
this.state.page = 1;
|
||||
this.state.date = { to: endDate, from: startDate };
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
async loadLogs() {
|
||||
return this.$async(async () => {
|
||||
this.state.logs = null;
|
||||
try {
|
||||
const { logs, totalCount } = { logs: [{}, {}, {}, {}, {}], totalCount: 5 };
|
||||
this.state.logs = logs;
|
||||
this.state.totalItems = totalCount;
|
||||
} catch (err) {
|
||||
this.Notifications.error('Failure', err, 'Failed loading user activity logs');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$onInit() {
|
||||
return this.$async(async () => {
|
||||
this.loadLogs();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
<page-header title="'User Activity'" breadcrumbs="['Activity Logs']" reload="true"> </page-header>
|
||||
|
||||
<div class="be-indicator-container limited-be mx-4">
|
||||
<div>
|
||||
<div class="limited-be-link vertical-center"><be-feature-indicator feature="$ctrl.limitedFeature"></be-feature-indicator></div>
|
||||
<div class="limited-be-content">
|
||||
<rd-widget>
|
||||
<rd-widget-body>
|
||||
<div class="form-horizontal">
|
||||
<div class="form-group">
|
||||
<label for="dateRangeInput" class="col-sm-2 control-label text-left">Date Range</label>
|
||||
<div class="col-sm-6">
|
||||
<input type="text" class="form-control" disabled />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p class="text-muted small vertical-center">
|
||||
<pr-icon icon="'info'" class-name="'icon icon-sm icon-primary'"></pr-icon>
|
||||
Portainer user activity logs have a maximum retention of 7 days.
|
||||
</p>
|
||||
<div>
|
||||
<button type="button" class="btn btn-sm btn-primary" limited-feature-dir="{{::$ctrl.limitedFeature}}" limited-feature-class="limited-be" limited-feature-disabled>
|
||||
<pr-icon icon="'download'" class-name="'icon icon-sm'"></pr-icon>
|
||||
Export as CSV
|
||||
</button>
|
||||
</div>
|
||||
</rd-widget-body>
|
||||
</rd-widget>
|
||||
<div class="row mt-5">
|
||||
<activity-logs-datatable
|
||||
logs="$ctrl.state.logs"
|
||||
keyword="$ctrl.state.keyword"
|
||||
sort="$ctrl.state.sort"
|
||||
limit="$ctrl.state.limit"
|
||||
context-filter="$ctrl.state.contextFilter"
|
||||
total-items="$ctrl.state.totalItems"
|
||||
current-page="$ctrl.state.currentPage"
|
||||
feature="{{:: $ctrl.limitedFeature}}"
|
||||
on-change-keyword="($ctrl.onChangeKeyword)"
|
||||
on-change-sort="($ctrl.onChangeSort)"
|
||||
on-change-limit="($ctrl.onChangeLimit)"
|
||||
on-change-page="($ctrl.onChangePage)"
|
||||
></activity-logs-datatable>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -1,6 +0,0 @@
|
|||
import controller from './activity-logs-view.controller.js';
|
||||
|
||||
export const activityLogsView = {
|
||||
templateUrl: './activity-logs-view.html',
|
||||
controller,
|
||||
};
|
|
@ -1,9 +0,0 @@
|
|||
import angular from 'angular';
|
||||
|
||||
import { activityLogsView } from './activity-logs-view';
|
||||
import { activityLogsDatatable } from './activity-logs-datatable';
|
||||
|
||||
export default angular
|
||||
.module('portainer.app.user-activity.activity-logs-view', [])
|
||||
.component('activityLogsDatatable', activityLogsDatatable)
|
||||
.component('activityLogsView', activityLogsView).name;
|
|
@ -3,9 +3,15 @@ import angular from 'angular';
|
|||
import { NotificationsViewAngular } from '@/react/portainer/notifications/NotificationsView';
|
||||
import { AccessHeaders } from '../authorization-guard';
|
||||
import authLogsViewModule from './auth-logs-view';
|
||||
import activityLogsViewModule from './activity-logs-view';
|
||||
import { UserActivityService } from './user-activity.service';
|
||||
import { UserActivity } from './user-activity.rest';
|
||||
|
||||
export default angular.module('portainer.app.user-activity', [authLogsViewModule, activityLogsViewModule]).component('notifications', NotificationsViewAngular).config(config).name;
|
||||
export default angular
|
||||
.module('portainer.app.user-activity', [authLogsViewModule])
|
||||
.service('UserActivity', UserActivity)
|
||||
.service('UserActivityService', UserActivityService)
|
||||
.component('notifications', NotificationsViewAngular)
|
||||
.config(config).name;
|
||||
|
||||
/* @ngInject */
|
||||
function config($stateRegistryProvider) {
|
||||
|
|
28
app/portainer/user-activity/user-activity.rest.js
Normal file
28
app/portainer/user-activity/user-activity.rest.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { baseHref } from '@/portainer/helpers/pathHelper';
|
||||
|
||||
/* @ngInject */
|
||||
export function UserActivity($resource, $http) {
|
||||
const BASE_URL = baseHref() + 'api/useractivity';
|
||||
|
||||
const resource = $resource(
|
||||
`${BASE_URL}/:action`,
|
||||
{},
|
||||
{
|
||||
authLogs: { method: 'GET', params: { action: 'authlogs' } },
|
||||
}
|
||||
);
|
||||
|
||||
return { authLogsAsCSV, ...resource };
|
||||
|
||||
async function authLogsAsCSV(params) {
|
||||
return $http({
|
||||
method: 'GET',
|
||||
url: `${BASE_URL}/authlogs.csv`,
|
||||
params,
|
||||
responseType: 'blob',
|
||||
headers: {
|
||||
'Content-type': 'text/csv',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
13
app/portainer/user-activity/user-activity.service.js
Normal file
13
app/portainer/user-activity/user-activity.service.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
/* @ngInject */
|
||||
export function UserActivityService(FileSaver, UserActivity) {
|
||||
return { authLogs, saveAuthLogsAsCSV };
|
||||
|
||||
function authLogs(offset, limit, sort, keyword, date, contexts, types) {
|
||||
return UserActivity.authLogs({ offset, limit, keyword, before: date.to, after: date.from, sortBy: sort.key, sortDesc: sort.desc, contexts, types }).$promise;
|
||||
}
|
||||
|
||||
async function saveAuthLogsAsCSV(sort, keyword, date, contexts, types) {
|
||||
const response = await UserActivity.authLogsAsCSV({ keyword, before: date.to, after: date.from, sortBy: sort.key, sortDesc: sort.desc, limit: 2000, contexts, types });
|
||||
return FileSaver.saveAs(response.data, 'logs.csv');
|
||||
}
|
||||
}
|
30
app/react/components/JsonTree.css
Normal file
30
app/react/components/JsonTree.css
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* json-tree override */
|
||||
.json-tree,
|
||||
json-tree {
|
||||
font-size: 13px;
|
||||
color: var(--blue-5);
|
||||
}
|
||||
|
||||
.json-tree .key,
|
||||
json-tree .key {
|
||||
color: var(--text-json-tree-color);
|
||||
}
|
||||
|
||||
json-tree .key {
|
||||
padding-right: 5px;
|
||||
}
|
||||
|
||||
.json-tree .branch-preview,
|
||||
json-tree .branch-preview {
|
||||
color: var(--text-json-tree-branch-preview-color);
|
||||
font-style: normal;
|
||||
font-size: 11px;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.json-tree .leaf-value,
|
||||
json-tree .leaf-value {
|
||||
color: var(--text-json-tree-leaf-color);
|
||||
}
|
||||
|
||||
/* !json-tree override */
|
41
app/react/components/JsonTree.tsx
Normal file
41
app/react/components/JsonTree.tsx
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { ComponentProps } from 'react';
|
||||
import { JsonView, defaultStyles } from 'react-json-view-lite';
|
||||
import 'react-json-view-lite/dist/index.css';
|
||||
import clsx from 'clsx';
|
||||
|
||||
import './JsonTree.css';
|
||||
|
||||
export function JsonTree({ style, ...props }: ComponentProps<typeof JsonView>) {
|
||||
const currentStyle = getCurrentStyle(style);
|
||||
return (
|
||||
<JsonView
|
||||
// eslint-disable-next-line react/jsx-props-no-spreading
|
||||
{...props}
|
||||
style={currentStyle}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
type StyleProps = ComponentProps<typeof JsonView>['style'];
|
||||
|
||||
function getCurrentStyle(style: StyleProps | undefined): StyleProps {
|
||||
if (style) {
|
||||
return style;
|
||||
}
|
||||
|
||||
return {
|
||||
...defaultStyles,
|
||||
container: 'json-tree',
|
||||
booleanValue: 'leaf-value',
|
||||
nullValue: 'leaf-value',
|
||||
otherValue: 'leaf-value',
|
||||
numberValue: 'leaf-value',
|
||||
stringValue: 'leaf-value',
|
||||
undefinedValue: 'leaf-value',
|
||||
label: 'key',
|
||||
punctuation: 'leaf-value',
|
||||
collapseIcon: clsx(defaultStyles.collapseIcon, 'key'),
|
||||
expandIcon: clsx(defaultStyles.expandIcon, 'key'),
|
||||
collapsedContent: clsx(defaultStyles.collapsedContent, 'branch-preview'),
|
||||
};
|
||||
}
|
|
@ -16,6 +16,9 @@ import { TextTip } from '@@/Tip/TextTip';
|
|||
|
||||
import { FormValues } from './types';
|
||||
|
||||
import 'react-datetime-picker/dist/DateTimePicker.css';
|
||||
import 'react-calendar/dist/Calendar.css';
|
||||
|
||||
interface Props {
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
|
110
app/react/portainer/logs/ActivityLogsView/ActivityLogsTable.tsx
Normal file
110
app/react/portainer/logs/ActivityLogsView/ActivityLogsTable.tsx
Normal file
|
@ -0,0 +1,110 @@
|
|||
import { createColumnHelper } from '@tanstack/react-table';
|
||||
import { History, Search } from 'lucide-react';
|
||||
|
||||
import { isoDateFromTimestamp } from '@/portainer/filters/filters';
|
||||
|
||||
import { ExpandableDatatable } from '@@/datatables/ExpandableDatatable';
|
||||
import { Button } from '@@/buttons';
|
||||
import { JsonTree } from '@@/JsonTree';
|
||||
|
||||
import { ActivityLog } from './types';
|
||||
import { getSortType } from './useActivityLogs';
|
||||
|
||||
const columnHelper = createColumnHelper<ActivityLog>();
|
||||
|
||||
const columns = [
|
||||
columnHelper.accessor('timestamp', {
|
||||
id: 'Timestamp',
|
||||
header: 'Time',
|
||||
cell: ({ getValue }) => {
|
||||
const value = getValue();
|
||||
return value ? isoDateFromTimestamp(value) : '';
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor('username', {
|
||||
id: 'Username',
|
||||
header: 'User',
|
||||
}),
|
||||
columnHelper.accessor('context', {
|
||||
id: 'Context',
|
||||
header: 'Environment',
|
||||
}),
|
||||
columnHelper.accessor('action', {
|
||||
id: 'Action',
|
||||
header: 'Action',
|
||||
}),
|
||||
columnHelper.accessor('payload', {
|
||||
header: 'Payload',
|
||||
enableSorting: false,
|
||||
cell: ({ row, getValue }) =>
|
||||
getValue() ? (
|
||||
<Button color="link" onClick={() => row.toggleExpanded()} icon={Search}>
|
||||
inspect
|
||||
</Button>
|
||||
) : null,
|
||||
}),
|
||||
];
|
||||
|
||||
export function ActivityLogsTable({
|
||||
dataset,
|
||||
currentPage,
|
||||
keyword,
|
||||
limit,
|
||||
onChangeKeyword,
|
||||
onChangeLimit,
|
||||
onChangePage,
|
||||
onChangeSort,
|
||||
sort,
|
||||
totalItems,
|
||||
}: {
|
||||
keyword: string;
|
||||
onChangeKeyword(keyword: string): void;
|
||||
sort: { id: string; desc: boolean } | undefined;
|
||||
onChangeSort(sort: { id: string; desc: boolean } | undefined): void;
|
||||
limit: number;
|
||||
onChangeLimit(limit: number): void;
|
||||
currentPage: number;
|
||||
onChangePage(page: number): void;
|
||||
totalItems: number;
|
||||
dataset?: Array<ActivityLog>;
|
||||
}) {
|
||||
return (
|
||||
<ExpandableDatatable<ActivityLog>
|
||||
title="Activity Logs"
|
||||
titleIcon={History}
|
||||
columns={columns}
|
||||
dataset={dataset || []}
|
||||
isLoading={!dataset}
|
||||
settingsManager={{
|
||||
pageSize: limit,
|
||||
search: keyword,
|
||||
setPageSize: onChangeLimit,
|
||||
setSearch: onChangeKeyword,
|
||||
setSortBy: (id, desc) =>
|
||||
onChangeSort({ id: getSortType(id) || 'Timestamp', desc }),
|
||||
sortBy: sort
|
||||
? {
|
||||
id: sort.id,
|
||||
desc: sort.desc,
|
||||
}
|
||||
: undefined,
|
||||
}}
|
||||
page={currentPage}
|
||||
onPageChange={onChangePage}
|
||||
isServerSidePagination
|
||||
totalCount={totalItems}
|
||||
disableSelect
|
||||
renderSubRow={(row) => <SubRow item={row.original} />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function SubRow({ item }: { item: ActivityLog }) {
|
||||
return (
|
||||
<tr>
|
||||
<td colSpan={Number.MAX_SAFE_INTEGER}>
|
||||
<JsonTree data={item.payload} />
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
import { useState } from 'react';
|
||||
|
||||
import { PageHeader } from '@@/PageHeader';
|
||||
import { useTableStateWithoutStorage } from '@@/datatables/useTableState';
|
||||
import { BEOverlay } from '@@/BEFeatureIndicator/BEOverlay';
|
||||
|
||||
import { FeatureId } from '../../feature-flags/enums';
|
||||
|
||||
import { ActivityLogsTable } from './ActivityLogsTable';
|
||||
import { useActivityLogs, getSortType } from './useActivityLogs';
|
||||
import { useExportMutation } from './useExportMutation';
|
||||
import { FilterBar } from './FilterBar';
|
||||
|
||||
export function ActivityLogsView() {
|
||||
const exportMutation = useExportMutation();
|
||||
const [range, setRange] = useState<
|
||||
{ start: Date; end: Date | null } | undefined
|
||||
>(undefined);
|
||||
const [page, setPage] = useState(0);
|
||||
const tableState = useTableStateWithoutStorage('Timestamp');
|
||||
const offset = page * tableState.pageSize;
|
||||
|
||||
const query = {
|
||||
offset,
|
||||
limit: tableState.pageSize,
|
||||
sortBy: getSortType(tableState.sortBy?.id),
|
||||
desc: tableState.sortBy?.desc,
|
||||
search: tableState.search,
|
||||
...(range
|
||||
? {
|
||||
after: seconds(range?.start?.valueOf()),
|
||||
before: seconds(range?.end?.valueOf()),
|
||||
}
|
||||
: undefined),
|
||||
};
|
||||
|
||||
const logsQuery = useActivityLogs(query);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader title="User Activity" breadcrumbs="Activity Logs" reload />
|
||||
|
||||
<div className="mx-4">
|
||||
<BEOverlay featureId={FeatureId.ACTIVITY_AUDIT}>
|
||||
<FilterBar
|
||||
value={range}
|
||||
onChange={setRange}
|
||||
onExport={handleExport}
|
||||
/>
|
||||
|
||||
<div className="-mx-[15px] mt-4">
|
||||
<ActivityLogsTable
|
||||
sort={tableState.sortBy}
|
||||
onChangeSort={(value) =>
|
||||
tableState.setSortBy(value?.id, value?.desc || false)
|
||||
}
|
||||
limit={tableState.pageSize}
|
||||
onChangeLimit={tableState.setPageSize}
|
||||
keyword={tableState.search}
|
||||
onChangeKeyword={tableState.setSearch}
|
||||
currentPage={page}
|
||||
onChangePage={setPage}
|
||||
totalItems={logsQuery.data?.totalCount || 0}
|
||||
dataset={logsQuery.data?.logs}
|
||||
/>
|
||||
</div>
|
||||
</BEOverlay>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
function handleExport() {
|
||||
exportMutation.mutate(query);
|
||||
}
|
||||
}
|
||||
|
||||
function seconds(ms?: number) {
|
||||
if (!ms) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return Math.floor(ms / 1000);
|
||||
}
|
45
app/react/portainer/logs/ActivityLogsView/FilterBar.tsx
Normal file
45
app/react/portainer/logs/ActivityLogsView/FilterBar.tsx
Normal file
|
@ -0,0 +1,45 @@
|
|||
import { DownloadIcon } from 'lucide-react';
|
||||
|
||||
import { Widget } from '@@/Widget';
|
||||
import { TextTip } from '@@/Tip/TextTip';
|
||||
import { Button } from '@@/buttons';
|
||||
import { BEFeatureIndicator } from '@@/BEFeatureIndicator';
|
||||
|
||||
import { FeatureId } from '../../feature-flags/enums';
|
||||
import { DateRangePicker } from '../components/DateRangePicker';
|
||||
|
||||
export function FilterBar({
|
||||
value,
|
||||
onChange,
|
||||
onExport,
|
||||
}: {
|
||||
value: { start: Date; end: Date | null } | undefined;
|
||||
onChange: (value?: { start: Date; end: Date | null }) => void;
|
||||
onExport: () => void;
|
||||
}) {
|
||||
return (
|
||||
<Widget>
|
||||
<Widget.Body>
|
||||
<form className="form-horizontal">
|
||||
<DateRangePicker value={value} onChange={onChange} />
|
||||
|
||||
<TextTip color="blue">
|
||||
Portainer user activity logs have a maximum retention of 7 days.
|
||||
</TextTip>
|
||||
|
||||
<div className="mt-4">
|
||||
<Button
|
||||
color="primary"
|
||||
icon={DownloadIcon}
|
||||
onClick={onExport}
|
||||
className="!ml-0"
|
||||
>
|
||||
Export as CSV
|
||||
</Button>
|
||||
<BEFeatureIndicator featureId={FeatureId.ACTIVITY_AUDIT} />
|
||||
</div>
|
||||
</form>
|
||||
</Widget.Body>
|
||||
</Widget>
|
||||
);
|
||||
}
|
8
app/react/portainer/logs/ActivityLogsView/types.ts
Normal file
8
app/react/portainer/logs/ActivityLogsView/types.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
export interface ActivityLog {
|
||||
timestamp: number;
|
||||
action: string;
|
||||
context: string;
|
||||
id: number;
|
||||
payload: object;
|
||||
username: string;
|
||||
}
|
58
app/react/portainer/logs/ActivityLogsView/useActivityLogs.ts
Normal file
58
app/react/portainer/logs/ActivityLogsView/useActivityLogs.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { useQuery } from 'react-query';
|
||||
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { isBE } from '../../feature-flags/feature-flags.service';
|
||||
|
||||
import { ActivityLog } from './types';
|
||||
|
||||
export const sortKeys = ['Context', 'Action', 'Timestamp', 'Username'] as const;
|
||||
export type SortKey = (typeof sortKeys)[number];
|
||||
export function isSortKey(value?: string): value is SortKey {
|
||||
return !!value && sortKeys.includes(value as SortKey);
|
||||
}
|
||||
export function getSortType(value?: string): SortKey | undefined {
|
||||
return isSortKey(value) ? value : undefined;
|
||||
}
|
||||
|
||||
export interface Query {
|
||||
offset: number;
|
||||
limit: number;
|
||||
sortBy?: SortKey;
|
||||
desc?: boolean;
|
||||
search: string;
|
||||
after?: number;
|
||||
before?: number;
|
||||
}
|
||||
|
||||
export function useActivityLogs(query: Query) {
|
||||
return useQuery({
|
||||
queryKey: ['activityLogs', query] as const,
|
||||
queryFn: () => fetchActivityLogs(query),
|
||||
keepPreviousData: true,
|
||||
});
|
||||
}
|
||||
|
||||
interface ActivityLogsResponse {
|
||||
logs: Array<ActivityLog>;
|
||||
totalCount: number;
|
||||
}
|
||||
|
||||
async function fetchActivityLogs(query: Query): Promise<ActivityLogsResponse> {
|
||||
try {
|
||||
if (!isBE) {
|
||||
return {
|
||||
logs: [{}, {}, {}, {}, {}] as Array<ActivityLog>,
|
||||
totalCount: 5,
|
||||
};
|
||||
}
|
||||
|
||||
const { data } = await axios.get<ActivityLogsResponse>(
|
||||
'/useractivity/logs',
|
||||
{ params: query }
|
||||
);
|
||||
return data;
|
||||
} catch (err) {
|
||||
throw parseAxiosError(err, 'Failed loading user activity logs csv');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
import { useMutation } from 'react-query';
|
||||
import { saveAs } from 'file-saver';
|
||||
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { Query } from './useActivityLogs';
|
||||
|
||||
export function useExportMutation() {
|
||||
return useMutation({
|
||||
mutationFn: exportActivityLogs,
|
||||
});
|
||||
}
|
||||
|
||||
async function exportActivityLogs(query: Omit<Query, 'limit'>) {
|
||||
try {
|
||||
const { data, headers } = await axios.get<Blob>('/useractivity/logs.csv', {
|
||||
params: { ...query, limit: 2000 },
|
||||
responseType: 'blob',
|
||||
headers: {
|
||||
'Content-type': 'text/csv',
|
||||
},
|
||||
});
|
||||
|
||||
const contentDispositionHeader = headers['content-disposition'] || '';
|
||||
const filename =
|
||||
contentDispositionHeader.replace('attachment; filename=', '').trim() ||
|
||||
'logs.csv';
|
||||
saveAs(data, filename);
|
||||
} catch (err) {
|
||||
throw parseAxiosError(err, 'Failed loading user activity logs csv');
|
||||
}
|
||||
}
|
65
app/react/portainer/logs/components/DateRangePicker.tsx
Normal file
65
app/react/portainer/logs/components/DateRangePicker.tsx
Normal file
|
@ -0,0 +1,65 @@
|
|||
import WojtekmajRangePicker from '@wojtekmaj/react-daterange-picker';
|
||||
import { Calendar, X } from 'lucide-react';
|
||||
import { date, object, SchemaOf } from 'yup';
|
||||
import { FormikErrors } from 'formik';
|
||||
|
||||
import '@wojtekmaj/react-daterange-picker/dist/DateRangePicker.css';
|
||||
import 'react-calendar/dist/Calendar.css';
|
||||
|
||||
import { FormControl } from '@@/form-components/FormControl';
|
||||
|
||||
import 'react-datetime-picker/dist/DateTimePicker.css';
|
||||
|
||||
type Value = { start: Date; end: Date | null };
|
||||
|
||||
export function DateRangePicker({
|
||||
value,
|
||||
onChange,
|
||||
name,
|
||||
error,
|
||||
}: {
|
||||
value: Value | undefined;
|
||||
onChange: (value?: Value) => void;
|
||||
name?: string;
|
||||
error?: FormikErrors<Value>;
|
||||
}) {
|
||||
return (
|
||||
<FormControl label="Date Range" errors={error}>
|
||||
<div className="w-1/2">
|
||||
<WojtekmajRangePicker
|
||||
format="y-MM-dd"
|
||||
className="form-control [&>div]:border-0"
|
||||
value={value ? [value.start, value.end] : null}
|
||||
onChange={(date) => {
|
||||
if (!date) {
|
||||
onChange(undefined);
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(date)) {
|
||||
if (date.length === 2 && date[0] && date[1]) {
|
||||
onChange({
|
||||
start: date[0],
|
||||
end: date[1],
|
||||
});
|
||||
return;
|
||||
}
|
||||
onChange(undefined);
|
||||
return;
|
||||
}
|
||||
onChange({ start: date, end: null });
|
||||
}}
|
||||
name={name}
|
||||
calendarIcon={<Calendar />}
|
||||
clearIcon={<X />}
|
||||
/>
|
||||
</div>
|
||||
</FormControl>
|
||||
);
|
||||
}
|
||||
|
||||
export function dateRangePickerValidation(): SchemaOf<Value> {
|
||||
return object({
|
||||
start: date().required(),
|
||||
end: date().nullable().default(null).required(),
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue