diff options
Diffstat (limited to 'scripts/misc.py')
-rw-r--r-- | scripts/misc.py | 156 |
1 files changed, 78 insertions, 78 deletions
diff --git a/scripts/misc.py b/scripts/misc.py index 8d0be5c..e5663c3 100644 --- a/scripts/misc.py +++ b/scripts/misc.py @@ -15,14 +15,15 @@ def idToText(table, id_): idToTextCache[table] = {} if id_ in idToTextCache[table]: - return idToTextCache[table][id_]; + return idToTextCache[table][id_] - text = ((execQuery( - "SELECT value FROM " + table + " WHERE id = " + str(id_) + - ";")[0][0]) if (id_ >= 0) else "") - idToTextCache[table][id_] = text; + text = (execQuery( + "SELECT value FROM " + table + " WHERE id = %s", (id_,))[0][0] + if (id_ >= 0) else "") + idToTextCache[table][id_] = text + + return text - return text; # ### 2 B DOCUMENTED! def textToId(table, text): @@ -33,15 +34,15 @@ def textToId(table, text): textToIdCache[table] = {} if text in textToIdCache[table]: - return textToIdCache[table][text]; + return textToIdCache[table][text] + + res = execQuery("SELECT id FROM " + table + " WHERE value = %s", (text,)) + id_ = res[0][0] if (len(res) > 0) else -1 - res = execQuery( - "SELECT id FROM " + table + " WHERE value = '" + str(text) + "';") - id_ = res[0][0] if (len(res) > 0) else -1; + textToIdCache[table][text] = id_ - textToIdCache[table][text] = id_; + return id_ - return id_; # ### 2 B DOCUMENTED! # Maybe also rename to lowerIsBetter() ? (but note that a global function with @@ -52,28 +53,30 @@ def metricIdToLowerIsBetter(metric_id): metricIdToLIBCache = {} if metric_id in metricIdToLIBCache: - return metricIdToLIBCache[metric_id]; + return metricIdToLIBCache[metric_id] lib = execQuery( - "SELECT lowerIsBetter FROM metric WHERE id = " + str(metric_id) + - ";")[0][0] - metricIdToLIBCache[metric_id] = lib; + "SELECT lowerIsBetter FROM metric WHERE id = %s", (metric_id,))[0][0] + metricIdToLIBCache[metric_id] = lib + + return lib - return lib; # Returns the non-negative ID of the given context, or -1 if not found. def getContext(host_id, platform_id, branch_id, sha1_id): - result = execQuery("SELECT id FROM context" - " WHERE hostId = %d" - " AND platformId = %d" - " AND branchId = %d" - " AND sha1Id = %d" - "LIMIT 1;" - % (host_id, platform_id, branch_id, sha1_id)) + result = execQuery( + "SELECT id FROM context" + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s" + " AND sha1Id = %s" + "LIMIT 1", + (host_id, platform_id, branch_id, sha1_id)) if len(result): return result[0][0] return -1 + # Returns the test case, test function, and data tag components of # a benchmark of the form <tc>:<tf>(<dt>). The test case and test function # components may not contain whitespace, or a ':', '(', or ')' character. @@ -86,12 +89,13 @@ def benchmarkToComponents(benchmark): else: raise BaseException("invalid benchmark syntax: >" + benchmark + "<") + +# Returns the timestamp associated with a particular context ID. This will +# be the UTC timestamp of the earliest upload for this context. def getTimestampFromContext(context_id): return execQuery( - "SELECT EXTRACT(EPOCH FROM timestamp)::INT" - " FROM context" - " WHERE id = %d;" - % context_id)[0][0] + "SELECT EXTRACT(EPOCH FROM timestamp)::INT FROM context WHERE id = %s", + (context_id,))[0][0] # Finds snapshots that match a host/platform/branch combination and that @@ -102,33 +106,27 @@ def getTimestampFromContext(context_id): # (sha1, first upload timestamp). def getSnapshots(host_id, platform_id, branch_id, sha11_id, sha12_id): timestamp1 = execQuery( - "SELECT EXTRACT(EPOCH FROM timestamp)::INT FROM context " + - "WHERE hostId = " + str(host_id) + - " AND platformId = " + str(platform_id) + - " AND branchId = " + str(branch_id) + - " AND sha1Id = " + str(sha11_id) + ";")[0][0] - + "SELECT EXTRACT(EPOCH FROM timestamp)::INT FROM context" + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s" + " AND sha1Id = %s", + (host_id, platform_id, branch_id, sha11_id))[0][0] if sha12_id >= 0: - timestamp2 = execQuery( - "SELECT EXTRACT(EPOCH FROM timestamp)::INT FROM context " + - "WHERE hostId = " + str(host_id) + - " AND platformId = " + str(platform_id) + - " AND branchId = " + str(branch_id) + - " AND sha1Id = " + str(sha12_id) + ";")[0][0] - + "SELECT EXTRACT(EPOCH FROM timestamp)::INT FROM context" + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s" + " AND sha1Id = %s", + (host_id, platform_id, branch_id, sha12_id))[0][0] # Ensure chronological order: if timestamp1 > timestamp2: timestamp1, timestamp2 = timestamp2, timestamp1 - range_expr = "BETWEEN %d AND %d" % (timestamp1, timestamp2) - else: - range_expr = ">= %d" % timestamp1 - - # Each distinct SHA-1 that occurs for this host/platform/branch # combination may occur multiple times with different upload times. # Get the list of distinct SHA-1 IDs along with the earliest upload @@ -136,14 +134,14 @@ def getSnapshots(host_id, platform_id, branch_id, sha11_id, sha12_id): # order of the uploadId attributes is consistent with the order # of their corresponding startTime attributes): snapshots = execQuery( - "SELECT sha1Id, EXTRACT(EPOCH FROM timestamp)::INT AS " - "firstUploadTime FROM context " - "WHERE hostId = %d" - " AND platformId = %d" - " AND branchId = %d" - " AND EXTRACT(EPOCH FROM timestamp)::INT %s" - " ORDER BY timestamp ASC;" - % (host_id, platform_id, branch_id, range_expr)) + "SELECT sha1Id, EXTRACT(EPOCH FROM timestamp)::INT AS firstUploadTime" + " FROM context" + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s" + " AND EXTRACT(EPOCH FROM timestamp)::INT " + range_expr + + " ORDER BY timestamp ASC", + (host_id, platform_id, branch_id)) return tuple(snapshots) @@ -151,7 +149,6 @@ def getSnapshots(host_id, platform_id, branch_id, sha11_id, sha12_id): # Finds all snapshots matching a host/platform/branch combination. # Returns a chronologically (except when reverse is True) ordered n-tuple # of 2-tuples: (sha1, first upload timestamp). -# def getAllSnapshots(host_id, platform_id, branch_id, reverse = False): # Each distinct SHA-1 that occurs for this host/platform/branch @@ -163,11 +160,11 @@ def getAllSnapshots(host_id, platform_id, branch_id, reverse = False): snapshots = execQuery( "SELECT sha1Id, EXTRACT(EPOCH FROM timestamp)::INT AS firstUploadTime" " FROM context" - " WHERE hostId = %d" - " AND platformId = %d" - " AND branchId = %d" - % (host_id, platform_id, branch_id) + - " ORDER BY timestamp " + ("DESC;" if reverse else "ASC;")) + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s" + " ORDER BY timestamp " + ("DESC" if reverse else "ASC"), + (host_id, platform_id, branch_id)) return tuple(snapshots) @@ -181,12 +178,12 @@ def getLastRankingSnapshot(host_id, platform_id, branch_id): " FROM ranking," " (SELECT id, sha1Id, timestamp" " FROM context" - " WHERE hostId = %d" - " AND platformId = %d" - " AND branchId = %d) AS matchingContext" + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s) AS matchingContext" " WHERE context2Id = matchingContext.id" - " ORDER BY timestamp DESC LIMIT 1;" - % (host_id, platform_id, branch_id)) + " ORDER BY timestamp DESC LIMIT 1", + (host_id, platform_id, branch_id)) if len(result): return result[0] return -1, -1 @@ -203,12 +200,12 @@ def getRankingContexts(host_id, platform_id, branch_id): " FROM ranking," " (SELECT id, sha1Id, timestamp" " FROM context" - " WHERE hostId = %d" - " AND platformId = %d" - " AND branchId = %d) AS matchingContext" + " WHERE hostId = %s" + " AND platformId = %s" + " AND branchId = %s) AS matchingContext" " WHERE context2Id = matchingContext.id" - " ORDER BY etimestamp DESC;" - % (host_id, platform_id, branch_id)) + " ORDER BY etimestamp DESC", + (host_id, platform_id, branch_id)) return result @@ -251,13 +248,14 @@ def getTimeSeries( contexts.append(getContext(host_id, platform_id, branch_id, sha1_id)) # Fetch raw values: + assert len(contexts) > 0 raw_values = (execQuery( "SELECT value, valid, contextId FROM result" - " WHERE contextId IN (%s)" - " AND benchmarkId = %d" - " AND metricId = %d" - " ORDER BY contextId;" - % (", ".join(map(str, contexts)), benchmark_id, metric_id)) + + " WHERE contextId IN (%s" + ", %s"*(len(contexts) - 1) + ")" + + " AND benchmarkId = %s" + " AND metricId = %s" + " ORDER BY contextId", + tuple(contexts) + (benchmark_id, metric_id)) + [(-1, -1, -1)]) # Note sentinel item # Compute per-sample stats: @@ -587,9 +585,11 @@ def getBMTimeSeriesStatsList( # Get all distinct benchmark/metric combinations that match the # host/platform/branch context and are within the selected snapshot # interval. Each such combination corresponds to a time series. - bmark_metrics = execQuery("SELECT DISTINCT benchmarkId, metricId" - " FROM result WHERE contextId IN (%s);" - % ", ".join(map(str, contexts))) + assert len(contexts) > 0 + bmark_metrics = execQuery( + "SELECT DISTINCT benchmarkId, metricId FROM result" + " WHERE contextId IN (%s" + ", %s"*(len(contexts) - 1) + ")", + contexts) bmstats_list = [] |