Skip to content

Commit

Permalink
Fix rendering of metrics with special characters
Browse files Browse the repository at this point in the history
My main motivation here is to get metrics with brackets to work in order
to support "pytest" test names:

- test_foo.py::test_bar[1]
- test_foo.py::test_bar[2]

I couldn't find an "HTML escape"-style function that would generate
valid html `id` attribute names from random strings, so I went with a
hash digest instead.
  • Loading branch information
knedlsepp committed Feb 27, 2025
1 parent 18c0d76 commit f7b6fed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib/Hydra/Controller/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Hydra::Helper::CatalystUtils;
use JSON::MaybeXS;
use Net::Prometheus;


sub job : Chained('/') PathPart('job') CaptureArgs(3) {
my ($self, $c, $projectName, $jobsetName, $jobName) = @_;

Expand Down
7 changes: 7 additions & 0 deletions src/lib/Hydra/View/TT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use base 'Catalyst::View::TT';
use Template::Plugin::HTML;
use Hydra::Helper::Nix;
use Time::Seconds;
use Digest::SHA qw(sha1_hex);

__PACKAGE__->config(
TEMPLATE_EXTENSION => '.tt',
Expand All @@ -25,8 +26,14 @@ __PACKAGE__->config(
makeNameTextForJobset
relativeDuration
stripSSHUser
metricDivId
/]);

sub metricDivId {
my ($self, $c, $text) = @_;
return "metric-" _ sha1_hex($text);
}

sub buildLogExists {
my ($self, $c, $build) = @_;
return 1 if defined $c->config->{log_prefix};
Expand Down
3 changes: 1 addition & 2 deletions src/root/job-metrics-tab.tt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

<h3>Metric: <a [% HTML.attributes(href => c.uri_for('/job' project.name jobset.name job 'metric' metric.name)) %]><tt>[%HTML.escape(metric.name)%]</tt></a></h3>

[% id = "metric-" _ metric.name;
id = id.replace('\.', '_');
[% id = metricDivId(metric.name);
INCLUDE createChart dataUrl=c.uri_for('/job' project.name jobset.name job 'metric' metric.name); %]

[% END %]
Expand Down

0 comments on commit f7b6fed

Please sign in to comment.