Workdays per Assignee per month - custom JS code

Hello,

I wrote this JS code for new custom field

[jira.customfield_wdassignee]
name = "Workdays Assignee per month"
data_type = "decimal"
measure = true
multiple_dimensions = ["Time","Assignee"]
scale = 5
javascript_code = '''

var assigneehistory = new Array();
var finalassigneehistory = new Array();
var datefrom = issue.fields.created;
var from_assignee = null; // assignee instead status
var currentdate = new Date();
var currentdate_endmonth = new Date(currentdate.getFullYear(), currentdate.getMonth()+1, 0);
currentdate_endmonth.setUTCHours(23,59,59,999);
var duration = null;
var qw = "";
var d4 = "nonecho";
issue.fields.customfield_wdassignee = null;
closed_date = null;

// iterate through chengelog entries to get the Assignee changes
if (issue.changelog && issue.changelog.histories && issue.changelog.histories.length > 0){
	var histories = issue.changelog.histories;
	var ordered_histories = histories.sort((a, b) => a.created > b.created ? 1 : -1);
	ordered_histories.forEach(function(history){
		history.items.forEach(function(historyItem){
			if (historyItem.field == "assignee") {
				to_assignee = historyItem.toString;

				from_assignee = historyItem.fromString; // from_assignee == status
				dateto = history.created;
				datestart = new Date(dateto);
				dateto_endmonth = new Date(datestart.getFullYear(), datestart.getMonth()+1, 0);
				dateto_endmonth.setUTCHours(23,59,59,999)
				datefinish = new Date(datefrom);
				datefrom_endmonth =  new Date(datefinish.getFullYear(), datefinish.getMonth()+1, 0);
				datefrom_endmonth.setUTCHours(23,59,59,999)
				if(from_assignee){ // check if an Assignee is not null == not 1st assignment
					if (Date.parse(dateto) <= Date.parse(datefrom_endmonth)){ // check if this is the same month to_Assignee and from _Assignee
						//duration = (Date.parse(dateto) - Date.parse(datefrom)) / 1000 / 60 / 60 / 24;
						duration = workdays_sat_sun(datefrom, dateto);
					}
					else {
						//Loop while date_from < end of month of change date
						datefrom_mid = new Date(datefrom);
						while(datefrom_endmonth <= datestart){
							//duration =  (Date.parse(datefrom_endmonth) - Date.parse(datefrom_mid)) / 1000 / 60 / 60 / 24;
							duration = workdays_sat_sun(datefrom_mid, datefrom_endmonth);
							assigneehistory.push(datefrom_endmonth.toISOString().split('T')[0] + "," + from_assignee + "," + duration);
							duration = 0;
							datefrom_endmonth = new Date(datefrom_endmonth.getFullYear(), datefrom_endmonth.getMonth()+2, 0);
							datefrom_endmonth.setUTCHours(23,59,59,999);
							datefrom_mid = new Date(datefrom_mid.getFullYear(), datefrom_mid.getMonth()+1, 1);
							datefrom_mid.setUTCHours(00,00,00,000);
						}
						//duration = (Date.parse(dateto) - Date.parse(datefrom_mid)) / 1000 / 60 / 60 / 24;
						duration = workdays_sat_sun(datefrom_mid, dateto);
					}
					assigneehistory.push(datefrom_endmonth.toISOString().split('T')[0] + "," + from_assignee + "," + duration);
					duration = 0;
				}
				datefrom = dateto;
				from_assignee = historyItem.toString;
			}
			if(historyItem.field == 'status'){
				movedToStatus = historyItem.toString;
				movedFromStatus = historyItem.fromString;
				if(statuslistdone.indexOf(movedToStatus) > -1 && statuslistdone.indexOf(movedFromStatus) == -1){
					closed_date = history.created;
				}
			}
		});	
	});
}

if(closed_date != null){
	var currentdate = new Date(closed_date);
	var currentdate_endmonth = new Date(currentdate.getFullYear(), currentdate.getMonth()+1, 0);
	currentdate_endmonth.setUTCHours(23,59,59,999);
}
else{
	var currentdate = new Date();
	var currentdate_endmonth = new Date(currentdate.getFullYear(), currentdate.getMonth()+1, 0);
	currentdate_endmonth.setUTCHours(23,59,59,999);
}

//Last Assignee checking
datefinish = new Date(datefrom);
datefrom_endmonth =  new Date(datefinish.getFullYear(), datefinish.getMonth()+1, 0);
datefrom_endmonth.setUTCHours(23,59,59,999)
if (datefrom_endmonth <= currentdate_endmonth && from_assignee){
	datefrom_mid = new Date(datefrom);
	datefrom_mid = new Date(datefrom);
   while (datefrom_endmonth <= currentdate){
		//duration =  (Date.parse(datefrom_endmonth) - Date.parse(datefrom_mid)) / 1000 / 60 / 60 / 24;
		duration = workdays_sat_sun(datefrom_mid, datefrom_endmonth);
		assigneehistory.push(datefrom_endmonth.toISOString().split('T')[0] + "," + from_assignee + "," + duration);
		duration = 0;
		datefrom_endmonth = new Date(datefrom_endmonth.getFullYear(), datefrom_endmonth.getMonth()+2, 0);
		datefrom_endmonth.setUTCHours(23,59,59,999);
		datefrom_mid = new Date(datefrom_mid.getFullYear(), datefrom_mid.getMonth()+1, 1);
		datefrom_mid.setUTCHours(00,00,00,000);
   }
   //duration = (Date.parse(currentdate) - Date.parse(datefrom_mid)) / 1000 / 60 / 60 / 24;
   duration = workdays_sat_sun(datefrom_mid, currentdate);
   assigneehistory.push(datefrom_endmonth.toISOString().split('T')[0] + "," + from_assignee + "," + duration);
   duration = 0;
}

// join all together
issue.fields.customfield_wdassignee = assigneehistory.join("\n");

'''

During the test of the code. I see the data

After data import. I do not see the data
image

Only see here - on All Assignee level (right sum of Workdays)

What should I do to fix it?
thank you

DKostiuk

Hi,

The likely reason why the measure is not showing per each assignee is that mapping to multiple dimensions requires the dimension member key for proper mapping. Currently, the code detects the user by the from string:

 from_assignee = historyItem.fromString; 

The from string gives the display name, not the key:

This change should fix the problem:

 from_assignee = historyItem.from; 

Kindly,
Janis, eazyBI support

Hi

I got it, and checked - even created a new account for it.
the attempt is unsuccessful.
But in Jira Import options - Additional filter - Custom JavaScript code
everything is Ok
image

please, any other ideas for the next step?

Thank you
DKostiuk

Hi,

The first thing is to do a full data re-import. Note that changed Javascript code during the reguler (incremental) import is applied for the newly updaged issues only.

If that does not explain the porblem, you should check if assignee key in eazyBI match that in the issue JSON:

Kindly,
Janis

Checked
[Assignee].CurrentHierarchyMember.KEY
I see emails

for [Assignee].CurrentHierarchyMember.AllProperties - something like it:
KEY: dkostyuk@ukr.net
DISPLAY_KEY: dkostyuk@ukr.net

please, how to fix it?

Hi,

I see that User key is the same as in
image

image