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'
examine open file limit with `ulimit -a` and set it with `ulimit -n NUMBER`
```bash
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
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
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
for i in range(number):
@ -1277,7 +1277,7 @@ class Story(object):
self.variableValues[name] = value
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:
return

View File

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