TestNg IReporter listener - test start and end time - listener

I am using a sample listener from. http://relevantcodes.com/testng-listener-using-extentreports/
Does someone know how to get the time test was started and end from the listener?
I checked. https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/reporters/SuiteHTMLReporter.java#L424 but its not clear how time info is pulled from. The code does
iim.getDate()
But I don't understand how that provides the diff because it just subtracts diff from itself only.

Karthik
I have updated the post, you can use the new code to retrieve started and ended times:
test.getTest().startedTime = new Date(result.getStartMillis());
test.getTest().endedTime = new Date(result.getEndMillis());
In TestNG, you can retrieve starting and ending milliseconds, which can be converted to Date.

Related

RTC query to get each day effort

How to query in RTC 7.0.2 for a formal project, to get each day effort (time tracking) or time spent each day for a task?
That might have changed since 2015, but this thread mentioned
Time tracking is not a field/an attribute. Time tracking entries are added to a work item using a reference/link. I know that others have done this in the past.
The article which deals with this topic is "The Work Item Time Tracking API" from Ralph
The following code update or create time tracking entries, but, in the process, access and read existing time tracking first, which is of interest here.
public void updateOrCreateTimeSheetEntry(WorkItemWorkingCopy workingCopy,
ITimeCode timeCode, Timestamp startDateTimeStamp,
Duration workDuration, Identifier workType,
IProgressMonitor monitor) throws TeamRepositoryException {
// set the active work item from the working copy
setWorkItem(workingCopy.getWorkItem());
// Find a matching time sheet if it exists.
ITimeSheetEntry timeSheet = findTimeSheetEntry(timeCode,
startDateTimeStamp, monitor);
if (timeSheet == null) {
// There is no time sheet for this entry
// Create a new one and create the link
timeSheet = createTimeSheet();
workingCopy.getReferences()
.add(WorkItemEndPoints.WORK_TIME,
IReferenceFactory.INSTANCE
.createReferenceToItem(timeSheet));
// Add the entry to the map to hold the data
addEntry(timeSheet, monitor);
} else {
// There is a time sheet, we need to update it
// Get the workingCopy of the time sheet
timeSheet = (ITimeSheetEntry) timeSheet.getWorkingCopy();
// remove the time spent from current time
setTotalDuration(new Duration(getTotalDuration().longValue()
- timeSheet.getTimeSpent().longValue()));
}
// Set the new data
timeSheet.setStartDate(startDateTimeStamp);
timeSheet.setTimeCodeId(timeCode.getTimeCodeId());
// TODO: If I leave this out it fails....
timeSheet.setTimeCode(timeCode.getTimeCodeLabel());
timeSheet.setTimeSpent(workDuration);
timeSheet.setWorkType(workType);
// add the new time back
setTotalDuration(getTotalDuration().add(workDuration));
// Update the value
// Note: it is important to set the duration value, of the work item
// otherwise the work item is not marked as dirty and in need to update
// in the repository and the save process will not save the time sheet
getWorkItem().setDuration(getTotalDuration().longValue());
workingCopy.getDependentItems().add(timeSheet);
}
You can make a query to fetch work items for a given date, used a stored query, and list your work items that way.

How to schedule a trigger on a script

The situation:
I have sheet containing JSON data that collects personal trading information from a stock exchange. When this API feed retrieves a new line of information (after a new trade has been placed) I need that to act as a trigger to run two Google Scripts to perform their function. These scripts can only be triggered when a new trade has been made, not on a regular time-based trigger.
What have I tried:
Initially, I started off trying onChange/onEdit however, both options will not work because onChange/onEdit search for user-made edits to the cell, which changes to an API feed are not. Because there is no material change to the formula and therefore onChange/onEdit do not react to trigger the script. I have also tried to find a solution for a trigger to activate a script on changes made within a formula (therefore to allow for new data to arriving through an API to trigger the script), but that doesn't appear to be possible.
What am I trying to achieve right now:
I am considering the possibility of establishing a time scheduled trigger via Scripts. Within the API feed, I get confirmation of the date and time a trade has been made. I plan for the script to search the lastRow of certain columns to identify a date and time to trigger this time scheduled script, which in turn will trigger the two other scripts mentioned above.
My coding:
function createTimeDrivenTriggers() {
// Trigger on 2019-12-11 at 21:00.
ScriptApp.newTrigger('priceCalc')
.timeBased()
.atDate(2019, 12, 11)
.atHour(21)
.create();
}
Explanation on coding:
Eventually, the information held within the .atDate() and .atHour() strings will contain information drawn from the lastRow of different columns on Google Sheets to identify the correct integer to feed in to this script. But for now, I am just trying to get this script to work based on fixed values.
In this example above, on 11th December 2019 at 21:00, the createTimeDrivenTriggers script should be triggered, which in turn runs the priceCalc script.
Questions:
1) I cannot get this script to work correctly at the date/time given. What am I doing wrong?
2) I also need to incorporate an .atMinute() and .atSecond() strings here, but that doesn't seem to be available. Can anyone advise how to incorporate this?
3) Finally, if anyone can think of a better way to find a solution for this other than a time scheduled trigger, I am happy to consider other options.
I am very much a novice of scripts, so helpful advise to sort my issue would be greatly appreciated. I have spent days trying to find a solution without any luck.
You can not use atDate(year, month, day) and atHour(hour) together
Indeed, the documentation specifies:
Frequency is required if you are using atHour() or nearMinute()
Instead, you can use at(date) with a date-time string, with the corresponding Javascript syntax.
Sample:
function createTimeDrivenTriggers() {
// Trigger on 2019-12-11 at 21:00.
var time=new Date('2019-12-12T21:00:00');
ScriptApp.newTrigger('priceCalc')
.timeBased()
.at(time)
.create();
}
When you run your code, it generates an error message:
Already chosen a specific date time with at() or atDate()....
What it means is that you cannot use both atDate() and atHour() in the same script.
The solution to creating a "time-of-day-and-hour" trigger is to use at().
The benefit of this is that you can specify a time interval down to seconds and milliseconds.
However, do not be misled. Google undertakes that the script will execute within +/-15 minutes from the specified time. So incorporating "second" parameters might make you feel good, but isn't guaranteed to make any real difference.
Props: #SpencerEaston (https://stackoverflow.com/a/30458103/1330560) for the definition of the date which may well be in the documentation, but I couldn't see it.
For example:
function createTimeDrivenTriggers() {
// Trigger on 2019-12-12 at 21:00.
//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var d = new Date(2019, 12, 12, 21, 00, 00, 00);
ScriptApp.newTrigger('triggertest')
.timeBased()
.at(d)
.create();
}

Why is Time not setting when inserting new task using Task Service?

I am trying to insert new task with due date and time in Google Tasks using Tasks Service. The issue i am facing is that date is setting correctly while time is not setting. The code is given below:
I had tried all the solutions given on stackoverflow and other platform, none of them worked.
var task = Tasks.newTask().setTitle(data[i]);
task.setDue("2019-6-25T10:10:10.000Z");
Tasks.Tasks.insert(task, taskList.id);
I expect both the date and time to be set but only date is setting.
The official document says as follows.
Due date of the task (as a RFC 3339 timestamp). Optional. The due date only records date information; the time portion of the timestamp is discarded when setting the due date. It isn't possible to read or write the time that a task is due via the API.
Unfortunately, in the current stage, by above specification, even if 2019-6-25T10:10:10.000Z is used for due, it becomes 2019-06-25T00:00:00.000Z. So it seems that in order to modify the time, it is required to manually modify it.
Reference:
Resource representations of Tasks

Strange results returning from Response.getId() in GAS

I am working with a script to pull the response ID from a form submission so that when users edit their response, I can match the edit to the initial response. My script creates a .pdf of the contents of each form submitted, but when users edit responses, it creates a new form containing only the edited data, without linking it to the initial response.
The edit response contains a timestamp and the edited information, and the timestamp is used with Response.getId(timestamp), iirc, and returns the ID of the form submission. However, I am sometimes getting a very strange return, and I don't know where it comes from. The response is much shorter than a typical response ID (10-12 alphanumeric instead of 30+). I will try to comeback and edit this question with the code, but I'm on a different log in at the moment, so I'll have to swap over and copy code and come back. Running unit tests hasn't really helped, as I get the expected result most of the time. It's just an occasional hiccup, but I have to find where the incorrect information is coming from.
function getResponseId(timestamp){
var form = FormApp.openById('<formID>');
if(typeof(timestamp) != 'number'){
timestamp = new Date(timestamp);
}
var responses = form.getResponses(timestamp);
var entryId = responses[0].getId();
return entryId;
}
That's is. I suppose the if(typeof... may be throwing things off if it's passing in a Date string instead of the actual timestamp of entry, but I don't think so. I'll double check it and debug it in the meantime, and if I answer my own question, I'll come back and fix it.
Hmm. Sounds like the same behavior. What I decided to do instead was to match the timestamps that are recorded, but you have to grab the stamps in the 3rd line of code, as the old stamp is overwritten quickly. I just subtracted oldTime from newTime, left a 5 sec window to match them, and that worked. I just haven't had a chance to post it yet, as I just got it working for new submissions, edits, and failed writes about an hour ago.

How to create AllDay Event with Google Apps Script that span multiple days and show as a single bar

I have a script that takes event information for google sheet and adds them into a specified google calendar. So far everything works fine except when I try to create an all day event that spans multiple days. The scenario that I am fighting with is for vacation. If I create the event manually, I can choose "All day" and set a start and end date of two different days. Note, "Repeat..." is not used. Visually the event spans the days specified. This is what I want to duplicate with my script.
As far as I can tell, the API only provides:
calendar.createEvent(title, startTime, endTime)
which creates a non-all day event that does span days, but it shows the start time which is not what I want. The event is truly and all day event.
calendar.createAllDayEvent(title, date)
which creates and all day event for only one day.
What seems to be missing is
calendar.createAllDayEvent(title, startDate, endDate)
which is what is discussed here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=952, but there isn't any significant movement on the issue.
A possible work around is discussed here: How to create two days AllDay Event with Google Apps Script?. The problem is creates what looks like multiple single all day events. I want the multi day to show up as spanning multiple days.
Has anyone found any other work around?
You can use createEventFromDescription(), it works perfectly for "n" full days.
var start = Utilities.formatDate(new Date(date_from), "GMT"-0500, "MM-dd-yyyy");
var end = Utilities.formatDate(new Date(date_to), "GMT"-0500, "MM-dd-yyyy");
var vacationCalendar = CalendarApp.getCalendarById(CALENDAR_ID);
vacationCalendar.createEventFromDescription(title+" "+start+"-"+end);
Edy's suggestion of createEventFromDescription() is nice and clean, +1.
With that being said, if you need more fine-grained control over the event and have a lot of metadata you're trying to set (such as location, description, attendees, etc), I would recommend using the new version of the Google Cal API aka "Advanced Calendar Service", which natively supports multiple-day all-day events.
Basically looks like this:
// end dates are exclusive, so this creates a 2-day event
// starting on the 9th and ending on the 10th
var event = {
summary: 'My Event',
start: {
date: '2015-04-09' // unfortunate it doesn't seem to accept JS Date() objects :(
},
end: {
date: '2015-04-11'
}
};
event = Calendar.Events.insert(event, calId);
For what it's worth, "use this new API" is also the answer given by a google dev on the issue ticket mentioned in the original description of this StackOverflow post.