##
# Copyright (c) 2012-2013 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Defines a set of HTTP requests to execute and return results.
"""

class HTTPTestBase(object):
    """
    Base class for an HTTP request that executes and results are returned for.
    """

    class SQLResults(object):

        def __init__(self, count, rows, timing):
            self.count = count
            self.rows = rows
            self.timing = timing


    def __init__(self, label, sessions, logFilePath):
        """
        @param label: label used to identify the test
        @type label: C{str}
        """
        self.label = label
        self.sessions = sessions
        self.logFilePath = logFilePath
        self.result = None


    def execute(self, count):
        """
        Execute the HTTP request and read the results.
        """

        self.prepare()
        self.clearLog()
        self.doRequest()
        self.collectResults(count)
        self.cleanup()
        return self.result


    def prepare(self):
        """
        Do some setup prior to the real request.
        """
        pass


    def clearLog(self):
        """
        Clear the server's SQL log file.
        """
        open(self.logFilePath, "w").write("")


    def doRequest(self):
        """
        Execute the actual HTTP request. Sub-classes override.
        """
        raise NotImplementedError


    def collectResults(self, event_count):
        """
        Parse the server log file to extract the details we need.
        """

        def extractInt(line):
            pos = line.find(": ")
            return int(line[pos + 2:])

        def extractFloat(line):
            pos = line.find(": ")
            return float(line[pos + 2:])

        data = open(self.logFilePath).read()
        lines = data.splitlines()
        count = extractInt(lines[4])
        rows = extractInt(lines[5])
        timing = extractFloat(lines[6])
        self.result = HTTPTestBase.SQLResults(count, rows, timing)

        with open("%s-%d-%s" % (self.logFilePath, event_count, self.label), "w") as f:
            f.write(data)


    def cleanup(self):
        """
        Do some cleanup after the real request.
        """
        pass

@@ -2261,32 +2261,85 @@
 ine%5Bpos + 2:%5D)%0A%0A
+        # Need to skip over stats that are unlabeled%0A
         data = o
@@ -2413,37 +2413,226 @@
     
-count = extractInt(lines%5B4%5D)%0A
+offset = 0%0A        while True:%0A            if lines%5Boffset%5D == %22*** SQL Stats ***%22:%0A                if lines%5Boffset + 2%5D.split()%5B1%5D != %22unlabeled%22:%0A                    count = extractInt(lines%5Boffset + 4%5D)%0A            
     
@@ -2663,20 +2663,36 @@
 nes%5B
-5%5D)%0A
+offset + 5%5D)%0A       
         
 timi
@@ -2687,16 +2687,21 @@
         
+     
 timing =
@@ -2724,12 +2724,33 @@
 nes%5B
-6%5D)%0A
+offset + 6%5D)%0A            
     
@@ -2811,16 +2811,143 @@
  timing)
+%0A                    break%0A            offset += 1%0A        else:%0A            self.result = HTTPTestBase.SQLResults(-1, -1, 0.0)
 %0A%0A      

Need to ignore unlabeled SQL statements.