-
Notifications
You must be signed in to change notification settings - Fork 17
task/WP-930: Audit Trails UI #1594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
erikriv16
wants to merge
33
commits into
main
Choose a base branch
from
task/WP-930
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 11 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d08025a
Adding audit trail to dropdown under Welcome, user! under "manage acc…
4ae5c54
New audit trails page made
8d47648
Returning JSON for most recent session with username as input woring
6857f50
Working UI for recent session, good starting point
8c3e670
Added pop-up modal functionality for data column (now named Details),…
e6458ca
Merge remote-tracking branch 'origin/main' into task/WP-930
530138a
Added antd modal feature, data at bottom of modal, another good start…
8077e97
Merge main into task/WP-930
2287234
Initial push
e06ce9e
Removed comments
31f2134
Delete unnecessary add-ons, linting error fixes
4d1876b
Added extra lines to files, added hooks, minor changes to audittrail.…
7ddcc49
Update designsafe/apps/accounts/templates/designsafe/apps/accounts/ba…
erikriv16 12ce212
Merge branch 'main' into task/WP-930
fnets 76f98e3
Fixed react/server side linting errors
fd31273
Added seperate file for table and added unit tests
5cb1a85
Good saving point, file portal search somewhat working, good exmaples…
3779d92
Another good saving point, going to try to rework search, have submit…
8776e11
Added functioning file search feature
2cf8247
Removed tests.py, fixed linting errors
0459011
Fixed linting errors
d497c04
last lint check
915d9af
Merge branch 'main' into task/WP-930
rstijerina c955edb
Merge branch 'main' into task/WP-930
fnets ee165cf
Added host to middle column dropdown
1268a9e
New API Response for frontend, stil need modifications to frontend
2437698
Refactor of UI for updated API return, added utils folder, WORKING
2c9b4de
Username filter on file search added
b3ad9bf
Submit Job action added
11d9812
File Search w/ Tapis Events
4aa4672
Merge branch 'main' into task/WP-930
erikriv16 73391b2
Format Checks
416ca52
Update main.tsx after rebase
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,357 @@ | ||
| import React, { useEffect, useState, useRef } from 'react'; | ||
| import styles from './AuditTrails.module.css'; | ||
| import { Modal } from 'antd'; | ||
|
|
||
| type PortalAuditApiResponse = { | ||
| data: PortalAuditEntry[]; | ||
| }; | ||
|
|
||
| type PortalAuditEntry = { | ||
| session_id: string; | ||
| timestamp: string; | ||
| portal: string; | ||
| username: string; | ||
| action: string; | ||
| tracking_id: string; | ||
| data: any; | ||
| }; | ||
|
|
||
| /* Not being used */ | ||
| type TapisFilesAuditApiResponse = { | ||
| data: TapisFilesAuditEntry[]; | ||
| }; | ||
|
|
||
| /* Will change depending on requirements for tapis file audit UI / not being used */ | ||
| type TapisFilesAuditEntry = { | ||
| writer_logtime: string; | ||
| action: string; | ||
| jwt_tenant: string; | ||
| jwt_user: string; | ||
| target_system_id: string; | ||
| target_path: string; | ||
| source_path: string; | ||
| tracking_id: string; | ||
| parent_tracking_id: string; | ||
| data: string; | ||
| }; | ||
|
|
||
| const AuditTrail: React.FC = () => { | ||
| const [username, setUsername] = useState(''); | ||
| const [source, setSource] = useState('portal'); | ||
| const [data, setData] = useState<PortalAuditApiResponse | null>(null); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [loading, setLoading] = useState(false); | ||
| const [allUsernames, setAllUsernames] = useState<string[]>([]); | ||
| const [filteredUsernames, setFilteredUsernames] = useState<string[]>([]); | ||
| const [showDropdown, setShowDropdown] = useState(false); | ||
| const containerRef = useRef<HTMLDivElement>(null); //dropdown closing on exit click | ||
| const [modalOpen, setModalOpen] = useState(false); | ||
| const [modalContent, setModalContent] = useState<string>(''); | ||
| const [footerEntry, setFooterEntry] = useState<PortalAuditEntry | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| function handleClickOutside(event: MouseEvent) { | ||
rstijerina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( | ||
| containerRef.current && | ||
| !containerRef.current.contains(event.target as Node) | ||
| ) { | ||
| setShowDropdown(false); | ||
| } | ||
| } | ||
| document.addEventListener('mousedown', handleClickOutside); | ||
| return () => { | ||
| document.removeEventListener('mousedown', handleClickOutside); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| fetch('/audit/api/usernames/portal') | ||
| .then((res) => res.json()) | ||
| .then((data) => setAllUsernames(data.usernames || [])); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (username.length > 0) { | ||
| setFilteredUsernames( | ||
| allUsernames | ||
| .filter((name) => name.toLowerCase().includes(username.toLowerCase())) | ||
| .slice(0, 20) | ||
| ); | ||
| if ( | ||
| allUsernames.some( | ||
| (name) => name.toLowerCase() === username.toLowerCase() | ||
| ) | ||
| ) { | ||
| setShowDropdown(false); | ||
| } | ||
| } else { | ||
| setFilteredUsernames([]); | ||
| setShowDropdown(false); | ||
| } | ||
| }, [username]); | ||
|
Check warning on line 91 in client/src/audit/AuditTrail.tsx
|
||
|
|
||
| const handleSubmit = async (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
| setError(null); | ||
| setData(null); | ||
| setLoading(true); | ||
| try { | ||
| const endpoint = source === 'portal' ? 'portal' : 'tapis'; | ||
| const res = await fetch(`/audit/api/user/${username}/${endpoint}/`); | ||
| console.log('username:', username); | ||
| console.log('source:', endpoint); | ||
|
|
||
| if (!res.ok) { | ||
| const errText = await res.text(); | ||
| throw new Error(`API error: ${res.status} ${errText}`); | ||
| } | ||
| const result = await res.json(); | ||
| console.log('APO RESPOSNE:', result); | ||
| setData(result); | ||
| } catch (err: any) { | ||
| setError(err.message || 'Unknown error'); | ||
| } | ||
| setLoading(false); | ||
| console.log(data); | ||
| }; | ||
|
|
||
| function truncate(str: string, n: number) { | ||
| return str.length > n ? str.slice(0, n) + '…' : str; | ||
| } | ||
|
|
||
| const extractActionData = (entry: PortalAuditEntry): string => { | ||
| if (!entry.data) return '-'; | ||
|
|
||
| try { | ||
| const action = entry.action?.toLowerCase(); | ||
| const parsedData = | ||
| typeof entry.data == 'string' ? JSON.parse(entry.data) : entry.data; | ||
| switch (action) { | ||
| case 'submitjob': | ||
| return extractDataField(parsedData, 'body.job.name') || '-'; | ||
|
|
||
| case 'getapp': | ||
| return extractDataField(parsedData, 'query.appId') || '-'; | ||
|
|
||
| case 'trash': | ||
| return extractDataField(parsedData, 'path') || '-'; | ||
|
|
||
| case 'upload': | ||
| return extractDataField(parsedData, 'path') || '-'; | ||
|
|
||
| case 'download': | ||
| return extractDataField(parsedData, 'filePath') || '-'; | ||
| } | ||
| } catch { | ||
| return '-'; | ||
| } | ||
| return '-'; | ||
| }; | ||
|
|
||
| const extractDataField = (data: any, path: string): string => { | ||
| if (!data) return '-'; | ||
| const fields = path.split('.'); | ||
| let value = data; | ||
| for (let i = 0; i < fields.length; i++) { | ||
| if (value && typeof value === 'object' && fields[i] in value) { | ||
| value = value[fields[i]]; | ||
| } else { | ||
| return '-'; | ||
| } | ||
| } | ||
| if (value === undefined || value == null || value === '') { | ||
| return '-'; | ||
| } | ||
| return String(value); | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <Modal | ||
| title="Details" | ||
| open={modalOpen} | ||
| onCancel={() => setModalOpen(false)} | ||
| footer={ | ||
| footerEntry && ( | ||
| <div | ||
| style={{ | ||
| marginTop: '-30px', | ||
| marginBottom: '10px', | ||
| textAlign: 'center', | ||
| }} | ||
| > | ||
| {footerEntry.username} | {footerEntry.timestamp} |{' '} | ||
| {footerEntry.portal} | {footerEntry.action} | ||
| </div> | ||
| ) | ||
| } | ||
| width={550} | ||
| style={{ | ||
| maxHeight: '70vh', | ||
| overflow: 'auto', | ||
| top: '200px', | ||
| }} | ||
| > | ||
| <pre style={{ whiteSpace: 'pre-wrap', wordBreak: 'break-all' }}> | ||
| {modalContent} | ||
| </pre> | ||
| </Modal> | ||
| {/*<h2>Audit Trail Test</h2>*/} | ||
fnets marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <form onSubmit={handleSubmit} style={{ marginBottom: 16 }}> | ||
| <div style={{ display: 'inline-flex', alignItems: 'center' }}> | ||
| <select | ||
| value={source} | ||
| onChange={(e) => setSource(e.target.value)} | ||
| style={{ marginRight: 8 }} | ||
| > | ||
| <option value="portal">Most Recent User Session Data</option> | ||
| <option value="tapis">File Search Data</option> | ||
| </select> | ||
| <div | ||
| ref={containerRef} | ||
| style={{ position: 'relative', display: 'inline-block' }} | ||
| > | ||
| <input | ||
| value={username} | ||
| onChange={(e) => { | ||
| setUsername(e.target.value); | ||
| setShowDropdown(source === 'portal'); | ||
| }} | ||
| onFocus={() => { | ||
| setShowDropdown(source === 'portal'); | ||
| }} | ||
| placeholder="Username/File Name:" | ||
| style={{ marginRight: 8, width: '100%' }} | ||
| /> | ||
| {showDropdown && | ||
| source === 'portal' && | ||
| filteredUsernames.length > 0 && ( | ||
| <ul className={styles.dropdownList}> | ||
| {filteredUsernames.map((name) => ( | ||
| <li | ||
| key={name} | ||
| onClick={() => { | ||
| setUsername(name); | ||
| setShowDropdown(false); | ||
| }} | ||
| style={{ | ||
| padding: '8px', | ||
| cursor: 'pointer', | ||
| borderBottom: '1px solid', | ||
| }} | ||
| > | ||
| {name} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| </div> | ||
| <button | ||
| type="submit" | ||
| disabled={loading || !username} | ||
| style={{ marginLeft: '8px' }} | ||
| > | ||
| {loading ? 'Loading…' : 'Submit'} | ||
| </button> | ||
| </div> | ||
| </form> | ||
|
|
||
| {error && <div style={{ color: 'red' }}>Error: {error}</div>} | ||
|
|
||
| {data?.data && data.data.length === 0 && ( | ||
| <div>No audit records found.</div> | ||
| )} | ||
|
|
||
| {data?.data && data.data.length > 0 && ( | ||
| <table | ||
| style={{ | ||
| width: '100%', | ||
| borderCollapse: 'collapse', | ||
| tableLayout: 'fixed', | ||
| }} | ||
| > | ||
| <thead> | ||
| <tr> | ||
| <th className={styles.headerCell} style={{ width: '50px' }}> | ||
| User | ||
| </th> | ||
| <th className={styles.headerCell} style={{ width: '50px' }}> | ||
| Date | ||
| </th> | ||
| <th className={styles.headerCell} style={{ width: '50px' }}> | ||
| Time | ||
| </th> | ||
| <th className={styles.headerCell} style={{ width: '100px' }}> | ||
| Portal | ||
| </th> | ||
| <th className={styles.headerCell} style={{ width: '200px' }}> | ||
| Action | ||
| </th> | ||
| <th className={styles.headerCell} style={{ width: '200px' }}> | ||
| Tracking ID | ||
| </th> | ||
| <th className={styles.headerCell} style={{ width: '100px' }}> | ||
| Details | ||
| </th> | ||
| </tr> | ||
| </thead> | ||
fnets marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <tbody> | ||
| {data.data.map((entry, idx) => { | ||
| let dateStr = '-'; | ||
| let timeStr = '-'; | ||
| if (entry.timestamp) { | ||
| const date = new Date(entry.timestamp); | ||
| dateStr = date.toLocaleDateString(); | ||
| timeStr = date.toLocaleTimeString(); | ||
| } | ||
| const actionDetails = extractActionData(entry); | ||
|
|
||
| return ( | ||
| <tr key={idx}> | ||
| <td className={styles.cell}>{entry.username || '-'}</td> | ||
| <td className={styles.cell}>{dateStr}</td> | ||
| <td className={styles.cell}>{timeStr}</td> | ||
| <td className={styles.cell}>{entry.portal || '-'}</td> | ||
| <td className={styles.cell}> | ||
| {entry.action || '-'} | ||
| {actionDetails !== '-' && | ||
| `: ${truncate(actionDetails, 50)}`} | ||
| </td> | ||
| <td className={styles.cell}>{entry.tracking_id || '-'}</td> | ||
| <td | ||
| className={styles.cell} | ||
| style={{ | ||
| wordBreak: 'break-all', | ||
| cursor: 'pointer', | ||
| textDecoration: 'underline', | ||
| }} | ||
| onClick={() => { | ||
| let content = ''; | ||
| if (entry.data) { | ||
| try { | ||
| const obj = | ||
| typeof entry.data === 'string' | ||
| ? JSON.parse(entry.data) | ||
| : entry.data; | ||
| content = JSON.stringify(obj, null, 2); | ||
| } catch { | ||
| content = entry.data; | ||
| } | ||
| } | ||
| setModalContent(content); | ||
| setFooterEntry(entry); | ||
| setModalOpen(true); | ||
| }} | ||
| > | ||
| View Logs | ||
| </td> | ||
| </tr> | ||
| ); | ||
| })} | ||
| </tbody> | ||
| </table> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
| export default AuditTrail; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| .dropdownList { | ||
| position: absolute; | ||
| background: white; | ||
| border: 1px solid #ccc; | ||
| width: 100%; | ||
| max-height: 185px; | ||
| overflow-y: auto; | ||
| padding: 0; | ||
| list-style: none; | ||
| z-index: 10; | ||
| } | ||
|
|
||
| .headerCell { | ||
| border: 1px solid var(--global-color-primary--dark); | ||
| padding: 10px; | ||
| text-align: center; | ||
| background: var(--global-color-primary--normal); | ||
| } | ||
|
|
||
| .cell { | ||
| border: 1px solid #ccc; | ||
| padding: 8px; | ||
| overflow: hidden; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.