Skip to content

Commit

Permalink
Avoid unnecessary fetches to speed up.
Browse files Browse the repository at this point in the history
  • Loading branch information
shenshan committed Oct 11, 2019
1 parent d702463 commit 8c46e79
Showing 1 changed file with 60 additions and 43 deletions.
103 changes: 60 additions & 43 deletions database/AnimalDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,25 @@
rightNow.weight = weight(end);
selector = sprintf('administration_date = "%4d-%1d-%2d"', AnimalDatabase.num2date(rightNow.date));
query = action.WaterAdministration() & ['subject_id = "', subj, '"'] & selector;
if ~isempty(query.fetchn('earned'))
rightNow.earned = query.fetchn('earned');
rightNow.supplement = query.fetchn('supplement');
rightNow.received = query.fetchn('received');
[earned, supplement, received] = query.fetchn('earned', 'supplement', 'received');
if ~isempty(earned)
rightNow.earned = earned;
rightNow.supplement = supplement;
rightNow.received = received;
else
rightNow.earned = 0;
rightNow.supplement = 0;
rightNow.received = 0;
end
selector = sprintf('session_date = "%4d-%1d-%2d"', AnimalDatabase.num2date(rightNow.date));
query = acquisition.Session() & ['subject_id = "', subj, '"'] & selector;
if ~isempty(query.fetchn('session_start_time'))
rightNow.trainStart = query.fetchn('session_start_time');
rightNow.trainEnd = query.fetchn('session_end_time');
rightNow.performance = query.fetchn('session_performance');
rightNow.mainMazeID = query.fetchn('level');
[session_start_time, session_end_time, session_performance, level] = ...
query.fetchn('session_start_time', 'session_end_time', 'session_performance', 'level');
if ~isempty(session_start_time)
rightNow.trainStart = session_start_time;
rightNow.trainEnd = session_end_time;
rightNow.performance = session_performance;
rightNow.mainMazeID = level;
else
rightNow.trainStart = NaN;
rightNow.trainEnd = NaN;
Expand All @@ -404,8 +407,9 @@

selector = sprintf('action_date = "%4d-%1d-%2d"', AnimalDatabase.num2date(rightNow.date));
actionItems = action.ActionItem & ['subject_id = "', subj, '"'] & selector;
if ~isempty(actionItems.fetchn('action'))
animals_dj(subj_idx).actItems = actionItems.fetchn('action')';
action = actionItems.fetchn('action');
if ~isempty(action)
animals_dj(subj_idx).actItems = action;
else
animals_dj(subj_idx).actItems = [];
end
Expand Down Expand Up @@ -5306,50 +5310,63 @@ function pollScale(obj, hObject, event, oneShot)

%Then the Technicians
query = lab.User() & 'primary_tech != "N/A"';
obj.Technicians = struct('Name', query.fetchn('full_name'), ...
'ID', query.fetchn('user_id'), ...
'Presence', query.fetchn('presence'), ...
'DayCutoffTime', query.fetchn('day_cutoff_time'), ...
'Phone', query.fetchn('phone'), ...
'Carrier', query.fetchn('carrier'), ...
'Email', query.fetchn('email'), ...
'Slack', query.fetchn('slack'), ...
'ContactVia', query.fetchn('contact_via'), ...
'slackWebhook', query.fetchn('slack_webhook'), ...
'primaryTech', query.fetchn('primary_tech'));
[full_name, user_id, presence, day_cutoff_time, phone, carrier, email, slack, contact_via, slack_webhook, primary_tech] = ...
query.fetchn('full_name', 'user_id', 'presence', 'day_cutoff_time', ...
'phone', 'carrier', 'email', 'slack', 'contact_via', 'slack_webhook', 'primary_tech');
obj.Technicians = struct('Name', full_name, ...
'ID', user_id, ...
'Presence', presence, ...
'DayCutoffTime', day_cutoff_time, ...
'Phone', phone, ...
'Carrier', carrier, ...
'Email', email, ...
'Slack', slack, ...
'ContactVia', contact_via, ...
'slackWebhook', slack_webhook, ...
'primaryTech', primary_tech);
overview_dj.Technicians = obj.Technicians';

%Then the Researchers
query = lab.User() & 'primary_tech = "N/A"';
obj.Researchers = struct('Name', query.fetchn('full_name'), ...
'ID', query.fetchn('user_id'), ...
'Presence', query.fetchn('presence'), ...
'DayCutoffTime', query.fetchn('day_cutoff_time'), ...
'Phone', query.fetchn('phone'), ...
'Carrier', query.fetchn('carrier'), ...
'Email', query.fetchn('email'), ...
'Slack', query.fetchn('slack'), ...
'ContactVia', query.fetchn('contact_via'), ...
'TechResponsibility', query.fetchn('tech_responsibility'), ...
[full_name, user_id, presence, day_cutoff_time, ...
phone, carrier, email, slack, contact_via, ...
tech_responsibility, slack_webhook] = ...
query.fetchn('full_name', 'user_id', 'presence', 'day_cutoff_time', ...
'phone', 'carrier', 'email', 'slack', 'contact_via', ...
'tech_responsibility', 'slack_webhook');
obj.Researchers = struct('Name', full_name, ...
'ID', user_id, ...
'Presence', presence, ...
'DayCutoffTime', day_cutoff_time, ...
'Phone', phone, ...
'Carrier', carrier, ...
'Email', email, ...
'Slack', slack, ...
'ContactVia', contact_via, ...
'TechResponsibility', tech_responsibility, ...
'Protocol', fetchn(lab.UserProtocol & query, 'protocol'), ...
'slackWebhook', query.fetchn('slack_webhook'));
user_ids = query.fetchn('user_id');
for l_idx = 1:length(user_ids)
user_lab = fetch( lab.UserLab & ['user_id = "' user_ids{l_idx} '"'], 'lab');
obj.Researchers(l_idx).PI = fetchn(lab.Lab() & ['lab = "' user_lab.lab '"'], 'pi_name');
obj.Researchers(l_idx).SecondaryContact = fetchn(lab.UserSecondaryContact & ['user_id = "' user_ids{l_idx} '"'], 'secondary_contact');
obj.Researchers(l_idx).animals = obj.getAnimalsDJ(user_ids{l_idx});
'slackWebhook', slack_webhook);

[user_ids, PIs, secondary_contacts] = fetchn(...
query * lab.UserLab * lab.Lab * lab.UserSecondaryContact, ...
'user_id', 'pi_name', 'secondary_contact');
for l_idx = 1:length(user_ids)
obj.Researchers(l_idx).PI = PIs(l_idx);
obj.Researchers(l_idx).SecondaryContact = secondary_contacts(l_idx);
obj.Researchers(l_idx).animals = obj.getAnimalsDJ(user_id{l_idx});
end

overview_dj.Researchers = obj.Researchers';

% Then the NotificationSettings; take only the most up-to-date entry
all_dates = fetchn(lab.NotificationSettings, 'notification_settings_date');
query = lab.NotificationSettings & ['notification_settings_date = "' all_dates{end} '"'];
obj.NotificationSettings.MaxResponseTime = query.fetchn('max_response_time');
obj.NotificationSettings.ChangeCutoffTime = cell2mat(query.fetchn('change_cutoff_time'));
obj.NotificationSettings.WeeklyDigestDay = char(query.fetchn('weekly_digest_day'));
obj.NotificationSettings.WeeklyDigestTime = cell2mat(query.fetchn('weekly_digest_time'));
[max_response_time, change_cutoff_time, weekly_digest_day, weekly_digest_time] = ...
query.fetchn('max_response_time', 'change_cutoff_time', 'weekly_digest_day', 'weekly_digest_time');
obj.NotificationSettings.MaxResponseTime = max_response_time;
obj.NotificationSettings.ChangeCutoffTime = cell2mat(change_cutoff_time);
obj.NotificationSettings.WeeklyDigestDay = char(weekly_digest_day);
obj.NotificationSettings.WeeklyDigestTime = cell2mat(weekly_digest_time);
overview_dj.NotificationSettings = obj.NotificationSettings;

overview = overview_dj;
Expand Down

0 comments on commit 8c46e79

Please sign in to comment.