def get_history(self, topic, seconds_back): conn = self._borrow_connection() cur = conn.cursor() start = now() - timedelta(seconds = seconds_back) cur.execute("EXECUTE get_history (%s, %s)", (topic, start)) data = cur.fetchall() self._retun_connection(conn) return map(self._result_to_dict, data)
def get_reminders(serv): init() queue = [] current_time = timeutils.now() c = sqlite3.connect(settings.datafile_remind) for row in c.execute("SELECT * FROM remind WHERE server = '{0}' AND time_end <= {1}".format(serv, str(current_time))): reminder = [row[1], float(row[2]), float(row[3]), row[4], row[5], row[6]] queue.append(reminder) c.execute("DELETE FROM remind WHERE server = '{0}' AND time_end <= {1}".format(serv, str(current_time))) c.commit() c.close() return queue
def is_recent_enough(self): if self.config.max_age_expected is None: return True max_age_expected = datetime.timedelta( seconds=self.config.max_age_expected) now = timeutils.now() last_updated = self.get_last_updated() if not last_updated: return False return (now - last_updated) < max_age_expected
def get_last_message(self, topic): conn = self._borrow_connection() cur = conn.cursor() cur.execute("EXECUTE get_last_message (%s)", (topic,)) data = cur.fetchone() if data == None: data = {'topic' : topic, 'time' : now(), 'value' : 0.0} else: data = self._result_to_dict(data) self._retun_connection(conn) return data
def inner(*args, **kwargs): t1 = timeutils.now() t2 = None try: with lock( name, lock_file_prefix, external, lock_path, do_log=False, semaphores=semaphores, delay=delay ): t2 = timeutils.now() LOG.debug( 'Lock "%(name)s" acquired by "%(function)s" :: ' "waited %(wait_secs)0.3fs", {"name": name, "function": reflection.get_callable_name(f), "wait_secs": (t2 - t1)}, ) return f(*args, **kwargs) finally: t3 = timeutils.now() if t2 is None: held_secs = "N/A" else: held_secs = "%0.3fs" % (t3 - t2) LOG.debug( 'Lock "%(name)s" released by "%(function)s" :: held ' "%(held_secs)s", {"name": name, "function": reflection.get_callable_name(f), "held_secs": held_secs}, )
def get_seconds_out_of_date(self): now = timeutils.now() max_age_expected = self.get_max_age_expected() last_updated = self.get_last_updated() """ - If `max_age_expected` is None we're saying 'hey, we're not going to check this'. - If `last_updated` is a null value, the data-set has never been updated, so it doesn't really give us any value to say 'you should update this thing you've never updated before' because we already know that, right? """ if max_age_expected is None or not last_updated: return None max_age_delta = datetime.timedelta(seconds=max_age_expected) return int((now - last_updated - max_age_delta).total_seconds())
def add_reminder(serv, chan, tdest, sender, target, msg): if timeutils.validate(tdest): current_time = timeutils.now() time_end = timeutils.parse(tdest) target = misc.sanitize_sql(target) msg = misc.sanitize_sql(msg) c = sqlite3.connect(settings.datafile_remind) c.execute("INSERT INTO remind VALUES ('{0}', '{1}', {2}, {3}, '{4}', '{5}', '{6}')".format(serv, chan, str(current_time), str(time_end), sender, target, msg)) c.commit() c.close() c = sqlite3.connect(settings.datafile_remind + '.archive') c.execute("INSERT INTO remind VALUES ('{0}', '{1}', {2}, {3}, '{4}', '{5}', '{6}')".format(serv, chan, str(current_time), str(time_end), sender, target, msg)) c.commit() c.close() return('Okay, I\'ll remind '+target+' in '+timeutils.timediff(time_end)+'!') else: return ('Invalid input.')
def get_seconds_out_of_date(self): now = timeutils.now() max_age_expected = self.get_max_age_expected() last_updated = self.get_last_updated() """ - If `max_age_expected` is None we're saying 'hey, we're not going to check this'. - If `last_updated` is a null value, the data-set has never been updated, so it doesn't really give us any value to say 'you should update this thing you've never updated before' because we already know that, right? """ if max_age_expected is None or not last_updated: return None max_age_delta = datetime.timedelta(seconds=max_age_expected) return int(( now - last_updated - max_age_delta ).total_seconds())
def float_parser(message): value = float(message) return {'time': now(), 'value': value}
def commit_report(data): '''Commit report and its counterparts. ''' project_name = data['project'] status = data['status'] risks = data['risks'] created = timeutils.now() author = data['author'] project = session.query(Project).filter(Project.name == project_name).first() report = Report(status, risks, author, created) report.project = project session.add(report) # Now process impediments and milestones impediments = data['impediments'] milestones = data['milestones'] # Cycle through impediments and add them if we have any if len(impediments) > 0: for imp in impediments: desc = imp['description'] comment = imp['comment'] start = timeutils.to_date_time_obj(imp['start_date']) try: end = timeutils.to_date_time_obj(imp['end_date']) except TypeError: end = None state_name = imp['state'] impediment = Impediment(desc, comment, start, end) impediment.project = project impediment.report = report state = session.query(Impediment_State).filter(Impediment_State.name == state_name).first() impediment.state = state # Create many-to-many relationship try: report.impediments.extend([impediment]) except AttributeError: report.impediments = [] report.impediments.extend([impediment]) session.add(impediment) # Now do similarly to milestones: if len(milestones) > 0: for mil in milestones: milestone = session.query(Milestone).filter(Milestone.name == mil['name']).first() try: completion_date = timeutils.to_date_time_obj(mil['expected_completion']) expected_completion = Expected_Completion(completion_date) expected_completion.milestone = milestone expected_completion.report = report except ValueError: pass try: report.milestones.extend([milestone]) except AttributeError: report.milestones = [] report.milestones.extend([milestone]) try: session.commit() except IntegrityError: session.rollback() print '\n*** DB INTEGRITY ERROR: Cannot commit report. Rolled back.'
def commit_report(data): '''Commit report and its counterparts. ''' project_name = data['project'] status = data['status'] risks = data['risks'] created = timeutils.now() author = data['author'] project = session.query(Project).filter( Project.name == project_name).first() report = Report(status, risks, author, created) report.project = project session.add(report) # Now process impediments and milestones impediments = data['impediments'] milestones = data['milestones'] # Cycle through impediments and add them if we have any if len(impediments) > 0: for imp in impediments: desc = imp['description'] comment = imp['comment'] start = timeutils.to_date_time_obj(imp['start_date']) try: end = timeutils.to_date_time_obj(imp['end_date']) except TypeError: end = None state_name = imp['state'] impediment = Impediment(desc, comment, start, end) impediment.project = project impediment.report = report state = session.query(Impediment_State).filter( Impediment_State.name == state_name).first() impediment.state = state # Create many-to-many relationship try: report.impediments.extend([impediment]) except AttributeError: report.impediments = [] report.impediments.extend([impediment]) session.add(impediment) # Now do similarly to milestones: if len(milestones) > 0: for mil in milestones: milestone = session.query(Milestone).filter( Milestone.name == mil['name']).first() try: completion_date = timeutils.to_date_time_obj( mil['expected_completion']) expected_completion = Expected_Completion(completion_date) expected_completion.milestone = milestone expected_completion.report = report except ValueError: pass try: report.milestones.extend([milestone]) except AttributeError: report.milestones = [] report.milestones.extend([milestone]) try: session.commit() except IntegrityError: session.rollback() print '\n*** DB INTEGRITY ERROR: Cannot commit report. Rolled back.'