Skip to content

Commit

Permalink
MAINT: add get_active_samples(); abstract code
Browse files Browse the repository at this point in the history
get_active_samples() is based on autocomplete_search_samples(), so
I abstracted some of the code for processing responses into its own
function that can be shared.

This seems to work alright! Would really like to test this with
multiple studies, though (biocore#437).

For future reference: you can get the value of a promise returned by
get_active_samples() as follows:

get_active_samples().then(
    function(v) {
        // Now, v is the array of active sample IDs
        console.log(v);
    }
);

This is 1/3 of the work towards multi-select (just gonna tag biocore#520).

What remains are the following:
2. On a paste operation when the multi-select checkbox is checked,
   identify all "new" specimen IDs. Get an array or something of
   these, preferably. (Or we can do this on a
   specimen-ID-by-specimen-ID basis, doesn't really matter.)
3. For each new specimen ID, attempt to match it with the active
   samples. Color cells / update values accordingly based on the
   following cases:
   *   0 matches (color cell red)
   *   1 match (leave cell uncolored, and change text to matching ID)
   * >=2 matches (color cell yellow)

And ...I think that should be it, knock on wood.
  • Loading branch information
fedarko committed Aug 22, 2019
1 parent be1233f commit e00fcd0
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions labcontrol/gui/static/js/plateViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,9 @@ function autocomplete_search_samples(request, response) {
// Perform all the requests to the server
var requests = [$.get("/sample/control?term=" + request.term)];
$.each(studyIds, function(index, value) {
requests.push($.get("/study/" + value + "/samples?limit=20&term=" + request.term));
requests.push(
$.get("/study/" + value + "/samples?limit=20&term=" + request.term)
);
});

$.when.apply($, requests).then(function() {
Expand All @@ -754,10 +756,7 @@ function autocomplete_search_samples(request, response) {
// of that request. On the other hand, if there was more than one request,
// then arguments is a list of results
var arg = requests.length === 1 ? [arguments] : arguments;
var samples = [];
$.each(arg, function(index, value) {
samples = samples.concat($.parseJSON(value[0]));
});
var samples = merge_sample_responses(arg);
// Format the samples in the way that autocomplete needs
var results = [];
$.each(samples, function(index, value) {
Expand All @@ -767,6 +766,32 @@ function autocomplete_search_samples(request, response) {
});
}

/**
* Given an array of responses of sample IDs (where each element in the array
* is a string representation of an array of sample IDs), returns a single
* array of sample IDs.
*
* This function was created in order to store redundant code between
* autocomplete_search_samples() (where this code was taken from) and
* get_active_samples().
*
* NOTE that this assumes that the sample IDs in the response array are all
* unique. If for whatever reason a sample ID is repeated in a study -- or
* multiple studies share a sample ID -- then this function won't have a
* problem with that, and will accordingly return an array containing duplicate
* sample IDs. (That should never happen, though.)
*
* @param {Array} responseArray: an array of request(s') output.
* @returns {Array} A list of the sample IDs contained within responseArray.
*/
function merge_sample_responses(responseArray) {
var samples = [];
$.each(responseArray, function(index, value) {
samples = samples.concat($.parseJSON(value[0]));
});
return samples;
}

/**
* Function to retrieve the selected studies from the UI
* @returns {Array} A list of study identifiers that are currently selected.
Expand All @@ -789,6 +814,44 @@ function get_active_studies() {
return studyIds;
}

/**
* Function to retrieve every sample ID associated with every active study.
*
* A lot of this code was based on autocomplete_search_samples() -- however,
* not using a sample count limit or taking into account control types means
* that this function is inherently simpler.
*
* This returns a Promise because this function does a few asynchronous calls
* under the hood (in particular, it issues one request per active study). In
* order to be consistent, a Promise is returned even if no studies are active
* (in this case the Promise will just resolve to []).
*
* @returns {Promise} A Promise that resolves to a list of sample IDs from all
* active studies.
*/
function get_active_samples() {
var studyIDs = get_active_studies();
if (studyIDs.length > 0) {
// Create an array of all the requests to be made (one request per study)
var requests = [];
$.each(studyIDs, function(index, value) {
requests.push($.get("/study/" + value + "/samples"));
});
// Wait for each request to be handled, then merge responses to get a list of
// sample IDs from all active studies
// We're going to return a Promise, and this Promise's value will be the
// array of all sample IDs from all active studies.
return $.when.apply($, requests).then(function() {
var arg = requests.length === 1 ? [arguments] : arguments;
return merge_sample_responses(arg);
});
} else {
return new Promise(function(resolve, reject) {
resolve([]);
});
}
}

/**
* Small widget to add notes and save them to a URI
*
Expand Down

0 comments on commit e00fcd0

Please sign in to comment.