source: qesdi/wms_ddc_vis/trunk/lib/wms_ddc_vis/lib/wms_request_log_utils.py @ 5734

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/wms_ddc_vis/trunk/lib/wms_ddc_vis/lib/wms_request_log_utils.py@5734
Revision 5734, 2.3 KB checked in by pnorton, 10 years ago (diff)

Added some wms request logging that writes the parameeters and the header information to a file. Hopefully this will enable statistics to be generated on the data usage.

Also made some changes to the demo page so that it works in IE.

Line 
1'''
2A couple of utility functions to create WMS request log strings.
3
4Created on 18 Sep 2009
5
6@author: pnorton
7'''
8# not realy long enough to be in their own module but i didn't want to duplicate
9# the code between the controllers or have one controller refrence another.
10
11import urllib
12import logging
13
14wms_request_logger = logging.getLogger('wms_request')
15
16request_order = ['request', 'service', 'layers', 'bbox', 'width', 'height', 'version', 'styles', 'transparent']
17"""
18This is the order to include request data in the log string, any other parameters
19will be included afterwards.
20"""
21
22header_order = []
23"""
24This is the order to include header params in the log string, any other parameters
25will be included afterwards.
26"""
27
28def buildLogString(request):
29    """
30    Builds a string containing all the request parameters and all the header information
31    for the current request.
32   
33    The keys are separated form values with a ':' and the key-value paris are ',' separated.
34    All the values are url encoded with urllib.quote to make parsing the result easier.
35   
36    The resulting string will be like:
37    |PARAMS| param1:value1,param2:value2,...paramN:valueN |HEADERS| header1:val1,header2:val2,...headerN,valN
38   
39    @type : pylons request object
40    @param request: pylons request object for the current request
41   
42    @rtype : string
43    @return: the params + headers log string
44    """
45   
46    paramString = _buildParamsString(request.params, request_order)
47    headerString = _buildParamsString(request.headers, header_order)
48   
49    return "|PARAMS| %s |HEADERS| %s\n" % (paramString, headerString)
50
51def _buildParamsString(itemDict, orderList):
52   
53    keys = itemDict.keys()
54   
55    #find the keys in the ordered list of items
56    orderedKeys = [k for k in keys if k.lower() in orderList]
57   
58    #sort these keys into the order in the order list
59    def sortByOrderList(x):
60        return orderList.index(x.lower())
61    orderedKeys.sort(key=sortByOrderList)
62   
63    #get any keys not mentioned in the orderList
64    otherKeys = [k for k in keys if k not in orderedKeys]
65    otherKeys.sort()
66   
67    logString = ""
68    for k in orderedKeys + otherKeys:
69        logString += "%s:%s," % (k, urllib.quote(itemDict[k]))
70       
71    #remove the last comma
72    logString = logString[:-1]
73   
74    return logString       
Note: See TracBrowser for help on using the repository browser.