Skip to content
Open
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- #950: Added support for listing installed Python packages using `list -python`, `list -py` and `list-installed -python`
- #822: The CPF resource processor now supports system expressions and macros in CPF merge files
- #578 Added functionality to record and display IPM history of install, uninstall, load, and update
- #992: Implement automatic history purge logic

### Changed
- #316: All parameters, except developer mode, included with a `load`, `install` or `update` command will be propagated to dependencies
Expand Down
37 changes: 37 additions & 0 deletions src/cls/IPM/General/History.cls
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,43 @@ ClassMethod DeleteHistory(ByRef filter) As %Integer
quit rs.%ROWCOUNT
}

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

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
if indx="" {
quit
}
set $namespace = "%SYS"
set ^IPM.settings(indx,"lastPurge") = +$horolog
}

ClassMethod GetHistoryLastPurge(indx As %String = {##class(%IPM.Repo.UniversalSettings).#HistoryRetain}) As %Integer
{
new $namespace
if indx="" {
quit
}
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).AutoPurgeOldHistory()
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).AutoPurgeOldHistory()
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
}

}