-# we don't really need to do this join, since we only want
-# one row from buildsystems. but it means we only have to run one
-# query. If it gets heavy we'll split it up and run two
-
-my $statement = <<EOS;
-
- select (now() at time zone 'GMT')::timestamp(0) - snapshot as when_ago,
- sysname, snapshot, b.status, stage,
- operating_system, os_version, compiler, compiler_version, architecture,
- owner_email,
- sys_notes_ts::date AS sys_notes_date, sys_notes
- from buildsystems s,
- build_status b
- where name = ?
- and branch = ?
- and s.status = 'approved'
- and name = sysname
- order by snapshot desc
- limit $hm
-
-EOS
+my $systemdata = q{
+ select operating_system, os_version, compiler, compiler_version, architecture,
+ owner_email, sys_notes_ts::date AS sys_notes_date, sys_notes
+ from buildsystems b
+ where b.status = 'approved'
+ and name = ?
+};
+
+my $statement = qq{
+ WITH x AS
+ ( select *
+ from build_status_recent_500
+ where sysname = ?
+ and branch = ?
+ )
+ SELECT (now() at time zone 'GMT')::timestamp(0) - x.snapshot as when_ago,
+ x.sysname, x.snapshot, x.status, x.stage, s.log_text
+ FROM x
+ LEFT JOIN build_status_log s
+ ON x.snapshot = s.snapshot AND s.log_stage = 'test-results.log'
+ ORDER BY x.snapshot desc
+ LIMIT $hm
+}