def run(self): session = Session() last_id = Log.get_last_id(session, simulate_time=self.simulate_time) if last_id is None: last_id = 0 last_id -= BUFFER_LEN time_delta = None if self.simulate_time: time_delta = monotonic_time() - self.simulate_time while not self.closing: query = session.query(Log).filter(Log.id > last_id).filter(Log.interesting == True).order_by(Log.id) if time_delta is not None: log2 = session.query(Log).filter(Log.interesting == True).filter(Log.timestamp <= monotonic_time() - time_delta).order_by(Log.timestamp.desc()).first() query = query.filter(Log.id <= log2.id) new_data = query.all() if len(new_data) > 0: last_id = new_data[-1].id #print >> sys.stderr, "Read %d records" % (len(new_data)) self.buffer += new_data self.buffer = self.buffer[-BUFFER_LEN:] session.expunge_all() session.rollback() time.sleep(SLEEP_TIME) session.rollback() session.close()
def main(): name = sys.argv[1] session = Session() team_a = session.query(Team).filter(Team.name == "Matematici").one() team_b = session.query(Team).filter(Team.name == "Fisici").one() sched_begin = datetime.datetime.strptime(sys.argv[2], '%Y-%m-%d %H:%M:%S') sched_end = datetime.datetime.strptime(sys.argv[3], '%Y-%m-%d %H:%M:%S') match_id = act_init_match(session, name, team_a, team_b, sched_begin, sched_end) session.commit() print "> Created match with ID %d" % (match_id)
def game_count(self): s = Session() gameCount = s.query(Champion).\ join(Match, Champion.match_id == Match.match_id).\ filter(Champion.champion_key == self.championKey).\ filter(Match.created_on > ( datetime.utcnow() - timedelta(days=self.days))).count() return gameCount
def listen_match(match_id, old_matches_id, post_url): session = Session() match = session.query(Match).filter(Match.id == match_id).one() old_matches = session.query(Match).filter(Match.id.in_(old_matches_id)).all() players = session.query(Player).all() old_player_matches = session.query(PlayerMatch).filter(PlayerMatch.match_id.in_(old_matches_id)).all() old_events = session.query(Event).filter(Event.match_id.in_(old_matches_id)).order_by(Event.timestamp).all() old_stats_player_matches = session.query(StatsPlayerMatch).filter(StatsPlayerMatch.match_id.in_(old_matches_id)).all() stats = Statistics(match, old_matches, players, old_player_matches, old_events, old_stats_player_matches) last_event_id = 0 last_player_match_id = 0 last_timestamp = None try: while True: session.rollback() for player_match in session.query(PlayerMatch).filter(PlayerMatch.match == match).filter(PlayerMatch.id > last_player_match_id).order_by(PlayerMatch.id): stats.new_player_match(player_match) last_player_match_id = player_match.id for event in session.query(Event).filter(Event.match == match).filter(Event.id > last_event_id).order_by(Event.id): if last_timestamp is not None and event.timestamp <= last_timestamp: print >> sys.stderr, "> Timestamp monotonicity error at %s!\n" % (event.timestamp) #sys.exit(1) stats.new_event(event) last_timestamp = event.timestamp last_event_id = event.id # Send data to the web server stats.generate_current_data() data = stats.data headers = {'content-type': 'application/json'} json_data = json.dumps({'action': 'set', 'password': PASSWD, 'data': data}) print json_data try: r = requests.post(post_url, data=json_data, headers=headers) print 'Request done', r.status_code print r.text except: print >> sys.stderr, "Exception caught, continuing..." traceback.print_exc() time.sleep(SLEEP_TIME) except KeyboardInterrupt: pass
def listen_match(match_id, old_matches_id): session = Session() match = session.query(Match).filter(Match.id == match_id).one() old_matches = session.query(Match).filter(Match.id.in_(old_matches_id)).all() players = session.query(Player).all() old_player_matches = session.query(PlayerMatch).filter(PlayerMatch.match_id.in_(old_matches_id)).all() old_events = session.query(Event).filter(Event.match_id.in_(old_matches_id)).order_by(Event.timestamp).all() old_stats_player_matches = session.query(StatsPlayerMatch).filter(StatsPlayerMatch.match_id.in_(old_matches_id)).all() stats = Statistics(match, old_matches, players, old_player_matches, old_events, old_stats_player_matches) last_event_id = 0 last_player_match_id = 0 last_timestamp = None overlay_logic = OverlayLogic(stats) last_display_event_id = 0 try: while True: session.rollback() for player_match in session.query(PlayerMatch).filter(PlayerMatch.match == match).filter(PlayerMatch.id > last_player_match_id).order_by(PlayerMatch.id): stats.new_player_match(player_match) last_player_match_id = player_match.id for event in session.query(Event).filter(Event.match == match).filter(Event.id > last_event_id).order_by(Event.id): if last_timestamp is not None and event.timestamp <= last_timestamp: print >> sys.stderr, "> Timestamp monotonicity error at %s!\n" % (event.timestamp) #sys.exit(1) stats.new_event(event) overlay_logic.receive_game_event(event) last_timestamp = event.timestamp last_event_id = event.id # Generate data stats.generate_current_data() data = stats.data # FIXME display_events = overlay_logic.get_display_events(last_display_event_id) for e in display_events: overlay_logic.ack_display_events_received(e.id) time.sleep(SLEEP_TIME) except KeyboardInterrupt: pass
def attach_foreignkeys(gen): s = Session() all_answers = {x[0] for x in s.query(Answers.id).all()} print "Answers loaded." for index, line in enumerate(gen): line = line.strip() if line.startswith("<row"): node = etree.fromstring(line) post_object = node.attrib if post_object.get('PostTypeId') == "1": attach_question(s, post_object, all_answers) if post_object.get('PostTypeId') == "2": attach_answer(s, post_object) if index % 2000 == 0: print index, "attached." sys.stdout.flush() if index % 50000 == 0: s.commit() s.commit()
def get(self): sess = Session() self.set_header("Content-Type", "application/json") self.write('{"num_nodes":3,"readings":[') i = 0 last_timestamp = {} now = datetime.datetime.utcnow() one_month_ago = now - datetime.timedelta(days=30) one_year_ago = now - datetime.timedelta(days=365) one_week_ago = now - datetime.timedelta(days=7) one_day_ago = now - datetime.timedelta(days=1) for r in (sess.query(Reading). filter(Reading.checksum_calc == Reading.checksum_sent). filter(Reading.created_at > one_year_ago). order_by(Reading.created_at)): if r.created_at < one_month_ago: min_delta = datetime.timedelta(hours=4) elif r.created_at < one_week_ago: min_delta = datetime.timedelta(hours=2) elif r.created_at < one_day_ago: min_delta = datetime.timedelta(hours=1) else: min_delta = datetime.timedelta() if (last_timestamp.get(r.node_id, None) is None or r.created_at > last_timestamp[r.node_id] + min_delta): self.write(("" if i == 0 else ",") + cjson.encode([time.mktime(r.created_at.timetuple()), #@UndefinedVariable r.reading if r.node_id == 1 else None, r.reading if r.node_id == 2 else None, r.reading if r.node_id == 3 else None])) last_timestamp[r.node_id] = r.created_at i += 1 if (i % 20) == 0: self.flush() self.finish("]}")
def standalone(): db = Session() while True: try: log.info("Opening serial port") serial = pyserial.Serial('COM3', 115200, timeout=10) #/dev/ttyACM0 log.info("Serial port open") while True: line = str(serial.readline(), 'ascii') if line: (node_id, seq_no, reading_type, temperature, checksum_sent, checksum_calc) = line.split(" ") reading = Reading() reading.node_id = int(node_id) reading.seq_no = int(seq_no) reading.reading_type = reading_type reading.reading = float(temperature) reading.checksum_sent = int(checksum_sent, 16) reading.checksum_calc = int(checksum_calc, 16) db.add(reading) db.commit() log.info("Read line: %r", line) setpoint = db.query(Setpoint).filter(Setpoint.zone_id == reading.node_id).order_by(desc(Setpoint.created_at)).first() if setpoint is not None: log.info("actual temp %s desired temp %s" % (reading.reading, setpoint.temperature)) if setpoint.temperature > reading.reading: serial.write(struct.pack('!B', (reading.node_id << 1))) # flip that zone to on log.info("opening zone %s" % reading.node_id) else: serial.write(struct.pack('!B', (reading.node_id << 1) + 1)) # flip that zone to off log.info("closing zone %s" % reading.node_id) except Exception: log.exception("Exception") time.sleep(20)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys import time from data import Session, Log if __name__ == '__main__': session = Session() for log in session.query(Log): session.delete(log) session.commit()
self.ends.append(time) def __str__(self): status = "" if self.running(): status = "(Now Playing)" seconds = int(self.total_time().total_seconds()) if seconds > 0: return "%s: %s seconds %s" % (self.performer.name, seconds, status) else: return "%s %s" % (self.performer.name, status) if __name__ == "__main__": from data import Session # Do an actual write. session = Session() dom = Performer(name="Dominic Rout", email="*****@*****.**", mobile = "07427549166") al_g = Performer(name="Al Gordon", email="*****@*****.**", mobile = "1234123412312") al_p = Performer(name="Al Pearson", email="*****@*****.**", mobile = "12341231123412312") session.add(dom) session.add(al_g) session.add(al_p) found_al = session.query(Performer).filter(Performer.name.like("%Al%")).first() print(found_al)
class SubottoCore: def __init__(self, match_id): self.session = Session() self.match = self.session.query(Match).filter(Match.id == match_id).one() self.teams = [self.match.team_a, self.match.team_b] self.order = [None, None] self.queues = [[], []] self.score = [0, 0] self.players = [[None, None], [None, None]] self.listeners = [] self.last_event_id = 0 self.last_player_match_id = 0 self.last_timestamp = None def close(self): self.session.rollback() self.session.close() def detect_team(self, team): if team == self.match.team_a: return 0 else: return 1 def new_player_match(self, player_match): print >> sys.stderr, "> Received new player match: %r" % (player_match) for listener in self.listeners: listener.new_player_match(player_match) def new_event(self, event): print >> sys.stderr, "> Received new event: %r" % (event) if event.type == Event.EV_TYPE_SWAP: self.order = [event.red_team, event.blue_team] elif event.type == Event.EV_TYPE_CHANGE: self.players[self.detect_team(event.team)] = [event.player_a, event.player_b] elif event.type == Event.EV_TYPE_GOAL: self.score[self.detect_team(event.team)] += 1 elif event.type == Event.EV_TYPE_GOAL_UNDO: self.score[self.detect_team(event.team)] -= 1 elif event.type == Event.EV_TYPE_ADVANTAGE_PHASE: pass else: print >> sys.stderr, "> Wrong event type %r\n" % (event.type) for listener in self.listeners: listener.new_event(event) def regenerate(self): print >> sys.stderr, "> Regeneration" for listener in self.listeners: listener.regenerate() def update(self): self.session.rollback() for player_match in self.session.query(PlayerMatch).filter(PlayerMatch.match == self.match).filter(PlayerMatch.id > self.last_player_match_id).order_by(PlayerMatch.id): self.new_player_match(player_match) self.last_player_match_id = player_match.id for event in self.session.query(Event).filter(Event.match == self.match).filter(Event.id > self.last_event_id).order_by(Event.id): if self.last_timestamp is not None and event.timestamp <= self.last_timestamp: print >> sys.stderr, "> Timestamp monotonicity error at %s!\n" % (event.timestamp) #sys.exit(1) self.new_event(event) self.last_timestamp = event.timestamp self.last_event_id = event.id for idx in [0, 1]: team = self.teams[idx] this_num = 0 self.queues[idx] = [] for queue_element in self.match.get_queue(team): if queue_element.num != this_num: printf >> sys.stderr, "> Error: queues are inconsistent" this_num += 1 self.queues[idx].append((queue_element.player_a, queue_element.player_b)) self.regenerate() return True def act_event(self, event, source=None): event.timestamp = now() event.match = self.match if source is None: event.source = Event.EV_SOURCE_MANUAL else: event.source = source if not event.check_type(): print >> sys.stderr, "> Sending bad event..." self.session.add(event) self.session.commit() self.update() def act_switch_teams(self, source=None): e = Event() e.type = Event.EV_TYPE_SWAP if self.order == [None, None]: e.red_team = self.teams[0] e.blue_team = self.teams[1] else: e.red_team = self.order[1] e.blue_team = self.order[0] self.act_event(e, source) def act_goal(self, team, source=None): e = Event() e.type = Event.EV_TYPE_GOAL e.team = team self.act_event(e, source) def act_goal_undo(self, team, source=None): e = Event() e.type = Event.EV_TYPE_GOAL_UNDO e.team = team self.act_event(e, source) def act_team_change(self, team, player_a, player_b, source=None): e = Event() e.type = Event.EV_TYPE_CHANGE e.team = team e.player_a = player_a e.player_b = player_b self.act_event(e, source) def act_add_to_queue(self, team, player_a, player_b): qe = QueueElement() qe.match = self.match qe.team = team qe.player_a = player_a qe.player_b = player_b qe.num = len(self.queues[self.detect_team(team)]) self.session.add(qe) self.session.commit() self.update() def act_remove_from_queue(self, team, num): queue = self.match.get_queue(team) self.session.delete(queue[num]) self.session.flush() del queue[num] # Indirect assignement to prevent failing constraints for i in xrange(num, len(queue)): queue[i].num = None self.session.flush() for i in xrange(num, len(queue)): queue[i].num = i self.session.commit() self.update() def act_swap_queue(self, team, num1, num2): queue = self.match.get_queue(team) # Indirect assignement to prevent failing constraints queue[num1].num = None queue[num2].num = None self.session.flush() queue[num1].num = num2 queue[num2].num = num1 self.session.commit() self.update() def act_begin_match(self, begin=None): if begin is None: begin = datetime.datetime.now() self.match.begin = begin self.session.commit() def act_end_match(self, end=None): if end is None: end = datetime.datetime.now() self.match.end = end self.session.commit() def act_add_player_match_from_name(self, team, fname, lname, comment=None, bulk=False): player = Player.get_or_create(self.session, fname, lname, comment) try: player_match = self.session.query(PlayerMatch).filter(PlayerMatch.match == self.match). \ filter(PlayerMatch.player == player).one() except NoResultFound: player_match = PlayerMatch() player_match.player = player player_match.match = self.match player_match.team = team self.session.add(player_match) if not bulk: self.session.commit() def easy_get_red_team(self): return self.order[0] def easy_get_blue_team(self): return self.order[1] def easy_get_red_score(self): return self.score[self.detect_team(self.easy_get_red_team())] def easy_get_blue_score(self): return self.score[self.detect_team(self.easy_get_blue_team())] def easy_act_red_goal_cell(self): self.act_goal(self.easy_get_blue_team(), source=Event.EV_SOURCE_CELL_RED_PLAIN) def easy_act_red_supergoal_cell(self): self.act_goal(self.easy_get_blue_team(), source=Event.EV_SOURCE_CELL_RED_SUPER) def easy_act_red_goal_button(self): self.act_goal(self.easy_get_red_team(), source=Event.EV_SOURCE_BUTTON_RED_GOAL) def easy_act_red_goalundo_button(self): self.act_goal_undo(self.easy_get_red_team(), source=Event.EV_SOURCE_BUTTON_RED_UNDO) def easy_act_blue_goal_cell(self): self.act_goal(self.easy_get_red_team(), source=Event.EV_SOURCE_CELL_BLUE_PLAIN) def easy_act_blue_supergoal_cell(self): self.act_goal(self.easy_get_red_team(), source=Event.EV_SOURCE_CELL_BLUE_SUPER) def easy_act_blue_goal_button(self): self.act_goal(self.easy_get_blue_team(), source=Event.EV_SOURCE_BUTTON_BLUE_GOAL) def easy_act_blue_goalundo_button(self): self.act_goal_undo(self.easy_get_blue_team(), source=Event.EV_SOURCE_BUTTON_BLUE_UNDO)
def replay_match(orig_match_id, mult=1.0, initial_wait=0.0): session = Session() # Retrieve the original match and prepare the new event list # (already sorted by SQLAlchemy) orig_match = session.query(Match).filter(Match.id == orig_match_id).one() events = [((min(max(x.timestamp, orig_match.begin), orig_match.end) - orig_match.begin).total_seconds(), x) for x in orig_match.events] ref_time = 0.0 for i in xrange(len(events)): delta = events[i][0] - ref_time ref_time = events[i][0] events[i] = (delta / mult, events[i][1]) # Replicate the original match match = Match() match.sched_begin = datetime.datetime.now() + datetime.timedelta(seconds=0.5 * initial_wait) match.sched_end = match.sched_begin + multiply_timedelta(1.0 / mult, orig_match.sched_end - orig_match.sched_begin) match.name = "Replay of \"%s\"" % (orig_match.name) match.team_a = orig_match.team_a match.team_b = orig_match.team_b session.add(match) # Add the advantage phases phase = AdvantagePhase() phase.match = match phase.start_sec = 0 phase.advantage = 10 session.add(phase) phase = AdvantagePhase() phase.match = match phase.start_sec = 30 * 60 phase.advantage = 5 session.add(phase) phase = AdvantagePhase() phase.match = match phase.start_sec = 60 * 60 phase.advantage = 3 session.add(phase) # Replicate the player_matches for orig_pm in session.query(PlayerMatch).filter(PlayerMatch.match == orig_match): pm = PlayerMatch() pm.match = match pm.player = orig_pm.player pm.team = orig_pm.team session.add(pm) # Flush and commit session.flush() session.commit() # Print match ID and start wait print "> Feeding match with ID %d" % (match.id) print "> Scheduled times: %s -- %s" % (match.sched_begin, match.sched_end) print "> Waiting initial %f seconds..." % (initial_wait) time.sleep(initial_wait) # Set begin match.begin = datetime.datetime.now() print "> Match begins at %s" % (match.begin) session.commit() # Replay events for wait_secs, orig_ev in events: print "> Waiting %f seconds..." % (wait_secs) time.sleep(wait_secs) ev = Event() ev.timestamp = datetime.datetime.now() ev.match = match ev.type = orig_ev.type ev.source = orig_ev.source ev.team = orig_ev.team ev.player_a = orig_ev.player_a ev.player_b = orig_ev.player_b ev.red_team = orig_ev.red_team ev.blue_team = orig_ev.blue_team ev.phase = orig_ev.phase print "> Pushing event of type %s and timestamp %s" % (ev.type, ev.timestamp) session.add(ev) ev.check_type() session.commit() # Set end match.end = datetime.datetime.now() print "> Match ends at %s" % (match.end) session.commit() print "> Finished feeding match with ID %d" % (match.id) print "> Scheduled times were: %s -- %s" % (match.sched_begin, match.sched_end)
def import_from_2012(): old_session = OldSession() session = Session() # Match and teams match = Match() match.name = "24 ore 2012" first_team = session.query(Team).filter(Team.name == "Matematici").one() second_team = session.query(Team).filter(Team.name == "Fisici").one() match.team_a = first_team match.team_b = second_team # Team mapping _team_map = {} old_first_team = old_session.query(OldTeam).filter(OldTeam.name == "Matematici").one() old_second_team = old_session.query(OldTeam).filter(OldTeam.name == "Fisici").one() _team_map[old_first_team.id] = first_team _team_map[old_second_team.id] = second_team team_map = lambda x: _team_map[x.id] # Player mapping _player_map = {} player_matches = [] found_player_id = set() for old_player in old_session.query(OldPlayer).all(): player = Player.get_or_create(session, old_player.fname, old_player.lname, None) _player_map[old_player.id] = player player_match = PlayerMatch() player_match.player = player player_match.match = match player_match.team = team_map(old_player.team) player_matches.append(player_match) player_map = lambda x: _player_map[x.id] # Events events = [] for old_event in old_session.query(OldEvent).order_by(OldEvent.timestamp).all(): if old_event.type == 'sched_begin': match.sched_begin = old_event.timestamp elif old_event.type == 'begin': match.begin = old_event.timestamp elif old_event.type == 'sched_end': match.sched_end = old_event.timestamp elif old_event.type == 'end': match.end = old_event.timestamp else: event = Event() event.match = match event.timestamp = old_event.timestamp event.source = Event.EV_SOURCE_MANUAL event.type = old_event.type if old_event.type in [Event.EV_TYPE_GOAL, Event.EV_TYPE_GOAL_UNDO]: event.team = _team_map[int(old_event.param)] elif old_event.type == Event.EV_TYPE_SWAP: old_red_team_id, old_blue_team_id = map(int, old_event.param.split(',')) event.red_team = _team_map[old_red_team_id] event.blue_team = _team_map[old_blue_team_id] elif old_event.type == Event.EV_TYPE_CHANGE: old_team_id, old_player_a_id, old_player_b_id = map(int, old_event.param.split(',')) event.team = _team_map[old_team_id] event.player_a = _player_map[old_player_a_id] event.player_b = _player_map[old_player_b_id] found_player_id.add(event.player_a.id) found_player_id.add(event.player_b.id) else: assert(False, "Command not supported") events.append(event) session.add(match) for pm in player_matches: if pm.player.id in found_player_id: session.add(pm) for ev in events: session.add(ev) assert(ev.check_type()) session.flush() old_session.rollback() session.commit()
""" Get email addresses from the database and export them one per line """ import data.performer as performer from data import Session import sys if len(sys.argv) != 2: print "Please enter a filename to which to save the email addresses" else: print "Saving to %s" % sys.argv[1] with open(sys.argv[1], "w") as f: session = Session() query = session.query(performer.Performer).all() for _performer in query: print >> f, _performer.email