IMPAX performance-script

IMPAX performance-script
' Author: Thomas Axelsen, RegionH
' Performance measurement on search criteria
' Replace sfd with relevant path to performance xml file
' v. 1.1
' last update: 26.02.2016
' added Cancel button so result does not get saved

Option Strict Off

Imports System
Imports AgfaHC
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

Module Script
Sub Main()

Dim path As String = "C:\Users\Public\Impax\Logs\client_performance.log"
Dim sfd As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\performance.xml"

Dim fileLines(), fileContent
Dim startTime, endTime
Dim sDate, numGrid
Dim xmlDoc, objRoot, objRecord, objDate, objTime
Dim objTimeDiff, objNumResults, objIntro, objResult, objMachName
Dim ts As TimeSpan
Dim sCounter As Integer = 1

Dim Stream As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Using reader As New StreamReader(Stream)
 fileContent = reader.ReadToEnd
 fileLines = fileContent.Split(vbCrLf)
 reader.Close()
 If fileLines.Length >= 5 Then
 startTime = reformatLog(fileLines(fileLines.GetUpperBound(0) - 4), 1)
 endTime = reformatLog(fileLines(fileLines.GetUpperBound(0) - 1), 1)
 sDate = reformatLog(fileLines(fileLines.GetUpperBound(0) - 1), 0)
 numGrid = reformatLog(fileLines(fileLines.GetUpperBound(0) - 1), 11)
 Dim tsEnd = TimeSpan.ParseExact(endTime, "c", Nothing)
 Dim tsStart = TimeSpan.ParseExact(startTime, "c", Nothing)
 ts = tsEnd.subtract(tsStart)
 Dim sButton As Integer = msgBox(sDate & " | " & StartTime & " | " _
 & ts.ToString & " | " & numGrid, MsgBoxStyle.OKCancel, "Resultat")
 If sButton = DialogResult.Cancel Then
 msgbox("Resultatet ikke gemt", MsgBoxStyle.OKOnly, "Advarsel")
 Exit Sub
 Else
 End If
 End If

End Using

Dim sSearch = ScriptListArea.GetSearchWorklistConstraint()

xmlDoc = CreateObject("Microsoft.XMLDOM")
 If File.Exists(sfd) = False
 objRoot = xmlDoc.createElement("SEARCH")
 xmlDoc.appendChild(objRoot)
 objResult = xmlDoc.createElement("RESULT")
 objRoot.appendChild(objResult)
 objRecord = xmlDoc.createElement("CRITERIA")
 objRecord.Text = sSearch
 objResult.appendChild(objRecord)
 objDate = xmlDoc.createElement("DATE")
 objDate.Text = sDate
 objResult.appendChild(objDate)
 objTime = xmlDoc.createElement("TIME")
 objTime.Text = startTime
 objResult.appendChild(objTime)
 objTimeDiff = xmlDoc.createElement("SEARCH_TIME")
 objTimeDiff.Text = ts.ToString
 objResult.appendChild(objTimeDiff)
 objNumResults = xmlDoc.createElement("NUM_RESULTS")
 objNumResults.Text = numGrid
 objResult.appendChild(objNumResults)
 objMachName = xmlDoc.createElement("MACHINE")
 objMachName.Text = Environment.machineName
 objResult.appendChild(objMachName)
 objIntro = xmlDoc.createProcessingInstruction ("xml","version='1.0'") 
 xmlDoc.insertBefore(objIntro,xmlDoc.childNodes(0))
 xmlDoc.Save(sfd)
 Else
 xmlDoc.Async = "False"
 xmlDoc.Load(sfd)
 objRoot = xmlDoc.documentElement
 objResult = xmlDoc.createElement("RESULT")
 objRoot.appendChild(objResult)
 objRecord = xmlDoc.createElement("CRITERIA")
 objRecord.Text = sSearch
 objResult.appendChild(objRecord)
 objDate = xmlDoc.createElement("DATE")
 objDate.Text = sDate
 objResult.appendChild(objDate)
 objTime = xmlDoc.createElement("TIME")
 objTime.Text = startTime
 objResult.appendChild(objTime)
 objTimeDiff = xmlDoc.createElement("SEARCH_TIME")
 objTimeDiff.Text = ts.ToString
 objResult.appendChild(objTimeDiff)
 objNumResults = xmlDoc.createElement("NUM_RESULTS")
 objNumResults.Text = numGrid
 objResult.appendChild(objNumResults)
 objMachName = xmlDoc.createElement("MACHINE")
 objMachName.Text = Environment.machineName
 objResult.appendChild(objMachName)
 xmlDoc.Save(sfd)
 End If
End Sub

Function reformatLog(ByVal sLog As String, ByVal index As Integer)
 'Dim patternString = "(.{10})(?:\|)(.{12})" '(?:.*)(?:gridItems=)(\d{1,3})"
 'Dim m As Match = Regex.Match(sLog, patternString)
 'Dim sReform As String = m.Groups(index).Value
 Dim sReformArray = Split(sLog, "|")
 Dim sReform As String = sReformArray(index)
 'msgBox(sReform, MsgBoxStyle.OKOnly, index)
 Return sReform
End Function

End Module