Skip to content

Commit

Permalink
Added log; fix for 1000 char limit; post weights script
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Oct 30, 2020
1 parent 61cc05b commit c0bb21b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
16 changes: 10 additions & 6 deletions +ww/formatTable.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@
case 'html'
dataInCells(cellfun('isempty', dataInCells)) = {' '};
% Print the headers
out = ['<table style="width:100%"><tr><th style="padding:5px">', ...
strjoin(strip(columnHeaders), '</th><th style="padding:5px">'), '</th></tr>'];
% Print each row
out = sprintf(...
'<table style="width:100%%">\n<tr>\n\t<th style="padding:5px">%s</th>\n</tr>',....
strjoin(strip(columnHeaders), '</th>\n\t<th style="padding:5px">'));
% Print each row -
% NB: Line breaks are essential to avoid 1000 charecter line limit.
for i = 1:size(dataInCells,1)
out = [out, '<tr><td style="padding:5px">', strjoin(dataInCells(i,:), ...
'</td><td style="padding:5px">'), '</td></tr>'];
rowStr = sprintf(...
'\n<tr>\n\t<td style="padding:5px">%s</td>\n</tr>',...
strjoin(dataInCells(i,:), '</td>\n\t<td style="padding:5px">'));
out = [out, rowStr];
end
out = [out, '</table>'];
out = [out, newline, '</table>'];
%%% FOR DEBUGGING %%%
if ww.Params().get('Mode') > 0
save(fullfile(userpath, 'printWeekendWaterVars.mat'))
Expand Down
25 changes: 18 additions & 7 deletions +ww/generate.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ function generate(nDays, varargin)
p.parse(varargin{:})
force = p.Results.force;
test = p.Results.test;
debug = params.get('Mode') > 0 || test;
% Temp dir for saving log and email
tmpdir = iff(ispc, getenv('APPDATA'), getenv('HOME'));
if debug
% In debug mode we activate the log
diaryState = get(0, 'Diary');
diaryFile = get(0, 'DiaryFile');
diary(fullfile(tmpdir, 'ww.log'))
end

% Get list of mice to be trained over the weekend. These will be marked as
% 'PIL' on the list (so long as they've been weighed)
Expand All @@ -40,7 +49,7 @@ function generate(nDays, varargin)

% Path to email file which will be sent
filename = sprintf('mail%s.txt', iff(test, '-test', ''));
mail = fullfile(iff(ispc, getenv('APPDATA'), getenv('HOME')), filename);
mail = fullfile(tmpdir, filename);

% Check when email was last generated and potentially return if too soon
mod = file.modDate(mail);
Expand Down Expand Up @@ -107,14 +116,14 @@ function generate(nDays, varargin)

%% 'Weekend water',...
% Write email to file
fid = fopen(mail, 'w');
fid = fopen(mail, 'w', 'n', 'UTF-8');
fprintf(fid, ['From: Alyx Database <%s>\n',...
'Reply-To: Alyx Database <%s>\n',...
'To: %s\nSubject: Weekend Water\n',...
'Content-Type: text/html; charset="us-ascii"\n',...
'Content-Type: text/html; charset="utf-8"\n',...
'Content-Transfer-Encoding: quoted-printable\n',...
'Mime-version: 1.0\n\n<html><head>',...
'<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii">\n',...
'Mime-version: 1.0\n\n<!DOCTYPE html><html lang="en-GB"><head>',...
'<title>Weekend Water Email</title>'...
'</head><body>\n',...
'Please find below the water table for this weekend. ',...
'Any blank spaces must be filled in manually by the respective ',...
Expand All @@ -128,7 +137,7 @@ function generate(nDays, varargin)
cmd = sprintf(['curl "%s:%i" -v --mail-from "%s" ',...
'--mail-rcpt "%s" --ssl -u %s:%s -T "%s" -k --anyauth'],...
params.get('SMTP_Server'), params.get('SMTP_Port'), params.get('E_mail'), ...
strjoin(to, "' --mail-rcpt '"), getpref('Internet','E_mail'),...
strjoin(to, '" --mail-rcpt "'), getpref('Internet','E_mail'),...
getpref('Internet','SMTP_Password'), strrep(mail, '\', '/'));

% Wrap in call to git bash
Expand All @@ -142,4 +151,6 @@ function generate(nDays, varargin)
% Restore previous preferences
for prop = string(fieldnames(internetPrefs))'
setpref('Internet', prop, internetPrefs.(prop))
end
end
% Restore diary state
if debug, diary(diaryState), set(0, 'DiaryFile', diaryFile), end
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.asv
34 changes: 34 additions & 0 deletions postWeightsToDev.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
% This script posts some weights to the dev database so that when we run
% the weekend water script in test mode the table will get fully populated.
% One of the mice will not have its weight updated.
params = ww.Params;
% Alyx instance
ai = Alyx('','');
ai.BaseURL = params.get('ALYX_DEV_URL');
ai = ai.login(params.get('ALYX_Login'), params.get('ALYX_Password'));
fprintf('Using test database: %s\n', ai.BaseURL);
% Post weights to test db:
wrSubs = ai.getData(ai.makeEndpoint('water-restricted-subjects'));
% Double-check mice aren't dead (should be done Alyx side)
alive = ai.listSubjects;
wrSubs = wrSubs(ismember({wrSubs.nickname}, alive));
n = params.get('nDaysInFuture');

% Go through posting weights for those that need one
for iSubject = 1:length(wrSubs)
if iSubject == 3, continue, end % Skip the third mouse
subject = wrSubs(iSubject).nickname;
endpnt = sprintf('water-requirement/%s?start_date=%s&end_date=%s', ...
subject, datestr(now - 1, 'yyyy-mm-dd'), datestr(now + n, 'yyyy-mm-dd'));
wr = ai.getData(endpnt);
records = catStructs(wr.records, nan);
% no weighings found
if isempty(wr.records) || isnan(records(2).weighing_at)
% If the mouse weight has no weight for yesterday, post one around
% 25g, otherwise use yesterday's weight.
w = iff(isnan(records(1).weighing_at), 24 + rand, records(1).weighing_at);
d = ai.postWeight(w, subject);
assert(contains(d.url, 'dev') && w == d.weight)
end
end

0 comments on commit c0bb21b

Please sign in to comment.