Domino Code Fragment

Code Name*
Purge Agent
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.220.160.216
Description*
Created By: Teresa Deane on 02/07/96 at 03:58 PM
Category: System Minimum required Notes: Notes 4.0
Author: Teresa Deane
Company: Iris Associates
E-Mail Address: InterNotes@iris.com Description of the Agent: This agent manages the database size based on selections in the administration note. It is set to run at 1:00AM nightly. It is a system agent which ships with the Web
Navigator template. Please refer to the Web Navigator Administration Guide for more information. Agent Script:
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
Sub Initialize

Dim session As New NotesSession
Dim webdb As NotesDatabase
Dim webview As notesview
Dim webcoll As notesdocumentcollection
Dim adminview As notesview
Dim admindoc As notesdocument
Dim currentdoc As NotesDocument
Dim expdate As Variant
Dim currdate As Variant
Dim cuttoff As New notesdatetime ("1/1/94")
Dim purge As Variant
Dim private_purge As Variant
Dim db_size_limit As Double
Dim db_size_admin As Variant
Dim db_pct_goal As Variant
Dim db_size_goal As Double
Dim age_criteria As Variant
Dim size_criteria As Variant
Dim origdate As Variant
Dim offsetdate As Variant
Dim lower As Long
Dim rating_del As Variant
Dim rating_purge_period As Variant
Dim purge_expired As Variant
Dim rating_offsetdate As Variant
Dim docs_deleted As Integer
Dim agent As NotesAgent
Dim DebugFlag As String
Dim InterNotesServer As String

'*
'* We have started! Check for debug flag in the configuration
'* file to see if we should log debug information.
'*
DebugFlag = Session.GetEnvironmentString("WebDebugPurgeAgent",True)

If ( DebugFlag = "1" ) Then
'*
'* We are going to write a whole bunch of stuff to the Notes log to
'* help "understand" which Web documents get purged and why.
'*
Print "=============================================================="
Print "Starting in debug mode..."
Print Session.NotesVersion
Print Session.Platform
Print "Last modified by: " + Session.EffectiveUserName
Print "Last run on: " + Cdat(Session.LastRun)
Print "Last exit status: " + Str(Session.LastExitStatus)
Print "=============================================================="
End If
'*
'* We need the InterNotes server name for this agent so we can find
'* the correct Admin document. If this agent is running on the client,
'* then the server name can be gotten from the database object. If
'* this agent is running on the server, then get the name from the
'* user name on the session.
'*
Set webdb = session.CurrentDatabase

If ( Session.IsOnServer ) Then
InterNotesServer = Session.Username
Else
InterNotesServer = WebDB.Server
End If

If ( DebugFlag = "1" ) Then
Print "InterNotes server: " + InterNotesServer
End If

'*
'* Now all of the fields are read from the admin document and assigned
'* to the specified variables. All databases and views are also
'* assigned and set to the proper values. All print statements at this
'* point are for debugging and will be pared down in the FCS version.
'*
'* Set agent = session.CurrentAgent
'* servername = agent.ServerName
Set adminview = webdb.getview("($Admin)")
Set admindoc = adminview.getdocumentbykey(InterNotesServer)
Set ratingview = webdb.getview("(AverageRating)")

docs_deleted =0
currdate = Today()
purge = admindoc.getitemvalue("Purge")
purge_expired = admindoc.getitemvalue("Expired")
private_purge = admindoc.getitemvalue("Private_Exp")
age_criteria = admindoc.getitemvalue("Age_Criteria")
offsetdate = Datenumber(Year(currdate), Month(currdate), Day(currdate) - Cint(age_criteria(0)))
db_size_admin = admindoc.getitemvalue("Size")
db_size_limit = Cdbl(db_size_admin(0)) * (1048576)
db_pct_goal = admindoc.getitemvalue("Pct_Db_Goal")
size_criteria = admindoc.getitemvalue("Size_Criteria")
db_size_goal = db_size_limit * (Cdbl(db_pct_goal(0))*.01)

Select Case size_criteria(0)
Case "1"
lower = 262144
Case "2"
lower = 524288
Case "3"
lower = 1048576
End Select

rating_del = admindoc.getitemvalue("Rating_del")
rating_purge_period = admindoc.getitemvalue("Rating_Purge_Period")
rating_offsetdate = Datenumber(Year(currdate), Month(currdate), Day(currdate) - Cint(rating_purge_period(0)))
'*
'* This sets the sizequota for the database
'* based on the setting in the admin note.
'*
If ( db_size_limit <> webdb.sizequota ) Then
webdb.sizequota = db_size_limit
End If
'*
'* This is the actual start of the code for the purge agent.
'* First it checks to see if purging is turned on in the admin note.
'* If so, it checks to see if the purging of private documents is turned
'* on also. Based on that selection it follows a series of for next
'* loops parsing selected documents in a collection.
'*
Print "Starting Purge Agent for web.nsf"
If ( purge(0) = "1" ) Then
Print "Database Purging is turned on"
If ( private_purge(0) = "1" ) Then
Print "Purging of private documents is turned on"
'*
'* This loop checks the setting for deleting expired documents
'* in the admin note and deletes expired documents including
'* private documents. Note that it uses a collection of
'* documents which are either regular html pages or html forms
'* with an expiration date. It must use a collection instead
'* of a view so that if knows how many documents to process.
'*
If ( purge_expired(0) = "1" ) Then
Set webcoll = webdb.search("(Form=""HTMLForm"" & Expiration <> NULL) | ($Title = ""HTMLFormDoc""& expiration <> NULL)", cuttoff, 0)
For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement checks for a non-blank value in the
'* expiration field and then compares it with todays
'* date. If the expiration date has already passed,
'* the document is deleted.
'*
If ( currentdoc.hasitem("Expiration") ) Then
If ( currentdoc.getitemvalue("Expiration")(0)<>"" ) Then
expdate = currentdoc.getitemvalue("Expiration")
If ( Cdat(expdate(0)) <= Cdat(currdate) ) Then
Call currentdoc.remove(True)
docs_deleted=docs_deleted+1
End If
End If
End If
Next
End If
'*
'* This loop checks to see if the database size has exceeded the
'* database size goal as set in the admin note. If it has then
'* it needs to start deleting documents based on their
'* origination date in the database (the time_date field).
'* This loop includes private documents.
'*
If ( db_size_goal < webdb.size ) Then
Set webcoll = webdb.search("Form=""HTMLForm"" | $Title = ""HTMLFormDoc""", cuttoff, 0)
For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement checks for the time_date
'* (origination date of the html document) field and
'* then compares it with the offset date computed
'* from the age criteria deletion field in the admin
'* note.
'*
If ( currentdoc.hasitem("Time_Date") ) Then
origdate = currentdoc.getitemvalue("Time_Date")
If ( Cdat(origdate(0)) < Cdat(offsetdate) ) Then
Call currentdoc.remove(True)
docs_deleted=docs_deleted+1
End If
End If
Next
End If
'*
'* This loop again checks to see if the database size has
'* exceeded the size goal as set in the admin note. If the
'* size goal still needs to be acheived, this loop deletes
'* based on document size including private documents.
'*
If ( db_size_goal < webdb.size ) Then
Set webcoll = webdb.search("Form=""HTMLForm"" | $Title = ""HTMLFormDoc""", cuttoff, 0)
For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement checks the current size of the
'* document and compares it against the size limit
'* set in the admin note. If it exceeds it, it
'* deletes the document.
'*
If ( currentdoc.size >= lower ) Then
Call currentdoc.remove(True)
docs_deleted = docs_deleted+1
End If
Next
End If
Else
'*
'* This loop is a repeat of the first one above except that it
'* deletes expired documents with regard to private pages.
'* Note that the search criteria in the collection specifies
'* privacy_flags not equal to one. This eliminates private
'* pages from the documents returned in the collection.
'*
If ( purge_expired(0) = "1" ) Then
Set webcoll = webdb.search("(Form=""HTMLForm"" &privacy_flags<>1&expiration<>NULL)|( $Title =
""HTMLFormDoc""&privacy_flags<>1&expiration<>NULL)", cuttoff, 0)
For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement checks for a non-blank value in the
'* expiration field and then compares it with todays
'* date. If the expiration date has already passed,
'* the document is deleted.
'*
If ( currentdoc.hasitem("Expiration") ) Then
If ( currentdoc.getitemvalue("Expiration")(0)<>"" ) Then
expdate = currentdoc.getitemvalue("Expiration")
If ( Cdat(expdate(0)) <= Cdat(currdate) ) Then
Call currentdoc.remove(True)
docs_deleted = docs_deleted+1
End If
End If
End If

Next
End If
'*
'* This loop checks to see if the database size has exceeded
'* the database size goal as set in the admin note. If it has
'* then it needs to start deleting documents based on their
'* origination date in the database (the time_date field).
'* This loop excludes private documents.
'*
If ( db_size_goal < webdb.size ) Then
Set webcoll = webdb.search("(Form=""HTMLForm"" & privacy_flags<>1) |( $Title = ""HTMLFormDoc""&privacy_flags<>1)", cuttoff, 0)

For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement checks for the time_date
'* (origination date of the html document) field and
'* then compares it with the offset date computed
'* from the age criteria deletion field in the admin
'* note.
'*
If ( currentdoc.hasitem("Time_Date") ) Then
origdate = currentdoc.getitemvalue("Time_Date")

If ( Cdat(origdate(0)) < Cdat(offsetdate) ) Then
Call currentdoc.remove(True)
docs_deleted = docs_deleted+1
End If
End If
Next
End If
'*
'* This loop again checks to see if the database size has
'* exceeded the size goal as set in the admin note. If the
'* size goal still needs to be achieved, this loop deletes
'* based on document size excluding private documents.
'*
If ( db_size_goal < webdb.size ) Then
Set webcoll = webdb.search("(Form=""HTMLForm"" &privacy_flags<>1)|( $Title = ""HTMLFormDoc""&privacy_flags<>1)", cuttoff, 0)
For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement checks the current size of the
'* document and compares it against the size limit
'* set in the admin note. If it exceeds it, it
'* deletes the document.
'*
If ( currentdoc.size >= lower ) Then
Call currentdoc.remove(True)
docs_deleted = docs_deleted+1
End If
Next
End If
End If
End If
'*
'* This is the code for purging rating pages. It first checks the field
'* in the admin note to determine if it should delete rated pages. Note
'* that this loop is driven by that field and is therefore seperate from
'* the purging loops above.
'*
If ( rating_del(0) = "1" ) Then
Set webcoll = webdb.search("Form=""Person Rating Form""", cuttoff, 0)
For j = 1 To webcoll.count
Set currentdoc = webcoll.GetNthDocument(j)
'*
'* This statement deletes rating documents based on
'* the age of the rating in the database.
'*
If ( currentdoc.hasitem("RatingDate") ) Then
origdate = currentdoc.getitemvalue("RatingDate")
If ( Cdat(origdate(0)) < Cdat(rating_offsetdate) ) Then
Call currentdoc.remove(True)
docs_deleted=docs_deleted+1
End If
End If
Next
End If
'*
'* This ends the purging code. What remains is some cleanup. The main
'* $All view needs to be refreshed and the database is properly closed.
'* If the database size goal has still not been met, some information
'* is printed to the notes log to inform the administrator that they
'* should up the size goal.
'*
Set webview = webdb.getview("($All)")
Call webview.refresh()
Print "Just refreshed the '$All' documents view"
Print "Purge Agent ending for web.nsf"
Print "Purge Agent deleted " + Cstr(docs_deleted) + " documents from web.nsf."
If (db_size_goal < webdb.size) Then
Print "Purge Agent was unable to delete enough documents to make the database meet it's size criteria."
Print "Please re-evaluate your purge criteria or raise your maximum database size in the Admin document."
End If

Call webdb.close()

End Sub