您好,我不是开发人员,因此我不了解最佳做法。我创建它以绕过日志数据的手动数据复制。此代理用于单个Feed,我将为每个额外的一个复制和调整。对于指定的订阅源,它会读取上次处理的日志和当前以及昨天处理的文件数。它还计算输入文件夹中的文件并读取服务器的时区。每个数据项由逗号分隔,用于csv和电子邮件,稍后将在网站上托管。感谢任何建设性的批评。
Sub Initialize
Dim customername As String
Dim servername As String
Dim feedname As String
Dim alertthresholdinhours As Integer
Dim inputfeedpath As String
' Set for each feed
customername = "gRrhio"
servername = "gRrhioEdge2"
feedname = "FF Thompson ADT"
alertthresholdinhours = 6
inputfeedpath = "\\mhinec\elycon\data\adt\*.*"
' Counts files in input folder
Dim pathName As String, fileName As String
Dim inputfeedcounter As Integer
inputfeedcounter = 0
pathName$ = inputfeedpath
fileName$ = Dir$(pathName$, 0)
Do While fileName$ <> ""
inputfeedcounter = inputfeedcounter + 1
fileName$ = Dir$()
Loop
Dim entry As NotesViewEntry
Dim vc As NotesViewEntryCollection
Dim filesprocessed As Integer
Dim session As New NotesSession
Dim db As NotesDatabase
Dim newDoc As NotesDocument
Dim rtitem As NotesRichTextItem
Set db = session.CurrentDatabase
Dim view As NotesView
Set view = db.GetView( "Sessions\by Feed" )
Set newDoc = New NotesDocument( db )
Set rtitem = New NotesRichTextItem( newDoc, "Body" )
Dim todaysdate As New NotesDateTime("Today")
Dim flag As Integer
Dim counter As Integer
Dim files As Integer
Dim errors As Integer
Dim lastdate As String
Dim lastdayran As String
Dim lasttime As String
Dim lasttimeran As String
Dim filesp As Integer
Dim lastdayfiles As Integer
Dim lastdaysfiles2 As Integer
Dim terrors As Integer
Dim lastdayerrors As Integer
lastdate = ""
lastdayran = ""
counter = 0
flag = 0
filesp = 0
lastdayfiles = 0
lastdaysfiles2 = 0
terrors = 0
lastdayerrors = 0
' Finds date for last time processed, counts files processed and errors
While flag = 0
Dim dateTime As New NotesDateTime(todaysdate.DateOnly)
Dim keyarray(1) As Variant
keyarray(0) = feedname
Set keyarray(1) = dateTime
Set vc = view.GetAllEntriesByKey(keyarray, False)
Set entry = vc.GetFirstEntry
If entry Is Nothing Then
Call todaysdate.AdjustDay(-1)
End If
While Not entry Is Nothing
files = 0
Forall colval In entry.ColumnValues
If counter = 9 Then
counter = 0
Elseif counter = 8 Then
counter = 9
Elseif counter = 7 Then
counter = 8
Elseif counter = 6 Then
errors = Cint(colval)
counter = 7
Elseif counter = 5 Then
counter = 6
Elseif counter = 4 Then
files = Cint(colval)
counter = 5
Elseif counter = 3 Then
counter = 4
Elseif counter = 2 Then
counter = 3
lasttime = colval
Elseif counter = 1 Then
counter = 2
lastdate = colval
Elseif counter = 0 Then
counter = 1
End If
End Forall
filesp = filesp + files
terrors = terrors + errors
Set entry=vc.GetNextEntry (entry)
flag = 1
Wend
Wend
lastdayfiles = filesp
lastdayerrors = terrors
lastdayran = lastdate
lasttimeran = lasttime
'Counts previous files processed
filesp = 0
terrors = 0
lastdate = ""
flag = 0
Call todaysdate.AdjustDay(-1)
While flag = 0
Dim dateTime2 As New NotesDateTime(todaysdate.DateOnly)
Dim keyarray2(1) As Variant
keyarray2(0) = feedname
Set keyarray2(1) = dateTime2
Set vc = view.GetAllEntriesByKey(keyarray2, False)
Set entry = vc.GetFirstEntry
If entry Is Nothing Then
Call todaysdate.AdjustDay(-1)
End If
While Not entry Is Nothing
files = 0
Forall colval In entry.ColumnValues
If counter = 9 Then
counter = 0
Elseif counter = 8 Then
counter = 9
Elseif counter = 7 Then
counter = 8
Elseif counter = 6 Then
counter = 7
Elseif counter = 5 Then
counter = 6
Elseif counter = 4 Then
files = Cint(colval)
counter = 5
Elseif counter = 3 Then
counter = 4
Elseif counter = 2 Then
counter = 3
Elseif counter = 1 Then
counter = 2
Elseif counter = 0 Then
counter = 1
End If
End Forall
filesp = filesp + files
Set entry=vc.GetNextEntry (entry)
flag = 1
Wend
Wend
lastdaysfiles2 = filesp
' Prints line of CSV into body of email
Call rtitem.AppendText ( customername )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( servername )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( datetime.timezone )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdayran )
Call rtitem.AppendText ( " " )
Call rtitem.AppendText ( lasttimeran )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdayfiles )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdayerrors )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdaysfiles2 )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( inputfeedcounter )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( alertthresholdinhours )
Call newDoc.Save( False, True )
newDoc.Subject = feedname
' Running from server line should be
'newDoc.SendTo = "Ecmon Feedcheck/Ecmonitor@ECMONITOR"
newDoc.SendTo = "AX1Forward Feedcheck/ACHQ@company.com"
newDoc.Send( False )
End Sub
答案 0 :(得分:2)
如果不是开发人员,可以编写很多代码:)
如果您正在寻找一些课程来开始成为一名优秀的开发人员,那么请接受Mitch的建议(来自评论)并将其分解为子程序。第1课:这里肯定有一些重复的代码,将重复的代码放入方法(函数或子例程)总是一个好主意,所以它只存在一次。计算处理的文件和处理的先前文件的部分看起来很相似,可能会被放入例如:
的例程中Function GetCountFilesProcessed() As Integer
'code here
End Function
但是,如果我正确理解您的代码,您甚至可以节省对此的需求。而不是在中间做那个奇怪的循环,看起来你正试图从一个viewentry的列中获取一个值。假设您在第4列的价值中感兴趣。您只需通过索引访问所需列,即可获得所需列的值。例如,您的文件变量可以通过此行直接设置为第4列的值
files = Cint(entry.ColumnValues(4)) 'check this, it might be 3 if the array is zero based.
无论如何,底线是如果这段代码有效,那么你就有了一个良好的开端!
答案 1 :(得分:2)
在更多的风格方面,我总是发现在他们
时维护其他人的代码更容易Option 'Explicit'
关于为同一问题的其他实例复制此代理的注释也会引发一个标记。尝试找出这些代理之间的共同点,并将这些函数推送到脚本库中。这样的事情在维护代码时节省了大量时间,因为您不需要考虑每个代理的不同之处(例如,我的更改是否适用于此代理的所有实例,或者仅适用于其中一些实例?)
答案 2 :(得分:2)
你想要更多地分解你的代码......还有一件小事。而不是
while not item is nothing
这是一个双重否定和流行的大脑弯曲..写道:
do until item is nothing
...
loop
这也允许你通过exit do
突破循环答案 3 :(得分:2)
您可以优化两个ForAll循环。 这是第一个看起来的样子:
Forall colval In entry.ColumnValues
Select Case (counter)
Case 1: lastdate = colval
Case 2: lasttime = colval
Case 4: files = Cint(colval)
Case 6: errors = Cint(colval)
End Select
counter = (counter + 1) Mod 10
End Forall
这就是第二种看法:
Forall colval In entry.ColumnValues
if (counter = 4) Then files = Cint(colval)
counter = (counter + 1) Mod 10
End Forall
答案 4 :(得分:1)
关于此位的说明
While Not entry Is Nothing
files = 0
Forall colval In entry.ColumnValues
If counter = 9 Then
counter = 0
Elseif counter = 8 Then
counter = 9
....
正如ken所说,您可以使用entry.ColumnValues(x)方法获取columnValues,因此不需要通过值进行交互。但;你可以做到这一点
While Not entry Is Nothing
files = 0
counter = 0
Forall colval In entry.ColumnValues
counter = counter + 1
Select case counter
case 6
errors = Cint(colval)
.....
end select
答案 5 :(得分:1)
已经有一些好处。要添加它们,如果您有共享公共对象的变量,则创建一个类。将变量添加到该类。
所以不要说:
Dim userFullName as String
Dim age as Integer
Dim addressLine1 as String
' ... etc.
你可以:
Class UserDetails
Dim fullName as String
Dim age as Integer
Dim addressLine1 as String
' ... etc
End Class
和参考:
Dim u as new UserDetails
u.fullName = "full name"
u.age = 22
u.addressLine1 = "1 main street"
这样做的好处是你可以添加操作数据的方法,并且你知道代码与该对象有关,而不是在你的应用程序中搜索。