-
Notifications
You must be signed in to change notification settings - Fork 1
/
AcousticEventsChart.js
84 lines (82 loc) · 2.96 KB
/
AcousticEventsChart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
import { recordingCoverageWithMeta, sunAndMoonData } from "../utils/helpers";
import withChartData from "../hoc/withChartData";
import { getAdjustedDateRange, restructureCallEventData } from "../utils/data";
import AcousticEventsChartLayout from "./AcousticEventsChartLayout";
const AcousticEventsChart = withChartData(AcousticEventsChartLayout, {
dataPointTypes: [`recordingCoverage`, `callEvent`],
chartDataFromResponses: (responses, { fromDate, toDate, settings } = {}) => {
const [recordingCoverageResult, callEventResult] = responses;
const callEventData = restructureCallEventData(
callEventResult.data,
settings
);
// Account for time zone slippage
const { adjustedMinDate, adjustedMaxDate } = getAdjustedDateRange(
callEventResult.data,
settings
);
const minDate =
adjustedMinDate && adjustedMinDate < fromDate
? adjustedMinDate
: fromDate;
const maxDate =
adjustedMaxDate && adjustedMaxDate > toDate ? adjustedMaxDate : toDate;
const recordingCoverageData = recordingCoverageWithMeta(
recordingCoverageResult.data,
minDate,
maxDate
);
const illuminationData = sunAndMoonData(minDate, maxDate, {
timeZone: settings.stationData.timeZone,
lat: settings.stationData.latitude,
lon: settings.stationData.longitude,
});
const detectionsByDate = callEventData
.map(({ rows }) => rows)
.flat()
.reduce((result, { date, callCount }) => {
result[date] = {
numEvents: (result[date]?.numEvents || 0) + 1,
numCalls: (result[date]?.numCalls || 0) + callCount,
};
return result;
}, {});
const dailySummaryCustomData = recordingCoverageData.map((row, index) => {
const formatTime = (hours24float) => {
const am = hours24float > 12 ? false : true;
const hoursFloat = hours24float > 12 ? hours24float - 12 : hours24float;
const minutes = Math.floor((hoursFloat % 1) * 60);
const hours = Math.floor(hoursFloat);
return `${hours}:${`${minutes}`.padStart(2, "0")} ${am ? "AM" : "PM"}`;
};
return {
...row,
...illuminationData[index],
detections: {
numEvents: detectionsByDate[row.date]?.numEvents || 0,
numCalls: detectionsByDate[row.date]?.numCalls || 0,
},
sunrise: formatTime(illuminationData[index].sunrise),
sunset: formatTime(illuminationData[index].sunset),
};
});
return {
callEventData: callEventData.map((item) => {
return {
...item,
rows: item.rows.map((item) => {
return {
// Add the daily customdata for the hovertemplate
...dailySummaryCustomData.find((row) => row.date === item.date),
...item,
};
}),
};
}),
recordingCoverageData,
illuminationData,
dailySummaryCustomData,
};
},
});
export default AcousticEventsChart;