dropDownList.js 10.4 KB
Newer Older
1
2
"use strict";

3
4
5
6
import {
  BASE_URL,
  QUERY_PARAMS_COMBINED,
  getDatastreamIdFromBuildingNumber,
7
8
9
  createDatastreamUrl,
  createObservationsUrl,
  performGetRequestUsingAxios,
10
  getMetadataFromSingleDatastream,
11
  formatDatastreamMetadataForChart,
12
13
14
15
16
  formatSensorThingsApiResponseForHeatMap,
  drawHeatMapHighcharts,
  formatSensorThingsApiResponseForLineChart,
  drawLineChartHighcharts,
  extractCombinedObservationsFromAllPages,
17
  getMetadataPlusObservationsFromSingleDatastream,
18
19
} from "./appChart.js";

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const buildingsAvailableSensorsArr = [
  ["--Select--", "", ""],

  ["Bau 101", "Vorlauftemperatur", "15 min"],
  ["Bau 101", "Vorlauftemperatur", "60 min"],
  ["Bau 101", "Rücklauftemperatur", "15 min"],
  ["Bau 101", "Rücklauftemperatur", "60 min"],

  ["Bau 102", "Vorlauftemperatur", "15 min"],
  ["Bau 102", "Vorlauftemperatur", "60 min"],
  ["Bau 102", "Rücklauftemperatur", "15 min"],
  ["Bau 102", "Rücklauftemperatur", "60 min"],

  ["Bau 107", "Vorlauftemperatur", "15 min"],
  ["Bau 107", "Vorlauftemperatur", "60 min"],
  ["Bau 107", "Rücklauftemperatur", "15 min"],
  ["Bau 107", "Rücklauftemperatur", "60 min"],

  ["Bau 112", "Vorlauftemperatur", "15 min"],
  ["Bau 112", "Vorlauftemperatur", "60 min"],
  ["Bau 112", "Rücklauftemperatur", "15 min"],
  ["Bau 112", "Rücklauftemperatur", "60 min"],

  ["Bau 125", "Vorlauftemperatur", "15 min"],
  ["Bau 125", "Vorlauftemperatur", "60 min"],
  ["Bau 125", "Rücklauftemperatur", "15 min"],
  ["Bau 125", "Rücklauftemperatur", "60 min"],

  ["Bau 225", "Vorlauftemperatur", "15 min"],
  ["Bau 225", "Vorlauftemperatur", "60 min"],
  ["Bau 225", "Rücklauftemperatur", "15 min"],
  ["Bau 225", "Rücklauftemperatur", "60 min"],
  ["Bau 225", "Durchfluss", "15 min"],
  ["Bau 225", "Durchfluss", "60 min"],
  ["Bau 225", "Leistung", "15 min"],
  ["Bau 225", "Leistung", "60 min"],
  ["Bau 225", "Energie", "15 min"],
  ["Bau 225", "Energie", "60 min"],
  ["Bau 225", "Energie_VERBR", "15 min"],
  ["Bau 225", "Energie_VERBR", "60 min"],
];

/**
 * Get the unique values from an array
 * @param {Array} dataArr Input array
 * @param {Number} index Integer representing a zero-based index of the columns in the array
 * @returns {Array} An array of unique options
 */
const getUniqueValues = function (dataArr, index) {
  const uniqueOptions = new Set();

  dataArr.forEach((row) => uniqueOptions.add(row[index]));

  return [...uniqueOptions];
74
75
};

76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * Populate the HTML elements that make up a drop down list
 * @param {String} element String corresponding to the ID of a drop down HTML element
 * @param {Array} uniqueValuesArr An array of unique values
 * @returns {undefined}
 */
const populateDropDown = function (element, uniqueValuesArr) {
  element.innerHTML = "";

  uniqueValuesArr.forEach((item) => {
    const option = document.createElement("option");
    option.textContent = item;
    element.appendChild(option);
  });
90
91
};

92
93
94
95
96
97
98
99
100
101
/**
 * Filter an array using filter strings of variable length
 * @param {*} dataArr Input array
 * @param {*} filtersAsArray An array of filter strings
 * @returns {Array} An array that contains the filter result
 */
const filterArray = function (dataArr, filtersAsArray) {
  return dataArr.filter((row) =>
    filtersAsArray.every((filterItem, i) => filterItem === row[i])
  );
Pithon Kabiro's avatar
Pithon Kabiro committed
102
103
};

104
105
106
107
108
/**
 * Create a drop down list in the HTML document
 * @param {*} dataArr Input array
 * @param {*} filtersAsArray An array of strings tp be used as filters
 * @param {*} targetElement String corresponding to the ID of a drop down HTML element
109
 * @returns {undefined}
110
111
112
113
114
115
116
117
118
119
120
121
122
 */
const makeDropDown = function (dataArr, filtersAsArray, targetElement) {
  const filteredArr = filterArray(dataArr, filtersAsArray);

  const uniqueList = getUniqueValues(filteredArr, filtersAsArray.length);

  getUniqueValues(dataArr);

  populateDropDown(targetElement, uniqueList);
};

/**
 * Use the `makeDropDown` function to create the first two levels of the linked drop down lists
123
 * @returns {undefined}
124
125
126
127
128
129
130
 */
const applyDropDown = function () {
  const selectLevel1Value = document.querySelector("#drop-down--bldg").value;
  const selectLevel2 = document.querySelector("#drop-down--sensor");
  makeDropDown(buildingsAvailableSensorsArr, [selectLevel1Value], selectLevel2);

  applyDropDown2();
131
132
};

133
134
/**
 * Use the `makeDropDown` function to create the third level of the linked drop down lists
135
 * @returns {undefined}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 */
const applyDropDown2 = function () {
  const selectLevel1Value = document.querySelector("#drop-down--bldg").value;
  const selectLevel2Value = document.querySelector("#drop-down--sensor").value;
  const selectLevel3 = document.querySelector("#drop-down--sampling-rate");
  makeDropDown(
    buildingsAvailableSensorsArr,
    [selectLevel1Value, selectLevel2Value],
    selectLevel3
  );
};

/**
 * Use the `populateDropDown` function to populate the first level drop down
150
 * @returns {undefined}
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 */
const populateFirstLevelDropDown = function () {
  const el = document.querySelector("#drop-down--bldg");
  const uniqueList = getUniqueValues(buildingsAvailableSensorsArr, 0);
  populateDropDown(el, uniqueList);
};

document
  .querySelector("#drop-down--bldg")
  .addEventListener("change", applyDropDown);
document
  .querySelector("#drop-down--sensor")
  .addEventListener("change", applyDropDown2);

// These functions run after "DOMContentLoaded" event
populateFirstLevelDropDown();
applyDropDown();
168
169
170
171
172
173
174
175
176
177
178
179

/**
 * Get the values from the currently selected options in the linked drop dpwn lists
 * @returns {Array} An array containing the values of the selected options
 */
const getSelectedOptionsFromDropDownLists = function () {
  const selectedBuilding = document.querySelector("#drop-down--bldg").value;
  const selectedSensor = document.querySelector("#drop-down--sensor").value;
  const selectedSamplingRate = document.querySelector(
    "#drop-down--sampling-rate"
  ).value;

180
181
182
183
184
185
186
  if (
    selectedBuilding === "--Select--" ||
    selectedSensor === "" ||
    selectedSamplingRate === ""
  )
    return;

187
188
  return [selectedBuilding, selectedSensor, selectedSamplingRate];
};
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

/**
 * Get the abbreviated form of building IDs, phenomenon names and sensor sampling rates
 * @param {String} buildingFullForm A string representation of the full form of a building ID
 * @param {String} phenomenonFullForm A string representation of the full form of a phenomenon name
 * @param {String} samplingRateFullForm A string representation of the full form of a sensor's sampling rate
 * @returns {Array} An array of abbreviated strings
 */
const getBuildingSensorSamplingRateAbbreviation = function (
  buildingFullForm,
  phenomenonFullForm,
  samplingRateFullForm
) {
  const fullFormToAbbreviationMapping = {
    buildings: {
      "Bau 101": "101",
      "Bau 102": "102",
      "Bau 107": "107",
      "Bau 112": "112, 118",
      "Bau 125": "125",
      "Bau 225": "225",
    },
    phenomenon: {
      Vorlauftemperatur: "vl",
      Rücklauftemperatur: "rl",
      Durchfluss: "flow",
      Leistung: "power",
      Energie: "energy",
      Energie_VERBR: "energy_verb",
    },
    samplingRate: {
      "15 min": "15min",
      "60 min": "60min",
    },
  };

  const buildingAbbrev =
    fullFormToAbbreviationMapping["buildings"]?.[buildingFullForm];

  const phenomenonAbbrev =
    fullFormToAbbreviationMapping["phenomenon"]?.[phenomenonFullForm];

  const samplingRateAbbrev =
    fullFormToAbbreviationMapping["samplingRate"]?.[samplingRateFullForm];

  return [buildingAbbrev, phenomenonAbbrev, samplingRateAbbrev];
};
236

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 * Show a loading indicator at the start of an async task. The indicator consists of a spinner and a transluscent mask placed on top of page elements
 * @returns {undefined}
 */
const showLoadingSpinner = function () {
  const loadingIndicatorMask = document.querySelector("#loadingIndicator");
  const loadingIconSpinner = document.querySelector("#loadingIcon");

  loadingIndicatorMask.style.display = "block";
  loadingIconSpinner.style.display = "block";
};

/**
 * Hide the loading indicator after completion of the async tasks
 * @returns {undefined}
 */
const hideLoadingSpinner = function () {
  const loadingIndicatorMask = document.querySelector("#loadingIndicator");
  const loadingIconSpinner = document.querySelector("#loadingIcon");

  loadingIndicatorMask.style.display = "none";
  loadingIconSpinner.style.display = "none";
};

/**
 * Callback function for chart selection using drop down list
 * @returns {undefined}
 */
265
const selectChartTypeFromDropDown = async function () {
266
267
268
  try {
    const selectedOptions = getSelectedOptionsFromDropDownLists();

269
270
    if (selectedOptions === undefined) return;

271
272
273
274
275
276
277
278
    const abbreviationsArr = getBuildingSensorSamplingRateAbbreviation(
      ...selectedOptions
    );

    const selectedDatastream = getDatastreamIdFromBuildingNumber(
      ...abbreviationsArr
    );

279
280
281
282
283
284
    const selectedChartType = document.querySelector(
      "#drop-down--chart-type"
    ).value;

    if (selectedChartType === "--Select--") return;

285
286
    // Display the loading indicator
    showLoadingSpinner();
287

288
289
290
291
292
    const URL_DATASTREAM = createDatastreamUrl(BASE_URL, selectedDatastream);
    const URL_OBSERVATIONS = createObservationsUrl(
      BASE_URL,
      selectedDatastream
    );
293
294

    // Create promises
295
296
    const promiseDatastreamMetadata =
      getMetadataFromSingleDatastream(URL_DATASTREAM);
297
298
    const promiseCombinedObservations = extractCombinedObservationsFromAllPages(
      performGetRequestUsingAxios(URL_OBSERVATIONS, QUERY_PARAMS_COMBINED)
299
300
301
    );

    // Pass promises to our async function
302
303
304
305
306
    const metadataPlusObservations =
      await getMetadataPlusObservationsFromSingleDatastream([
        promiseCombinedObservations,
        promiseDatastreamMetadata,
      ]);
307
308
309
310
311

    // Extract the metadata and the observations from resulting array
    const combinedObs = metadataPlusObservations[0];
    const datastreamMetadata = metadataPlusObservations[1];

312
    if (selectedChartType === "Line") {
313
314
      drawLineChartHighcharts(
        formatSensorThingsApiResponseForLineChart(combinedObs),
315
316
317
        formatDatastreamMetadataForChart(datastreamMetadata)
      );
    } else if (selectedChartType === "Heatmap") {
318
319
      drawHeatMapHighcharts(
        formatSensorThingsApiResponseForHeatMap(combinedObs),
320
321
322
        formatDatastreamMetadataForChart(datastreamMetadata)
      );
    }
323
324
  } catch (err) {
    console.error(err);
325
326
327
  } finally {
    // Hide the loading indicator
    hideLoadingSpinner();
328
329
330
331
  }
};

document
Pithon Kabiro's avatar
Pithon Kabiro committed
332
  .querySelector("#drop-down--chart-type")
333
  .addEventListener("change", selectChartTypeFromDropDown);