Connect moment-range with sails backend - momentjs

I want to get date range between start_time and end_time.
var newrange = sails.moment.range(start_time, end_time);
But it says,
TypeError: sails.moment.range is not a function

Try setting up the imports like below and then run the function:
const moment = require("moment");
const momentRange = require("moment-range");
momentRange.extendMoment(moment);
var newrange = moment.range(start_time, end_time);

Related

Luxon setZone not working when getting epoch time (valueOf)

I have been trying to use Luxon to get (for comparison reasons) the epoch miliseconds from two DateTimes in different timezone.
I'm using the .valueOf() method to do so, but even that I'm setting the different timezones for each DateTime, that method always return the same value:
const { DateTime } = require('luxon')
const dtString = '2022-01-15 13:00:00'
const d1 = DateTime.fromSQL(dtString).setZone('America/Sao_Paulo').valueOf()
const d2 = DateTime.fromSQL(dtString).setZone('America/New_York').valueOf()
console.log(d1) // logs 1642262400000
console.log(d2) // logs 1642262400000 (same)
What is wrong?
Instead of parsing the dates in one zone and then shifting the times around, just parse them in the zone you want:
const dtString = '2022-01-15 13:00:00';
const d1 = DateTime.fromSQL(dtString, { zone: "America/Sao_Paulo" }).valueOf();
const d2 = DateTime.fromSQL(dtString, { zone: "America/New_York" }).valueOf();
d1 //=> 1642262400000
d2 //=> 1642269600000
The difference there is that in yours, Luxon parses the string using your system zone. Then you change the zone, but the time is still the time, unless you use keepLocalTime. In mine, I'm telling Luxon what zone to use in interpreting the string, which results in a different time being computed for the two cases.
Based on this Luxon issue, I found the fix for that:
We should use the keepCalendarTime: true option in the setZone method to make it work.
That extra flag says "yes, change the actual time"
So the functional code looks like:
const { DateTime } = require('luxon')
const dtString = '2022-01-15 13:00:00'
const opts = { keepCalendarTime: true }
const d1 = DateTime.fromSQL(dtString).setZone('America/Sao_Paulo', opts).valueOf()
const d2 = DateTime.fromSQL(dtString).setZone('America/New_York', opts).valueOf()
console.log(d1) // logs 1642262400000
console.log(d2) // logs 1642269600000 (prints different value)

Firebase RealtimeDB to Google Sheets through AppScript

I am trying to write Firebase RealtimeDB data into a Google Sheets spreadsheet through Google Appscript.
This is what my RealtimeDB looks like:
My goal is
I have tried the following code snippet:
function writeSheets() {
var firebaseUrl = "https://<mydatabase>.firebaseio.com/Attendees";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData();
var ss = SpreadsheetApp.openById("<mySheet>");
var sheet = ss.getSheetByName("Sheet1");
var num = 2;
range = ss.getRange("A"+num+":B"+num+"");
for(var i in data) {
var values = [[ data[i].ID, data[i].Name ] ];
range.setValues(values);
num += 1;
range = sheet.getRange("A"+num+":B"+num+"");
}
console.log(JSON.stringify(data));
}
It seems to print empty cells into Google Sheet. Not exactly sure why.
Any input would be greatly appreciated. Thanks.
EDIT:
Console Log :
[20-04-08 20:46:26:102 HKT] TypeError: Cannot read property 'ID' of null
at writeSheets(Code:13:27)
There is a extra null in the data. Filter it out and map the array of objects to a 2D array and setValues the 2D array:
const data = [null,{"ID":1001,"Name":"Bob"},{"ID":1069,"Name":"Steve"},{"ID":1420,"Name":"Bill"}];
const out = data.filter(Boolean).map(Object.values);
console.log(out);
/*SetValues the entire array*/
//sheet.getRange(1,1,out.length, out[0].length).setValues(out);

Firebase Function is returning the wrong time

firebase cloud functions UTC time is off by four hours.
Im working on a react-native app for debating. I am working on getting each case to close by comparing the completion time to the time now. I am using a firebase cloud function to do this. The function was working perfectly last week however when i started working on the app this week the function was consolelogging the wrong time and when i do the same calculation from my app running on my computer it is correct so i can rule out how im calculating it!
const date = require('date-and-time');
const now = new Date()
const currentDate = date.format(now, 'MM/DD/YYYY, HH:mm:ss')
const dateFrom = userCase.completionTime
const DateDiff = require('date-diff')
const date1 = new Date(dateFrom)
const date2 = new Date(currentDate)
const difff = new DateDiff(date1, date2)
const time = difff.seconds()
const totalyesVotes = _.size(yesVotes)
const totalnoVotes = _.size(noVotes)
console.log(currentDate + "current time")
so i console.logged these at the same time this one from google cloud
05/06/2019, 20:50:49
and this one from my computer
05/06/2019, 16:50:49

How to wire up virtual-hyperscript, hyperscript-helpers, and main-loop

I'm looking at an example by substack of using hyperscript, main-loop, and hyperx.
I'd like to recreate this example using hyperscript-helpers to get code similar to Elm. That module says it supports both hyperscript and virtual-hyperscript, so I'm trying virtual-hyperscript.
My code looks like this:
var vdom = require('virtual-dom')
var vh = require('virtual-hyperscript');
var hh = require('hyperscript-helpers')(vh);
var main = require('main-loop')
var div = hh.div;
var span = hh.span;
var h1 = hh.h1;
var loop = main({ times: 0 }, render, vdom)
document.querySelector('#content').appendChild(loop.target)
function render(state) {
return h1('title');
}
And it gives me an error:
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
What's going wrong? I assume something's not wired up correctly because
console.log(loop.target) //null
If it helps, I can post my html and the browserify build command I'm using
virtual-hyperscript is moved to https://github.com/Matt-Esch/virtual-dom/tree/master/virtual-hyperscript
See README at https://github.com/Raynos/virtual-hyperscript
The virtual-dom/h is just a new version of virtual-hyperscript.

How to apply segment using Google Analytics .NET client

I am trying to apply a segment for a query through Google Analytics .NET client but I am not able to get it working. Here is what I have been trying:
var segments = analyticsService.Management.Segments.List().Execute();
var engagedTeamsSegment = segments.Items.FirstOrDefault(x => x.Name.Equals("Engaged Teams", StringComparison.OrdinalIgnoreCase));
var format = "yyyy-MM-dd";
var today = DateTime.UtcNow.Date;
var thirtyDaysAgo = today.Subtract(TimeSpan.FromDays(30));
var metrics = engagedTeamsSegment.Definition.Replace(';', ',');
var gaData = analyticsService
.Data.Ga
.Get($"ga:{profile.Id}", today.ToString(format), thirtyDaysAgo.ToString(format), metrics)
.Execute();
It's getting me the below error
An unhandled exception of type 'Google.GoogleApiException' occurred in
Google.Apis.dll
Additional information: Google.Apis.Requests.RequestError
Invalid value
'users::condition::ga:dimension2!=0,ga:sessionCount>=2,ga:daysSinceLastSession<=14'.
Values must match the following regular expression: 'ga:.+' [400]
Errors [
Message[Invalid value
'users::condition::ga:dimension2!=0,ga:sessionCount>=2,ga:daysSinceLastSession<=14'.
Values must match the following regular expression: 'ga:.+']
Location[metrics - parameter] Reason[invalidParameter] Domain[global]
]
I am probably doing something wrong but not sure what. Any ideas?
Found the solution thanks to this question. There was a Segment parameter on the request. Below code did the trick:
var segments = analyticsService.Management.Segments.List().Execute();
var engagedTeamsSegment = segments.Items.FirstOrDefault(x => x.Name.Equals("Engaged Teams", StringComparison.OrdinalIgnoreCase));
var format = "yyyy-MM-dd";
var today = DateTime.UtcNow.Date;
var thirtyDaysAgo = today.Subtract(TimeSpan.FromDays(30));
var gaDataRequest = analyticsService
.Data.Ga
.Get($"ga:{profile.Id}", thirtyDaysAgo.ToString(format), today.ToString(format), "ga:users");
gaDataRequest.Segment = engagedTeamsSegment.Definition;
var gaData = gaDataRequest.Execute();

Resources