fetchedDataProcessing.mjs 6.16 KB
Newer Older
Pithon Kabiro's avatar
Pithon Kabiro committed
1
2
3
4
5
6
7
8
9
10
11
12
13
"use strict";

/**
 * Match the unitOfMeasurement's string representation of a symbol to an actual symbol, where necessary
 * @param {String} unitOfMeasurementSymbolString String representation of the unitOfMeasurement's symbol
 * @returns {String} The unitOfMeasurement's symbol
 */
const matchUnitOfMeasurementSymbolStringToSymbol = function (
  unitOfMeasurementSymbolString
) {
  const unicodeCodePointDegreeSymbol = "\u00B0";
  const unicodeCodePointSuperscriptThree = "\u00B3";

14
15
  // Symbol - temperature
  if (unitOfMeasurementSymbolString === "degC") {
Pithon Kabiro's avatar
Pithon Kabiro committed
16
    return `${unicodeCodePointDegreeSymbol}C`;
17
18
19
  }
  // Symbol - flow rate
  else if (unitOfMeasurementSymbolString === "m3/h") {
Pithon Kabiro's avatar
Pithon Kabiro committed
20
    return `m${unicodeCodePointSuperscriptThree}/h`;
21
  }
Pithon Kabiro's avatar
Pithon Kabiro committed
22
  // If no symbol exists
23
24
25
  else {
    return unitOfMeasurementSymbolString;
  }
Pithon Kabiro's avatar
Pithon Kabiro committed
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
};

/**
 * Extract the phenomenon name from a Datastream's name
 * @param {String} datastreamName A string representing the Datastream's name
 * @returns {String} The extracted phenomenon name
 */
const extractPhenomenonNameFromDatastreamName = function (datastreamName) {
  const regex = /\/ (.*) DS/;
  return datastreamName.match(regex)[1]; // use second element in array
};

/**
 * Extract the building Id and phenomenon name from a Datastream's name
 * @param {String} datastreamName A string representing the Datastream's name
 * @returns {String} The extracted building ID and phenomenon name
 */
const extractBuildingIdPhenomenonNameFromDatastreamName = function (
  datastreamName
) {
  // The negative index should remove these nine characters: ` DS:60min`
  return datastreamName.slice(0, -9);
};

/**
 * Format the response containing a Datastream's metadata from Sensorthings API
 * @param {Object} datastreamMetadata An object containing a Datastream's metadata
 * @returns {Object} An object containing the formatted metadata that is suitable for use in a chart
 */
55
const formatDatastreamMetadataForChart = function (datastreamMetadata) {
Pithon Kabiro's avatar
Pithon Kabiro committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  const {
    description: datastreamDescription,
    name: datastreamName,
    unitOfMeasurement,
  } = datastreamMetadata;

  // Extract phenomenon name from Datastream name
  const phenomenonName =
    extractPhenomenonNameFromDatastreamName(datastreamName);

  // Extract building ID + phenomenon name from Datastream name
  const buildingIdPhenomenonName =
    extractBuildingIdPhenomenonNameFromDatastreamName(datastreamName);

  // Get the unitOfMeasurement's symbol
  const unitOfMeasurementSymbol = matchUnitOfMeasurementSymbolStringToSymbol(
    unitOfMeasurement.symbol
  );

  return {
    datastreamDescription,
    datastreamName,
78
    phenomenonName,
Pithon Kabiro's avatar
Pithon Kabiro committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    buildingIdPhenomenonName,
    unitOfMeasurementSymbol,
  };
};

/**
 * Extract the properties that make up the formatted datastream metadata object(s)
 * @param {Array} formattedDatastreamsMetadataArr An array of formatted metadata object(s) from one or more datastreams
 * @param {Boolean} isMetadataForAggregation A flag to determine if the datastream metadata will be used for aggregation. Set to `true` if metadata will be used for aggregation, `false` if not
 * @param {String} [aggregationInterval] The aggregation interval as a string, either "daily" or "monthly". Required when `isMetadataForAggregation = true`
 * @param {String} [aggregationType] The aggregation type as a string, either "sum" or "average". Required when `isMetadataForAggregation = true`
 * @returns {Object} An object that contains array(s) of formatted datastream metadata properties
 */
const extractPropertiesFromFormattedDatastreamMetadata = function (
  formattedDatastreamsMetadataArr,
  isMetadataForAggregation,
  aggregationInterval,
  aggregationType
) {
  if (
    formattedDatastreamsMetadataArr === undefined ||
    isMetadataForAggregation === undefined
101
  ) {
Pithon Kabiro's avatar
Pithon Kabiro committed
102
103
104
    throw new Error(
      "This function expects two arguments, ensure that both have been supplied"
    );
105
  } else if (
Pithon Kabiro's avatar
Pithon Kabiro committed
106
107
108
    formattedDatastreamsMetadataArr &&
    isMetadataForAggregation &&
    (aggregationInterval === undefined || aggregationType === undefined)
109
  ) {
Pithon Kabiro's avatar
Pithon Kabiro committed
110
111
112
    throw new Error(
      "This function expects four arguments, ensure that all of them have been supplied"
    );
113
  } else if (
Pithon Kabiro's avatar
Pithon Kabiro committed
114
115
116
    isMetadataForAggregation &&
    aggregationInterval !== "daily" &&
    aggregationInterval !== "monthly"
117
  ) {
Pithon Kabiro's avatar
Pithon Kabiro committed
118
119
120
    throw new Error(
      `The supported aggegation interval strings are "daily" or "monthly"`
    );
121
  } else if (
Pithon Kabiro's avatar
Pithon Kabiro committed
122
    isMetadataForAggregation &&
123
124
    aggregationType !== "minimum" &&
    aggregationType !== "maximum" &&
Pithon Kabiro's avatar
Pithon Kabiro committed
125
126
    aggregationType !== "sum" &&
    aggregationType !== "average"
127
  ) {
Pithon Kabiro's avatar
Pithon Kabiro committed
128
    throw new Error(
129
      `The supported aggegation type strings are "minimum", "maximum", "sum" or "average"`
Pithon Kabiro's avatar
Pithon Kabiro committed
130
    );
131
  }
Pithon Kabiro's avatar
Pithon Kabiro committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

  // Create arrays from the properties of the formatted datastream metadata
  const datastreamDescriptionsArr = formattedDatastreamsMetadataArr.map(
    (datastreamMetadata) => datastreamMetadata.datastreamDescription
  );

  const datastreamNamesArr = formattedDatastreamsMetadataArr.map(
    (datastreamMetadata) => datastreamMetadata.datastreamName
  );

  const phenomenonNamesArr = formattedDatastreamsMetadataArr.map(
    (datastreamMetadata) => datastreamMetadata.phenomenonName
  );

  const buildingIdsPhenomenonNamesArr = formattedDatastreamsMetadataArr.map(
    (datastreamMetadata) => datastreamMetadata.buildingIdPhenomenonName
  );

  const unitOfMeasurementSymbolsArr = formattedDatastreamsMetadataArr.map(
    (datastreamMetadata) => datastreamMetadata.unitOfMeasurementSymbol
  );

  // Case 1: Metadata NOT USED for aggregation; "isMetadataForAggregation" = false
155
  if (!isMetadataForAggregation) {
Pithon Kabiro's avatar
Pithon Kabiro committed
156
157
158
159
    return {
      datastreamDescriptionsArr,
      datastreamNamesArr,
      phenomenonNamesArr,
160
      buildingIdsPhenomenonNamesArr,
Pithon Kabiro's avatar
Pithon Kabiro committed
161
162
      unitOfMeasurementSymbolsArr,
    };
163
  }
Pithon Kabiro's avatar
Pithon Kabiro committed
164
  // Case 2: Metadata USED for aggregation; "isMetadataForAggregation" = true
165
166
167
168
169
170
171
172
173
174
175
  else {
    return {
      datastreamDescriptionsArr,
      datastreamNamesArr,
      phenomenonNamesArr,
      buildingIdsPhenomenonNamesArr,
      unitOfMeasurementSymbolsArr,
      aggregationInterval,
      aggregationType,
    };
  }
Pithon Kabiro's avatar
Pithon Kabiro committed
176
177
178
179
180
181
182
};

export {
  extractPhenomenonNameFromDatastreamName,
  formatDatastreamMetadataForChart,
  extractPropertiesFromFormattedDatastreamMetadata,
};