2 # Thierry Parmentelat -- INRIA
4 # Utilities for filtering on leases
6 # w0622: we keep on using 'filter' as a variable name
7 # pylint: disable=c0111, c0103, w0622
11 from PLC.Faults import * # pylint: disable=w0401, w0614
12 from PLC.Filter import Filter
13 from PLC.Parameter import Parameter, Mixed
14 from PLC.Timestamp import Timestamp
16 # supersede the generic Filter class to support time intersection
19 class LeaseFilter(Filter):
21 # general notes on input parameters
22 # int_timestamp: number of seconds since the epoch
23 # str_timestamp: see Timestamp.sql_validate
24 # timeslot: a tuple (from, until), each being either int_timestamp or
29 Parameter(int, "int_timestamp: leases alive at that time"),
30 Parameter(str, "str_timestamp: leases alive at that time"),
31 Parameter(tuple, "timeslot: the leases alive during this timeslot")),
33 Parameter(int, "int_timestamp: leases alive after that time"),
34 Parameter(str, "str_timestamp: leases alive after that time"),
35 Parameter(tuple, "timeslot: the leases alive during this timeslot")),
37 # {'day' : 0} : all leases from today and on
38 # {'day' : 1} : all leases today (localtime at the myplc)
39 # {'day' : 2} : all leases today and tomorrow (localtime at the myplc)
43 "clip on a number of days from today and on;"
44 " 0 means no limit in the future"),
47 def __init__(self, fields=None, filter=None,
48 doc="Lease filter -- adds the 'alive' and 'clip'"
49 "capabilities for filtering on leases"):
54 Filter.__init__(self, fields, filter, doc)
55 self.fields.update(LeaseFilter.local_fields)
60 return Timestamp.cast_long(timestamp)
64 def sql_time_intersect(f1, u1, f2, u2):
65 # either f2 is in [f1,u1], or u2 is in [f1,u1], or f2<=f1<=u1<=u2
67 "(({f1} <= {f2}) AND ({f2} <= {u1})) "
68 "OR (({f1} <= {u2}) AND ({u2} <= {u1})) "
69 "OR (({f2}<={f1}) AND ({u1}<={u2}))"
70 .format(f1=f1, u1=u1, f2=f2, u2=u2))
73 def time_in_range(timestamp, f1, u1):
74 return Timestamp.cast_long(f1) <= Timestamp.cast_long(timestamp) \
75 and Timestamp.cast_long(timestamp) <= Timestamp.cast_long(u1)
78 def sql_time_in_range(timestamp, f1, u1):
79 # is timestamp in [f1, u1]
81 "(({f1} <= {timestamp}) AND ({timestamp} <= {u1}))"
82 .format(timestamp=timestamp, f1=f1, u1=u1))
85 def sql_timeslot_after(f1, u1, mark):
86 # is the lease alive after mark, i.e. u1 >= mark
87 return "({u1} >= {mark})".format(u1=u1, mark=mark)
89 # hooks for the local fields
90 def sql_alive(self, alive):
91 if isinstance(alive, (int, str)):
92 # the lease is alive at that time if from <= alive <= until
93 alive = LeaseFilter.quote(alive)
94 return LeaseFilter.sql_time_in_range(alive, 't_from', 't_until')
95 elif isinstance(alive, tuple):
97 f = LeaseFilter.quote(f)
98 u = LeaseFilter.quote(u)
99 return LeaseFilter.sql_time_intersect(f, u, 't_from', 't_until')
101 raise PLCInvalidArgument("LeaseFilter: alive field {}"
104 def sql_clip(self, clip):
105 if isinstance(clip, int) or isinstance(clip, str):
106 start = LeaseFilter.quote(clip)
107 return LeaseFilter.sql_timeslot_after('t_from', 't_until', start)
108 elif isinstance(clip, tuple):
110 f = LeaseFilter.quote(f)
111 u = LeaseFilter.quote(u)
112 return LeaseFilter.sql_time_intersect(f, u, 't_from', 't_until')
114 raise PLCInvalidArgument("LeaseFilter: clip field {}"
117 # the whole key to implementing day is to compute today's beginning
118 def today_start(self):
120 st = time.localtime()
121 seconds_today = st.tm_hour * 3600 + st.tm_min * 60 + st.tm_sec
122 return int(time.time()) - seconds_today
124 # supersede the generic Filter 'sql' method
125 def sql(self, api, join_with="AND"):
126 # implement 'day' as a clip
129 raise PLCInvalidArgument("LeaseFilter cannot have both 'clip' and 'day'")
130 today = self.today_start()
131 nb_days = self['day']
135 self['clip'] = (today, today + nb_days * 24 * 3600)
138 # preserve locally what belongs to us, hide it from the superclass
139 # self.local is a dict local_key : user_value
140 # self.negation is a dict local_key : string
143 for (k, v) in list(LeaseFilter.local_fields.items()):
145 self.local[k] = self[k]
147 self.negation[k] = ""
148 elif ('~' + k) in self:
149 self.local[k] = self['~' + k]
151 self.negation[k] = "NOT "
152 # run the generic filtering code
153 (where_part, clip_part) = Filter.sql(self, api, join_with)
154 for (k, v) in list(self.local.items()):
156 # locate hook function associated with key
157 method = LeaseFilter.__dict__['sql_' + k]
158 where_part += " {} {}({})"\
159 .format(self.join_with,
161 method(self, self.local[k]))
162 except Exception as e:
163 raise PLCInvalidArgument(
164 "LeaseFilter: something wrong with filter"
165 "key {}, val was {} -- {}".format(k, v, e))
166 return (where_part, clip_part)
168 # xxx not sure where this belongs yet
169 # given a set of nodes, and a timeslot,
170 # returns the available leases that have at least a given duration
173 def free_leases(api, node_ids, t_from, t_until, min_duration):
175 # get the leases for these nodes and timeslot
176 filter = {'node_id': node_ids,
177 'clip': (t_from, t_until),
178 # sort by node, and inside one node, chronologically
179 '-SORT': ('node_id', 't_from'),
181 leases = Leases(api, filter)
188 # scan nodes from the input
190 # scan nodes from the leases
193 return '?? what now ??'
196 def node_free_leases(node_id, node_leases, t_from, t_until):
198 # no lease yet : return one solid lease
200 return [{'node_id': node_id,
205 current_time = t_from
206 is_on = LeaseFilter.time_in_range(
207 node_leases[0]['t_from'], t_from, t_until)
210 # print 'DBG','current_time',current_time,'is_on',is_on,'result',result
213 current_time = node_leases[0]['t_until']
218 # free, has no remaining lease
219 elif not node_leases:
222 't_from': current_time, 't_until': t_until})
224 # free and has remaining leases
226 next_time = node_leases[0]['t_from']
229 't_from': current_time, 't_until': next_time})
230 current_time = next_time