Imports System.Text.RegularExpressions
Namespace LogExplorer
Public Class Session
Private _StartTime As Date
Private _EndTime As Date
Private _ElapsedTime As TimeSpan
Private _Messages As Int32
Private _HTML As String
Private _Contacts As List(Of String)
Public ReadOnly Property StartTime() As Date
Get
Return _StartTime
End Get
End Property
Public ReadOnly Property EndTime() As Date
Get
Return _EndTime
End Get
End Property
Public ReadOnly Property ElapsedTime() As TimeSpan
Get
Return _ElapsedTime
End Get
End Property
Public ReadOnly Property Contacts() As List(Of String)
Get
Return _Contacts
End Get
End Property
Public ReadOnly Property HTML() As String
Get
Return _HTML
End Get
End Property
Public ReadOnly Property Messages() As Int32
Get
Return _Messages
End Get
End Property
Private Function ParseSession(ByVal MplSession As HtmlElement) As Session
Dim Id As String = MplSession.Id
Dim TimeStart, TimeEnd As Date
Dim Users As New List(Of String)
Dim Msg As Int32 = 0
Dim Result As New Session
Dim Time As New Regex("Session_(?<Year>\d{4})\-(?<Month>\d{2})\-(?<Day>\d{2})T(?<Hour>\d{2})\-(?<Minutes>\d{2})\-(?<Seconds>\d{2})")
If Time.IsMatch(Id) Then
Dim Match As Match = Time.Match(Id)
Dim Year As Int16 = Match.Groups("Year").Value
Dim Month As Byte = Match.Groups("Month").Value
Dim Day As Byte = Match.Groups("Day").Value
Dim Hour As Byte = Match.Groups("Hour").Value
Dim Minutes As Byte = Match.Groups("Minutes").Value
Dim Seconds As Byte = Match.Groups("Seconds").Value
TimeStart = New Date(Year, Month, Day, Hour, Minutes, Seconds)
End If
For Each Li As HtmlElement In MplSession.GetElementsByTagName("LI")
Users.Add(Li.InnerText)
Next
Dim SpanTime As New Regex("(?im)\<span class\=time\>\((?<Hour>\d{2})\.(?<Minutes>\d{2})\)", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Dim Matches As MatchCollection = SpanTime.Matches(MplSession.InnerHtml)
Dim Max As Date = TimeStart.AddSeconds(-TimeStart.Second)
Msg = Matches.Count
For Each M As Match In Matches
Dim Hour As String = M.Groups("Hour").Value
Dim Minutes As String = M.Groups("Minutes").Value
Dim Temp As Date
Temp = Date.Parse(TimeStart.ToShortDateString & " " & Hour & "." & Minutes)
If Temp < Max Then
Temp.AddDays(1)
End If
If Temp > Max Then
Max = Temp
End If
Next
If Max.Year > 2000 Then
TimeEnd = Max
Else
TimeEnd = TimeStart
End If
With Result
._StartTime = TimeStart
._EndTime = TimeEnd
._ElapsedTime = TimeEnd - TimeStart
._Contacts = Users
._HTML = MplSession.InnerHtml
._Messages = Msg
End With
Return Result
End Function
Private Sub New()
End Sub
Sub New(ByVal MplSession As HtmlElement)
Dim Result As Session = ParseSession(MplSession)
_StartTime = Result.StartTime
_EndTime = Result.EndTime
_ElapsedTime = Result.ElapsedTime
_Contacts = Result.Contacts
_HTML = Result.HTML
_Messages = Result.Messages
End Sub
Public Overrides Function ToString() As String
Return String.Format("Sessione di {0}, dalle ore {1} alle ore {2}", _
StartTime.ToLongDateString, StartTime.ToShortTimeString, _
IIf(EndTime.Day = StartTime.Day, EndTime.ToShortTimeString, EndTime.ToShortTimeString & " del giorno " & EndTime.ToShortDateString))
End Function
End Class
Public Class ContactLogInfo
Private _LogSize As Int32
Private _SessionNumber As Int32
Private _SessionTime As New TimeSpan
Private _Messages As Int32
Private _Name As String
Public Property LogSize() As Int32
Get
Return _LogSize
End Get
Set(ByVal Value As Int32)
_LogSize = Value
End Set
End Property
Public Property SessionNumber() As Int32
Get
Return _SessionNumber
End Get
Set(ByVal Value As Int32)
_SessionNumber = Value
End Set
End Property
Public Property SessionTime() As TimeSpan
Get
Return _SessionTime
End Get
Set(ByVal Value As TimeSpan)
_SessionTime = Value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Messages() As Int32
Get
Return _Messages
End Get
Set(ByVal value As Int32)
_Messages = value
End Set
End Property
End Class
Public Class SessionByStartTimeComparer
Implements IComparer(Of Session)
Public Function Compare(ByVal x As Session, ByVal y As Session) As Integer Implements System.Collections.Generic.IComparer(Of Session).Compare
Return x.StartTime.CompareTo(y.StartTime)
End Function
End Class
Public Class Log
Private _Sessions As New List(Of Session)
Private _Size As Int32
Private _IsLoaded As Boolean
Public ReadOnly Property Sessions() As List(Of Session)
Get
Return _Sessions
End Get
End Property
Public ReadOnly Property Size() As Int32
Get
Return _Size
End Get
End Property
Public ReadOnly Property IsLoaded() As Boolean
Get
Return _IsLoaded
End Get
End Property
Sub New(ByVal FileName As String)
Dim WB As New WebBrowser
Dim Doc As HtmlDocument = WB.Document
_Size = FileLen(FileName)
_IsLoaded = False
AddHandler WB.DocumentCompleted, AddressOf PageLoaded
WB.Navigate("file:///" & FileName.Replace("\", "/"))
End Sub
Private Sub New()
End Sub
Private Sub PageLoaded(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
For Each El As HtmlElement In DirectCast(sender, WebBrowser).Document.GetElementsByTagName("DIV")
If El.Id.Contains("Session") Then
Me.Sessions.Add(New Session(El))
End If
Next
_IsLoaded = True
End Sub
Public Shared Function MergeLogs(ByVal Logs As List(Of Log))
Dim Result As New Log
For Each Log As Log In Logs
Result.Sessions.AddRange(Log.Sessions)
Result._Size += Log.Size
Next
Return Result
End Function
End Class
Public Class OldLog
Private _Sessions As Int32
Private _Size As Int32
Private _ElapsedTime As TimeSpan
Private _Messages As Int32
Public ReadOnly Property Sessions() As Int32
Get
Return _Sessions
End Get
End Property
Public ReadOnly Property Size() As Int32
Get
Return _Size
End Get
End Property
Public ReadOnly Property ElapsedTime() As TimeSpan
Get
Return _ElapsedTime
End Get
End Property
Public ReadOnly Property Messages() As Int32
Get
Return _Messages
End Get
End Property
Private Sub New()
End Sub
Sub New(ByVal XmlLog As String)
Dim Xml
As String = IO.
File.
ReadAllText(XmlLog
)
Dim Main As New Regex("\<Log FirstSessionID\=""1"" LastSessionID\=""(?<Sessions>\d+)""\>")
Dim Message As New Regex( _
"\<Message Date\=""(?<Date>\d+\/\d+\/\d+)"" Time\=""(?<Time>\d+\.\d+\.\d+)"" DateTime\=""[\d\w\-\:\.]+"" SessionID\=""(?<SID>\d+)""", RegexOptions.Multiline)
Dim Matches As MatchCollection = Message.Matches(Xml)
Dim MMatch As Match = Main.Match(Xml)
If Not MMatch.Success Then
Throw New FormatException
End If
_Sessions = CInt(MMatch.Groups("Sessions").Value)
_Size = FileLen(XmlLog)
_Messages = Matches.Count
Dim Min, Max, Temp As Date
Dim TempID As Int16 = CInt(Matches(0).Groups("SID").Value)
For Each M As Match In Matches
If CInt(M.Groups("SID").Value) <> TempID Then
_ElapsedTime = _ElapsedTime.Add(Max - Min)
TempID = CInt(M.Groups("SID").Value)
Temp = Nothing
Min = Nothing
Max = Nothing
End If
If Temp = Nothing Then
Temp = Date.Parse(M.Groups("Date").Value & " " & M.Groups("Time").Value)
Min = Temp
Max = Temp
End If
Temp = Date.Parse(M.Groups("Date").Value & " " & M.Groups("Time").Value)
If Temp < Min Then
Min = Temp
End If
If Temp > Max Then
Max = Temp
End If
Next
_ElapsedTime = _ElapsedTime.Add(Max - Min)
End Sub
End Class
End Namespace