Persisting Statistics

CountryHawk keeps a running total of all statistics in memory. This means that once the application or web services that use CountryHawk are restarted, the statistics will be zeroed.

The CountryStats COM object and CountryStats .NET object provide the PeristToFile and LoadFromFile methods that can be used to persist all collected statistics to a file, and load these persisted statistics back into memory in the future so that additional lookups performed by CountryHawk will be added to the existing results.

For web developers, the best place to call the PersistToFile method is from the global.asa (ASP) or global.asax (ASP.NET) Application shutdown event. This event is called when your web services are shutdown or restarted, or when your web server is shutdown. Calling this method results in a dump of all statistics to a text file that can be loaded in the future by calling LoadFromFile.

Likewise, the best place to call the LoadFromFile method is from the global.asa / global.asax application start-up event. This event is called when your web services are started. CountryHawk will read the contents of the file created by a previous call to PersistToFile, and initialize all stats counters to their previous values, rather than zero as would otherwise be the case.

ASP Example:

Here is an example of how to configure global.asa to automatically persist your stats at shutdown and reload the stats at start-up:

<script language="vbscript" runat="server">

sub Application_OnStart

set chObj = CreateObject("cyScape.CountryStats")

chObj.LoadFromFile

end sub

 

sub Application_OnEnd

set chObj = CreateObject("cyScape.CountryStats")

chObj.PersistToFile

end sub

 

sub Session_OnStart

 'Automatically counts this user in our stats, and store the

 'country object for us in a session variable so

 'so we can use it later. If you only want to log stats and

 'not do anything based on their country at run-time then there

 'is no need to save this object in a session variable.

 set session("CHawkObj") = Server.CreateObject("cyScape.CountryObj")

end sub

 

sub Session_OnEnd

end sub

</script>

ASP.NET Example:

Here is an example of how to configure global.asax to automatically persist your stats at shutdown and reload the stats at start-up:

using cyScape.CountryHawk;

protected void Application_Start(Object sender, EventArgs e)

{

CountryStats.LoadFromFile();

}

 

protected void Application_End(Object sender, EventArgs e)

{

 CountryStats.PersitToFile();

}

 

Note: It is possible that if your server has a problem and is not shutdown properly that the Application OnEnd event may not fire. In that case the stats will not be persisted and any stats accumulated since the last PersistToFile operation occurred will be lost. If it is important that this never happen (you may not mind if you lose a few days worth of data, but if you do read on) you should create an ASP or ASP.NET page and schedule it to be run once or twice a day by a background process. In that page call the PersistToFile method. This will ensure that in a scenario where the server is not shut down properly you will in the worse case scenario lose just one day's worth of stats.

Note: By default stats are saved by the PersistToFile method to a file named countrystats.properties. You should regularly make a backup copy of this file.

Note: Use of these methods requires the Enterprise Edition of CountryHawk.

See Also:

Using the CountryStats Object

Using the RegionStats Object

Collecting Stats in Real-time

Collecting Stats by Batch Processing Log Files

CountryStats Object and Methods

RegionStats Object and Methods