Skip to content

Commit

Permalink
Merge pull request #35 from mattboehm/delete-command
Browse files Browse the repository at this point in the history
Add ability to delete runs in CLI/web
  • Loading branch information
dhellmann committed Feb 16, 2016
2 parents 390e927 + 6a32cf9 commit 5c80de7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ setup-hooks =
console_scripts =
smiley = smiley.app:main
smiley.commands =
delete = smiley.commands.delete:Delete
export = smiley.commands.export:Export
import = smiley.commands.export:Import
list = smiley.commands.list:List
Expand Down
34 changes: 34 additions & 0 deletions smiley/commands/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging

from cliff import command

from smiley import db


class Delete(command.Command):
"""Delete a run from the database
"""
log = logging.getLogger(__name__)

def get_parser(self, prog_name):
parser = super(Delete, self).get_parser(prog_name)
parser.add_argument(
'--database',
default='smiley.db',
help='filename for the database (%(default)s)',
)
parser.add_argument(
'run_id',
help='identifier for the run',
)
return parser

def take_action(self, parsed_args):
db_ = db.DB(parsed_args.database)
try:
db_.delete_run(parsed_args.run_id)
self.log.info(u"Deleted run {}".format(parsed_args.run_id))
except db.NoSuchRun:
self.log.warn(u"No run found with id '{}' delete failed".format(
parsed_args.run_id))
14 changes: 14 additions & 0 deletions smiley/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ def get_trace(self, run_id, thread_id=None):
return (_make_trace(t)
for t in c.fetchall())

def delete_run(self, run_id):
"""Remove a run and all of its trace events from the database"""
# Ensure that the run exists. This will raise NoSuchRun if it doesn't.
self.get_run(run_id)
with transaction(self.conn) as c:
c.execute(
u""" DELETE FROM trace WHERE run_id = :run_id""",
{"run_id": run_id}
)
c.execute(
u"""DELETE FROM run WHERE id = :run_id""",
{"run_id": run_id}
)

def cache_file_for_run(self, run_id, filename, body):
signature_maker = hashlib.sha1()
if isinstance(filename, six.text_type):
Expand Down
15 changes: 15 additions & 0 deletions smiley/web/controllers/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

from pecan import expose, redirect, request

LOG = logging.getLogger(__name__)


class DeleteController(object):

@expose(generic=True)
def index(self, run_id):
"""Delete a run and redirect to the list of runs"""
if run_id:
request.db.delete_run(run_id)
redirect("/runs")
2 changes: 2 additions & 0 deletions smiley/web/controllers/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from smiley.presentation import syntax
from smiley.presentation import trace
from smiley.web import nav
from smiley.web.controllers import delete
from smiley.web.controllers import files
from smiley.web.controllers import stats
from smiley.web.controllers import run_context
Expand All @@ -21,6 +22,7 @@

class RunController(RestController):

delete = delete.DeleteController()
files = files.FileController()
stats = stats.StatsController()
threads = thread_controller.ThreadController()
Expand Down
4 changes: 3 additions & 1 deletion smiley/web/templates/runs.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<table class="table table-striped">
<thead>
<tr>
<th title="Delete">X</th>
<th>Id</th>
<th>Description</th>
<th>Start Time</th>
Expand All @@ -24,6 +25,7 @@
% else:
<tr>
% endif
<td><a href="/runs/delete?run_id=${run.id}" title="Delete">X</a></td>
<td><a href="/runs/${run.id}">${run.id}</a></td>
<td>${run.description}</td>
<td>${run.start_time}</td>
Expand All @@ -34,7 +36,7 @@
<td></td>
% endif
</tr>
% endfor
% endfor
</tbody>
</table>

Expand Down

0 comments on commit 5c80de7

Please sign in to comment.