Backend service for simulating and collecting data on randomly generated soups for Conway's Game of Life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

102 lines
3.6 KiB

from os import listdir, system
import mysql.connector, json
with open("config.json", "r") as fp:
config = json.load(fp)
mydb = mysql.connector.connect(
host=config["mysql"]["host"],
user=config["mysql"]["user"],
password=config["mysql"]["password"],
database=config["mysql"]["database"]
)
home = config["home"] + "/"
# Get logs
logs = [home + "logs/" + f for f in listdir(home + "logs/")]
logs.sort()
for log in logs:
print("processing " + log)
# Load log file into dictionary
res = {}
# Get log unix timestamp
res["time"] = int(log.split(".")[1])
with open(log, "r") as fp:
fp.readline() # payosha256 key
fp.readline() # Blank
res["version"] = fp.readline().split(" ")[1][:-1]
res["md5"] = fp.readline().split(" ")[1][:-1]
res["root"] = fp.readline().split(" ")[1][:-1]
res["rule"] = fp.readline().split(" ")[1][:-1]
res["symmetry"] = fp.readline().split(" ")[1][:-1]
res["num_soups"] = int(fp.readline().split(" ")[1])
res["num_objects"] = int(fp.readline().split(" ")[1])
fp.readline() # Blank
fp.readline() # CENSUS TABLE
l = fp.readline() # First census (probably xs4_33)
res["census"] = {}
while l != "\n":
split = l.split(" ")
res["census"][split[0]] = int(split[1])
l = fp.readline()
fp.readline() # SAMPLE_SOUPIDS
l = fp.readline() # First sample (probably xs4_33)
res["samples"] = {}
while l != "":
split = l.split(" ")
split[-1] = split[-1][:-1] # Removes \n from end of last value
res["samples"][split[0]] = [res["root"] + c for c in split[1:]] # Combine id with root for further reference
l = fp.readline()
# Complete census
for life in res["census"]:
cursor = mydb.cursor()
sql = "SELECT * FROM census WHERE apgcode=%s"
code = (life, )
cursor.execute(sql, code)
result = cursor.fetchall()
# Check if no element exists
if result == []:
sql = "INSERT INTO census VALUES ('%s', %d, %d, %d)" % (life, res["census"][life], res["time"], res["time"])
cursor.execute(sql)
else:
# Update element with new total and new time
apgcode, population, firstFound, lastFound = result[0]
population += res["census"][life]
if lastFound < res["time"]:
sql = "UPDATE census SET population=%d, lastFound=%d WHERE apgcode='%s'" % (population, res["time"], life)
else:
sql = "UPDATE census SET population=%d WHERE apgcode='%s'" % (population, life)
cursor.execute(sql)
# Add samples
for life in res["samples"]:
cursorn = mydb.cursor()
sql = "SELECT * FROM samples WHERE apgcode='%s'" % life
cursorn.execute(sql)
result = cursorn.fetchall()
n = len(result)
for sample in res["samples"][life]:
sql = "INSERT INTO samples VALUES ('%s', %d, '%s')" % (life, n ,sample)
if n < 25 or sample[:1] == "me": # "me" is the prefix to "megasized" "messless" and "methuselah"
cursorn.execute(sql)
n += 1
elif n == 25:
cursorn.execute(sql)
break
else:
break
system("mv " + log + " " + home + "processed")
mydb.commit()
system("gzip " + home + "processed/*.txt")