xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for interacting with datastore.""" |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 6 | # pylint: disable=g-tzinfo-replace |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 7 | |
| 8 | import pytz |
| 9 | |
| 10 | from google.appengine.ext import ndb |
| 11 | |
| 12 | |
| 13 | class LastExecutionRecord(ndb.Model): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 14 | """Models a last_execute_record entry with keyword & exec_time.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 15 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 16 | # The keyword represents different types of events, e.g. |
| 17 | # Key('LastExecutionRecord', 'nightly') |
| 18 | event_type = ndb.StringProperty() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 19 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 20 | # The last execute time for a given keyword. |
| 21 | exec_time = ndb.DateTimeProperty() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class LastExecutionRecordStore(object): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 25 | """Base class for reading google datastore.""" |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 26 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 27 | def set_last_execute_time(self, event_type, exec_time): |
| 28 | """Set the last execute time for the given keyword. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 29 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 30 | Args: |
| 31 | event_type: the keyword for saving last execute time. |
| 32 | exec_time: The UTC timestamp for last execute time. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 33 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 34 | Returns: |
| 35 | The Key('LastExecutionRecord', event_type) to save the exec_time. |
| 36 | """ |
| 37 | if exec_time.tzinfo is not None: |
| 38 | exec_time = exec_time.replace(tzinfo=None) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 39 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 40 | cur_record_key = ndb.Key(LastExecutionRecord, event_type) |
| 41 | cur_record = cur_record_key.get() |
| 42 | if cur_record is not None: |
| 43 | cur_record.exec_time = exec_time |
| 44 | else: |
| 45 | cur_record = LastExecutionRecord(event_type=event_type, |
| 46 | exec_time=exec_time) |
| 47 | cur_record.key = cur_record_key |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 48 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 49 | return cur_record.put() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 50 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 51 | def get_last_execute_time(self, event_type): |
| 52 | """Get the last execute time for the given event_type. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 53 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 54 | Args: |
| 55 | event_type: the keyword to get its last execute time. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 56 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 57 | Returns: |
| 58 | last_exec_time: an offset-aware datetime object with timezone |
| 59 | pytz.utc. |
| 60 | """ |
| 61 | cur_record = ndb.Key(LastExecutionRecord, event_type).get() |
| 62 | if cur_record is not None: |
| 63 | return cur_record.exec_time.replace(tzinfo=pytz.utc) |
| 64 | else: |
| 65 | return None |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 66 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 67 | def del_last_execute_time(self, event_type): |
| 68 | """Delete the last execute time for the given event_type. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 69 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 70 | Args: |
| 71 | event_type: the keyword to delete its last execute time. |
| 72 | """ |
| 73 | cur_record_key = ndb.Key(LastExecutionRecord, event_type) |
| 74 | if cur_record_key.get() is not None: |
| 75 | cur_record_key.delete() |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 76 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 77 | def get_all(self): |
| 78 | """Get the last execute time for all event_types in datastore. |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 79 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 80 | Returns: |
| 81 | A list of LastExecutionRecord objects, whose exec_time is a |
| 82 | offset-aware datetime object with timezone pytz.utc. |
| 83 | """ |
| 84 | qry = LastExecutionRecord.query() |
| 85 | for q in qry.iter(): |
| 86 | q.exec_time = q.exec_time.replace(tzinfo=pytz.utc) |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 87 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 88 | return [q for q in qry.iter()] |
xixuan | 878b1eb | 2017-03-20 15:58:17 -0700 | [diff] [blame] | 89 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 90 | def delete_all(self): |
| 91 | """Delete the last execute time for all event_types in datastore.""" |
| 92 | for q in LastExecutionRecord.query().iter(): |
| 93 | q.key.delete() |