Variable store now per language, fix #63

This commit is contained in:
Ruben van de Ven 2019-07-10 13:57:42 +02:00
parent afdc1f860d
commit 8cfd4745f9
3 changed files with 17 additions and 12 deletions

View file

@ -118,6 +118,8 @@ for i in {1..26}; do echo $i;ssh pi@hugvey$i.local "cd hugvey && sudo bash insta
To make sure it will not die with 'Too many files open' To make sure it will not die with 'Too many files open'
examine open file limit with `ulimit -a` and set it with `ulimit -n NUMBER`
```bash ```bash
lsof -p $(ps aux|grep "[h]ugvey_server.py" |awk '{print $2}')| awk '{print $9}'|sort -rn|uniq -c|sort -rn|head -20 lsof -p $(ps aux|grep "[h]ugvey_server.py" |awk '{print $2}')| awk '{print $9}'|sort -rn|uniq -c|sort -rn|head -20
``` ```

View file

@ -467,11 +467,11 @@ class Condition(object):
return False return False
number = int(self.vars['number']) number = int(self.vars['number'])
varValues = story.hugvey.command.variableStore.getLastOfName(self.vars['var_name'], number) varValues = story.hugvey.command.variableStore.getLastOfName(self.vars['var_name'], story.language_code, number)
self.hasRan = True self.hasRan = True
if len(varValues) < number: if len(varValues) < number:
story.logger.critical(f"{self.id}: Too few instances of {self.vars['var_name']}, only {len(varValues)} in store") story.logger.warn(f"{self.id}: Too few instances of {self.vars['var_name']}, only {len(varValues)} in store")
return False return False
for i in range(number): for i in range(number):
@ -1277,7 +1277,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.hugvey.command.variableStore.addVariable(name, value, self.hugvey.id, self.language_code)
if name not in self.variables: if name not in self.variables:
return return

View file

@ -8,10 +8,11 @@ logger = mainLogger.getChild("variableStore")
class Variable: class Variable:
def __init__(self, name: str, value: str, hugveyId: int): def __init__(self, name: str, value: str, hugveyId: int, languageCode: str):
self.name = name self.name = name
self.value = value self.value = value
self.hugveyId = hugveyId self.hugveyId = hugveyId
self.languageCode = languageCode
class VariableStore: class VariableStore:
def __init__(self, db_filename): def __init__(self, db_filename):
@ -22,12 +23,14 @@ class VariableStore:
CREATE TABLE IF NOT EXISTS `variables` ( CREATE TABLE IF NOT EXISTS `variables` (
`name` VARCHAR(255), `name` VARCHAR(255),
`hugvey` INTEGER, `hugvey` INTEGER,
`language_code` VARCHAR(100),
`createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`val` VARCHAR(1024) `val` VARCHAR(1024)
); );
""", """,
""" """
CREATE INDEX IF NOT EXISTS `name_time` ON `variables` ( CREATE INDEX IF NOT EXISTS `name_time` ON `variables` (
`language_code`,
`name`, `name`,
`createdAt` DESC `createdAt` DESC
); );
@ -38,24 +41,24 @@ class VariableStore:
self.conn.commit() self.conn.commit()
self.q = asyncio.Queue() self.q = asyncio.Queue()
def addVariable(self, name, value, hugveyId): def addVariable(self, name, value, hugveyId, languageCode):
logger.debug(f"Queing storing of {name} for {hugveyId}") logger.debug(f"Queing storing of {name} for {hugveyId} ({languageCode})")
self.q.put_nowait(Variable(name, value, hugveyId)) self.q.put_nowait(Variable(name, value, hugveyId, languageCode))
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.value}'") logger.info(f"Store variable {v.name} for {v.hugveyId} ({v.languageCode}): '{v.value}'")
c.execute("INSERT INTO variables (name, hugvey, createdAt, val) VALUES (?,?, current_timestamp,?)", (v.name, v.hugveyId, v.value)) c.execute("INSERT INTO variables (name, hugvey, language_code, createdAt, val) VALUES (?,?, ?, current_timestamp,?)", (v.name, v.hugveyId, v.languageCode, v.value))
self.conn.commit() self.conn.commit()
c.close() c.close()
def getLastOfName(self, name, n = 10): def getLastOfName(self, name, languageCode, n = 10):
cur = self.conn.cursor() cur = self.conn.cursor()
logging.debug(f"Get last {n} stored variables of {name}") logging.debug(f"Get last {n} stored variables of {name} for {languageCode}")
cur.execute("SELECT val FROM variables WHERE name = ? ORDER BY createdAt DESC LIMIT ?", (name, n)) cur.execute("SELECT val FROM variables WHERE language_code = ? AND name = ? ORDER BY createdAt DESC LIMIT ?", (languageCode, name, n))
values = [v[0] for v in cur.fetchall()] values = [v[0] for v in cur.fetchall()]
cur.close() cur.close()
return values return values