blob: 5438d477e5890d263fba5a5644f5c2cd3a293027 [file] [log] [blame]
xixuan878b1eb2017-03-20 15:58:17 -07001# 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 Wu5d6063e2017-09-05 16:15:07 -07006# pylint: disable=g-tzinfo-replace
xixuan878b1eb2017-03-20 15:58:17 -07007
8import pytz
9
10from google.appengine.ext import ndb
11
12
13class LastExecutionRecord(ndb.Model):
Xixuan Wu5d6063e2017-09-05 16:15:07 -070014 """Models a last_execute_record entry with keyword & exec_time."""
xixuan878b1eb2017-03-20 15:58:17 -070015
Xixuan Wu5d6063e2017-09-05 16:15:07 -070016 # The keyword represents different types of events, e.g.
17 # Key('LastExecutionRecord', 'nightly')
18 event_type = ndb.StringProperty()
xixuan878b1eb2017-03-20 15:58:17 -070019
Xixuan Wu5d6063e2017-09-05 16:15:07 -070020 # The last execute time for a given keyword.
21 exec_time = ndb.DateTimeProperty()
xixuan878b1eb2017-03-20 15:58:17 -070022
23
24class LastExecutionRecordStore(object):
Xixuan Wu5d6063e2017-09-05 16:15:07 -070025 """Base class for reading google datastore."""
xixuan878b1eb2017-03-20 15:58:17 -070026
Xixuan Wu5d6063e2017-09-05 16:15:07 -070027 def set_last_execute_time(self, event_type, exec_time):
28 """Set the last execute time for the given keyword.
xixuan878b1eb2017-03-20 15:58:17 -070029
Xixuan Wu5d6063e2017-09-05 16:15:07 -070030 Args:
31 event_type: the keyword for saving last execute time.
32 exec_time: The UTC timestamp for last execute time.
xixuan878b1eb2017-03-20 15:58:17 -070033
Xixuan Wu5d6063e2017-09-05 16:15:07 -070034 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)
xixuan878b1eb2017-03-20 15:58:17 -070039
Xixuan Wu5d6063e2017-09-05 16:15:07 -070040 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
xixuan878b1eb2017-03-20 15:58:17 -070048
Xixuan Wu5d6063e2017-09-05 16:15:07 -070049 return cur_record.put()
xixuan878b1eb2017-03-20 15:58:17 -070050
Xixuan Wu5d6063e2017-09-05 16:15:07 -070051 def get_last_execute_time(self, event_type):
52 """Get the last execute time for the given event_type.
xixuan878b1eb2017-03-20 15:58:17 -070053
Xixuan Wu5d6063e2017-09-05 16:15:07 -070054 Args:
55 event_type: the keyword to get its last execute time.
xixuan878b1eb2017-03-20 15:58:17 -070056
Xixuan Wu5d6063e2017-09-05 16:15:07 -070057 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
xixuan878b1eb2017-03-20 15:58:17 -070066
Xixuan Wu5d6063e2017-09-05 16:15:07 -070067 def del_last_execute_time(self, event_type):
68 """Delete the last execute time for the given event_type.
xixuan878b1eb2017-03-20 15:58:17 -070069
Xixuan Wu5d6063e2017-09-05 16:15:07 -070070 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()
xixuan878b1eb2017-03-20 15:58:17 -070076
Xixuan Wu5d6063e2017-09-05 16:15:07 -070077 def get_all(self):
78 """Get the last execute time for all event_types in datastore.
xixuan878b1eb2017-03-20 15:58:17 -070079
Xixuan Wu5d6063e2017-09-05 16:15:07 -070080 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)
xixuan878b1eb2017-03-20 15:58:17 -070087
Xixuan Wu5d6063e2017-09-05 16:15:07 -070088 return [q for q in qry.iter()]
xixuan878b1eb2017-03-20 15:58:17 -070089
Xixuan Wu5d6063e2017-09-05 16:15:07 -070090 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()