Tips_tutorials   >   Studiojs202   >   Web App Stats Reports

Web App Stats Reports

We want to get summary statistics from the detailed Webappstat records. There are many ways to accomplish this. For this tutorial we'll add methods to the oWebMonitor object which return the stats in a list variable. The reason for doing this, is so that the results can be used for a report class, web page, or an email message.

What statistics would we like to know for a given time period? (Day, Week, Month, Year)

  1. Total number of connections.
  2. Number of connections from each unique client IP address.
  3. Nmber of connections for each remote task class.
  4. Max, min, and averge response time for the total connection, for each unique IP address, for each remote task class.
  5. Number of failed connections. (busy or rejected)

Create listdef Schema Class

I like to use dummy schema classes for defining lists which get passed around my application. This makes it easy to find the column names because they can be viewed in the F9 Catalog.

Create a list definition dummy schema class for our web app stats summary values.

  1. F2 Browser > ContactsWeb > New Class > Schema
  2. Name the class sWebStatsSummary_listdef
  3. Add the following columns to the listdef schema class.
    1. BeginDate - Short Date
    2. EndDate - Short Date
    3. TotalConnections - Integer
    4. FailedConnections - Integer
    5. MaxResponseSeconds - Integer
    6. MinResponseSeconds - Integer
    7. AvgResponseSeconds - Integer
    8. ClientAddress - Character 15
    9. RemoteTaskClassName - Character 50
  4. Close the schema class.

Web Stats List Method

There are numerous ways to compile the summary statistics. If you are an advanced SQL guru and using a 3rd party RDBMS you could get the statistics using SQL. Since we are working with the Omnis data file and I'm not an advanced SQL guru we'll compile the statistics using Omnis list notation.

The summary stats method is fairly lengthy. We won't go into detail explaining the code because learning SQL and learning lists notation is not the purpose of this tutorial.

  1. Add a new method, $retWebStatsList, to the oWebMonitor object.
  2. Add the following parameters to the method.
    1. pBeginDate - Short Date
    2. pEndDate - Short Date
    3. pkSubtotalPeriod - Long Integer - Default value zero (0)

  3. Add the following code to the method:

    ; Prepare the statement object using bind variables.
    Do dbsessionobj.$newstatement() Returns StmntObj
    Calculate SQLText as "SELECT * FROM Webappstat WHERE ConnectionTime >= @[BeginDate] AND ConnectionTime <= @[EndDate]"
    Do StmntObj.$prepare(SQLText) Returns FlagOK
    If not(FlagOK)
       Calculate Mssg as con("SQL Error occured while compiling Web App stats. ",kCr,"SQLText = ",SQLText,kCr,"Error = ",StmntObj.$nativeerrortext)
       Do errhndlr.$logError($cmethod,Mssg)
    Else
       
       ; Loop from the begin date to the end date in the specified subtotal period.
       If pBeginDate>pEndDate
          Calculate BeginDate as pEndDate
       Else
          Calculate BeginDate as pBeginDate
       End If
       Calculate DateTo as dadd(kDay,1,pEndDate)
       Calculate DateTo as dadd(kSecond,-1,DateTo)
       While BeginDate<=DateTo
          
          Switch pkSubtotalPeriod
             Case kDay
                Calculate EndDate as BeginDate
                Calculate NextBeginDate as dadd(kDay,1,BeginDate)
             Case kWeek
                Calculate EndDate as lday(kWeek,BeginDate)
                Calculate NextBeginDate as dadd(kDay,1,EndDate)
             Case kMonth
                Calculate EndDate as lday(kMonth,BeginDate)
                Calculate NextBeginDate as dadd(kDay,1,EndDate)
             Case kYear
                Calculate EndDate as lday(kYear,BeginDate)
                Calculate NextBeginDate as dadd(kDay,1,EndDate)
             Default
                Calculate EndDate as pEndDate
                Calculate NextBeginDate as dadd(kDay,1,EndDate)
          End Switch
          
          ; If the end date is greater than pDateTo, cut it back.
          If EndDate>pEndDate
             Calculate EndDate as pEndDate
          End If
          
          ; Move the end date to the last minute of the day.
          Calculate EndDate as dadd(kDay,1,EndDate)
          Calculate EndDate as dadd(kSecond,-1,EndDate)
          
          ; Execute the prepared SQL statement.
          Do StmntObj.$execute() Returns FlagOK
          If FlagOK
             
             ; Fetch the selected records.
             Do StmntObj.$fetch(FetchList,kFetchAll) Returns FetchStatus
             If FetchStatus=kFetchError
                Calculate FlagOK as kFalse
             End If
             
          End If
          If not(FlagOK)
             Calculate Mssg as con("SQL Error occured while compiling Web App stats.",kCr,StmntObj.$nativeerrortext)
             Do errhndlr.$logError($cmethod,Mssg)
             Break to end of loop
          Else
             
             ; Summarize the stats as per the specified subtotal period.
             Do method compileStatsList (BeginDate,EndDate,FetchList) Returns RetStatsList
             If StatsList.$linecount=0
                Calculate StatsList as RetStatsList
             Else
                Do StatsList.$merge(RetStatsList)
             End If
             
          End If
          
          ; Move the begin date to the 'next' begin date.
          Calculate BeginDate as NextBeginDate
          
       End While
       If not(FlagOK)
          Do StatsList.$define()
       End If
    End If
    Quit method StatsList

  4. Add a new method, complileStatsList, to the oWebMonitor object.

  5. Add the following parameters to the method.

    1. pBeginDate - Short Date
    2. pEndDate - Short Date
    3. pfFetchList - Field reference

  6. Add the following code to the method:

    ; Define the stats list.
    If iEmptyStatsList.$colcount=0
       Do iEmptyStatsList.$definefromsqlclass('sWebStatsSummary_listdef')
    End If
    Calculate StatsList as iEmptyStatsList

    Calculate MasterList as pfFetchList

    ; Set any event code to value of 1 (evBusy or evRejected)
    Do MasterList.$sendall($ref.EventCode.$assign(pick(len(MasterList.EventCode)>0,0,1)))

    Calculate TempList as MasterList

    ; Total connections
    Do StatsList.$add()
    Do StatsList.$line.$assign($ref.$linecount)
    Calculate StatsList.BeginDate as pBeginDate
    Calculate StatsList.EndDate as pEndDate
    Calculate StatsList.FailedConnections as TempList.$cols.EventCode.$total()
    Calculate StatsList.TotalConnections as TempList.$linecount-StatsList.FailedConnections
    Calculate StatsList.MaxResponseSeconds as TempList.$cols.ResponseSeconds.$maximum()
    Calculate StatsList.MinResponseSeconds as TempList.$cols.ResponseSeconds.$minimum()
    Calculate StatsList.AvgResponseSeconds as TempList.$cols.ResponseSeconds.$average()

    ; Connections by client address.
    Calculate TempList as MasterList
    While TempList.$linecount
       
       ; Select the clientaddress lines matching the first line.
       Do TempList.$search($ref.ClientAddress=TempList.1.ClientAddress)
       
       Do StatsList.$add()
       Do StatsList.$line.$assign($ref.$linecount)
       Calculate StatsList.BeginDate as pBeginDate
       Calculate StatsList.EndDate as pEndDate
       Calculate StatsList.FailedConnections as TempList.$cols.EventCode.$total(kTrue)
       Calculate StatsList.TotalConnections as TempList.$cols.ConnectionTime.$count(kTrue)-StatsList.FailedConnections
       Calculate StatsList.MaxResponseSeconds as TempList.$cols.ResponseSeconds.$maximum(kTrue)
       Calculate StatsList.MinResponseSeconds as TempList.$cols.ResponseSeconds.$minimum(kTrue)
       Calculate StatsList.AvgResponseSeconds as TempList.$cols.ResponseSeconds.$average(kTrue)
       Calculate StatsList.ClientAddress as TempList.1.ClientAddress
       
       ; Delete the selected clientaddress lines.
       Do TempList.$remove(kListDeleteSelected)
       
    End While

    ; Connections by remote task class.
    Calculate TempList as MasterList
    While TempList.$linecount
       
       ; Select the remote task class lines matching the first line.
       Do TempList.$search($ref.RemoteTaskClassName=TempList.1.RemoteTaskClassName)
       
       Do StatsList.$add()
       Do StatsList.$line.$assign($ref.$linecount)
       Calculate StatsList.BeginDate as pBeginDate
       Calculate StatsList.EndDate as pEndDate
       Calculate StatsList.FailedConnections as TempList.$cols.EventCode.$total(kTrue)
       Calculate StatsList.TotalConnections as TempList.$cols.ConnectionTime.$count(kTrue)-StatsList.FailedConnections
       Calculate StatsList.MaxResponseSeconds as TempList.$cols.ResponseSeconds.$maximum(kTrue)
       Calculate StatsList.MinResponseSeconds as TempList.$cols.ResponseSeconds.$minimum(kTrue)
       Calculate StatsList.AvgResponseSeconds as TempList.$cols.ResponseSeconds.$average(kTrue)
       Calculate StatsList.RemoteTaskClassName as TempList.1.RemoteTaskClassName
       
       ; Delete the selected remote task class lines.
       Do TempList.$remove(kListDeleteSelected)
       
    End While

    Quit method StatsList

Test Web Stats List Method

We can test the $retWebStatsList method from the Programmer Test Method of the Contacts menu.

  1. Contacts menu > Programmer Test Method...
  2. After the Breakpoint and Quit method enter the following code:

    Calculate BeginDate as fday(kMonth,#D)
    Calculate EndDate as lday(kMonth,#D)
    Do webmon.$retWebStatsList(BeginDate,EndDate) Returns #L1

  3. Contact menu > Programmer Test Method...
  4. Double-click the BeginDate line of code to set the Go point to that line.
  5. Click the Step-over button 3 times and then check the return value of #L1

Convert Web Stats List to Text

The web stats list could be put into a report, an HTML table, or converted to text.

We'll add a method to oWebMonitor to convert the web stats list to text.

  1. Add a new method, $convertWebStatsListToText, to the oWebMonitor object.
  2. Add the parameter, pfWebStatsList, Field Reference to the method.
  3. Add the following code to the method:

; Total connections.
Calculate List as pfWebStatsList

Do List.$search($ref.ClientAddress<>''|$ref.RemoteTaskClassName<>'')
Do List.$remove(kListDeleteSelected)
Do List.$sort($ref.BeginDate)

Calculate Text as con("Total Connections")
For List.$line from 1 to List.$linecount step 1
   
   Calculate Text as con(Text,kCr,List.BeginDate," - ",List.EndDate," : ",List.TotalConnections)
   If List.FailedConnections>0
      Calculate Text as con(Text," (",List.FailedConnections," Failed)")
   End If
   
End For

; Connections by Client IP Address
Calculate List as pfWebStatsList

Do List.$search($ref.ClientAddress<>'')
Do List.$remove(kListKeepSelected)
Do List.$sort($ref.BeginDate)

Calculate Text as con(Text,kCr,kCr,"Connections by Client IP Address")
While List.$linecount
   
   Do List.$search($ref.ClientAddress=List.1.ClientAddress)
   
   Calculate NumConn as List.$cols.TotalConnections.$total(kTrue)
   Calculate Text as con(Text,kCr,List.BeginDate," - ",List.EndDate," - ",List.1.ClientAddress," : ",NumConn)
   
   Calculate FailedNum as List.$cols.FailedConnections.$total(kTrue)
   If FailedNum>0
      Calculate Text as con(Text," (",FailedNum," Failed)")
   End If
   
   Do List.$remove(kListDeleteSelected)
   
End While

; Connections by Remote Task
Calculate List as pfWebStatsList

Do List.$search($ref.RemoteTaskClassName<>'')
Do List.$remove(kListKeepSelected)
Do List.$sort($ref.BeginDate)

Calculate Text as con(Text,kCr,kCr,"Connections by Remote Task Class")
While List.$linecount
   
   Do List.$search($ref.RemoteTaskClassName=List.1.RemoteTaskClassName)
   
   Calculate NumConn as List.$cols.TotalConnections.$total(kTrue)
   Calculate Text as con(Text,kCr,List.BeginDate," - ",List.EndDate," - ",List.1.RemoteTaskClassName," : ",NumConn)
   
   Calculate FailedNum as List.$cols.FailedConnections.$total(kTrue)
   If FailedNum>0
      Calculate Text as con(Text," (",FailedNum," Failed)")
   End If
   
   Do List.$remove(kListDeleteSelected)
   
End While

Quit method Text

Test Convert List To Text Method

We can test the $convertWebStatsListToText method from the Programmer Test Method of the Contacts menu.

  1. Contacts menu > Programmer Test Method...
  2. Add a line to the code so that the method reads as follows:

    Calculate BeginDate as fday(kMonth,#D)
    Calculate EndDate as lday(kMonth,#D)
    Do webmon.$retWebStatsList(BeginDate,EndDate) Returns #L1
    Do webmon.$convertWebStatsListToText(#L1) Returns #S1
    OK message {[#S1]}

  3. Contact menu > Programmer Test Method...
  4. Double-click the BeginDate line of code to set the Go point to that line.
  5. Click the Go button. All going well the OK message will report the web stats to you.