I am trying to take values with specific timestamps and aggregate them into 30 minute intervals.
The data looks like this,
0: {created: 1601820360, sentiment: -0.1, magnitude: 0.1, createdDay: "2020-10-05"}
1: {created: 1601820365, sentiment: -0.8, magnitude: 0.8, createdDay: "2020-10-05"}
2: {created: 1601900938, sentiment: -0.2, magnitude: 0.2, createdDay: "2020-10-05"}
3: {created: 1601900956, sentiment: -0.2, magnitude: 0.2, createdDay: "2020-10-05"}
4: {created: 1601900971, sentiment: 0.2, magnitude: 0.2, createdDay: "2020-10-05"}
The code I'm using currently looks like this,
function generateSentimentPoints(Sentiments) {
/** @ Create a function that parses timestamps into specific time intervals
* Data Constructed As: {dateCreated,sentiment,magnitude}
* Where dateCreated is fixed to a specific time interval
* */
let nOfEntries = Array(24).fill(1);
let values = Array(24).fill(0)
let createdDate;
var createdDateInit;
Sentiments.forEach((item) => {
//for each specific date, create a new array of 24 hour intervals?
//need to know if the date has changed at all
if (createdDateInit != createdDate) {
console.log("Does this conditional work, created Date Init Changing?",createdDateInit)
var createdDay = new Date(item.created * 1000);
var createdDate = createdDay.getFullYear()+'/'+(createdDay.getMonth()+1)+'/'+createdDay.getDate();
nOfEntries = Array(24).fill(1);
createdDateInit = createdDate;
}
const createdHour = new Date(item.created * 1000).getHours();
values[createdHour] = values[createdHour] + item.sentiment;
values[createdHour] = values[createdHour] + item.sentiment;
nOfEntries[createdHour] += 1;
});
/**
* Take average of values with the noOfEntries.
*/
values = values.map((val, index) =>
nOfEntries[index] - 1 === 0 ? val : val / (nOfEntries[index] - 1)
);
This is getting me the first day outputting correctly, but I need it to work for everyday >< . Would love some pointers on achieving this outcome
createdDate without a time zone is ambiguousThe timestamp values held in created properties are in UTC, but the 5th Oct 2020 in Victoria would have been 11 hours east of Greenwich because 0ct 4th 2020 was a Sunday marking the start of daylight saving (but requiring an internet search to find out).
This impacts converting time stamps into time of day without creating a dependency on what time zone the processing device is set to.
If processing has access to the UTC offset at the place of data generation, the date in the createdDate property becomes largely documentary, and the offset expressed in seconds could simplify processing to aggregate data into 30 minute segments across multiple days in a report or table.
I would suggest that supplying the difference between the time of day of data samples and GMT, whether in data records or in meta data, is the first thing to address.