Is the report format a user selects accessible in a report template

I’m customizing the existing “Jobs - Invocation report template” and have found a way to make everything look the way I want. For an HTML report it involves injecting html style codes. The problem is that the codes show up in the output of csv, json, and yaml reports. Is there a variable that is accessible in the report template to indicate what format the user chose?

Foreman and Proxy versions:
Foreman: 2.5.2
Proxy: 2.5.2

Foreman and Proxy plugin versions:
|foreman-tasks |4.1.2|
|foreman_remote_execution |4.5.1|

Dynflow

Version 0.3.0

Registration

Version 2.5.2

SSH

Version 0.3.2

Distribution and version:
Ubuntu 20.04.2

Yes, looks like you can use report_format.id to check the format before calling report_render.

report_render itself uses it here:

1 Like

Thank you so much. That gave me exactly what I was looking for.

1 Like

@plazarch please consider sharing back what you ended up building, nicer styling would probably be appreciated by more people :slight_smile: or consider writing a blog post about this, not everyone figured out they can add some CSS and make their reports nicer or even branded.

Glad to. It isn’t an elegant solution. I re-arranged the report a little bit so there is only 1 row per host and all output is put in a single cell. I then wanted to compress any extra lines in that cell. The CSS white-space:pre-wrap worked great. Unfortunately since the html isn’t injected in the proper place it affects all cells in the table, but the final output works for what I wanted. Here is my complete Jobs - Invocation report template:

<%#
name: Jobs - Invocation report template
snippet: false
template_inputs:
- name: job_id
  required: true
  input_type: user
  description: ID of job to report
  advanced: false
  value_type: plain
  resource_type: JobInvocation
  hidden_value: false
- name: hosts
  required: false
  input_type: user
  description: Field for filter hosts of job invocation. Leave blank for all hosts.
  advanced: false
  value_type: search
  resource_type: Host
  hidden_value: false
model: ReportTemplate
require:
- plugin: foreman_remote_execution
 version: 4.4.0
-%>
<%- invocation = find_job_invocation_by_id(input('job_id')) -%>
<%- parts = ["job_invocation.id = #{input('job_id')}"] %>
<%- parts << input('hosts') unless input('hosts').blank? %>
<%- search = parts.map { |part| '(' + part + ')' }.join(' AND ') %>
<%- load_hosts(search: search).each do |batch| -%>
<%-   batch.each do |host|  -%>
<%-     myOutput = '' -%>
<%-     myTime = '' -%>
<%-     task = invocation.sub_task_for_host(host) -%>
<%-     task.action_continuous_output.each do |output| -%>
<%-       myTime = output['timestamp'] -%>
<%-       myOutput += output['output'] %>
<%-     end -%>
<%-     if report_format&.id == :html %>
<%-       myOutput = '<style>td{ white-space:pre-wrap; font-size:14px; font-weight: normal; }</style>' + myOutput %>
<%-     end %>
<%-     report_row(
          Host: host.name,
          Result: task.result,
          Time: format_time(myTime),
          Message: myOutput.html_safe,

        ) -%>

<%-   end -%>
<%- end -%>
<%= report_render -%>

Nice, thanks for sharing! Btw the default report is about to change to similar structure Fixes #33223 - Improving Job Invocation Report Template by domitea · Pull Request #8711 · theforeman/foreman · GitHub we’ll have single entry for one host and we’ll combine the output.

I think the styling you’re adding may be useful elsewhere too. I’ll think about some cleaner, more native, way of injecting CSS. Also the html format could add some css classes by default to make it easier for styling.