comparison getan/backend.py @ 499:199b3e3657aa

Every minute the time of the current entry is saved.
author Magnus Schieder <mschieder@intevation.de>
date Mon, 17 Sep 2018 20:35:36 +0200
parents 59d9c5840273
children f5e1a78173cb
comparison
equal deleted inserted replaced
497:ccd225b130a6 499:199b3e3657aa
38 stop_time TIMESTAMP NOT NULL, 38 stop_time TIMESTAMP NOT NULL,
39 description VARCHAR(256), 39 description VARCHAR(256),
40 40
41 CHECK (strftime('%s', start_time) <= strftime('%s', stop_time)) 41 CHECK (strftime('%s', start_time) <= strftime('%s', stop_time))
42 ) 42 )
43 """,
44 """
45 CREATE TABLE recover(
46 id INTEGER PRIMARY KEY,
47 project_id INTEGER REFERENCES projects(id),
48 start_time TIMESTAMP NOT NULL,
49 stop_time TIMESTAMP NOT NULL,
50 description VARCHAR(256),
51
52 CHECK (strftime('%s', start_time) <= strftime('%s', stop_time))
53 )
43 """ 54 """
44 ] 55 ]
45 56
46 LOAD_ACTIVE_PROJECTS = ''' 57 LOAD_ACTIVE_PROJECTS = '''
47 SELECT id, key, description, total 58 SELECT id, key, description, total
141 152
142 INSERT_PROJECT_ENTRY = ''' 153 INSERT_PROJECT_ENTRY = '''
143 INSERT INTO entries (project_id, start_time, stop_time, description) 154 INSERT INTO entries (project_id, start_time, stop_time, description)
144 VALUES(?,?,?,?) 155 VALUES(?,?,?,?)
145 ''' 156 '''
157
158 INSERT_RECOVER= '''
159 INSERT OR REPLACE INTO recover(id, project_id, start_time, stop_time, description)
160 VALUES(1,?,?,?,?)
161 '''
162
163 LOAD_RECOVER= '''
164 SELECT
165 id,
166 project_id,
167 start_time as "[timestamp]",
168 stop_time as "[timestamp]",
169 description
170 FROM
171 recover
172 WHERE
173 id = 1
174 '''
175
176 DELETE_RECOVER= "DELETE FROM recover"
146 177
147 INSERT_PROJECT = ''' 178 INSERT_PROJECT = '''
148 INSERT INTO projects (key, description, active) VALUES (?,?,1) 179 INSERT INTO projects (key, description, active) VALUES (?,?,1)
149 ''' 180 '''
150 181
221 projects.append(proj) 252 projects.append(proj)
222 253
223 logger.info("found %i active projects." % len(projects)) 254 logger.info("found %i active projects." % len(projects))
224 return projects 255 return projects
225 256
257 finally:
258 close(cur)
259
260 def load_recover(self):
261 cor = None
262 try:
263 cur = self.con.cursor()
264 cur.execute(LOAD_RECOVER)
265 recover = cur.fetchone()
266 if not recover:
267 return
268
269 _, project_id, start_time, stop_time, desc = recover
270
271 cur.execute(INSERT_PROJECT_ENTRY, (
272 project_id, start_time, stop_time, desc))
273 cur.execute(DELETE_RECOVER)
274 self.con.commit()
226 finally: 275 finally:
227 close(cur) 276 close(cur)
228 277
229 def load_project(self, key): 278 def load_project(self, key):
230 logger.debug("load active projects from database.") 279 logger.debug("load active projects from database.")
288 cur = None 337 cur = None
289 try: 338 try:
290 cur = self.con.cursor() 339 cur = self.con.cursor()
291 cur.execute(INSERT_PROJECT_ENTRY, ( 340 cur.execute(INSERT_PROJECT_ENTRY, (
292 project.id, project.start, stop_time, desc)) 341 project.id, project.start, stop_time, desc))
342 cur.execute(DELETE_RECOVER)
293 self.con.commit() 343 self.con.commit()
294 logger.debug("Added new entry '%s' of project '%s' into db" 344 logger.debug("Added new entry '%s' of project '%s' into db"
295 % (desc, project.desc)) 345 % (desc, project.desc))
296 346
297 project.load_entries() 347 project.load_entries()
348 finally:
349 close(cur)
350
351 def insert_recover(self, project, stop_time, desc):
352 if project is None:
353 return
354 cur = None
355 try:
356 cur = self.con.cursor()
357 cur.execute(INSERT_RECOVER, (
358 project.id, project.start, stop_time, desc))
359 self.con.commit()
298 finally: 360 finally:
299 close(cur) 361 close(cur)
300 362
301 def insert_project(self, key, description): 363 def insert_project(self, key, description):
302 if key is None or description is None: 364 if key is None or description is None:
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)