Commit 67048cd2 authored by Pithon Kabiro's avatar Pithon Kabiro
Browse files

New function: fetch metadata and observations

... from multiple datastreams. Its single parameter is an array of
strings representing the building, sensor and sampling rate.
parent eba0a6df
Showing with 107 additions and 113 deletions
+107 -113
...@@ -182,6 +182,7 @@ const getMetadataFromSingleDatastream = async function (urlDatastream) { ...@@ -182,6 +182,7 @@ const getMetadataFromSingleDatastream = async function (urlDatastream) {
/** /**
* Retrieve metadata from multiple datastreams * Retrieve metadata from multiple datastreams
* @async
* @param {Array} datastreamsUrlArr An array that contains N Datastream URL strings * @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 * @returns {Promise} A promise that contains an array of N Datastream metadata objects when fulfilled
*/ */
...@@ -193,7 +194,9 @@ const getMetadataFromMultipleDatastreams = async function (datastreamsUrlArr) { ...@@ -193,7 +194,9 @@ const getMetadataFromMultipleDatastreams = async function (datastreamsUrlArr) {
// 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 datastreamUrl of datastreamsUrlArr) { for (const datastreamUrl of datastreamsUrlArr) {
// Metadata from a single Datastream // Metadata from a single Datastream
const datastreamMetadata = await getDatastreamMetadata(datastreamUrl); const datastreamMetadata = await getMetadataFromSingleDatastream(
datastreamUrl
);
datastreamMetadataArr.push(datastreamMetadata); datastreamMetadataArr.push(datastreamMetadata);
} }
...@@ -525,7 +528,7 @@ const getCombinedObservationsFromAllNextLinks = function ( ...@@ -525,7 +528,7 @@ const getCombinedObservationsFromAllNextLinks = function (
* @param {Promise} metadataPlusObsPromiseArray An array that contains two promises, one for datastream metadata, the other for observations * @param {Promise} metadataPlusObsPromiseArray An array that contains two promises, one for datastream metadata, the other for observations
* @returns {Promise} A promise that contains two arrays when fulfilled, one for datastream metadata and the other for observations * @returns {Promise} A promise that contains two arrays when fulfilled, one for datastream metadata and the other for observations
*/ */
const getMetadataPlusObservationsForChart = async function ( const getMetadataPlusObservationsFromSingleDatastream = async function (
metadataPlusObsPromiseArray metadataPlusObsPromiseArray
) { ) {
try { try {
...@@ -571,57 +574,93 @@ const getObservationsFromMultipleDatastreams = async function ( ...@@ -571,57 +574,93 @@ const getObservationsFromMultipleDatastreams = async function (
} }
}; };
// Building + phenomenon + sampling rate /**
const buildingsSensorSamplingRateRLArr = [ * Retrieve the metadata from multiple Datastreams and the Observations corresponding to these Datastreams
["101", "rl", "60min"], * @async
["102", "rl", "60min"], * @param {Array} bldgSensorSamplingRateArr A 3*N array containing buildings, sensors & sampling rates as strings, e.g. ["101", "rl", "60min"]
["107", "rl", "60min"], * @returns {Promise} A promise that contains a N*2 array (the first element is an array of Observations and the second element is an array of Datastream metadata objects) when fulfilled
["112, 118", "rl", "60min"], */
["125", "rl", "60min"], const getMetadataPlusObservationsFromMultipleDatastreams = async function (
["225", "rl", "60min"], bldgSensorSamplingRateArr
]; ) {
try {
// Datastreams IDs if (!bldgSensorSamplingRateArr) return;
const datastreamsRLArr = buildingsSensorSamplingRateRLArr.map((bldg) =>
getDatastreamIdFromBuildingNumber(...bldg)
);
// Datastreams URLs // Datastreams IDs
const datastreamsUrlRLArr = datastreamsRLArr.map((datastreamId) => const datastreamsIdsArr = bldgSensorSamplingRateArr.map(
getObservationsUrl(BASE_URL, datastreamId) (bldgSensorSamplingRate) =>
); getDatastreamIdFromBuildingNumber(...bldgSensorSamplingRate)
);
// Promise objects - Observations / RL // Observations URLs
const observationsPromisesRLArr = datastreamsUrlRLArr.map((obsUrl) => const observationsUrlArr = datastreamsIdsArr.map((datastreamId) =>
getCombinedObservationsFromAllNextLinks( getObservationsUrl(BASE_URL, datastreamId)
axiosGetRequest(obsUrl, QUERY_PARAMS_COMBINED) );
)
); // Datastreams URLs
const datastreamsUrlArr = datastreamsIdsArr.map((datastreamId) =>
getDatastreamUrl(BASE_URL, datastreamId)
);
// Promise objects - Observations
const observationsPromisesArr = observationsUrlArr.map((obsUrl) =>
getCombinedObservationsFromAllNextLinks(
axiosGetRequest(obsUrl, QUERY_PARAMS_COMBINED)
)
);
// Observations array
const observationsArr = await getObservationsFromMultipleDatastreams(
observationsPromisesArr
);
// Metadata array
const metadataArr = await getMetadataFromMultipleDatastreams(
datastreamsUrlArr
);
// getObservationsFromMultipleDatastreams(observationsPromisesRLArr).then((x) => return [observationsArr, metadataArr];
// console.log(x) } catch (err) {
// ); console.error(err);
}
};
const drawScatterPlotHC = function ( const drawScatterPlotHC = function (
formattedObsArrayForSeriesOne, formattedObsArrayForSeriesOne,
formattedDatastreamMetadataSeriesOne = "", formattedDatastreamMetadataSeriesOne,
formattedObsArrayForSeriesTwo, formattedObsArrayForSeriesTwo,
formattedDatastreamMetadataSeriesTwo = "" formattedDatastreamMetadataSeriesTwo
) { ) {
const CHART_TITLE = "Height Versus Weight of 507 Individuals by Gender"; const {
const CHART_SUBTITLE = "Source: Heinz 2003"; datastreamDescription: DATASTREAM_DESCRIPTION_SERIES_1,
datastreamName: DATASTREAM_NAME_SERIES_1,
phenomenonName: PHENOMENON_NAME_SERIES_1,
unitOfMeasurementSymbol: PHENOMENON_SYMBOL_SERIES_1,
} = formattedDatastreamMetadataSeriesOne;
const X_AXIS_TITLE = "Height (cm)"; const {
datastreamDescription: DATASTREAM_DESCRIPTION_SERIES_2,
datastreamName: DATASTREAM_NAME_SERIES_2,
phenomenonName: PHENOMENON_NAME_SERIES_2,
unitOfMeasurementSymbol: PHENOMENON_SYMBOL_SERIES_2,
} = formattedDatastreamMetadataSeriesTwo;
const CHART_TITLE = `${PHENOMENON_NAME_SERIES_1} Versus ${PHENOMENON_NAME_SERIES_2}`;
const CHART_SUBTITLE = `Source: ${DATASTREAM_NAME_SERIES_1} & ${DATASTREAM_NAME_SERIES_2}`;
const X_AXIS_TITLE = `Date / Time`;
const SERIES_1_NAME = "Rücklauftemp"; const SERIES_1_NAME = `${PHENOMENON_NAME_SERIES_1}`;
const SERIES_1_SYMBOL_COLOR = "rgba(119, 152, 191, .5)"; const SERIES_1_SYMBOL_COLOR_RGB_ELEMENTS = "119, 152, 191";
const SERIES_1_TEXT_COLOR = "rgb(119, 152, 191)"; // remove transparency from symbol color for a more "intense" color const SERIES_1_SYMBOL_COLOR = `rgba(${SERIES_1_SYMBOL_COLOR_RGB_ELEMENTS}, .5)`;
const SERIES_1_SYMBOL = "°C"; const SERIES_1_TEXT_COLOR = `rgb(${SERIES_1_SYMBOL_COLOR_RGB_ELEMENTS})`; // remove transparency from symbol color for a more "intense" color
const SERIES_1_SYMBOL = `${PHENOMENON_SYMBOL_SERIES_1}`;
const SERIES_2_NAME = "Power"; const SERIES_2_NAME = `${PHENOMENON_NAME_SERIES_2}`;
const SERIES_2_SYMBOL_COLOR = "rgba(223, 83, 83, .5)"; const SERIES_2_SYMBOL_COLOR_RGB_ELEMENTS = "223, 83, 83";
const SERIES_2_TEXT_COLOR = "rgb(223, 83, 83)"; // remove transparency from symbol color for a more "intense" color const SERIES_2_SYMBOL_COLOR = `rgba(${SERIES_2_SYMBOL_COLOR_RGB_ELEMENTS}, .5)`;
const SERIES_2_SYMBOL = "kW"; const SERIES_2_TEXT_COLOR = `rgb(${SERIES_2_SYMBOL_COLOR_RGB_ELEMENTS})`; // remove transparency from symbol color for a more "intense" color
const SERIES_2_SYMBOL = `${PHENOMENON_SYMBOL_SERIES_2}`;
Highcharts.chart("chart-scatter-plot", { Highcharts.chart("chart-scatter-plot", {
chart: { chart: {
...@@ -744,75 +783,29 @@ const drawScatterPlotHC = function ( ...@@ -744,75 +783,29 @@ const drawScatterPlotHC = function (
}; };
(async () => { (async () => {
const DATASTREAM_ID_SERIES_1 = getDatastreamIdFromBuildingNumber( // Input array - building, sensor, samplingRate
"weather_station_521", const sensorsOfInterestArr = [
"outside_temp", ["weather_station_521", "outside_temp", "60min"],
"60min" ["225", "vl", "60min"],
); ];
const URL_DATASTREAM_SERIES_1 = getDatastreamUrl(
BASE_URL, const observationsPlusMetadata =
DATASTREAM_ID_SERIES_1 await getMetadataPlusObservationsFromMultipleDatastreams(
); sensorsOfInterestArr
const URL_OBSERVATIONS_SERIES_1 = getObservationsUrl(
BASE_URL,
DATASTREAM_ID_SERIES_1
);
const DATASTREAM_ID_SERIES_2 = getDatastreamIdFromBuildingNumber(
"225",
"vl",
"60min"
);
const URL_DATASTREAM_SERIES_2 = getDatastreamUrl(
BASE_URL,
DATASTREAM_ID_SERIES_2
);
const URL_OBSERVATIONS_SERIES_2 = getObservationsUrl(
BASE_URL,
DATASTREAM_ID_SERIES_2
);
// Create promises
const promiseDatastreamMetadataSeries1 = getMetadataFromSingleDatastream(
URL_DATASTREAM_SERIES_1
);
const promiseCombinedObservationsSeries1 =
getCombinedObservationsFromAllNextLinks(
axiosGetRequest(URL_OBSERVATIONS_SERIES_1, QUERY_PARAMS_COMBINED)
);
const promiseDatastreamMetadataSeries2 = getMetadataFromSingleDatastream(
URL_DATASTREAM_SERIES_2
);
const promiseCombinedObservationsSeries2 =
getCombinedObservationsFromAllNextLinks(
axiosGetRequest(URL_OBSERVATIONS_SERIES_2, QUERY_PARAMS_COMBINED)
); );
// Pass promises to our async function // Extract the observations and metadata for each sensor
const metadataPlusObservationsSeries1 = // Array elements in same order as input array
await getMetadataPlusObservationsForChart([ const [
promiseCombinedObservationsSeries1, [obsSensorOneArr, obsSensorTwoArr],
promiseDatastreamMetadataSeries1, [metadataSensorOne, metadataSensorTwo],
]); ] = observationsPlusMetadata;
const metadataPlusObservationsSeries2 =
await getMetadataPlusObservationsForChart([
promiseCombinedObservationsSeries2,
promiseDatastreamMetadataSeries2,
]);
// Extract the metadata and the observations from resulting arrays
const combinedObsSeries1 = metadataPlusObservationsSeries1[0];
const datastreamMetadataSeries1 = metadataPlusObservationsSeries1[1];
const combinedObsSeries2 = metadataPlusObservationsSeries2[0];
const datastreamMetadataSeries2 = metadataPlusObservationsSeries2[1];
drawScatterPlotHC( drawScatterPlotHC(
formatSTAResponseForLineChartOrScatterPlot(combinedObsSeries1), formatSTAResponseForLineChartOrScatterPlot(obsSensorOneArr),
formatDatastreamMetadataForChart(datastreamMetadataSeries1), formatDatastreamMetadataForChart(metadataSensorOne),
formatSTAResponseForLineChartOrScatterPlot(combinedObsSeries2), formatSTAResponseForLineChartOrScatterPlot(obsSensorTwoArr),
formatDatastreamMetadataForChart(datastreamMetadataSeries2) formatDatastreamMetadataForChart(metadataSensorTwo)
); );
})(); })();
...@@ -831,5 +824,5 @@ export { ...@@ -831,5 +824,5 @@ export {
formatSTAResponseForLineChartOrScatterPlot, formatSTAResponseForLineChartOrScatterPlot,
drawLineChartHC, drawLineChartHC,
getCombinedObservationsFromAllNextLinks, getCombinedObservationsFromAllNextLinks,
getMetadataPlusObservationsForChart, getMetadataPlusObservationsFromSingleDatastream,
}; };
...@@ -14,7 +14,7 @@ import { ...@@ -14,7 +14,7 @@ import {
formatSTAResponseForLineChartOrScatterPlot, formatSTAResponseForLineChartOrScatterPlot,
drawLineChartHC, drawLineChartHC,
getCombinedObservationsFromAllNextLinks, getCombinedObservationsFromAllNextLinks,
getMetadataPlusObservationsForChart, getMetadataPlusObservationsFromSingleDatastream,
} from "./appChart.js"; } from "./appChart.js";
const buildingsAvailableSensorsArr = [ const buildingsAvailableSensorsArr = [
...@@ -296,10 +296,11 @@ const selectChartTypeFromDropDown = async function () { ...@@ -296,10 +296,11 @@ const selectChartTypeFromDropDown = async function () {
); );
// Pass promises to our async function // Pass promises to our async function
const metadataPlusObservations = await getMetadataPlusObservationsForChart([ const metadataPlusObservations =
promiseCombinedObservations, await getMetadataPlusObservationsFromSingleDatastream([
promiseDatastreamMetadata, promiseCombinedObservations,
]); promiseDatastreamMetadata,
]);
// Extract the metadata and the observations from resulting array // Extract the metadata and the observations from resulting array
const combinedObs = metadataPlusObservations[0]; const combinedObs = metadataPlusObservations[0];
......
Supports Markdown
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