Tips_tutorials   >   Studiojs202   >   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)
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
.Create a list definition dummy schema class for our web app stats summary values.
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.
; 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
; 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
We can test the $retWebStatsList method from the of the menu.
Calculate BeginDate as fday(kMonth,#D)
Calculate EndDate as lday(kMonth,#D)
Do webmon.$retWebStatsList(BeginDate,EndDate) Returns #L1
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.
; 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
We can test the $convertWebStatsListToText method from the of the Contacts menu.
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]}