Browse Source

Include sample data in email when bad data checks fail

Andrew Kane 7 years ago
parent
commit
f5fdb71300

+ 4 - 0
CHANGELOG.md

@@ -1,3 +1,7 @@
+## 1.7.3 [unreleased]
+
+- Include sample data in email when bad data checks fail
+
 ## 1.7.2
 
 - Cancel all queries on page nav

+ 6 - 1
app/mailers/blazer/check_mailer.rb

@@ -3,13 +3,18 @@ module Blazer
     include ActionView::Helpers::TextHelper
 
     default from: Blazer.from_email if Blazer.from_email
+    layout false
 
-    def state_change(check, state, state_was, rows_count, error)
+    def state_change(check, state, state_was, rows_count, error, columns, rows, column_types, check_type)
       @check = check
       @state = state
       @state_was = state_was
       @rows_count = rows_count
       @error = error
+      @columns = columns
+      @rows = rows
+      @column_types = column_types
+      @check_type = check_type
       mail to: check.emails, reply_to: check.emails, subject: "Check #{state.titleize}: #{check.query.name}"
     end
 

+ 1 - 1
app/models/blazer/check.rb

@@ -58,7 +58,7 @@ module Blazer
 
       # do not notify on creation, except when not passing
       if (state_was != "new" || state != "passing") && state != state_was && emails.present?
-        Blazer::CheckMailer.state_change(self, state, state_was, result.rows.size, message).deliver_later
+        Blazer::CheckMailer.state_change(self, state, state_was, result.rows.size, message, result.columns, result.rows.first(10).as_json, result.column_types, check_type).deliver_later
       end
       save! if changed?
     end

+ 37 - 6
app/views/blazer/check_mailer/state_change.html.erb

@@ -1,6 +1,37 @@
-<p><%= link_to "View", query_url(@check.query_id) %></p>
-<% if @error %>
-  <p><%= @error %></p>
-<% elsif @rows_count > 0 %>
-  <p><%= pluralize(@rows_count, "row") %></p>
-<% end %>
+<html>
+  <head>
+  </head>
+  <body style="font-family: 'Helvetica Neue', Arial, Helvetica; font-size: 14px; color: #333;">
+    <p><%= link_to "View", query_url(@check.query_id) %></p>
+    <% if @error %>
+      <p><%= @error %></p>
+    <% elsif @rows_count > 0 && @check_type == "bad_data" %>
+      <p><%= pluralize(@rows_count, "row") %></p>
+      <p><strong>Sample</strong></p>
+      <table style="width: 100%; border-spacing: 0; border-collapse: collapse;">
+        <thead>
+          <% @columns.first(5).each do |column| %>
+            <th style="padding: 8px; line-height: 1.4; text-align: left; vertical-align: bottom; border-bottom: 2px solid #ddd; width: <%= (100 / @columns.size).round(2) %>%;">
+              <%= column %>
+            </th>
+          <% end %>
+        </thead>
+        <tbody>
+          <% @rows.first(10).each do |row| %>
+            <tr>
+              <% @columns.first(5).each_with_index do |column, i| %>
+                <td style="padding: 8px; line-height: 1.4; vertical-align: top; border-top: 1px solid #ddd;">
+                  <% value = row[i] %>
+                  <% if @column_types[i] == "time" && value.to_s.length > 10 %>
+                    <% value = Time.parse(value).in_time_zone(Blazer.time_zone) rescue value %>
+                  <% end %>
+                  <%= value %>
+                </td>
+              <% end %>
+            </tr>
+          <% end %>
+        </tbody>
+      </table>
+    <% end %>
+  </body>
+</html>