Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
maarchRM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maarch
maarchRM
Merge requests
!792
You need to sign in or sign up before continuing.
Feat/24620 journal 24h
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Feat/24620 journal 24h
feat/24620_journal_24h
into
develop
Overview
0
Commits
27
Pipelines
1
Changes
1
Merged
Jerome Boucher
requested to merge
feat/24620_journal_24h
into
develop
1 year ago
Overview
0
Commits
27
Pipelines
1
Changes
1
Expand
0
0
Merge request reports
Viewing commit
6e2c7d18
Prev
Next
Show latest version
1 file
+
2
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Verified
6e2c7d18
Fix error when no journal and searching first event
· 6e2c7d18
Cyril Vazquez
authored
1 year ago
src/bundle/audit/Controller/journal.php
+
142
−
70
Options
@@ -29,10 +29,11 @@ class journal
protected
$sdoFactory
;
protected
$eventController
;
protected
$logController
;
protected
$archiveController
;
protected
$separateInstance
;
/**
* Constructor
* @param \dependency\sdo\Factory $sdoFactory
@@ -42,94 +43,120 @@ class journal
{
$this
->
sdoFactory
=
$sdoFactory
;
$this
->
eventController
=
\laabs
::
newController
(
'audit/event'
);
$this
->
logController
=
\laabs
::
newController
(
'recordsManagement/log'
);
$this
->
archiveController
=
\laabs
::
newController
(
'recordsManagement/archive'
);
$this
->
separateInstance
=
$separateInstance
;
}
/**
* Chain the last journal
*
* @return string The chained journal file name
*/
public
function
chainJournal
()
{
$logController
=
\laabs
::
newController
(
'recordsManagement/log'
);
$tmpdir
=
\laabs
::
getTmpDir
();
{
// Get start date from previous journal or first event ever written
$previousJournal
=
$this
->
logController
->
getLastJournal
(
'application'
);
if
(
$previousJournal
)
{
$previousJournalId
=
$previousJournal
->
archiveId
;
$fromDate
=
$previousJournal
->
toDate
;
}
else
{
$previousJournalId
=
null
;
$firstEvents
=
$this
->
sdoFactory
->
find
(
'audit/event'
,
null
,
null
,
"<eventDate"
,
0
,
1
);
$firstEvent
=
$firstEvents
[
0
];
$fromDate
=
$firstEvent
->
eventDate
;
}
$newJournal
=
\laabs
::
newInstance
(
'recordsManagement/log'
);
$newJournal
->
archiveId
=
\laabs
::
newId
();
$newJournal
->
type
=
"application"
;
$newJournal
->
toDate
=
\laabs
::
newTimestamp
();
// End date is now, will be lowered to start of current second
$toDate
=
\laabs
::
newTimestamp
();
// Get ranges of max 24h
$dateRanges
=
$this
->
getTimeRanges
(
$fromDate
,
$toDate
);
// Lopp over ranges to create journals
$startTime
=
$fromDate
;
foreach
(
$dateRanges
as
$endTime
)
{
// Event after startTime up to endTime
$journal
=
$this
->
getJournal
(
$startTime
,
$endTime
,
$previousJournal
);
$events
=
$this
->
eventController
->
byDate
(
$startTime
,
$endTime
);
$this
->
writeJournalFile
(
$journal
,
$events
,
$previousJournal
);
$previousJournal
=
$journal
;
$startTime
=
$endTime
;
}
}
protected
function
getTimeRanges
(
$fromDate
,
$toDate
)
{
// If less than 24 hours ellapsed since last journal consider all range up to start of current second
$diff
=
$toDate
->
diff
(
$fromDate
);
if
(
$diff
->
d
==
0
)
{
return
[
\laabs
::
newTimestamp
(
$toDate
->
format
(
'Y-m-d\TH:i:s'
)
.
".000000Z"
)
];
}
// Get events to write in the new journal
$previousJournal
=
$logController
->
getLastJournal
(
'application'
);
if
(
$previousJournal
)
{
$newJournal
->
fromDate
=
$previousJournal
->
toDate
;
$newJournal
->
previousJournalId
=
$previousJournal
->
archiveId
;
// If more than 24 hours ellapsed since last journal get ranges for max 24h
$interval
=
\laabs
::
newDuration
(
'PT24H'
);
$eod
=
"T23:59:59.999999Z"
;
$sod
=
"T00:00:00.000000Z"
;
$sos
=
".000000Z"
;
// Keep only days discard time
$fromDay
=
\laabs
::
newDate
(
$fromDate
->
format
(
'Y-m-d'
));
$toDay
=
\laabs
::
newDate
(
$toDate
->
format
(
'Y-m-d'
));
$timeRanges
=
[];
// First range finishes the start day
$timeRanges
[]
=
\laabs
::
newTimestamp
(
$fromDay
.
$eod
);
// Get range exclusing start date
$dateRange
=
new
\DatePeriod
(
$fromDay
,
$interval
,
$toDay
,
1
);
foreach
(
$dateRange
as
$date
)
{
$timeRanges
[]
=
\laabs
::
newTimestamp
(
$date
->
format
(
'Y-m-d'
)
.
$eod
);
}
$queryString
=
"eventDate > '
$newJournal->fromDate
' AND eventDate <= '
$newJournal->toDate
'"
;
// Last range starts the end day
$timeRanges
[]
=
\laabs
::
newTimestamp
(
$toDate
->
format
(
'Y-m-d\TH:i:s'
)
.
$sos
);
if
(
$this
->
separateInstance
)
{
$queryString
.
=
"AND instanceName = '"
.
\laabs
::
getInstanceName
()
.
"'"
;
}
return
$timeRanges
;
}
$events
=
$this
->
sdoFactory
->
find
(
'audit/event'
,
$queryString
,
[],
"<eventDate"
);
public
function
getJournal
(
$fromDate
,
$toDate
,
$previousJournal
=
null
)
{
$journal
=
\laabs
::
newInstance
(
'recordsManagement/log'
);
$journal
->
archiveId
=
\laabs
::
newId
();
$journal
->
type
=
"application"
;
// If end of previous day, set start time to 00:00 current day
if
(
$fromDate
->
format
(
'u'
)
==
'999999'
)
{
$interval
=
\laabs
::
newDuration
(
'PT1S'
);
$fromDate
->
add
(
$interval
);
$sos
=
".000000Z"
;
$fromDate
=
\laabs
::
newTimestamp
(
$fromDate
->
format
(
'Y-m-d\TH:i:s'
)
.
$sos
);
}
}
else
{
// No previous journal, select all events
$events
=
$this
->
sdoFactory
->
find
(
'audit/event'
,
"eventDate <= '
$newJournal->toDate
'"
,
null
,
"<eventDate"
);
if
(
count
(
$events
)
>
0
)
{
$newJournal
->
fromDate
=
reset
(
$events
)
->
eventDate
;
}
else
{
$newJournal
->
fromDate
=
\laabs
::
newTimestamp
(
'1970-01-01'
);
}
$journal
->
fromDate
=
$fromDate
;
$journal
->
toDate
=
$toDate
;
if
(
$previousJournal
)
{
$journal
->
previousJournalId
=
$previousJournal
->
archiveId
;
}
$journal
Filename
=
$tmpdir
.
DIRECTORY_SEPARATOR
.
(
string
)
$newJournal
->
archiveId
.
".csv"
;
$journalFile
=
fopen
(
$journalFilename
,
"w"
);
return
$journal
;
}
// First event : chain with
previous
j
ournal
$eventLine
=
array
();
$
eventLine
[
0
]
=
(
string
)
$newJournal
->
archiveId
;
$
eventLine
[
1
]
=
(
string
)
$
newJ
ournal
->
fromDate
;
$
eventLine
[
2
]
=
(
string
)
$newJournal
->
toDate
;
protected
function
writeJournalFile
(
$journal
,
$events
,
$
previous
J
ournal
=
null
)
{
$
tmpdir
=
\laabs
::
getTmpDir
()
;
$
journalFilename
=
$tmpdir
.
DIRECTORY_SEPARATOR
.
(
string
)
$
j
ournal
->
archiveId
.
".csv"
;
$
journalFile
=
fopen
(
$journalFilename
,
"w"
)
;
// Write previous journal informations
if
(
$previousJournal
)
{
$eventLine
[
3
]
=
(
string
)
$previousJournal
->
archiveId
;
$archiveController
=
\laabs
::
newController
(
'recordsManagement/archive'
);
$resources
=
$archiveController
->
getDigitalResources
(
$previousJournal
->
archiveId
);
$journalResource
=
$resources
[
0
];
$eventLine
[
4
]
=
(
string
)
$journalResource
->
hashAlgorithm
;
$eventLine
[
5
]
=
(
string
)
$journalResource
->
hash
;
$eventLine
=
$this
->
getPreviousJournalEvent
(
$journal
,
$previousJournal
);
fputcsv
(
$journalFile
,
$eventLine
);
}
fputcsv
(
$journalFile
,
$eventLine
);
// Write events
foreach
(
$events
as
$event
)
{
$eventLine
=
array
();
$eventLine
[]
=
(
string
)
$event
->
eventDate
;
$eventLine
[]
=
(
string
)
$event
->
accountId
;
$eventLine
[]
=
(
string
)
$event
->
path
;
$eventLine
[]
=
(
string
)
$event
->
status
;
if
(
isset
(
$event
->
output
)
&&
!
empty
(
$output
))
{
$output
=
json_decode
(
$event
->
output
);
if
(
is_object
(
$output
)
||
is_array
(
$output
))
{
$messages
=
[];
foreach
(
$output
as
$value
)
{
$messages
[]
=
$value
->
fullMessage
;
}
$eventLine
[]
=
implode
(
' '
,
$messages
);
}
elseif
(
is_scalar
(
$output
))
{
$eventLine
[]
=
$output
;
}
}
$eventLine
=
$this
->
getEventLine
(
$event
);
fputcsv
(
$journalFile
,
$eventLine
);
}
@@ -147,6 +174,51 @@ class journal
}
}
return
$logController
->
archiveJournal
(
$journalFilename
,
$newJournal
,
$timestampFileName
);
return
$this
->
logController
->
archiveJournal
(
$journalFilename
,
$journal
,
$timestampFileName
);
}
protected
function
getPreviousJournalEvent
(
$journal
,
$previousJournal
)
{
// First event : chain with previous journal
$eventLine
=
array
();
$eventLine
[
0
]
=
(
string
)
$journal
->
archiveId
;
$eventLine
[
1
]
=
(
string
)
$journal
->
fromDate
;
$eventLine
[
2
]
=
(
string
)
$journal
->
toDate
;
// Write previous journal informations
if
(
$previousJournal
)
{
$eventLine
[
3
]
=
(
string
)
$previousJournal
->
archiveId
;
$resources
=
$this
->
archiveController
->
getDigitalResources
(
$previousJournal
->
archiveId
);
$journalResource
=
$resources
[
0
];
$eventLine
[
4
]
=
(
string
)
$journalResource
->
hashAlgorithm
;
$eventLine
[
5
]
=
(
string
)
$journalResource
->
hash
;
}
return
$eventLine
;
}
protected
function
getEventLine
(
$event
)
{
$eventLine
=
array
();
$eventLine
[]
=
(
string
)
$event
->
eventDate
;
$eventLine
[]
=
(
string
)
$event
->
accountId
;
$eventLine
[]
=
(
string
)
$event
->
path
;
$eventLine
[]
=
(
string
)
$event
->
status
;
if
(
isset
(
$event
->
output
))
{
$output
=
json_decode
(
$event
->
output
);
if
(
$output
)
{
$messages
=
[];
foreach
(
$output
as
$value
)
{
$messages
[]
=
$value
->
fullMessage
;
}
$eventLine
[]
=
implode
(
' '
,
$messages
);
}
}
return
$eventLine
;
}
}
Loading