def progress(lst, fn): # Start the progressbar l = len(lst) try: pbar = ProgressBar(l, [Percentage(), ' ', Bar(), ' ', ETA()], fd=sys.stdout).start() except: pbar = None i = 0 for row in lst: fn(row) i = i + 1 if pbar != None: try: pbar.update(i) except: pass if pbar != None: try: pbar.finish() except: pass
def progress(lst, fn): # Start the progressbar l = len(lst) try: pbar = ProgressBar(l, [Percentage(), ' ', Bar(), ' ', ETA()]).start() except: pbar = None i = 0 for row in lst: fn(row) i = i + 1 if pbar != None: try: pbar.update(i) except: pass if pbar != None: try: pbar.finish() except: pass
def Main(self, dbc, players_to_update, all): # Localize dbc self.dbc = dbc # Here we do calculations, which are to heavy for real-time print "Calculating data ..." # Update the game time factor print " - Calculating game time factor" self.dbc.execute("SELECT `game_id` FROM `games` ORDER BY `game_id` DESC LIMIT 0, 1") result = self.dbc.fetchone() if result != None: last_game = result[0] else: last_game = 1 self.dbc.execute("""UPDATE `players` SET `player_game_time_factor` = POW( SQRT(`player_games_played`) / SQRT( %s - `player_first_game_id` + 1 ), 2 )""", (last_game)) # Calculate efficiencies player_count = 0 if all == True: self.dbc.execute("SELECT `player_id` FROM `players` ORDER BY `player_id` DESC LIMIT 0, 1") result = self.dbc.fetchone() if result != None: player_count = result[0] else: player_count = len(players_to_update) print " - Calculating efficiency for %d players" % (player_count) # Start the progressbar try: pbar = ProgressBar(player_count, [Percentage(), ' ', Bar(), ' ', ETA()]).start() except: pbar = None # Iterate the stack player_id = 0 while (1): if all == True: player_id += 1 if player_id > player_count: break; else: if len(players_to_update) == 0: break; player_id = players_to_update.pop() if player_id == 0: continue # Calculate kill- and destruction efficiency self.dbc.execute("""UPDATE players SET `player_kill_efficiency` = ( `player_kills` - `player_teamkills` - ((`player_deaths_world_alien` + `player_deaths_world_human`) / 10) ) / (`player_deaths_enemy` + 1), `player_destruction_efficiency` = IFNULL(( SELECT SUM(`building_efficiency_multiplier`) FROM `buildings` LEFT JOIN `destructions` ON `destruct_building_id` = `building_id` AND `destruct_player_id` = %s INNER JOIN `weapons` ON `weapon_id` = `destruct_weapon_id` WHERE `destruct_id` IS NOT NULL AND `weapon_team` != `building_team` AND `weapon_constant` != 'MOD_NOCREEP' ) / 5 / (`player_deaths_enemy` + 1), 0), `player_total_efficiency` = `player_kill_efficiency` + `player_destruction_efficiency` WHERE `player_id` = %s """, (player_id, player_id)) # Update the progressbar if pbar != None: try: pbar.update(player_count - len(players_to_update)) except: pass # Finish the progressbar if pbar != None: try: pbar.finish() except: pass
def Log_read(self): logstart = 0 logstop = 0 oldlength = 0 lines = 0 source = self.games_log filesize = os.path.getsize(source) # run through log file to find start and end positions print "Looking for start and stop position within log file..." self.dbc.execute("SELECT `log_offset`, `log_filesize` FROM `state` WHERE `log_id` = 0") result = self.dbc.fetchone() if result != None: logstart = result[0] oldlength = result[1] print "previous run stopped parsing at offset", logstart, "with length", oldlength if oldlength > filesize: print "log file offset reset to zero due to truncated (rotated?) log file" logstart = 0 # Start the progressbar try: pbar = ProgressBar(filesize - logstart, [Percentage(), ' ', Bar(), ' ', ETA()]).start() sizedone = 0 except: pbar = None # Read the file line per line log = open(source, 'r') log.seek(logstart); while True: line = log.readline() if not line: break if line.find(" ShutdownGame:") != -1: logstop = log.tell() if pbar != None: sizedone += len(line) try: pbar.update(sizedone) except: pass log.close() # Finish the progressbar if pbar != None: try: pbar.finish() except: pass if logstop < logstart: logstop = logstart print "nothing new to parse." return None print "log parse offsets are", logstart, "to", logstop, "(", logstop - logstart, "bytes )" # Everything OK, parse the file to the database print "Parsing logfile ..." # Start the progressbar try: pbar = ProgressBar(filesize - logstart, [Percentage(), ' ', Bar(), ' ', ETA()]).start() sizedone = 0 except: pbar = None # Read the file line per line log = open(source, 'r') log.seek(logstart) while True: # Get current line line = log.readline() # If line is EOF, break if not line: break # If end of parsing, break if log.tell() > logstop: break # Truncate the line if pbar != None: sizedone += len(line) line = line[:-1] lines += 1 # Parse the line self.Log_parse_line(line) # Update the progressbar if pbar != None: try: pbar.update(sizedone) except: pass log.close() # Finish the progressbar if pbar != None: try: pbar.finish() except: pass # Store log stop point, to use as start on next run self.dbc.execute("SELECT `log_id` FROM `state` WHERE `log_id` = 0") result = self.dbc.fetchone() if result == None: self.dbc.execute("INSERT INTO `state` (`log_id`, `log_offset`, `log_filesize`, `log_runcount`) VALUES (%s, %s, %s, %s)", (0, logstop, filesize, 1)) else: dt = datetime.datetime.now() stamp = dt.isoformat() self.dbc.execute("UPDATE `state` SET `log_runcount` = `log_runcount` + 1, `log_offset` = %s, `log_filesize` = %s, `log_timestamp` = %s WHERE `log_id` = 0", (logstop, filesize, stamp)) return lines
def Main(self, dbc, players_to_update, all): # Localize dbc self.dbc = dbc # Here we do calculations, which are to heavy for real-time print "Calculating data ..." # Update the game time factor print " - Calculating game time factor" self.dbc.execute( "SELECT `game_id` FROM `games` ORDER BY `game_id` DESC LIMIT 0, 1") result = self.dbc.fetchone() if result != None: last_game = result[0] else: last_game = 1 self.dbc.execute( """UPDATE `players` SET `player_game_time_factor` = POW( SQRT(`player_games_played`) / SQRT( %s - `player_first_game_id` + 1 ), 2 )""", (last_game)) # Calculate efficiencies player_count = 0 if all == True: self.dbc.execute( "SELECT `player_id` FROM `players` ORDER BY `player_id` DESC LIMIT 0, 1" ) result = self.dbc.fetchone() if result != None: player_count = result[0] else: player_count = len(players_to_update) print " - Calculating efficiency for %d players" % (player_count) # Start the progressbar try: pbar = ProgressBar( player_count, [Percentage(), ' ', Bar(), ' ', ETA()], fd=sys.stdout).start() except: pbar = None # Iterate the stack player_id = 0 while (1): if all == True: player_id += 1 if player_id > player_count: break else: if len(players_to_update) == 0: break player_id = players_to_update.pop() if player_id == 0: continue # Calculate kill- and destruction efficiency # Use of assists here is a little arbitrary self.dbc.execute( """UPDATE players SET `player_kill_efficiency` = ( `player_kills` - `player_teamkills` + (`player_assists` / 2.5) - (`player_enemyassists` / 5) - ((`player_deaths_world_alien` + `player_deaths_world_human`) / 10) ) / (`player_deaths_enemy` + 1), `player_destruction_efficiency` = IFNULL(( SELECT SUM(`building_efficiency_multiplier`) FROM `buildings` LEFT JOIN `destructions` ON `destruct_building_id` = `building_id` AND `destruct_player_id` = %s INNER JOIN `weapons` ON `weapon_id` = `destruct_weapon_id` WHERE `destruct_id` IS NOT NULL AND `weapon_team` != `building_team` AND `weapon_constant` != 'MOD_NOCREEP' ) / 5 / (`player_deaths_enemy` + 1), 0), `player_total_efficiency` = `player_kill_efficiency` + `player_destruction_efficiency` WHERE `player_id` = %s """, (player_id, player_id)) # Update the progressbar if pbar != None: try: pbar.update(player_count - len(players_to_update)) except: pass # Finish the progressbar if pbar != None: try: pbar.finish() except: pass
def Log_read(self): logstart = 0 logstop = 0 oldlength = 0 lines = 0 source = self.games_log filesize = os.path.getsize(source) # run through log file to find start and end positions print "Looking for start and stop position within log file..." self.dbc.execute( "SELECT `log_offset`, `log_filesize` FROM `state` WHERE `log_id` = 0" ) result = self.dbc.fetchone() if result != None: logstart = result[0] oldlength = result[1] print "previous run stopped parsing at offset", logstart, "with length", oldlength if oldlength > filesize: print "log file offset reset to zero due to truncated (rotated?) log file" logstart = 0 # Start the progressbar try: pbar = ProgressBar( filesize - logstart, [Percentage(), ' ', Bar(), ' ', ETA()], fd=sys.stdout).start() sizedone = 0 except: pbar = None # Read the file line per line log = open(source, 'r') log.seek(logstart) while True: line = log.readline() if not line: break if line.find(" ShutdownGame:") != -1: logstop = log.tell() if pbar != None: sizedone += len(line) try: pbar.update(sizedone) except: pass log.close() # Finish the progressbar if pbar != None: try: pbar.finish() except: pass if logstop < logstart: logstop = logstart print "nothing new to parse." return None print "log parse offsets are", logstart, "to", logstop, "(", logstop - logstart, "bytes )" # Everything OK, parse the file to the database print "Parsing logfile ..." # Start the progressbar try: pbar = ProgressBar( filesize - logstart, [Percentage(), ' ', Bar(), ' ', ETA()], fd=sys.stdout).start() sizedone = 0 except: pbar = None # Read the file line per line log = open(source, 'r') log.seek(logstart) while True: # Get current line line = log.readline() # If line is EOF, break if not line: break # If end of parsing, break if log.tell() > logstop: break # Truncate the line if pbar != None: sizedone += len(line) line = line[:-1] lines += 1 # Parse the line self.Log_parse_line(line) # Update the progressbar if pbar != None: try: pbar.update(sizedone) except: pass log.close() # Finish the progressbar if pbar != None: try: pbar.finish() except: pass # Store log stop point, to use as start on next run self.dbc.execute("SELECT `log_id` FROM `state` WHERE `log_id` = 0") result = self.dbc.fetchone() if result == None: self.dbc.execute( "INSERT INTO `state` (`log_id`, `log_offset`, `log_filesize`, `log_runcount`) VALUES (%s, %s, %s, %s)", (0, logstop, filesize, 1)) else: dt = datetime.datetime.utcnow() stamp = dt.isoformat() self.dbc.execute( "UPDATE `state` SET `log_runcount` = `log_runcount` + 1, `log_offset` = %s, `log_filesize` = %s, `log_timestamp` = %s WHERE `log_id` = 0", (logstop, filesize, stamp)) return lines