2020-11-04 - Sort Result - Functions
Jump to navigation
Jump to search
Lambda Functions
initSortResult
/**
* Initial request that checks if a matching, not expired, sort result record exists, creates a new record if not, returns sortResultId
* @param {string} event.searchDataId - {searchType}_{filterId}_{keyPropertiesHash}_{requiredDataHash}
* @param {Object[]} event.sortFields - sort fields
* @param {string} [event.sortFields[].dataField] - name of the field being sorted, not used if randomSeed is set
* @param {string} [event.sortFields[].randomSeed] - set when wanting random order only
*
* @returns {string} sortResultId
*/
logic
- Create sortResultId by invoking 2020-11-04 - Sort Result - Functions:CreateSortResultId
- Query SortResultMain table
- If not have existing record
- save record into SortResultMain table
- status = new_sortresult_created
- if error when creating new record (eg another process already created the record), retry query SortResultMain table and check again
- If have existing record, expired, and status complete
- update SortResultMain record
- status = waiting_for_searchresults
- conditional update, only if status still complete, if not (eg another process already updated the record), retry query SortResultMain table and check again
- return sortResultId and status
processSortResult
/**
* Pulls data from Search Result service and saves into SortResultData table
* @param {string} event.searchDataId - {searchType}_{filterId}_{keyPropertiesHash}_{requiredDataHash}
* @param {Object[]} event.sortFields - sort fields
*
*/
logic
- moving to new logic that saves directly into SortedData from SearchResultData
- Create sortResultId by invoking 2020-11-04 - Sort Result - Functions:CreateSortResultId
- Add a retry loop for the below to allow for retrying after conditional statement does not pass
- Query SortResultMain table
- If status is waiting_for_searchresults
- Do a conditional update of SortResultMain record checking status = waiting_for_searchresults, setting status to processing
- If conditional statement does not pass retry by "continue"'ing back into retry loop
- If conditional statement passes remove all old records from SortResultData
- If status is new_sortresult_created
- Do a conditional update of SortResultMain record checking status = new_sortresult_created, setting status to processing
- If conditional statement does not pass retry by "continue"'ing back into retry loop
- If status is processing | error | complete, return
- .. want to pull data from SearchResultData table, insert into SortResultData table but sorted according to SortFields -- try to work out most effective method for large sets of data
- Need to copy across all data (eg reqData values)
- * It might be possible to batch insert Data records into Dynamo to save on resources
- ..
- this could be put in a separate Lambda if processing takes too long
- ..
- iterate through all StringData/NumericData, inserting records into SortedData table according to their sorted location (increment sortId per record) - put in separate function so easy to split into separate Lambda later
- .. eg:
- recursive function that receives one lower level result and queries StringData/NumericData (depending on sortType) for all matching records (records will be sorted in correct order by sortValue/sort key)
- for each record found:
- if is not the final level, invoke recursive function for that value
- else:
- save each value into SortedData
- increment sortId
- return new sortId so next records increment correctly
- ..
- Conditional update of SortResultMain record checking status = processing
- status = complete (or error if failed for some reason)
- expiry = current timestamp + expiryTimeInSec from Config table
- If conditional statement does not pass should have an error, should not happen
getSortResults
/**
* Request a page of data
* @param {string} sortResultId
* @param {numeric} startId - first sortId to return
* @param {numeric} limit - number of records to return
* @param {boolean} event.reverse - true|false, default false, reverse the sort results
*
*/
handler
- triggered by API gateway request
logic
- query SortedData table for a set of data, starting from startId, with limit number of records returned
Functions
CreateSortResultId
/**
* Creates sortResultId from searchDataId and sortFields
* @param {string} searchDataId
* @param {Object[]} sortFields
*
* @returns {string} sortResultId
*/
module.exports.CreateSortResultId = (searchDataId, sortFields) => {
logic
- conatenate searchDataId + "_" + hash(sortFields)