summaryrefslogtreecommitdiffstats
path: root/storperf/db/job_db.py
blob: f24ccf4c95f2f27bc829c9d8c23a076623695d40 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
##############################################################################
# Copyright (c) 2015 EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from _sqlite3 import OperationalError
from threading import Lock
import calendar
import logging
import sqlite3
import time
import uuid

import requests


db_mutex = Lock()


class JobDB(object):

    db_name = "StorPerfJob.db"

    def __init__(self):
        """
        Creates the StorPerfJob.db and jobs tables on demand
        """

        self.logger = logging.getLogger(__name__)
        self.logger.debug("Connecting to " + JobDB.db_name)
        self.job_id = None

        with db_mutex:
            db = sqlite3.connect(JobDB.db_name)
            cursor = db.cursor()
            try:
                cursor.execute('''CREATE TABLE jobs
                (job_id text,
                workload text,
                start text,
                end text)''')
                self.logger.debug("Created job table")
            except OperationalError:
                self.logger.debug("Job table exists")

            cursor.execute('SELECT * FROM jobs')
            db.commit()
            db.close()

    def create_job_id(self):
        """
        Returns a job id that is guaranteed to be unique in this
        StorPerf instance.
        """
        with db_mutex:
            db = sqlite3.connect(JobDB.db_name)
            cursor = db.cursor()

            self.job_id = str(uuid.uuid4())
            row = cursor.execute(
                "select * from jobs where job_id = ?", (self.job_id,))

            while (row.fetchone() is not None):
                self.logger.info("Duplicate job id found, regenerating")
                self.job_id = str(uuid.uuid4())
                row = cursor.execute(
                    "select * from jobs where job_id = ?", (self.job_id,))

            cursor.execute(
                "insert into jobs(job_id) values (?)", (self.job_id,))
            self.logger.debug("Reserved job id " + self.job_id)
            db.commit()
            db.close()

    def start_workload(self, workload_name):
        """
        Records the start time for the given workload
        """
        with db_mutex:
            if (self.job_id is None):
                self.create_job_id()

            db = sqlite3.connect(JobDB.db_name)
            cursor = db.cursor()

            now = str(calendar.timegm(time.gmtime()))

            row = cursor.execute(
                """select * from jobs
                           where job_id = ?
                           and workload = ?""",
                (self.job_id, workload_name,))

            if (row.fetchone() is None):
                cursor.execute(
                    """insert into jobs
                               (job_id,
                               workload,
                               start)
                               values (?, ?, ?)""",
                    (self.job_id,
                     workload_name,
                     now,))
            else:
                self.logger.warn("Duplicate start time for workload "
                                 + workload_name)
                cursor.execute(
                    """update jobs set
                               job_id = ?,
                               start = ?
                               where workload = ?""",
                    (self.job_id,
                     now,
                     workload_name,))

            db.commit()
            db.close()

    def end_workload(self, workload_name):
        """
        Records the end time for the given workload
        """
        with db_mutex:
            if (self.job_id is None):
                self.create_job_id()

            db = sqlite3.connect(JobDB.db_name)
            cursor = db.cursor()
            now = str(calendar.timegm(time.gmtime()))

            row = cursor.execute(
                """select * from jobs
                           where job_id = ?
                           and workload = ?""",
                (self.job_id, workload_name,))

            if (row.fetchone() is None):
                self.logger.warn("No start time recorded for workload "
                                 + workload_name)
                cursor.execute(
                    """insert into jobs
                               (job_id,
                               workload,
                               start,
                               end)
                               values (?, ?, ?, ?)""",
                    (self.job_id,
                     workload_name,
                     now,
                     now))
            else:
                cursor.execute(
                    """update jobs set
                               job_id = ?,
                               end = ?
                               where workload = ?""",
                    (self.job_id,
                     now,
                     workload_name,))

            db.commit()
            db.close()

    def fetch_results(self, workload_prefix=""):
        if (workload_prefix is None):
            workload_prefix = ""

        workload_prefix = workload_prefix + "%"

        stats = ()

        start_time = str(calendar.timegm(time.gmtime()))
        end_time = "0"

        self.logger.debug("Workload like: " + workload_prefix)

        with db_mutex:

            db = sqlite3.connect(JobDB.db_name)
            cursor = db.cursor()
            cursor.execute("""select start, end, workload
                from jobs where workload like ?""",
                           (workload_prefix,))

            while (True):
                row = cursor.fetchone()
                if (row is None):
                    break

                start_time = str(row[0])
                end_time = str(row[1])
                workload = str(row[2])

                # for most of these stats, we just want the final one
                # as that is cumulative average or whatever for the whole
                # run

                self.logger.info("workload=" + workload +
                                 "start=" + start_time + " end=" + end_time)

                request = 'http://127.0.0.1:8000/render/?target=*.' + self.job_id + \
                    '.' + workload + '.jobs.1.*.clat.mean&format=json&from=' + \
                    start_time + "&until=" + end_time

                response = requests.get(request)

                if (response.status_code == 200):
                    data = response.json()
                    print data
                else:
                    pass
            db.close()