bdmeyer Super Contributor.
Super Contributor.
663 views

Can't Divide a Velocity Template variable against a number

My first stab at using a Velocity Template is failing miserably.

What I want to do is perform:

$deviceCustomNumber2 / 1000 inside of a Custom Column definition.

Below are the bitter fruits of my labor: (Melodramatic) 🙂

I looked here:

http://velocity.apache.org/engine/1.7/user-guide.html#the-mud-store-example

and according to this examples:

#set( $result = $query.criteria("name") )

The result of the first query is $result

#set( $result = $query.criteria("address") )

The result of the second query is $result

it shouldMy example above should work.

I created this example:

#set ($one = 1)

#set ($two= $one + $one)

#set ($thousand = $two + 998)

#set ($testmsec=60 * $thousand)

#set ($maths = $testmsec / $two)

$maths

When I run this I get 30000 as expected

 

Next, I adapt and substitute $deviceCustomNumber2 for 60000 in $testmsec

#set ($testmsec=$deviceCustomNumber2)

#set ($maths = "$testmsec / 1000")

$maths

When I run this I get literal results like:

138674 / 1000

I’ve considered that this may be an issue with not using the proper variable naming convention, but that makes no sense as I AM getting the parsed value (138674 in the example above)

So it’s something to do with how I am encapsulating the portion: (Or that’s today's reasoning!)

"$testmsec / 1000"

I’ve tried:

#set ($maths = ("$testmsec / 1000"))      (no difference)

#set ($maths = ($testmsec / 1000)         (prints $maths)

#set ($maths = \$testmsec / 1000)         (prints $maths)

#set ($maths = '$testmsec / 1000')        (prints #testmsec / 1000)

I am very interested in learning the error of my ways. (At least as far as Velocity templates are concerned.)

Thank-You,

-= Bruce

-= Bruce
Labels (2)
0 Likes
2 Replies
bdmeyer Super Contributor.
Super Contributor.

Re: Can't Divide a Velocity Template variable against a number

I have an answer for this.

I can't take full credit for this, as several people contributed to getting this figured out. Ultimately Brendan Pelarski, and Chris Allen from HP figured this out while teaching me the ASE Course.

I'm providing two answers here. One: The short answer, and the second answer is a full implementation for a single custom column that evaluates all possible Trend conditions and displays the Number of records inserts and the time it took for the Trends Query to run (In Minutes and seconds) instead of msec's. Or it displays a generic message where it doesn't apply. (Like Trend Run Started.)

When i started this question, I was trying to do math on the field:

$deviceCustomNumber2

Which seemed to not be an integer.

Brendan emailed back and forth with Chris Allen from HP, and the discovered it was a LONG integer. That still wasn't enough though.

By reading the Apache Velocity web page, Brendan figured out that the correct variable name I needed was:

$deviceCustomNumber2.intValue()

If all you needed was how to do math on the variable, your done. If you want the full implementation of how I used it for my Custom Column, it is below: (I have not quadruple checked for typo's. It IS working right now, so at a minimum this is good enough to get you going.

<HTML>

  <TABLE>

    <TR> ## New Row

      #if ($deviceEventClassId=="trend:100") ## trend run started

        <TD nowrap><font color="#800000">Trend Run Started</font></TD>

      #elseif ($deviceEventClassId=="trend:101") ## trend run success

       <font color="#800000">Trend Run Success</font>

     

      #elseif ($deviceEventClassId=="trend:501") ##501 trend task started

        <font color="#800000">Trend Task Started</font>

      #elseif ($deviceEventClassId=="trend:502") ## Trend task ended

        <b><font color="#006400">Insert Count: </font>$deviceCustomNumber1 records.</b>

      #elseif ($deviceEventClassId=="trend:102")  ## Trend run failure

        <b><font color="red">An error prevented the trend run from completion</font></b>

      #elseif ($deviceEventClassId=="trend:201") ## trend scavenge success

        <TD nowrap><font color="#4B0082">Trend Scavenge Success.</font></TD>

      #elseif ($deviceEventClassId=="trend:202") ## trend scavenge success

        <TD nowrap><font color="red">Trend Scavenge Failure.</font></TD>

      #elseif ($deviceEventClassId=="trend:401") ## trend scavenge success

        <TD nowrap><font color="#006400">Trend Enabled.</font></TD>

      #elseif ($deviceEventClassId=="trend:402") ## trend scavenge success

        <TD nowrap><font color="red">Trend Disabled.</font></TD>

      #elseif ($deviceEventClassId=="trend:501") ## trend scavenge success

        <TD nowrap><font color="#006400">Trend Scavenge Success.</font></TD>

      #elseif ($deviceEventClassId=="trend:502") ## trend scavenge success

        <TD nowrap><font color="#800000">Trend Task Ended.</font></TD

      #elseif ($deviceEventClassId=="trend:601") ## trend scavenge success

        <TD nowrap><b><font color="red">Trend was automatically deactivated because of too many failures.</font></b></TD

      #elseif ($deviceEventClassId=="trend:701") ## trend scavenge success

        <TD nowrap><font color="#006400">Trend successfully updated an active list.</font></TD

      #end

    </TR>

    <TR>

## Logic for the second line in the table

#set ($leftOfDec = $deviceCustomNumber2.intValue() / 1000/60)

#set ($rightOfDec = $deviceCustomNumber2.intValue() % 1000/60)

#set ($queryTime = "$leftOfDec.$rightOfDec")

## Start with non value success type EventID's

#if ($deviceEventClassId=="trend:100")

<TD nowrap><font color="#800000">(No Query Time or Insert Count for a "Trend Run Started")</font></TD>

#elseif ($deviceEventClassId=="trend:101")

<TD nowrap><font color="#800000">No Query Time or Insert Count for a "Trend Run Success"</font></TD>

#elseif ($deviceEventClassId=="trend:501")

<TD nowrap><font color="#800000">No Query Time or Insert Count for a "Trend Task Started"</font></TD>

#elseif ($deviceEventClassId=="trend:102")

<TD nowrap><font color="red">No Query Time or Insert Count for a "Trend Run Failure"</font></TD>

#elseif ($deviceEventClassId=="trend:502")

<TD nowrap><b><font color="#006400">Query Run Time: </font> $queryTime Minutes.</font></b></TD>

#elseif ($deviceEventClassId=="trend:201")

<TD nowrap><font color="#800000">No Query Time or Insert Count for a "Trend Scavenge Success"</font></TD>

#elseif ($deviceEventClassId=="trend:202")

<TD nowrap><b><font color="red">Trend Scavenge failure"</font></b></TD>

#elseif ($deviceEventClassId=="trend:401")

<TD nowrap><font color="#800000">No Query Time or Insert Count for a "Trend Enabled"</font></TD>

#elseif ($deviceEventClassId=="trend:402")

<TD nowrap><font color="#800000">No Query Time or Insert Count for a "Trend Disabled"</font></TD>

#elseif ($deviceEventClassId=="trend:601")

<TD nowrap><b><font color="red">Trend was automatically deactivated because of too many failures."</font></b></TD>

#elseif ($deviceEventClassId=="trend:701")

<TD nowrap><font color="#006400">Trend successfully updated an active list. 701</font></TD>

#end

   </TR>

</TABLE>

</HTML>

-= Bruce
0 Likes
Gayan Acclaimed Contributor.
Acclaimed Contributor.

Re: Can't Divide a Velocity Template variable against a number

Very helpful.

Mr
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.