1

您好,我不是开发人员,因此不了解最佳实践。我创建它是为了绕过日志数据的手动数据复制。此代理适用于单个提要,我将针对每个附加提要进行复制和调整。对于指定的提要,它会读取上次处理的日志以及今天和昨天处理的文件数。它还计算输入文件夹中的文件并读取服务器的时区。每个数据项用逗号分隔 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
4

6 回答 6

2

因为不是开发人员,你可以写很多代码:)

如果您正在寻找一些课程来开始成为一名优秀的开发人员,那么请采纳 Mitch 的建议(来自评论)并将其分解为子例程。第 1 课:这里肯定有一些重复的代码,将重复的代码放入方法(函数或子例程)中总是一个好主意,这样它只存在一次。计算已处理文件和已处理文件的部分看起来很相似,可能可以放入如下例程中:

Function GetCountFilesProcessed() As Integer

    'code here

End Function

但是,如果我正确理解您的代码,您甚至可以节省对它的需求。而不是在中间做那个奇怪的循环,看起来你只是试图从一个视图条目的列中获取一个值。假设您对第 4 列的值感兴趣。您可以通过索引访问它来简单地获取所需列的值。例如,您的 files 变量可以通过此行直接设置为第 4 列的值

files = Cint(entry.ColumnValues(4))  'check this, it might be 3 if the array is zero based.

无论如何,底线是如果此代码有效,那么您将有一个良好的开端!

于 2009-03-16T17:38:40.060 回答
2

在更多的风格方面,我总是发现维护其他人的代码更容易

  1. 在声明Option 'Explicit'部分声明
  2. 声明的变量从大约高到低(例如,在 db 之前的会话之前在 doc 之前的视图)
  3. 带有前缀的注释对象及其类型(docMail、dbMyDatabase、viewOutstandingInvoices)
  4. 将所有声明放在前面(有助于在遇到变量时找到声明)
  5. 正如其他人所提到的,在适用的情况下将其分解为函数/子项。

您关于为相同问题的其他实例复制此代理的评论也引发了一个标志。尝试找出这些代理之间的共同点并将这些功能推送到脚本库中。这种事情在维护代码时可以节省大量时间,因为您无需考虑每个代理的不同之处(例如,我的更改是否适用于该代理的所有实例,还是仅适用于其中一些?)

于 2009-05-14T00:41:26.157 回答
2

你想要更多地分解你的代码并且......一件小事。代替

while not item is nothing

这是双重否定和流行的脑筋急转弯..写:

do until item is nothing
  ...
loop

这也允许您使用 exit do 跳出循环

于 2009-06-30T13:12:14.050 回答
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
于 2009-11-07T12:18:43.130 回答
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
于 2009-03-18T02:57:42.820 回答
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"

这样做的好处是您可以添加方法来操作该数据,并且您知道代码与该对象相关,而不是在您的应用程序中搜索。

于 2010-02-25T21:39:55.703 回答