Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
MSN Live Log Manager - LogExplorer.vb

LogExplorer.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Text.RegularExpressions
  2. Namespace LogExplorer
  3.     Public Class Session
  4.         Private _StartTime As Date
  5.         Private _EndTime As Date
  6.         Private _ElapsedTime As TimeSpan
  7.         Private _Messages As Int32
  8.         Private _HTML As String
  9.         Private _Contacts As List(Of String)
  10.  
  11.         Public ReadOnly Property StartTime() As Date
  12.             Get
  13.                 Return _StartTime
  14.             End Get
  15.         End Property
  16.  
  17.         Public ReadOnly Property EndTime() As Date
  18.             Get
  19.                 Return _EndTime
  20.             End Get
  21.         End Property
  22.  
  23.         Public ReadOnly Property ElapsedTime() As TimeSpan
  24.             Get
  25.                 Return _ElapsedTime
  26.             End Get
  27.         End Property
  28.  
  29.         Public ReadOnly Property Contacts() As List(Of String)
  30.             Get
  31.                 Return _Contacts
  32.             End Get
  33.         End Property
  34.  
  35.         Public ReadOnly Property HTML() As String
  36.             Get
  37.                 Return _HTML
  38.             End Get
  39.         End Property
  40.  
  41.         Public ReadOnly Property Messages() As Int32
  42.             Get
  43.                 Return _Messages
  44.             End Get
  45.         End Property
  46.  
  47.         Private Function ParseSession(ByVal MplSession As HtmlElement) As Session
  48.             Dim Id As String = MplSession.Id
  49.             Dim TimeStart, TimeEnd As Date
  50.             Dim Users As New List(Of String)
  51.             Dim Msg As Int32 = 0
  52.             Dim Result As New Session
  53.  
  54.             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})")
  55.             If Time.IsMatch(Id) Then
  56.                 Dim Match As Match = Time.Match(Id)
  57.                 Dim Year As Int16 = Match.Groups("Year").Value
  58.                 Dim Month As Byte = Match.Groups("Month").Value
  59.                 Dim Day As Byte = Match.Groups("Day").Value
  60.                 Dim Hour As Byte = Match.Groups("Hour").Value
  61.                 Dim Minutes As Byte = Match.Groups("Minutes").Value
  62.                 Dim Seconds As Byte = Match.Groups("Seconds").Value
  63.  
  64.                 TimeStart = New Date(Year, Month, Day, Hour, Minutes, Seconds)
  65.             End If
  66.  
  67.             For Each Li As HtmlElement In MplSession.GetElementsByTagName("LI")
  68.                 Users.Add(Li.InnerText)
  69.             Next
  70.  
  71.             Dim SpanTime As New Regex("(?im)\<span class\=time\>\((?<Hour>\d{2})\.(?<Minutes>\d{2})\)", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  72.             Dim Matches As MatchCollection = SpanTime.Matches(MplSession.InnerHtml)
  73.             Dim Max As Date = TimeStart.AddSeconds(-TimeStart.Second)
  74.             Msg = Matches.Count
  75.             For Each M As Match In Matches
  76.                 Dim Hour As String = M.Groups("Hour").Value
  77.                 Dim Minutes As String = M.Groups("Minutes").Value
  78.                 Dim Temp As Date
  79.                 Temp = Date.Parse(TimeStart.ToShortDateString & " " & Hour & "." & Minutes)
  80.                 If Temp < Max Then
  81.                     Temp.AddDays(1)
  82.                 End If
  83.                 If Temp > Max Then
  84.                     Max = Temp
  85.                 End If
  86.             Next
  87.             If Max.Year > 2000 Then
  88.                 TimeEnd = Max
  89.             Else
  90.                 TimeEnd = TimeStart
  91.             End If
  92.  
  93.             With Result
  94.                 ._StartTime = TimeStart
  95.                 ._EndTime = TimeEnd
  96.                 ._ElapsedTime = TimeEnd - TimeStart
  97.                 ._Contacts = Users
  98.                 ._HTML = MplSession.InnerHtml
  99.                 ._Messages = Msg
  100.             End With
  101.  
  102.             Return Result
  103.         End Function
  104.  
  105.         Private Sub New()
  106.  
  107.         End Sub
  108.  
  109.         Sub New(ByVal MplSession As HtmlElement)
  110.             Dim Result As Session = ParseSession(MplSession)
  111.  
  112.             _StartTime = Result.StartTime
  113.             _EndTime = Result.EndTime
  114.             _ElapsedTime = Result.ElapsedTime
  115.             _Contacts = Result.Contacts
  116.             _HTML = Result.HTML
  117.             _Messages = Result.Messages
  118.         End Sub
  119.  
  120.         Public Overrides Function ToString() As String
  121.             Return String.Format("Sessione di {0}, dalle ore {1} alle ore {2}", _
  122.             StartTime.ToLongDateString, StartTime.ToShortTimeString, _
  123.             IIf(EndTime.Day = StartTime.Day, EndTime.ToShortTimeString, EndTime.ToShortTimeString & " del giorno " & EndTime.ToShortDateString))
  124.         End Function
  125.     End Class
  126.  
  127.     Public Class ContactLogInfo
  128.         Private _LogSize As Int32
  129.         Private _SessionNumber As Int32
  130.         Private _SessionTime As New TimeSpan
  131.         Private _Messages As Int32
  132.         Private _Name As String
  133.  
  134.         Public Property LogSize() As Int32
  135.             Get
  136.                 Return _LogSize
  137.             End Get
  138.             Set(ByVal Value As Int32)
  139.                 _LogSize = Value
  140.             End Set
  141.         End Property
  142.  
  143.         Public Property SessionNumber() As Int32
  144.             Get
  145.                 Return _SessionNumber
  146.             End Get
  147.             Set(ByVal Value As Int32)
  148.                 _SessionNumber = Value
  149.             End Set
  150.         End Property
  151.  
  152.         Public Property SessionTime() As TimeSpan
  153.             Get
  154.                 Return _SessionTime
  155.             End Get
  156.             Set(ByVal Value As TimeSpan)
  157.                 _SessionTime = Value
  158.             End Set
  159.         End Property
  160.  
  161.         Public Property Name() As String
  162.             Get
  163.                 Return _Name
  164.             End Get
  165.             Set(ByVal value As String)
  166.                 _Name = value
  167.             End Set
  168.         End Property
  169.  
  170.         Public Property Messages() As Int32
  171.             Get
  172.                 Return _Messages
  173.             End Get
  174.             Set(ByVal value As Int32)
  175.                 _Messages = value
  176.             End Set
  177.         End Property
  178.     End Class
  179.  
  180.     Public Class SessionByStartTimeComparer
  181.         Implements IComparer(Of Session)
  182.  
  183.         Public Function Compare(ByVal x As Session, ByVal y As Session) As Integer Implements System.Collections.Generic.IComparer(Of Session).Compare
  184.             Return x.StartTime.CompareTo(y.StartTime)
  185.         End Function
  186.     End Class
  187.  
  188.     Public Class Log
  189.         Private _Sessions As New List(Of Session)
  190.         Private _Size As Int32
  191.         Private _IsLoaded As Boolean
  192.  
  193.         Public ReadOnly Property Sessions() As List(Of Session)
  194.             Get
  195.                 Return _Sessions
  196.             End Get
  197.         End Property
  198.  
  199.         Public ReadOnly Property Size() As Int32
  200.             Get
  201.                 Return _Size
  202.             End Get
  203.         End Property
  204.  
  205.         Public ReadOnly Property IsLoaded() As Boolean
  206.             Get
  207.                 Return _IsLoaded
  208.             End Get
  209.         End Property
  210.  
  211.         Sub New(ByVal FileName As String)
  212.             Dim WB As New WebBrowser
  213.             Dim Doc As HtmlDocument = WB.Document
  214.  
  215.             _Size = FileLen(FileName)
  216.             _IsLoaded = False
  217.             AddHandler WB.DocumentCompleted, AddressOf PageLoaded
  218.             WB.Navigate("file:///" & FileName.Replace("\", "/"))
  219.         End Sub
  220.  
  221.         Private Sub New()
  222.  
  223.         End Sub
  224.  
  225.         Private Sub PageLoaded(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
  226.             For Each El As HtmlElement In DirectCast(sender, WebBrowser).Document.GetElementsByTagName("DIV")
  227.                 If El.Id.Contains("Session") Then
  228.                     Me.Sessions.Add(New Session(El))
  229.                 End If
  230.             Next
  231.             _IsLoaded = True
  232.         End Sub
  233.  
  234.         Public Shared Function MergeLogs(ByVal Logs As List(Of Log))
  235.             Dim Result As New Log
  236.  
  237.             For Each Log As Log In Logs
  238.                 Result.Sessions.AddRange(Log.Sessions)
  239.                 Result._Size += Log.Size
  240.             Next
  241.  
  242.             Return Result
  243.         End Function
  244.     End Class
  245.  
  246.     Public Class OldLog
  247.         Private _Sessions As Int32
  248.         Private _Size As Int32
  249.         Private _ElapsedTime As TimeSpan
  250.         Private _Messages As Int32
  251.  
  252.         Public ReadOnly Property Sessions() As Int32
  253.             Get
  254.                 Return _Sessions
  255.             End Get
  256.         End Property
  257.  
  258.         Public ReadOnly Property Size() As Int32
  259.             Get
  260.                 Return _Size
  261.             End Get
  262.         End Property
  263.  
  264.         Public ReadOnly Property ElapsedTime() As TimeSpan
  265.             Get
  266.                 Return _ElapsedTime
  267.             End Get
  268.         End Property
  269.  
  270.         Public ReadOnly Property Messages() As Int32
  271.             Get
  272.                 Return _Messages
  273.             End Get
  274.         End Property
  275.  
  276.         Private Sub New()
  277.  
  278.         End Sub
  279.  
  280.         Sub New(ByVal XmlLog As String)
  281.             Dim Xml As String = IO.File.ReadAllText(XmlLog)
  282.             Dim Main As New Regex("\<Log FirstSessionID\=""1"" LastSessionID\=""(?<Sessions>\d+)""\>")
  283.             Dim Message As New Regex( _
  284.             "\<Message Date\=""(?<Date>\d+\/\d+\/\d+)"" Time\=""(?<Time>\d+\.\d+\.\d+)"" DateTime\=""[\d\w\-\:\.]+"" SessionID\=""(?<SID>\d+)""", RegexOptions.Multiline)
  285.             Dim Matches As MatchCollection = Message.Matches(Xml)
  286.             Dim MMatch As Match = Main.Match(Xml)
  287.  
  288.             If Not MMatch.Success Then
  289.                 Throw New FormatException
  290.             End If
  291.  
  292.             _Sessions = CInt(MMatch.Groups("Sessions").Value)
  293.             _Size = FileLen(XmlLog)
  294.             _Messages = Matches.Count
  295.  
  296.             Dim Min, Max, Temp As Date
  297.             Dim TempID As Int16 = CInt(Matches(0).Groups("SID").Value)
  298.  
  299.             For Each M As Match In Matches
  300.                 If CInt(M.Groups("SID").Value) <> TempID Then
  301.                     _ElapsedTime = _ElapsedTime.Add(Max - Min)
  302.                     TempID = CInt(M.Groups("SID").Value)
  303.                     Temp = Nothing
  304.                     Min = Nothing
  305.                     Max = Nothing
  306.                 End If
  307.  
  308.                 If Temp = Nothing Then
  309.                     Temp = Date.Parse(M.Groups("Date").Value & " " & M.Groups("Time").Value)
  310.                     Min = Temp
  311.                     Max = Temp
  312.                 End If
  313.  
  314.                 Temp = Date.Parse(M.Groups("Date").Value & " " & M.Groups("Time").Value)
  315.  
  316.                 If Temp < Min Then
  317.                     Min = Temp
  318.                 End If
  319.                 If Temp > Max Then
  320.                     Max = Temp
  321.                 End If
  322.             Next
  323.             _ElapsedTime = _ElapsedTime.Add(Max - Min)
  324.         End Sub
  325.     End Class
  326. End Namespace