The CreatorCon Call for Content is officially open! Get started here.

Need help with the script to calculate the duration between 2 dates

Rohit8
Tera Expert

Hello,

Wish you all a very Happy New Year.

I am trying to build an Indicator to calculate the Summed age of Assets (to later find out the Average age). The asset table has the columns for Release Date and Retired Date and I need to calculate their difference. The screenshot (A) has the format of the dates.

The 2nd screenshot has the script screen and I am using the above variables. I am not proficient with scripts at all so I am pretty sure I am doing something totally wrong.

var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};

var years=function(x,y){return diff(x,y)/(365*24*60*60*1000);};

years(current.u_avg_hw_release_date, current.retired_date_at);

The job returns the error:

Error during JavaScript evaluation com.snc.pa.dc.ScriptException: JavaScript evaluation returned: NaN in script: var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};

var years=function(x,y){return diff(x,y)/(365*24*60*60*1000);};

years(current.u_avg_hw_release_date, current.retired_date_at);

at com.snc.pa.dc.Script.handleException(Script.java:215)

at com.snc.pa.dc.Script.evaluate(Script.java:179)

at com.snc.pa.dc.Script.evaluate(Script.java:156)

at com.snc.pa.dc.DataCollector.collect(DataCollector.java:261)

at com.snc.pa.dc.DataCollector.collect(DataCollector.java:214)

at com.snc.pa.dc.DataCollector.collect(DataCollector.java:172)

at com.snc.pa.dc.DataCollectorJob.collect(DataCollectorJob.java:246)

at com.snc.pa.dc.DataCollectorJob.collectWithMutex(DataCollectorJob.java:162)

at com.snc.pa.dc.DataCollectorJob.execute(DataCollectorJob.java:147)

at com.glide.schedule.JobExecutor.executeJob(JobExecutor.java:103)

at com.glide.schedule.JobExecutor.execute(JobExecutor.java:89)

at com.glide.schedule.GlideScheduleWorker.executeJob(GlideScheduleWorker.java:219)

at com.glide.schedule.GlideScheduleWorker.lambda$process$39(GlideScheduleWorker.java:161)

at com.glide.worker.TransactionalWorkerThread.executeInTransaction(TransactionalWorkerThread.java:35)

at com.glide.schedule.GlideScheduleWorker.process(GlideScheduleWorker.java:161)

at com.glide.schedule.GlideScheduleWorker.run(GlideScheduleWorker.java:72)

Caused by: java.lang.Exception: JavaScript evaluation returned: NaN

... 15 more

Can anyone help me correct the script or let me know what am I doing wrong.

Thanks a lot.

1 ACCEPTED SOLUTION

Last try



var sgd1 = new GlideDate();  


var sgd2 = new GlideDate();  


sgd1.setDisplayValue(current.retired.getDisplayValue());  


sgd2.setDisplayValue(current.u_abg_hw_release_date.getDisplayValue());  


var duration = GlideDate.subtract(sgd1, sgd2);  


Math.floor(duration.getDayPart()/365);  


 



Cheers,


Thilo


View solution in original post

12 REPLIES 12

Last try



var sgd1 = new GlideDate();  


var sgd2 = new GlideDate();  


sgd1.setDisplayValue(current.retired.getDisplayValue());  


sgd2.setDisplayValue(current.u_abg_hw_release_date.getDisplayValue());  


var duration = GlideDate.subtract(sgd1, sgd2);  


Math.floor(duration.getDayPart()/365);  


 



Cheers,


Thilo


Thank you so much for your help so far.



So, I saved the script and ran the job and finally it ran without any errors (but it did have warnings) !!



When I checked the Indicator scorecard, it doesn't seem to have any score at all and has 0 instead (because of what I selected for Value when Nil).



21.JPG



222.JPG



22223.JPG



Data Collector logs:



SequenceSortCreatedSortLevelSortMessageSortSourceSort
125

07-Jan-18 01:32:31


WarningNo data was collected between 01-Jan-08 and 31-Dec-08 for indicator "Summed age of Assets (Desktops)" in the indicator source "Assets.Workstations.Yearly". Data within this range is older than the maximum age, based on the indicator frequency and collection periods.DataCollector
88

07-Jan-18 01:32:29


WarningNo list of records (snapshot) was collected between 01-Jan-04 and 01-Jan-11 for indicator source "Assets.Workstations.Yearly". Snapshot within this range is older than the maximum age, based on indicator frequency and snapshot periods, for all indicators associated with this indicator source.DataCollector
126

07-Jan-18 01:32:31


WarningNo data was collected between 01-Jan-04 and 01-Jan-08 for indicator source "Assets.Workstations.Yearly". Data within this range is older than the maximum age, based on indicator frequency and collection periods, for all indicators associated with this indicator source.DataCollector
87

07-Jan-18 01:32:29


WarningNo list of records (snapshot) was collected between 01-Jan-11 and 31-Dec-11 for indicator "Summed age of Assets (Desktops)" in the indicator source "Assets.Workstations.Yearly". Snapshot within this range is older than the maximum age, based on the indicator frequency and snapshot periods.DataCollector
122

07-Jan-18 01:32:31


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
22

07-Jan-18 01:32:10


InformationDeleted previous results 0DataCollector
61

07-Jan-18 01:32:24


InformationStored collected resultsDataCollector
6

07-Jan-18 01:31:58


InformationFetched 20,538 rows from alm_hardwareDataCollector
130

07-Jan-18 01:32:31


InformationAnalyzed table pa_snapshots successfullyDataCollector
50

07-Jan-18 01:32:21


InformationCollecting for 20150101 finishedDataCollector
8

07-Jan-18 01:32:04


InformationApplied map/reduce functionDataCollector
67

07-Jan-18 01:32:24


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
103

07-Jan-18 01:32:30


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2010-12-31'DataCollector
82

07-Jan-18 01:32:29


InformationDeleted previous results 0DataCollector
100

07-Jan-18 01:32:30


InformationCollecting for 20110101 finishedDataCollector
109

07-Jan-18 01:32:31


InformationStoring collected results for indicator source Assets.Workstations.YearlyDataCollector
23

07-Jan-18 01:32:10


InformationStoring collected results for indicator source Assets.Workstations.YearlyDataCollector
110

07-Jan-18 01:32:31


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
38

07-Jan-18 01:32:16


InformationCollecting for 20160101 finishedDataCollector
107

07-Jan-18 01:32:31


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
132

07-Jan-18 01:32:31


InformationCollection endedDataCollector
51

07-Jan-18 01:32:21


InformationCollecting for 20140101DataCollector
9

07-Jan-18 01:32:04


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
78

07-Jan-18 01:32:27


InformationFetched 7,255 rows from alm_hardwareDataCollector
108

07-Jan-18 01:32:31


InformationDeleted previous results 0DataCollector
91

07-Jan-18 01:32:29


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2011-12-31'DataCollector
113

07-Jan-18 01:32:31


InformationCollecting for 20090101DataCollector
117

07-Jan-18 01:32:31


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
106

07-Jan-18 01:32:31


InformationApplied map/reduce functionDataCollector
114

07-Jan-18 01:32:31


InformationFetching "model.u_subcategory,sys_id,u_avg_hw_release_date,install_status,retired" from "alm_hardware"DataCollector
111

07-Jan-18 01:32:31


InformationStored collected resultsDataCollector
13

07-Jan-18 01:32:04


InformationStored collected resultsDataCollector
1

07-Jan-18 01:31:56


InformationStarting collectionDataCollector
10

07-Jan-18 01:32:04


InformationDeleted previous results 0DataCollector
42

07-Jan-18 01:32:16


InformationFetched 16,790 rows from alm_hardwareDataCollector
116

07-Jan-18 01:32:31


InformationFetched 319 rows from alm_hardwareDataCollector
92

07-Jan-18 01:32:30


InformationFetched 3,355 rows from alm_hardwareDataCollector
30

07-Jan-18 01:32:11


InformationFetched 19,485 rows from alm_hardwareDataCollector
97

07-Jan-18 01:32:30


InformationStoring collected results for indicator source Assets.Workstations.YearlyDataCollector
115

07-Jan-18 01:32:31


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2009-12-31'DataCollector
31

07-Jan-18 01:32:11


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
129

07-Jan-18 01:32:31


InformationStarting to analyze table pa_snapshotsDataCollector
120

07-Jan-18 01:32:31


InformationDeleted previous results 0DataCollector
54

07-Jan-18 01:32:21


InformationFetched 12,346 rows from alm_hardwareDataCollector
12

07-Jan-18 01:32:04


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
17

07-Jan-18 01:32:04


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2017-12-31'DataCollector
55

07-Jan-18 01:32:21


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
96

07-Jan-18 01:32:30


InformationDeleted previous results 0DataCollector
74

07-Jan-18 01:32:27


InformationCollecting for 20130101 finishedDataCollector
101

07-Jan-18 01:32:30


InformationCollecting for 20100101DataCollector
105

07-Jan-18 01:32:31


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
60

07-Jan-18 01:32:24


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
98

07-Jan-18 01:32:30


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
59

07-Jan-18 01:32:24


InformationStoring collected results for indicator source Assets.Workstations.YearlyDataCollector
56

07-Jan-18 01:32:24


InformationApplied map/reduce functionDataCollector
2

07-Jan-18 01:31:56


InformationProcessing Indicator Source Assets.Workstations.YearlyDataCollector
90

07-Jan-18 01:32:29


InformationFetching "model.u_subcategory,sys_id,u_avg_hw_release_date,install_status,retired" from "alm_hardware"DataCollector
45

07-Jan-18 01:32:20


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
19

07-Jan-18 01:32:04


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
104

07-Jan-18 01:32:31


InformationFetched 1,427 rows from alm_hardwareDataCollector
3

07-Jan-18 01:31:58


InformationCollecting for 20180101DataCollector
69

07-Jan-18 01:32:27


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
26

07-Jan-18 01:32:10


InformationCollecting for 20170101 finishedDataCollector
64

07-Jan-18 01:32:24


InformationFetching "model.u_subcategory,sys_id,u_avg_hw_release_date,install_status,retired" from "alm_hardware"DataCollector
102

07-Jan-18 01:32:30


InformationFetching "model.u_subcategory,sys_id,u_avg_hw_release_date,install_status,retired" from "alm_hardware"DataCollector
39

07-Jan-18 01:32:16


InformationCollecting for 20150101DataCollector
65

07-Jan-18 01:32:24


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2013-12-31'DataCollector
81

07-Jan-18 01:32:29


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
84

07-Jan-18 01:32:29


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
49

07-Jan-18 01:32:20


InformationStored collected resultsDataCollector
16

07-Jan-18 01:32:04


InformationFetching "model.u_subcategory,sys_id,u_avg_hw_release_date,install_status,retired" from "alm_hardware"DataCollector
21

07-Jan-18 01:32:10


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
124

07-Jan-18 01:32:31


InformationCollecting for 20090101 finishedDataCollector
73

07-Jan-18 01:32:27


InformationStored collected resultsDataCollector
68

07-Jan-18 01:32:27


InformationApplied map/reduce functionDataCollector
32

07-Jan-18 01:32:16


InformationApplied map/reduce functionDataCollector
62

07-Jan-18 01:32:24


InformationCollecting for 20140101 finishedDataCollector
33

07-Jan-18 01:32:16


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
93

07-Jan-18 01:32:30


InformationApplying map/reduce function for indicator source Assets.Workstations.YearlyDataCollector
14

07-Jan-18 01:32:04


InformationCollecting for 20180101 finishedDataCollector
24

07-Jan-18 01:32:10


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
89

07-Jan-18 01:32:29


InformationCollecting for 20110101DataCollector
44

07-Jan-18 01:32:20


InformationApplied map/reduce functionDataCollector
25

07-Jan-18 01:32:10


InformationStored collected resultsDataCollector
20

07-Jan-18 01:32:10


InformationApplied map/reduce functionDataCollector
119

07-Jan-18 01:32:31


InformationDeleting previous results for indicator source Assets.Workstations.YearlyDataCollector
77

07-Jan-18 01:32:27


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2012-12-31'DataCollector
41

07-Jan-18 01:32:16


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2015-12-31'DataCollector
63

07-Jan-18 01:32:24


InformationCollecting for 20130101DataCollector
127

07-Jan-18 01:32:31


InformationStarting to analyze table pa_scoresDataCollector
37

07-Jan-18 01:32:16


InformationStored collected resultsDataCollector
85

07-Jan-18 01:32:29


InformationStored collected resultsDataCollector
18

07-Jan-18 01:32:04


InformationFetched 20,538 rows from alm_hardwareDataCollector
28

07-Jan-18 01:32:10


InformationFetching "model.u_subcategory,sys_id,u_avg_hw_release_date,install_status,retired" from "alm_hardware"DataCollector
86

07-Jan-18 01:32:29


InformationCollecting for 20120101 finishedDataCollector
53

07-Jan-18 01:32:21


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2014-12-31'DataCollector
72

07-Jan-18 01:32:27


InformationStored 'Value when nil': 0.0 as score for indicator: Summed age of Assets (Desktops)DataCollector
29

07-Jan-18 01:32:10


InformationSELECT alm_hardware0.`sys_id` FROM ((alm_hardware alm_hardware0 INNER JOIN alm_asset alm_asset0 ON alm_hardware0.`sys_id` = alm_asset0.`sys_id` ) LEFT JOIN cmdb_model cmdb_model1 ON alm_asset0.`model` = cmdb_model1.`sys_id` ) WHERE alm_asset0.`install_status` IN (6 , 1) AND cmdb_model1.`u_subcategory` IN ('Desktops' , 'Laptop' , 'Notebooks') AND alm_asset0.`u_avg_hw_release_date` <= '2016-12-31'DataCollector
123

07-Jan-18 01:32:31


InformationStored collected resultsDataCollector
118

07-Jan-18 01:32:31


InformationApplied map/reduce functionDataCollector

Bottom of table




I have been getting the errors about Maximum age when I try to go further back than 2010 with other indicators, but they do return the latest results from last 6-7 years. And there are Assets in the table with the Release and Retired Data both from last 6-7 years,



Thank you.


Thilo,



I figured it out. I checked my Indicator trail and it turned out that the 0 result was due to the Indicator source not configured to include the "Retired" machines. It was only set up to include the "In stock" or "In use" machines. Hence, it was not able to use the Retired machines in the calculation thus had nothing to calculate.



After correcting that, your script worked like a charm !!


In fact, the script I provided initially worked fine too after correcting the field names.



var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};


var years=function(x,y){return diff(x,y)/(365*24*60*60*1000);};


years(current.u_avg_hw_release_date,current.retired);



I can't thank you enough for hanging through this, with me.



Sorry for all the headache I might've caused you.