Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Bug 931542 - Make json logs also return tags #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions schema/versions/003_Add_Tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sqlalchemy import MetaData, Table, Column, String

meta = MetaData()


def upgrade(migrate_engine):
meta.bind = migrate_engine
trees = Table('trees', meta, autoload=True)
tags = Column('tags', String(256), default="", server_default="", nullable=False)
tags.create(trees)

status_stacks = Table('status_stacks', meta, autoload=True)
tags = Column('tags', String(256), default="", server_default="", nullable=False)
tags.create(status_stacks)



def downgrade(migrate_engine):
meta.bind = migrate_engine
trees = Table('trees', meta, autoload=True)
trees.c.tags.drop()

status_stacks = Table('status_stacks', meta, autoload=True)
status_stacks.c.tags.drop()
11 changes: 9 additions & 2 deletions treestatus/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def get_tree(self, tree):
t = request.session.query(model.DbTree).get(tree)
if t:
t = t.to_dict()
t['tags'] = loads(t['tags'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this part necessary given that to_dict() sets tags?

if self.memcache:
self._mcPut('tree:%s' % tree, t, expires=60)

Expand All @@ -166,6 +167,10 @@ def get_trees(self):
treenames = []
for t in request.session.query(model.DbTree):
trees[t.tree] = t.to_dict()
try:
trees[t.tree]['tags'] = loads(trees[t.tree]['tags'])
except:
trees[t.tree]['tags'] = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, I'd trust t.to_dict() to do the right thing

treenames.append(t.tree)
if self.memcache:
self._mcPut('tree:%s' % t.tree, trees[t.tree], expires=60)
Expand All @@ -184,6 +189,7 @@ def set_status(self, who, tree, status, reason, tags, flush_stack=True):
db_tree = session.query(model.DbTree).get(tree)
db_tree.status = status
db_tree.reason = reason.strip()
db_tree.tags = tags
if flush_stack:
for s in session.query(model.DbStatusStackTree).filter_by(tree=tree):
stack = s.stack
Expand Down Expand Up @@ -212,22 +218,23 @@ def restore_status(self, who, stack_id):
# Restore its state
last_state = loads(tree.last_state)
self.set_status(who, tree.tree, last_state['status'], last_state['reason'],
'', flush_stack=False)
last_state['tags'], flush_stack=False)

# Delete everything
for tree in stack.trees:
session.delete(tree)
session.delete(stack)
session.commit()

def remember_state(self, who, trees, status, reason):
def remember_state(self, who, trees, status, reason, tags):
if not trees:
return
stack = model.DbStatusStack()
stack.who = who
stack.reason = reason
stack.when = datetime.utcnow()
stack.status = status
stack.tags = tags
session = request.session
session.add(stack)
log.debug("Remembering %s", stack)
Expand Down
8 changes: 6 additions & 2 deletions treestatus/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

Session = None


def setup(config):
engine = sa.engine_from_config(config, pool_recycle=60)
# Make sure we're up-to-date
Expand All @@ -28,7 +27,9 @@ def setup(config):
# Put it under version control
# If we have a 'trees' table, it's version 1, otherwise we're version 0
insp = Inspector.from_engine(engine)
if "trees" in insp.get_table_names():
if "trees" in insp.get_table_names() and "tags" in insp.get_columns("trees"):
version = 2
elif "trees" in insp.get_table_names():
version = 1
else:
version = 0
Expand All @@ -49,13 +50,15 @@ class DbTree(DbBase):
status = Column(String(64), default="open", nullable=False)
reason = Column(String(256), default="", nullable=False)
message_of_the_day = Column(String(800), default="", nullable=False)
tags = Column(String(256), default="", nullable=False)

def to_dict(self):
return dict(
tree=self.tree,
status=self.status,
reason=self.reason,
message_of_the_day=self.message_of_the_day,
tags = self.tags,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove whitespace around ' = '

)


Expand Down Expand Up @@ -104,6 +107,7 @@ class DbStatusStack(DbBase):
reason = Column(String(256), nullable=False)
when = Column(DateTime, nullable=False, index=True)
status = Column(String(64), nullable=False)
tags = Column(String(256), nullable=False)


class DbStatusStackTree(DbBase):
Expand Down