Skip to content

Instantly share code, notes, and snippets.

WITH a AS
(SELECT calories,
SUM(calories) OVER (PARTITION BY 'calories' ROWS BETWEEN UNBOUNDED preceding AND CURRENT ROW) calories_diff,
CASE
WHEN calories IS NULL THEN (SUM(calories) OVER (PARTITION BY 'calories' ROWS BETWEEN UNBOUNDED preceding AND CURRENT ROW))
ELSE NULL
END AS caloriesdiffwhennull
FROM
(SELECT 1000 AS 'calories'
UNION ALL SELECT 2000
@mbutsko
mbutsko / whits.json
Last active November 29, 2022 21:01
Caramel Custard with a Dash of Sea Salt and Caramel Swirls
10
11
12
12778
12780
12806
12820
12821
12822
12832
class TeamDecider
def should_we_split_v1?(this_date = 10.days.from_now)
is_mike_the_right_choice? && is_the_right_time?(this_date)
end
def should_we_split_on_v2?(this_date = 10.days.from_now)
is_the_right_time?(this_date) && is_mike_the_right_choice?
end
def is_mike_ready?

Redash as Guard Rails for Unexpected Conditions

Production is the best environment to learn things. These are tips to help us learn and grow from production data using Redash.

Using redash queries and alerts as a way to notify your team of unexpected conditions in production can be a great way to provide assurance that expectations are being met and/or error.

Redash can (but should not be) used as a safe guard for data integrity issues. Database constraints and model validations are the best way to go, but especially when thinking about heuristics as warning signs, this is not always possible.

@mbutsko
mbutsko / process_missed_sces.rb
Last active June 16, 2020 14:51
process_missed_sces.rb
def enqueue_missed_scheduled_closure_endorsements
sces = ScheduledClosureEndorsement.ready_for_batch_processing(due_date_range: (Date.new(2020, 5, 1)..Date.new(2020, 6, 13)))
raise "Wrong Count" unless sces.count == 510
sces.each do |sce|
next unless sce.policy_term == sce.policy_term.policy.active_term
ProcessClosureEndorsementJob.perform_later(scheduled_closure_endorsement_id: sce.id)
end
end
@mbutsko
mbutsko / go.rb
Last active June 4, 2020 16:37
Reactivate improperly deactivated scheduled closure endorsements
sql = "
with latest_scheduled_closure_endorsements as ( SELECT * FROM ( select ptr.*, DENSE_RANK() OVER newest_first FROM scheduled_closure_endorsements ptr WINDOW newest_first as (PARTITION BY ptr.policy_term_id ORDER BY ptr.created_at DESC)) _ WHERE DENSE_RANK = 1)
, latest_policy_term_revisions as ( SELECT * FROM ( select ptr.*, DENSE_RANK() OVER newest_first FROM policy_term_revisions ptr WINDOW newest_first as (PARTITION BY ptr.policy_term_id ORDER BY ptr.created_at DESC)) _ WHERE DENSE_RANK = 1)
select sce.id from latest_scheduled_closure_endorsements sce
join policy_term_revisions ptr on ptr.quote_id=sce.quote_id
and ptr.revision_type='endorsement' -- Because you could have reinstated directly on to your target
join scheduled_closure_endorsement_policy_payment_amounts sceppa on sceppa.scheduled_closure_endorsement_id=sce.id
join policy_payment_amounts ppa on ppa.id=sceppa.policy_payment_amount_id
account_id = '4bae2dcd-fc39-4aad-b5b2-b3dcc562995a'
account = Account.find(account_id)
policy = PolicyService.current_policy(:account_id => account.id)
base_quote = policy.active_term.quote
new_profile = ProfileService.update(policy.profile, []).profile
profile_rating_data = EndorsementService.endorsement_profile_rating_data(
:account_id => account_id,
@mbutsko
mbutsko / tables.html
Created January 31, 2018 14:06
Table Width Calculation is Weird
<html>
<head>
<style>
table {
width: 100%;
}
th, td {
border: 1px solid black;
}
@mbutsko
mbutsko / workout peg
Created June 23, 2017 00:01
Basic PEG for Workouts
workout = exercise+
exercise = exercise:word ": " sets:integer "x" reps:integer "\n"? { return { exercise: exercise.join(''), sets: sets, reps: reps } }
word = [A-Za-z]+
integer = [0-9]