Commit eba0a6df authored by Pithon Kabiro's avatar Pithon Kabiro
Browse files

New function: metadata from multiple datastreams

Retrieves metadata from an array of Datastream URL strings
parent 0c220981
...@@ -167,7 +167,7 @@ const axiosGetRequest = function (urlObservations, urlParamObj) { ...@@ -167,7 +167,7 @@ const axiosGetRequest = function (urlObservations, urlParamObj) {
* @param {String} urlDatastream A URL that fetches a Datastream from an STA instance * @param {String} urlDatastream A URL that fetches a Datastream from an STA instance
* @returns {Promise} A promise that contains a metadata object for a Datastream when fulfilled * @returns {Promise} A promise that contains a metadata object for a Datastream when fulfilled
*/ */
const getDatastreamMetadata = async function (urlDatastream) { const getMetadataFromSingleDatastream = async function (urlDatastream) {
try { try {
// Extract properties of interest // Extract properties of interest
const { const {
...@@ -180,6 +180,29 @@ const getDatastreamMetadata = async function (urlDatastream) { ...@@ -180,6 +180,29 @@ const getDatastreamMetadata = async function (urlDatastream) {
} }
}; };
/**
* Retrieve metadata from multiple datastreams
* @param {Array} datastreamsUrlArr An array that contains N Datastream URL strings
* @returns {Promise} A promise that contains an array of N Datastream metadata objects when fulfilled
*/
const getMetadataFromMultipleDatastreams = async function (datastreamsUrlArr) {
try {
// Array to store our final result
const datastreamMetadataArr = [];
// Use for/of loop - we need to maintain the order of execution of the async operations
for (const datastreamUrl of datastreamsUrlArr) {
// Metadata from a single Datastream
const datastreamMetadata = await getDatastreamMetadata(datastreamUrl);
datastreamMetadataArr.push(datastreamMetadata);
}
return datastreamMetadataArr;
} catch (err) {
console.error(err);
}
};
/** /**
* Format the response containing a Datastream's metadata from Sensorthings API * Format the response containing a Datastream's metadata from Sensorthings API
* @param {Object} datastreamMetadata An object containing a Datastream's metadata * @param {Object} datastreamMetadata An object containing a Datastream's metadata
...@@ -505,20 +528,21 @@ const getCombinedObservationsFromAllNextLinks = function ( ...@@ -505,20 +528,21 @@ const getCombinedObservationsFromAllNextLinks = function (
const getMetadataPlusObservationsForChart = async function ( const getMetadataPlusObservationsForChart = async function (
metadataPlusObsPromiseArray metadataPlusObsPromiseArray
) { ) {
// Array to store our final result try {
const combinedResolvedPromises = []; // Array to store our final result
const combinedResolvedPromises = [];
// Use for/of loop - we need to maintain the order of execution of the async operations // Use for/of loop - we need to maintain the order of execution of the async operations
for (const promise of metadataPlusObsPromiseArray) { for (const promise of metadataPlusObsPromiseArray) {
try {
// Resolved value of a single promise // Resolved value of a single promise
const resolvedPromise = await promise; const resolvedPromise = await promise;
combinedResolvedPromises.push(resolvedPromise); combinedResolvedPromises.push(resolvedPromise);
} catch (err) {
console.error(err);
} }
return combinedResolvedPromises;
} catch (err) {
console.error(err);
} }
return combinedResolvedPromises;
}; };
/** /**
...@@ -530,20 +554,21 @@ const getMetadataPlusObservationsForChart = async function ( ...@@ -530,20 +554,21 @@ const getMetadataPlusObservationsForChart = async function (
const getObservationsFromMultipleDatastreams = async function ( const getObservationsFromMultipleDatastreams = async function (
observationPromiseArray observationPromiseArray
) { ) {
// Array to store our final result try {
const observationsAllDatastreamsArr = []; // Array to store our final result
const observationsAllDatastreamsArr = [];
// Use for/of loop - we need to maintain the order of execution of the async operations // Use for/of loop - we need to maintain the order of execution of the async operations
for (const observationPromise of observationPromiseArray) { for (const observationPromise of observationPromiseArray) {
try {
// Observations from a single Datastream // Observations from a single Datastream
const observations = await observationPromise; const observations = await observationPromise;
observationsAllDatastreamsArr.push(observations); observationsAllDatastreamsArr.push(observations);
} catch (err) {
console.error(err);
} }
return observationsAllDatastreamsArr;
} catch (err) {
console.error(err);
} }
return observationsAllDatastreamsArr;
}; };
// Building + phenomenon + sampling rate // Building + phenomenon + sampling rate
...@@ -748,7 +773,7 @@ const drawScatterPlotHC = function ( ...@@ -748,7 +773,7 @@ const drawScatterPlotHC = function (
); );
// Create promises // Create promises
const promiseDatastreamMetadataSeries1 = getDatastreamMetadata( const promiseDatastreamMetadataSeries1 = getMetadataFromSingleDatastream(
URL_DATASTREAM_SERIES_1 URL_DATASTREAM_SERIES_1
); );
const promiseCombinedObservationsSeries1 = const promiseCombinedObservationsSeries1 =
...@@ -756,7 +781,7 @@ const drawScatterPlotHC = function ( ...@@ -756,7 +781,7 @@ const drawScatterPlotHC = function (
axiosGetRequest(URL_OBSERVATIONS_SERIES_1, QUERY_PARAMS_COMBINED) axiosGetRequest(URL_OBSERVATIONS_SERIES_1, QUERY_PARAMS_COMBINED)
); );
const promiseDatastreamMetadataSeries2 = getDatastreamMetadata( const promiseDatastreamMetadataSeries2 = getMetadataFromSingleDatastream(
URL_DATASTREAM_SERIES_2 URL_DATASTREAM_SERIES_2
); );
const promiseCombinedObservationsSeries2 = const promiseCombinedObservationsSeries2 =
...@@ -799,7 +824,7 @@ export { ...@@ -799,7 +824,7 @@ export {
getObservationsUrl, getObservationsUrl,
createTemporalFilterString, createTemporalFilterString,
axiosGetRequest, axiosGetRequest,
getDatastreamMetadata, getMetadataFromSingleDatastream,
formatDatastreamMetadataForChart, formatDatastreamMetadataForChart,
formatSTAResponseForHeatMap, formatSTAResponseForHeatMap,
drawHeatMapHC, drawHeatMapHC,
......
...@@ -7,7 +7,7 @@ import { ...@@ -7,7 +7,7 @@ import {
getDatastreamUrl, getDatastreamUrl,
getObservationsUrl, getObservationsUrl,
axiosGetRequest, axiosGetRequest,
getDatastreamMetadata, getMetadataFromSingleDatastream,
formatDatastreamMetadataForChart, formatDatastreamMetadataForChart,
formatSTAResponseForHeatMap, formatSTAResponseForHeatMap,
drawHeatMapHC, drawHeatMapHC,
...@@ -289,7 +289,8 @@ const selectChartTypeFromDropDown = async function () { ...@@ -289,7 +289,8 @@ const selectChartTypeFromDropDown = async function () {
const URL_OBSERVATIONS = getObservationsUrl(BASE_URL, selectedDatastream); const URL_OBSERVATIONS = getObservationsUrl(BASE_URL, selectedDatastream);
// Create promises // Create promises
const promiseDatastreamMetadata = getDatastreamMetadata(URL_DATASTREAM); const promiseDatastreamMetadata =
getMetadataFromSingleDatastream(URL_DATASTREAM);
const promiseCombinedObservations = getCombinedObservationsFromAllNextLinks( const promiseCombinedObservations = getCombinedObservationsFromAllNextLinks(
axiosGetRequest(URL_OBSERVATIONS, QUERY_PARAMS_COMBINED) axiosGetRequest(URL_OBSERVATIONS, QUERY_PARAMS_COMBINED)
); );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment