Control Structures

The following control structures are supported: list, var, for, if.

list / endlist

Assigns a list of comma separated elements, optionally located on multiple lines, to a named array which can be iterated with a for loop.

list servers = awsswgvml001, nurswgvml003, 
     nurswgvml006, nurswgvml007, nurswgvml009
endlist

If the elements fit on one line, the endlist command is not required.

list servers = awsswgvml001, nurswgvml003
[widget]

The list command is similar to var, without having to quote the elements and enclose them in square brackets:

var servers = ['awsswgvml001', 'nurswgvml003']
list servers = awsswgvml001, nurswgvml003

var / endvar

var assigns an array, an object, or a function to a variable whose value and fields can be accessed with a placeholder.
If the var assignment on is one line, closing with the endvar command is not required.

var disks = [[9,2], [9,3], [8,0], [9,0], [9,1], [8,16]]
for di in disks
    [series]
      [tags]
      major = @{di[0]}
      minor = @{di[1]}
endfor

The list of entities can be loaded into a var  array from the server, using the getEntities function.

var hosts = getEntities('svl-hosts')
for host in hosts
    [series]
      entity = @{host}
endfor

for / endfor

Iterates over a list (created with the list command), an array (declared with the var command or specified inline), or variable properties.
To access the current element use the @{elementname} placeholder syntax, or in simple cases, @elementname.
Multiple for or endfor loops can be nested.

Example: Iterating over a list.

list servers = awsswgvml001, nurswgvml003
for server in servers
	[series] 
		entity = @{server}
endfor

Example: Iterating over an inline array.

for server in ['awsswgvml001', 'nurswgvml003']
	[series]
		entity = @{server}
endfor

Example: Iterating over all object properties. Order is arbitrary when using the Object.keys function.

var tags = {
  'level': 'ERROR',
  'command': 'com.axibase.tsd.Server',
  'logger': 'com.axibase.tsd.web.DefaultExceptionHandler'
}
endvar
 
  [widget]
    type = chart
    [series]
      [tags]
        for tag_name in Object.keys(tags):
          @{tag_name} = @{tags[tag_name]}
        endfor

Example: Iterating over objects with named fields.

var servers = [
  {'name': 'nurswgvml001', 'networks': ['en3', 'en4']},
  {'name': 'nurswgvml002', 'networks': ['en5']}
]
endvar
 
for server in servers
  [widget] 
    type = box
    entity = @{server.name}
    metric = network_sent_mb  
    for net in server.networks
    [series]
      [tags]
       interface = @{net}  
    endfor
endfor

Example: Iterating over object properties. Order is arbitrary.

var servers = {
    'nurswgvml001': ['en3', 'en4'],
    'nurswgvml002': ['en5']
}
endvar
 
for server in servers
[widget] 
  type = box 
  entity = @{server} 
  for net in servers[server]
  [series]
    [tags]
      interface = @{net}  
  endfor
endfor

Example: Iterating over a multi-dimensional array.

var servers = [
      ['nurswgvml001', ['en3', 'en4']],
      ['nurswgvml002', ['en5']]
  ]
endvar
 
for server in servers
  [widget] 
    type = box
    entity = @{server[0]}
    metric = network_sent_mb  
    for net in server[1]
    [series]
      [tags]
       interface = @{net}  
    endfor
endfor

elementname_index (no @ control symbol and brackets) can be used to access current iterator index starting with 0.

Example: Insert [group] line for each 4th element in the array.

for server in servers
  #add [group] before every 4th element to display 4 widget per row
  if server_index % 4 == 0
  [group]
  endif
 
  [widget]
    entity = @{server}
endfor

Similar layout with automated row breaks can be accomplished by specifying the widgets-per-row setting under the group tag:

[group]
  widgets-per-row = 4
for server in servers
  [widget]
    entity = @{server}
endfor

Text inside placeholders @{elementname} is evaluated as an expression and can be used for concatenation and formatting:

list servers = 001, 003, 006, 007, 009
for server in servers
  [series]
    entity = @{'nurswgvml' + server}
endfor

if / elseif / else / endif

Evaluates an expression and prints out settings if the expression is true.
If the expression returns false, content located within the branch is excluded from the widget configuration.
Array elements are accessed in the if or elseif expression by name, not as a placeholder.

for server in servers
  [series]
    entity = @{server}
    if server == 'nurswgvml007'
      color = red
    elseif server == 'nurswgvml006'
      color = yellow
    endif
endfor

Troubleshooting

To review the final configuration text after pre-processing, add the script = console.log(widgetConfig) anywhere in the widget configuration and review the text in Developer Console (F12) -> Console tab.

script = console.log(widgetConfig)