Add variable only once per run

This commit is contained in:
Ruben van de Ven 2019-08-27 17:06:40 +02:00
parent 6c6c4dd397
commit 293bc3a2ea
2 changed files with 18 additions and 7 deletions

View file

@ -1275,6 +1275,7 @@ class Story(object):
self.interruptionDiversions = [] self.interruptionDiversions = []
self.variables = {} self.variables = {}
self.variableValues = {} # captured variables from replies self.variableValues = {} # captured variables from replies
self.runId = uuid.uuid4().hex
def pause(self): def pause(self):
self.logger.debug('pause hugvey') self.logger.debug('pause hugvey')
@ -1314,7 +1315,7 @@ class Story(object):
self.variableValues[name] = value self.variableValues[name] = value
if store: if store:
self.hugvey.command.variableStore.addVariable(name, value, self.hugvey.id, self.language_code) self.hugvey.command.variableStore.addVariable(self.runId, name, value, self.hugvey.id, self.language_code)
if name not in self.variables: if name not in self.variables:
return return
@ -1397,6 +1398,7 @@ class Story(object):
self.lastSpeechEndTime = None self.lastSpeechEndTime = None
self.variableValues = {} # captured variables from replies self.variableValues = {} # captured variables from replies
self.finish_time = False self.finish_time = False
self.runId = uuid.uuid4().hex
self.events = [] # queue of received events self.events = [] # queue of received events
self.commands = [] # queue of commands to send self.commands = [] # queue of commands to send

View file

@ -8,11 +8,12 @@ logger = mainLogger.getChild("variableStore")
class Variable: class Variable:
def __init__(self, name: str, value: str, hugveyId: int, languageCode: str): def __init__(self, name: str, value: str, hugveyId: int, languageCode: str, runId: str):
self.name = name self.name = name
self.value = value self.value = value
self.hugveyId = hugveyId self.hugveyId = hugveyId
self.languageCode = languageCode self.languageCode = languageCode
self.runId = runId
class VariableStore: class VariableStore:
def __init__(self, db_filename): def __init__(self, db_filename):
@ -21,6 +22,7 @@ class VariableStore:
# make sure the table exits. # make sure the table exits.
createSqls = [""" createSqls = ["""
CREATE TABLE IF NOT EXISTS `variables` ( CREATE TABLE IF NOT EXISTS `variables` (
`run_id` VARCHAR(32),
`name` VARCHAR(255), `name` VARCHAR(255),
`hugvey` INTEGER, `hugvey` INTEGER,
`language_code` VARCHAR(100), `language_code` VARCHAR(100),
@ -34,6 +36,12 @@ class VariableStore:
`name`, `name`,
`createdAt` DESC `createdAt` DESC
); );
""",
"""
CREATE UNIQUE INDEX IF NOT EXISTS `unique_per_run` ON `variables` (
`run_id`,
`name`
);
"""] """]
cur = self.conn.cursor() cur = self.conn.cursor()
for sql in createSqls: for sql in createSqls:
@ -41,17 +49,18 @@ class VariableStore:
self.conn.commit() self.conn.commit()
self.q = asyncio.Queue() self.q = asyncio.Queue()
def addVariable(self, name, value, hugveyId, languageCode): def addVariable(self, runId, name, value, hugveyId, languageCode):
logger.debug(f"Queing storing of {name} for {hugveyId} ({languageCode})") logger.debug(f"Queing storing of {name} for {hugveyId} ({languageCode}) - run {runId}")
self.q.put_nowait(Variable(name, value, hugveyId, languageCode)) self.q.put_nowait(Variable(name, value, hugveyId, languageCode, runId))
async def queueProcessor(self): async def queueProcessor(self):
while True: while True:
#: :var v: Variable #: :var v: Variable
v = await self.q.get() v = await self.q.get()
c = self.conn.cursor() c = self.conn.cursor()
logger.info(f"Store variable {v.name} for {v.hugveyId} ({v.languageCode}): '{v.value}'") logger.info(f"Store variable {v.name} for {v.hugveyId} ({v.languageCode}): '{v.value}' - run {v.runId}")
c.execute("INSERT INTO variables (name, hugvey, language_code, createdAt, val) VALUES (?,?, ?, current_timestamp,?)", (v.name, v.hugveyId, v.languageCode, v.value)) # use runId to update, rather than insert a variable for the same run.
c.execute("INSERT OR REPLACE INTO variables (run_id, name, hugvey, language_code, createdAt, val) VALUES (?,?,?, ?, current_timestamp,?)", (v.runId,v.name, v.hugveyId, v.languageCode, v.value))
self.conn.commit() self.conn.commit()
c.close() c.close()