Skip to content
31 changes: 31 additions & 0 deletions src/cls/IPM/General/History.cls
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,37 @@ ClassMethod DeleteHistory(ByRef filter) As %Integer
quit rs.%ROWCOUNT
}

/// Automatically purge old history based on retention settings
ClassMethod AutPurgeOldHistory()
{
if ..GetHistoryLastPurge() = +$horolog {
quit
}
set retainDays = ##class(%IPM.Repo.UniversalSettings).GetHistoryRetain()
quit:(+retainDays <=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General code style comment:
We generally dislike postconditionals for code readability. I'd personally make an exception for:

  • Loop exit conditions with simple tests (e.g., quit:key="" or quit:AtEnd)
  • Write:verbose !,"Not worth putting this in it's own if block"

Adding this to my list of things to formalize in https://github.com/intersystems/ipm/blob/main/CONTRIBUTING.md.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!. I've modified the post condition


set cutoffDate = $system.SQL.Functions.DATEADD("d", -retainDays, +$horolog)
set filter("TimeStart") = "<='" _cutoffDate_"'"
set tSC = ..DeleteHistoryGlobally(.filter, 0)
do ..SetHistoryLastPurge()
}

ClassMethod SetHistoryLastPurge(indx As %String = {##class(%IPM.Repo.UniversalSettings).#HistoryRetain})
{
new $namespace
return:(indx="") ""
set $namespace = "%SYS"
set ^IPM.settings(indx,"lastPurge") = +$horolog
}

ClassMethod GetHistoryLastPurge(indx As %String = {##class(%IPM.Repo.UniversalSettings).#HistoryRetain}) As %Integer
{
new $namespace
return:(indx="") ""
set $namespace = "%SYS"
return $get(^IPM.settings(indx,"lastPurge"))
}

ClassMethod DeleteHistoryGlobally(
ByRef filter,
verbose As %Boolean = 0) As %Integer
Expand Down
4 changes: 4 additions & 0 deletions src/cls/IPM/Main.cls
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ generate /my/path -export 00000,PacketName2,IgnorePacket2^00000,PacketName3,Igno
<example description="Show only installation history of package &quot;zpip&quot; in all namespaces ">history find -globally -Daction=install -Dpackage=zpip</example>
<example description="Delete all histories in the namespace">history delete -confirm</example>
<example description="Show details of history item with ID 3 and information for each undergone lifecyle phase">history details 3 -phases</example>
<example description="Enable history auto-purge. Example: config set HistoryRetain 10.">config set HistoryRetain "days" </example>
<example description="Disable history auto-purge">config set HistoryRetain 0</example>
<example description="Display the current history retention setting.">config get HistoryRetain</example>
</command>

</commands>
Expand All @@ -806,6 +809,7 @@ ClassMethod Shell(
{
set tSC = $$$OK
do ..ShellInternal(pCommand,.tException)
do ##class(%IPM.General.History).AutPurgeOldHistory()
if $isobject(tException) {
if pTerminateOnError {
do $system.Process.Terminate($job,1)
Expand Down
11 changes: 10 additions & 1 deletion src/cls/IPM/Repo/UniversalSettings.cls
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ Parameter UseStandalonePip = "UseStandalonePip";
/// Default value is 0, where 1.0.0-anystring is considered a pre-release of 1.0.0, hence 1.0.0-anystring < 1.0.0
Parameter SemVerPostRelease = "SemVerPostRelease";

Parameter CONFIGURABLE = "trackingId,analytics,ColorScheme,TerminalPrompt,PublishTimeout,PipCaller,UseStandalonePip,SemVerPostRelease,DefaultLogEntryLimit";
Parameter HistoryRetain = "history_retain";

Parameter CONFIGURABLE = "trackingId,analytics,ColorScheme,TerminalPrompt,PublishTimeout,PipCaller,UseStandalonePip,SemVerPostRelease,DefaultLogEntryLimit,HistoryRetain";

/// Returns configArray, that includes all configurable settings
ClassMethod GetAll(Output configArray) As %Status
Expand Down Expand Up @@ -181,4 +183,11 @@ ClassMethod SetAnalyticsAvailable(
return ..SetValue(..#analytics, +val, overwrite)
}

ClassMethod GetHistoryRetain(
val As %String,
overwrite As %Boolean = 1) As %Integer
{
return ..GetValue(..#HistoryRetain)
}

}
19 changes: 19 additions & 0 deletions tests/unit_tests/Test/PM/Unit/CLI.cls
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,23 @@ Method TestListPython() As %Status
quit sc
}

Method TestAutoPurgeOldHistory() As %Status
{
set sc = $$$OK
set commandsList = $listbuild("config set HistoryRetain 10", "config get HistoryRetain")
set ptr = 0
while $listnext(commandsList,ptr,command) {
do ..RunCommand(command)
}
do $$$LogMessage("Update HistoryRetain from 10 days to 1 day")
do ..RunCommand("config set HistoryRetain 1")
do $$$LogMessage("Purging old history entries older than 1 day")
do ##class(%IPM.General.History).AutPurgeOldHistory()
set afterPurge = ##class(%IPM.General.History).GetHistoryLastPurge("history_retain")
do $$$LogMessage("After purge timestamp: "_afterPurge)
do $$$LogMessage("rest set the HistoryRetain to default value by deleting the config")
do ..RunCommand("config delete HistoryRetain")
quit sc
}

}
Loading