summaryrefslogtreecommitdiffstats
path: root/scripts/getresultdetails2.py
blob: b0d429cd4fbc3368f3acee9f6579165939820c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from dbaccess import execQuery
from misc import textToId, printJSONHeader

class GetResultDetails2:

    def __init__(
        self, host1, platform1, branch1, sha11, host2, platform2, branch2,
        sha12, benchmark, metric):
        self.host1 = host1
        self.platform1 = platform1
        self.branch1 = branch1
        self.sha11 = sha11
        self.host2 = host2
        self.platform2 = platform2
        self.branch2 = branch2
        self.sha12 = sha12
        self.benchmark = benchmark
        self.metric = metric

        self.host1_id = textToId("host", host1)
        self.platform1_id = textToId("platform", platform1)
        self.branch1_id = textToId("branch", branch1)
        self.sha11_id = textToId("sha1", sha11)
        self.host2_id = textToId("host", host2)
        self.platform2_id = textToId("platform", platform2)
        self.branch2_id = textToId("branch", branch2)
        self.sha12_id = textToId("sha1", sha12)
        self.benchmark_id = textToId("benchmark", benchmark)
        self.metric_id = textToId("metric", metric)

    def execute(self):
        self.sample1, self.sample2 = self.fetchSamples()
        self.writeOutput()

    # Fetches the sample (i.e. individual observations) for both contexts.
    def fetchSamples(self):

        sample1 = execQuery(
            "SELECT value, valid, extract(epoch FROM startTime)::int " +
            "FROM result, upload WHERE uploadId = upload.id" +
            " AND hostId = " + str(self.host1_id) +
            " AND platformId = " + str(self.platform1_id) + " AND branchId = " +
            str(self.branch1_id) + " AND sha1Id = " + str(self.sha11_id) +
            " AND benchmarkId = " + str(self.benchmark_id) +
            " AND metricId = " + str(self.metric_id) +
            " ORDER BY value DESC;"
            )
        sample2 = execQuery(
            "SELECT value, valid, extract(epoch FROM startTime)::int " +
            "FROM result, upload WHERE uploadId = upload.id" +
            " AND hostId = " + str(self.host2_id) +
            " AND platformId = " + str(self.platform2_id) + " AND branchId = " +
            str(self.branch2_id) + " AND sha1Id = " + str(self.sha12_id) +
            " AND benchmarkId = " + str(self.benchmark_id) +
            " AND metricId = " + str(self.metric_id) +
            " ORDER BY value DESC;"
            )

        return sample1, sample2

    def writeOutputAsJSON(self):
        printJSONHeader()
        print "{"

        first_sample_row = True;
        for s in (("1", self.sample1), ("2", self.sample2)):
            if not first_sample_row:
                print ","
            first_sample_row = False

            print "\"sample" + s[0] + "\": ["
            first_obs_row = True
            for obs in s[1]:
                if not first_obs_row:
                    print ","
                first_obs_row = False

                print (
                    "{\"value\": \"" + str(obs[0]) + "\", \"valid\": " +
                    ("1" if obs[1] else "0") + ", \"timestamp\":" +
                    "\"" + str(obs[2]) + "\" }");
            print "]"

        print "}"


class GetResultDetails2AsJSON(GetResultDetails2):
    def writeOutput(self):
        self.writeOutputAsJSON()