appChart.js 5.38 KB
Newer Older
Pithon Kabiro's avatar
Pithon Kabiro committed
1
"use strict";
Pithon Kabiro's avatar
Pithon Kabiro committed
2
3

// Request parameters
4
5

// Observations WITHOUT data gap - Bau 225
Pithon Kabiro's avatar
Pithon Kabiro committed
6
const BASE_URL =
Pithon Kabiro's avatar
Pithon Kabiro committed
7
  "http://193.196.39.91:8080/frost-icity-tp31/v1.1/Datastreams(80)/Observations";
8

Pithon Kabiro's avatar
Pithon Kabiro committed
9
10
11
const PARAM_RESULT_FORMAT = "dataArray";
const PARAM_ORDER_BY = "phenomenonTime asc";
const PARAM_FILTER =
12
  "resultTime ge 2020-01-01T00:00:00.000Z and resultTime le 2021-01-01T00:00:00.000Z";
Pithon Kabiro's avatar
Pithon Kabiro committed
13
14
const PARAM_SELECT = "result,phenomenonTime";

15
16
17
18
19
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Observations WITH data gap - Bau 112
const BASE_URL2 =
  "http://193.196.39.91:8080/frost-icity-tp31-v2/v1.1/Datastreams(78)/Observations";
const PARAM_FILTER2 =
  "resultTime ge 2020-06-01T00:00:00.000Z and resultTime le 2021-01-01T00:00:00.000Z";

/**
 * Draw a heatmap using Highcharts library
 * @param {*} obsArray - Response from SensorThings API as array
 * @returns {void}
 */
const drawHeatMapHC = function (obsArray) {
  /**
   * Format the response from SensorThings API to make it suitable for heatmap
   * @returns {Array}
   */
  const formatSTAResponseForHeatMap = function () {
    const dataSTAFormatted = [];
    obsArray.forEach((obs) => {
      // Get the date/time string; first element in input array; remove trailing "Z"
      const obsDateTimeInput = obs[0].slice(0, -1);
      // Get the "date" part of an observation
      const obsDateInput = obs[0].slice(0, 10);
      // Create Date objects
      const obsDateTime = new Date(obsDateTimeInput);
      const obsDate = new Date(obsDateInput);
      // x-axis -> timestamp; will be the same for observations from the same date
      const timestamp = Date.parse(obsDate);
      // y-axis -> hourOfDay
      const hourOfDay = obsDateTime.getHours();
      // value -> the observation's value; second element in input array
      const value = obs[1];
      dataSTAFormatted.push([timestamp, hourOfDay, value]);
    });
    return dataSTAFormatted;
  };

  Highcharts.chart("chart-heatmap", {
    chart: {
      type: "heatmap",
      zoomType: "x",
    },

    boost: {
      useGPUTranslations: true,
    },

    title: {
      text: "Inlet flow (Vorlauf)",
      align: "left",
      x: 40,
    },

    subtitle: {
      text: "Temperature variation by day and hour in 2020",
      align: "left",
      x: 40,
    },

    xAxis: {
      type: "datetime",
      // min: Date.UTC(2017, 0, 1),
      // max: Date.UTC(2017, 11, 31, 23, 59, 59),
      labels: {
        align: "left",
        x: 5,
        y: 14,
        format: "{value:%B}", // long month
      },
      showLastLabel: false,
      tickLength: 16,
    },

    yAxis: {
      title: {
        text: null,
      },
      labels: {
        format: "{value}:00",
      },
      minPadding: 0,
      maxPadding: 0,
      startOnTick: false,
      endOnTick: false,
      // tickPositions: [0, 6, 12, 18, 24],
      tickPositions: [0, 3, 6, 9, 12, 15, 18, 21, 24],
      tickWidth: 1,
      min: 0,
      max: 23,
      reversed: true,
    },

    colorAxis: {
      stops: [
        [0, "#3060cf"],
        [0.5, "#fffbbc"],
        [0.9, "#c4463a"],
        [1, "#c4463a"],
      ],
      min: 60,
      max: 85,
      startOnTick: false,
      endOnTick: false,
      labels: {
        format: "{value}℃",
      },
    },

    series: [
      {
        data: formatSTAResponseForHeatMap(),
        boostThreshold: 100,
        borderWidth: 0,
        nullColor: "#525252",
        colsize: 24 * 36e5, // one day
        tooltip: {
          headerFormat: "Temperature<br/>",
          pointFormat:
            "{point.x:%e %b, %Y} {point.y}:00: <b>{point.value} ℃</b>",
        },
        turboThreshold: Number.MAX_VALUE, // #3404, remove after 4.0.5 release
      },
    ],
  });
};

Pithon Kabiro's avatar
Pithon Kabiro committed
141
/**
Pithon Kabiro's avatar
Pithon Kabiro committed
142
 * Follows "@iot.nextLink" links in SensorThingsAPI's response
Pithon Kabiro's avatar
Pithon Kabiro committed
143
 * Appends new results to existing results
Pithon Kabiro's avatar
Pithon Kabiro committed
144
145
146
 * @async
 * @param {Object} responsePromise - Promise object
 * @returns {Object} - Object containing results from all the "@iot.nextLink" links
Pithon Kabiro's avatar
Pithon Kabiro committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 */
const followNextLink = function (responsePromise) {
  return responsePromise
    .then(function (lastSuccess) {
      if (lastSuccess.data["@iot.nextLink"]) {
        return followNextLink(
          axios.get(lastSuccess.data["@iot.nextLink"])
        ).then(function (nextLinkSuccess) {
          nextLinkSuccess.data.value = lastSuccess.data.value.concat(
            nextLinkSuccess.data.value
          );
          return nextLinkSuccess;
        });
      } else {
        return lastSuccess;
      }
    })
    .catch(function (err) {
      console.log(err);
    });
};

// Get "ALL" the Observations that satisfy our query
followNextLink(
  axios.get(BASE_URL, {
    params: {
      "$resultFormat": PARAM_RESULT_FORMAT,
      "$orderBy": PARAM_ORDER_BY,
      "$filter": PARAM_FILTER,
      "$select": PARAM_SELECT,
    },
  })
)
  .then((success) => {
    const successValue = success.data.value;

    // Array that will hold the combined observations
    const combinedObservations = [];

    successValue.forEach((dataObj) => {
      // Each page of results will have a dataArray that holds the observations
      const dataArrays = dataObj.dataArray;

      combinedObservations.push(...dataArrays);
    });
    // DEBUG: Check total number of observations
    console.log(combinedObservations.length);
Pithon Kabiro's avatar
Pithon Kabiro committed
194
195
    // DEBUG: Print the array of observations
    console.log(combinedObservations);
Pithon Kabiro's avatar
Pithon Kabiro committed
196
197
198
199
200
201
202

    return combinedObservations;
  })
  .catch((err) => {
    console.log(err);
  })
  .then((observationArr) => {
203
204
    // drawHeatMapAC2(observationArr);
    drawHeatMapHC(observationArr);
Pithon Kabiro's avatar
Pithon Kabiro committed
205
  });