Skip to content

Commit

Permalink
Title variable functionality (#490)
Browse files Browse the repository at this point in the history
* add title variable functionality

* formatting

* optimize db queries

* return title value for incidents call

* .get
  • Loading branch information
diegocepedaw authored and dwang159 committed Feb 15, 2019
1 parent f607fe4 commit d6d01e6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
22 changes: 11 additions & 11 deletions db/dummy_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,17 @@ UNLOCK TABLES;

LOCK TABLES `template_variable` WRITE;
INSERT INTO `template_variable` VALUES
(1,8,'fabric',0),
(2,8,'console_url',0),
(3,8,'filename',0),
(4,8,'name',0),
(5,8,'graph_image_url',0),
(7,8,'zones',0),
(8,8,'nodes',0),
(9,8,'metanodes',0),
(10,8,'notes',0),
(11,12,'description',0),
(12,12,'requester',0);
(1,8,'fabric',0, 0),
(2,8,'console_url',0, 0),
(3,8,'filename',0, 0),
(4,8,'name',0, 0),
(5,8,'graph_image_url',0, 0),
(7,8,'zones',0, 0),
(8,8,'nodes',0, 0),
(9,8,'metanodes',0, 0),
(10,8,'notes',0, 0),
(11,12,'description',0, 0),
(12,12,'requester',0, 0);
UNLOCK TABLES;

LOCK TABLES `twilio_delivery_status` WRITE;
Expand Down
1 change: 1 addition & 0 deletions db/schema_0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ CREATE TABLE `template_variable` (
`application_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`required` tinyint(1) NOT NULL DEFAULT '0',
`title_variable` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_template_variable_application_id` (`application_id`),
CONSTRAINT `template_variable_ibfk_1` FOREIGN KEY (`application_id`) REFERENCES `application` (`id`)
Expand Down
48 changes: 43 additions & 5 deletions src/iris/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def ts_to_sql_datetime(ts):
'created': 'UNIX_TIMESTAMP(`incident`.`created`) as `created`',
'owner': '`target`.`name` as `owner`',
'current_step': '`incident`.`current_step` as `current_step`',
'title_variable_name': '`template_variable`.`name` as `title_variable_name`'
}

incident_filters = {
Expand All @@ -188,7 +189,8 @@ def ts_to_sql_datetime(ts):
incident_query = '''SELECT DISTINCT %s FROM `incident`
JOIN `plan` ON `incident`.`plan_id` = `plan`.`id`
LEFT OUTER JOIN `target` ON `incident`.`owner_id` = `target`.`id`
JOIN `application` ON `incident`.`application_id` = `application`.`id`'''
JOIN `application` ON `incident`.`application_id` = `application`.`id`
LEFT OUTER JOIN `template_variable` ON (`template_variable`.`application_id` = `application`.`id` AND `template_variable`.`title_variable` = 1)'''

single_incident_query = '''SELECT `incident`.`id` as `id`,
`incident`.`plan_id` as `plan_id`,
Expand All @@ -204,6 +206,7 @@ def ts_to_sql_datetime(ts):
JOIN `plan` ON `incident`.`plan_id` = `plan`.`id`
LEFT OUTER JOIN `target` ON `incident`.`owner_id` = `target`.`id`
JOIN `application` ON `incident`.`application_id` = `application`.`id`
LEFT OUTER JOIN `template_variable` ON (`template_variable`.`application_id` = `application`.`id` AND `template_variable`.`title_variable` = 1)
WHERE `incident`.`id` = %s'''

single_incident_query_steps = '''SELECT `message`.`id` as `id`,
Expand Down Expand Up @@ -530,7 +533,7 @@ def ts_to_sql_datetime(ts):
FROM `application`
WHERE `auth_only` is False'''

get_vars_query = 'SELECT `name`, `required` FROM `template_variable` WHERE `application_id` = %s ORDER BY `required` DESC, `name` ASC'
get_vars_query = 'SELECT `name`, `required`, `title_variable` FROM `template_variable` WHERE `application_id` = %s ORDER BY `required` DESC, `name` ASC'

get_allowed_roles_query = '''SELECT `target_role`.`id`
FROM `target_role`
Expand Down Expand Up @@ -574,9 +577,15 @@ def ts_to_sql_datetime(ts):
uuid4hex = re.compile('[0-9a-f]{32}\Z', re.I)


def stream_incidents_with_context(cursor):
def stream_incidents_with_context(cursor, title=False):
for row in cursor:
row['context'] = ujson.loads(row['context'])
if title:
title_variable_name = row.get('title_variable_name')
if title_variable_name:
row['title'] = row['context'][title_variable_name]
else:
row['title'] = None
yield row


Expand Down Expand Up @@ -1429,7 +1438,10 @@ def on_get(self, req, resp):
cursor.execute(query, sql_values)

if 'context' in fields:
payload = ujson.dumps(stream_incidents_with_context(cursor))
if 'title_variable_name' in fields:
payload = ujson.dumps(stream_incidents_with_context(cursor, True))
else:
payload = ujson.dumps(stream_incidents_with_context(cursor, False))
else:
payload = ujson.dumps(cursor)
connection.close()
Expand Down Expand Up @@ -2447,10 +2459,13 @@ def on_get(self, req, resp, app_name):
cursor.execute(get_vars_query, app['id'])
app['variables'] = []
app['required_variables'] = []
app['title_variable'] = None
for row in cursor:
app['variables'].append(row['name'])
if row['required']:
app['required_variables'].append(row['name'])
if row['title_variable']:
app['title_variable'] = row['name']

cursor.execute(get_default_application_modes_query, app_name)
app['default_modes'] = {row['priority']: row['mode'] for row in cursor}
Expand Down Expand Up @@ -2514,18 +2529,38 @@ def on_put(self, req, resp, app_name):
{'application_id': app['id']})
}

title_variable = data.get('title_variable')
# if no variables are set then title variable will be null
if not new_variables:
title_variable = None
elif title_variable and title_variable not in new_variables:
raise HTTPBadRequest('title variable is invalid')

kill_variables = existing_variables - new_variables

# insert new variables and update the value of title_variable for existing variables
for variable in new_variables - existing_variables:
session.execute('''INSERT INTO `template_variable` (`application_id`, `name`)
VALUES (:application_id, :variable)''',
VALUES (:application_id, :variable)''',
{'application_id': app['id'], 'variable': variable})

if kill_variables:
session.execute('''DELETE FROM `template_variable`
WHERE `application_id` = :application_id AND `name` IN :variables''',
{'application_id': app['id'], 'variables': tuple(kill_variables)})

# update value of title variable for application
if title_variable:
session.execute('''UPDATE `template_variable`
SET `title_variable` = IF(`name` = :title_val, 1, 0)
WHERE `application_id`= :application_id''',
{'application_id': app['id'], 'title_val': title_variable})
else:
session.execute('''UPDATE `template_variable`
SET `title_variable` = 0
WHERE `application_id`= :application_id''',
{'application_id': app['id']})

# Only owners can (optionally) change owners
new_owners = data.get('owners')
if new_owners is not None:
Expand Down Expand Up @@ -3254,13 +3289,16 @@ def on_get(self, req, resp):
cursor.execute(get_applications_query + ' ORDER BY `application`.`name` ASC')
apps = cursor.fetchall()
for app in apps:
app['title_variable'] = None
cursor.execute(get_vars_query, app['id'])
app['variables'] = []
app['required_variables'] = []
for row in cursor:
app['variables'].append(row['name'])
if row['required']:
app['required_variables'].append(row['name'])
if row['title_variable'] == 1:
app['title_variable'] = row['name']

cursor.execute(get_default_application_modes_query, app['name'])
app['default_modes'] = {row['priority']: row['mode'] for row in cursor}
Expand Down
9 changes: 9 additions & 0 deletions src/iris/ui/static/js/iris.js
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,7 @@ iris = {
app.showEditOwners = isAdmin || isOwner;
app.allowDangerousActions = isAdmin || isOwner;
app.allowEditingSupportedModes = isAdmin;
app.allowEditingTitle = isAdmin || isOwner;

// This gets turned to true when the edit button is clicked if the user
// is an admin
Expand Down Expand Up @@ -2608,6 +2609,14 @@ iris = {
}
}
}

if (self.data.model.allowEditingTitle){
self.data.model.title_variable = $('#title-variable-select').val();
if(self.data.model.title_variable == 'null'){
self.data.model.title_variable = null;
}
}

ajaxCalls.push($.ajax({
url: self.data.url + self.data.application,
data: JSON.stringify(self.data.model),
Expand Down
13 changes: 11 additions & 2 deletions src/iris/ui/templates/application.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,18 @@ <h3>
{{/if}}
</h3>
<span class="light">Owners: {{#if owners}}{{#each owners}}<span class="label label-info">{{this}} {{#if @root.showEditOwners}}{{#ifCanShowDeleteUserButton this}}<i class="glyphicon glyphicon-remove remove-owner" data-owner="{{this}}"></i>{{/ifCanShowDeleteUserButton}}{{/if}}</span>{{/each}}{{else}}<i>none</i>{{/if}}</span>
{{#if @root.showEditOwners}}<form id="add-owner-form"><input type="text" class="form-control border-bottom input-sm typeahead" data-targettype="user" placeholder="Add username" id="add-owner-box"></input><button type="submit" class="btn btn-primary btn-xs">Add</button></form>{{/if}}<br>
{{#if @root.showEditOwners}}<form id="add-owner-form"><input type="text" class="form-control border-bottom input-sm typeahead" data-targettype="user" placeholder="Add username" id="add-owner-box"></input><button type="submit" class="btn btn-primary btn-xs">Add</button></form>{{/if}}<br>
<span class="light">Variables: {{#if variables}}{{#each variables}}<span class="label label-info">{{this}} <i class="glyphicon glyphicon-remove remove-variable" data-variable="{{this}}"></i></span>{{/each}}{{else}}<i>none</i>{{/if}}</span>
<form id="add-variable-form"><input type="text" class="form-control border-bottom input-sm" placeholder="Add variable" id="add-variable-box"></input><button type="submit" class="btn btn-primary btn-xs">Add</button></form><br>
<form id="add-variable-form"><input type="text" class="form-control border-bottom input-sm" placeholder="Add variable" id="add-variable-box"></input><button type="submit" class="btn btn-primary btn-xs">Add</button></form>
<br>
{{#if @root.isInViewMode}}
<span class="light">Incident title variable: {{#if title_variable}}<span class="label label-info">{{title_variable}} <i class="glyphicon glyphicon-remove remove-variable" data-variable="{{title_variable}}"></i></span>{{else}}<i>none</i>{{/if}}</span>
{{else}}
<span class="light">Incident title variable: {{#if variables}}<select id="title-variable-select">{{#each variables}}<option value="{{this}}">{{this}}</option>{{/each}}<option value='null'>NONE</option></select>{{else}}<i>None</i>{{/if}}</span>
{{/if}}
<br>


<div class="light" id="supported-modes-checkboxes">Supported modes:
{{#each @root.modes}}
<label><input type="checkbox" name="supported_modes" value="{{this}}" {{#ifIn this @root.supported_modes}}checked{{/ifIn}} {{#if @root.isInViewMode}}disabled{{else}}{{#ifNot @root.allowEditingSupportedModes}}disabled{{/ifNot}}{{/if}}> {{this}}</label>
Expand Down
2 changes: 1 addition & 1 deletion test/e2etest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ def test_post_plan_noc(sample_user, sample_team, sample_application_name):


def test_get_applications(sample_application_name):
app_keys = set(['variables', 'required_variables', 'name', 'context_template', 'summary_template', 'sample_context', 'default_modes', 'supported_modes', 'owners', 'mobile_template'])
app_keys = set(['variables', 'required_variables', 'name', 'context_template', 'summary_template', 'sample_context', 'default_modes', 'supported_modes', 'owners', 'title_variable', 'mobile_template'])
# TODO: insert application data before get
re = requests.get(base_url + 'applications/' + sample_application_name)
assert re.status_code == 200
Expand Down

0 comments on commit d6d01e6

Please sign in to comment.