Skip to content

Commit

Permalink
Fix javascript errors and explicitly drop temp table to avoid worker …
Browse files Browse the repository at this point in the history
…pool problems (linkedin#201)

* fix javascriot errors and only copy events that could be used

* explicitly drop table

* fix not displaying all types of roles in populate bug
  • Loading branch information
diegocepedaw authored and dwang159 committed Aug 8, 2018
1 parent c07bc8f commit ff530d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
33 changes: 24 additions & 9 deletions src/oncall/api/v0/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
from schedules import get_schedules
from falcon import HTTPNotFound
from oncall.bin.scheduler import load_scheduler
import operator


def on_get(req, resp, schedule_id):
"""
Run the scheduler on demand from a given point in time. Unlike populate it doen't permanently delete or insert anything.
"""
start_time = float(req.get_param('start', required=True))
start__lt = req.get_param('start__lt', required=True)
end__ge = req.get_param('end__ge', required=True)
team__eq = req.get_param('team__eq', required=True)
last_end = 0
table_name = 'temp_event'

connection = db.connect()
Expand All @@ -25,27 +30,37 @@ def on_get(req, resp, schedule_id):
scheduler_name = cursor.fetchone()['name']
scheduler = load_scheduler(scheduler_name)
schedule = get_schedules({'id': schedule_id})[0]
team_id = schedule['team_id']

start__lt = req.get_param('start__lt', required=True)
end__ge = req.get_param('end__ge', required=True)
team__eq = req.get_param('team__eq', required=True)
roster_id = schedule['roster_id']
# get earliest relevant end time
query = '''
SELECT `user_id`, MAX(`end`) AS `last_end` FROM `event`
WHERE (`team_id` = %s OR `team_id` IN (SELECT `subscription_id` FROM team_subscription WHERE `team_id` = %s)) AND `end` <= %s
GROUP BY `user_id`
'''

cursor.execute(query, (team_id, team_id, start_time))
if cursor.rowcount != 0:
last_end = min(cursor.fetchall(), key=operator.itemgetter('last_end'))['last_end']

# create a temporary table with the events that include members of the team's roster
# create a temporary table with the events that include members of the team's rosters and subscriptions
query = '''
CREATE TEMPORARY TABLE IF NOT EXISTS `temp_event` AS
(SELECT `event`.`id`, `event`.`team_id`, `event`.`role_id`,
CREATE TEMPORARY TABLE `temp_event` AS
(SELECT DISTINCT `event`.`id`, `event`.`team_id`, `event`.`role_id`,
`event`.`schedule_id`, `event`.`link_id`, `event`.`user_id`,
`event`.`start`, `event`.`end`, `event`.`note`
FROM `event`
INNER JOIN `roster_user`
ON `event`.`user_id`=`roster_user`.`user_id`
WHERE `roster_user`.`roster_id`=%s)
WHERE `roster_user`.`roster_id` IN
(SELECT `id` FROM `roster` WHERE (`team_id` = %s OR `team_id` IN (SELECT `subscription_id` FROM team_subscription WHERE `team_id` = %s)))
AND `event`.`end` >= %s)
'''

cursor.execute(query, roster_id)
cursor.execute(query, (team_id, team_id, last_end))

scheduler.populate(schedule, start_time, (connection, cursor), table_name)
resp.body = scheduler.build_preview_response(cursor, start__lt, end__ge, team__eq, table_name)
cursor.execute("DROP TEMPORARY TABLE `temp_event`")
cursor.close()
connection.close()
15 changes: 4 additions & 11 deletions src/oncall/ui/static/js/oncall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ var oncall = {
scheduleId = scheduleData.id;
threshold = scheduleData.auto_populate_threshold;

$calContainer.incalendar({
$modal.find('#modal-calendar-container').data('incalendar', null).incalendar({
eventsUrl: '/api/v0/events',
getEventsUrl: '/api/v0/events?team__eq=' + self.data.teamName,
readOnly: true,
Expand All @@ -2210,17 +2210,8 @@ var oncall = {
$modal.attr('data-schedule-id', scheduleId);
$modalThreshold.text(threshold + ' Days');
$modalDate.val(now.format('YYYY/MM/DD'));
}).on('hidden.bs.modal', function(){
}).on('hidden.bs.modal', function(e){
$(this).find('.alert').remove();
$modal.find('#modal-calendar-container').data('incalendar', null).incalendar({
eventsUrl: '/api/v0/schedules/'+ scheduleId+'/preview',
getEventsUrl: '/api/v0/schedules/'+ scheduleId+'/preview?team__eq=' + self.data.teamName + '&start=' + date + '&teamName=' + self.data.teamName,
readOnly: true,
onEventGet: function(events, $cal){
$cal.find('[data-schedule-id="' + scheduleId + '"]').attr('data-highlighted', true);
$cta.removeClass('loading disabled').prop('disabled', false);
}
});
});
$modalBtn.on('click', function(){
var date = new Date($modalDate.val());
Expand Down Expand Up @@ -2266,6 +2257,7 @@ var oncall = {
eventsUrl: '/api/v0/events',
getEventsUrl: '/api/v0/events?team__eq=' + self.data.teamName,
readOnly: true,
persistSettings: false,
onEventGet: function(events, $cal){
$cal.find('[data-schedule-id="' + scheduleId + '"]').attr('data-highlighted', true);
}
Expand All @@ -2290,6 +2282,7 @@ var oncall = {
eventsUrl: '/api/v0/schedules/'+ scheduleId+'/preview',
getEventsUrl: '/api/v0/schedules/'+ scheduleId+'/preview?team__eq=' + self.data.teamName + '&start=' + date + '&teamName=' + self.data.teamName,
readOnly: true,
persistSettings: false,
onEventGet: function(events, $cal){
$cal.find('[data-schedule-id="' + scheduleId + '"]').attr('data-highlighted', true);
},
Expand Down
2 changes: 1 addition & 1 deletion src/oncall/ui/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ <h4 class="modal-title">Populate Schedule</h4>
<div>
<label class="light"> Populate start time: </label><input type="text" id="populate-schedule-date" class="form-control" placeholder="YYYY/MM/DD">
<button type="button" class="btn btn-primary" id="populate-schedule-btn"> <span class="btn-text">Populate</span> <i class="loader loader-small"></i> </button>
<button type="button" class="btn btn-blue" id="preview-schedule-btn" style="display: none;" disabled> <span class="btn-text">Preview</span> <i class="loader loader-small"></i> </button>
<button type="button" class="btn btn-blue" id="preview-schedule-btn"> <span class="btn-text">Preview</span> <i class="loader loader-small"></i> </button>
</div>
</div>
</div>
Expand Down

0 comments on commit ff530d0

Please sign in to comment.