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.variables = {}
self.variableValues = {} # captured variables from replies
self.runId = uuid.uuid4().hex
def pause(self):
self.logger.debug('pause hugvey')
@ -1314,7 +1315,7 @@ class Story(object):
self.variableValues[name] = value
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:
return
@ -1397,6 +1398,7 @@ class Story(object):
self.lastSpeechEndTime = None
self.variableValues = {} # captured variables from replies
self.finish_time = False
self.runId = uuid.uuid4().hex
self.events = [] # queue of received events
self.commands = [] # queue of commands to send

View file

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