compare two integers using groovy in jenkins pipeline script - jenkins

I am trying to compare two integers in jenkins script using groovy. I am getting different response each time. My doubt is how to convert the string to integer then compare it and then produce the desired output.
my script looks like this:
pipeline {
agent any
stages {
stage('checkout') {
steps {
script{
dec1='5.11.03'
dec2='5.9.06'
a=dec1.split("\\.")[1]
b=dec2.split("\\.")[1]
c='10'
one="$a" as Integer
two="$b" as Integer
three="$c" as Integer
println("$one")
println("$two")
println("$one".compareTo(10))
println("$two".compareTo(10))
list1 = ("$one" >= "$three") ? 'total 7 repos' : 'total 4 repos'
list2 = ("$two" >= "$three") ? 'total 7 repos': 'total 4 repos'
println("the result for $dec1 is $list1")
println("the result for $dec2 is $list2")
}
}
}
}
}
Here I am trying to compare the second part of a decimal digit and check if it is greater than 10. If it is greater than 10 it must print 'total 7 repos' or print 'total 4 repos'. I did also try using compareTo() which is producing different result. can anyone please help me with this. Thanks in advance.
The output i am getting is:
11
[Pipeline] echo
9
[Pipeline] echo
1
[Pipeline] echo
8
[Pipeline] echo
the result for 5.11.03 is total 7 repos
[Pipeline] echo
the result for 5.9.06 is total 7 repos

The problem here is that you are using quotes and $ symbols around your variables, but that is incorrect syntax. Groovy follows a Java-like syntax, and you can check the doc here on Groovy variables:
https://groovy-lang.org/semantics.html#_variable_assignment
Basically, your "" and $ are the problem.
Switch your lines 11-14 with:
println(one.compareTo(10))
println(two.compareTo(10))
list1 = (one >= three) ? 'total 7 repos' : 'total 4 repos'
list2 = (two >= three) ? 'total 7 repos': 'total 4 repos'
Should solve the problem.

Related

grep, sum and average from large output

I have an output file called filename.mat0 which contains a large list of data points for a number of different variables for a number of different time steps. I want to use something like the grep command to retrieve all the instances for a given variable, i.e. variable_A, then sum the total value associated with variable_A, then take an average.
The number of time steps is constant so variable_A, variable_B, etc all appear 100 times in my .mat file.
Please can you suggest the best way to do this?
An example of the output data is:
Timestep1 Variable_A 10
Timestep1 Variable_B 20
Timestep1 Variable_C 30
Timestep2 Variable_A 40
Timestep2 Variable_B 50
Timestep2 Variable_C 60
Timestep3 Variable_A 70
Timestep3 Variable_B 80
Timestep3 Variable_C 90
Desired output:
Variable_A = 40
Referencing to this.
awk should be able to solve the problem. Check the link for how to use awk.
The below command should be okay for your case, but it is not easy to use if there is many Variable .Hope anyone more familiar with awk can suggest how to improve.
awk '{if ($2 == "Variable_A"){ total += $3; count++ }} END { print "Variable_A = " total/count }' sample.mat > avg_a.txt
Above command will do for each row, check if column 2(correspond to $2) equals "Variable_A", if yes, sum the value in column 3(correspond to $3) and add a count. After processing all rows, print the average to a text file.
For further question
In order to show multiple variables average in same file, you can make use of array and for loop in AWK. Add elements to vars for more variables.
awk 'BEGIN {vars[0]="Variable_A"; vars[1]="Variable_B"; vars[2] ="Variable_C" } { for (i in vars) { if ($2 == vars[i]){ total[i] += $3; count[i]++ }}} END { for(i in vars) {print vars[i]" = " total[i]/count[i]}}' sample.mat > avg.txt

Printing k, v from a defaultdict(list)

I have a program that connects via SSH (Paramiko library) to a Cisco Wireless
LAN Controller (WLC). I then run a 'show client summary' and parse\process
the output to generate a report.
Everything works except the printing.
NOTE: 'e' is a dictionary created with: defaultdict(list)
If I use this:
for k, v in e.items():
print('{:25}'.format(k), end='')
for i in v:
print('{:5}'.format(i), end='')
print("\n")
The output looks like this:
AP Count
------------------------------
AP0027.e3f1.9208 8 7 6
AP70df.2f42.3450 1 1 1
AP25-AthleticOffice 4 4 3
AP70df.2f74.9868 1 1 1
AP70df.2f42.3174 2 2 2
I don't want the extra blank line between the data lines.
But if I simply get rid of the last line: print("\n"),
then I get this format for the output:
AP0027.e3f1.9208 8 7 6AP70df.2f42.3450 1 1 1AP25-AthleticOffice 4 4 3AP70df.2f42.3174 1 1 1AP70df.2f42.3174 2 2 2
No carriage return.
I am either getting zero carriage return or two.
This happens because print() already appends the end character - which is \n by default. You can fix it by printing just an empty string (which is the same as print('', end='\n')):
for k, v in e.items():
print('{:25}'.format(k), end='')
for i in v:
print('{:5}'.format(i), end='')
print('')

Performing exact match when comparing variables in SPSS Statistics

I'm wondering if there's a way for me to perform an exact match compare in SPSS. Currently, using the following will return system missing (null) in cases where one variable is sysmis:
compute var1_comparison = * Some logic here.
compute var1_check = var1 = var1_comparison.
The results look like this (hypens representing null values):
ID var1 var1_comparison var1_check
1 3 3 1
2 4 3 0
3 - 2 -
4 1 1 1
5 - - -
What I want is this:
ID var1 var1_comparison var1_check
1 3 3 1
2 4 3 0
3 - 2 0
4 1 1 1
5 - - 1
Is this possible using just plain SPSS syntax? I'm also open to using the Python extension, though I'm not as familiar with it.
Here's a slightly different approach, using temporary scratch variables (prefixed by a hash (#)):
recode var1 var1_comparison (sysmis=-99) (else=copy) into #v1 #v2.
compute Check=(#v1 = #v2).
This is to recreate your example:
data list list/ID var1 var1_comparison.
begin data
1, 3, 3
2 , 4, 3
3, , 2
4, 1, 1
5, ,
end data.
Now you have to deal separately with the situation where both values are missing, and then complete the calculation in all other situations:
do if missing(var1) or missing(var1_comparison).
compute var1_check=(missing(var1) and missing(var1_comparison)).
else.
compute var1_check = (var1 = var1_comparison).
end if.

JQL for this condition

how would the JQL look like for this condition?:
Generate a report of all HIGH severity JIRA tickets for a project with key X (or name X) that were created from 9 PM EST to 12 AM EST from the start of the year?
I tried something like :
Project = X AND Severity = "HIGH" AND created > "2015/01/01 21:00" and created < "2015/09/09",
but I need only those issues that are created between 9 PM and 12 AM everyday, from the beginning of the year.
Any ideas would be greatly appreciated.
Unfortunately there seems to be no tool to get the hour from created date but you can workaround it.
My two ideas are:
find these tickets directly on Jira database (if you have access) it should be very easy since there are functions like hour (mySQL) or truncate (postgres)
prepare JQL filter using some generator script - it is definetely less comfortable but possible to achieve even when you have not acces to database. The worst thing is that Jira filter fields accepts only 2000 characters string so you would need to copy that filter few lines by few lines.
Little crazy but ok - it works so what's the idea? The idea is to use startOfYear() JQL function and its *offset version**. For example:
created >= startOfYear(21h) and created < startOfYear(24h)
will give you all tickets from 1 Jan 21:00 - 2 Jan 00:00
then you can use this Python script:
step = 27
maxDay = 1
while maxDay <= 365 + step:
maxDay += step
output = "project = X and Severity = HIGH and ("
for i in range(maxDay-step, maxDay):
output += " (created >= startOfYear(" + str( ( (i-1) * 24 ) + 21) + "h) and created < startOfYear(" + str(i*24) + "h)) or"
output = output[:-3]
output += ")"
print output
print
which will generate you set of JQL requests to copy-paste and execute (it is actually 15 of them - you can see here). Every set bounds 28 days because of 2000 limit of filter input in Jira.
I fixed this issue by writing a custom JQL function and then using that function with a JQL query, which fits well with our requirements :
created >= "2015-01-01" and created <= "2015-12-31" and issue in getIssuesForTimeRange("21", "24")

How do I get a random number in template toolkit?

I want to get a random number using template toolkit. It doesn't have to be particularly random. How do I do it?
Hmm, you might have issues if you don't have (or cannot import) Slash::Test.
From a "vanilla" installation of TT, you can simply use the Math plugin:
USE Math;
GET Math.rand; # outputs a random number from 0 to 1
See this link in the template toolkit manual for more information on the Math plugin and the various methods.
Update: Math.rand requires a parameter. Therefore to get a random number from 0 to 1, use:
GET Math.rand(1);
From this post at Slashcode:
[slash#yaz slash]$ perl -MSlash::Test -leDisplay
[%
digits = [ 0 .. 9 ];
anumber = digits.rand _ digits.rand _ digits.rand;
anumber;
%]
^D
769

Resources