Skip to content

Instantly share code, notes, and snippets.

View sahglie's full-sized avatar

Steven Hansen sahglie

View GitHub Profile
@sahglie
sahglie / db.md
Last active October 4, 2022 16:33
DB Optimization

This questions is a db design/optimization question. We're going to start off with some background. At UCB users connect to the campus wifi system via an access point. Connecting to an access point creates a wifi_accounting event. Throughout a user's time on wifi, wifi_accounting events will continue be created every 5-30 seconds. On average, we get about 20 million such events per day.

The following table represents the data we capture for each wifi_accounting event:

wifi_accounting
------------------------------
ip              INET
username        VARCHAR
mac MACADDR
@sahglie
sahglie / question.md
Last active October 4, 2022 16:34
programming interview question

Instructions

You are going to write a function that can validate a chart-string identification number. A chart-string number normally contain dashes and looks like: 3-598-21508-8

Background

The chart-string format is 9 digits (0 to 9) plus one check character (either a digit or an X only). In the case the check character is an X, this represents the value '10'. These may be communicated with or without hyphens. In addition to conforming to the above format, a chart-string must also be validated by the following formula:

(x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0

Keybase proof

I hereby claim:

  • I am sahglie on github.
  • I am runner_berkeley (https://keybase.io/runner_berkeley) on keybase.
  • I have a public key whose fingerprint is 662F 1A24 6B11 3909 002F 46E7 1841 E608 786B B4B5

To claim this, I am signing this object:

#!/usr/bin/env ruby
require_relative "../config/environment"
require 'airbrake/logger'
Rails.logger = Airbrake::AirbrakeLogger.new(Rails.logger)
class One
Error = Class.new(StandardError)
def self.run
@sahglie
sahglie / dhcp_consumer.rb
Created October 26, 2018 17:47
Racecar config
class DhcpConsumer < Racecar::Consumer
self.group_id = "sock-dhcp-#{Rails.env.to_s}"
TOPICS = ["dhcp-infoblox"]
subscribes_to(*TOPICS, start_from_beginning: false, max_bytes_per_partition: 250_000)
MAX_THREADS = 5
def initialize
Setting Up A Localhost Self-signed Certificate For Local Https Development
==========================================================================
- GENERATE THE CERTIFICATE
# 1. generate key
- create an 'ssl' folder somewhere
- cd to it
_.extend Backbone.Validation.callbacks,
valid: (view, attr, selector) ->
control = view.$('[' + selector + '=' + attr + ']')
group = control.parents(".control-group")
group.removeClass("error")
if control.data("error-style") == "tooltip"
# CAUTION: calling tooltip("hide") on an uninitialized tooltip
# causes bootstraps tooltips to crash somehow...
control.tooltip "hide" if control.data("tooltip")
require 'net/smtp'
require 'pp'
module Net
class SMTPMailToError < ProtoServerError
attr_accessor :emails
def initialize(emails=[])
@emails = emails
end
@sahglie
sahglie / gist:c1f5c1c73485dc7681d7
Created November 14, 2014 00:59
regex with named captures
require 'time'
require 'pp'
ALERT_TOKENS_REGEX = /
<\d+>
(?<alert_ts>[a-zA-z]{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})\s
.+\s
(?<name>SNS-ALERT-CAT[1-3].+)\s
{(?<protocol>.+)}\s
(?<src_ip>(?:\d+\.){3}\d+):(?<src_port>\d+)
SELECT schemaname, relname,
seq_scan as table_scans,
idx_scan as index_scans,
pg_size_pretty(pg_relation_size(relid)) as table_size,
n_tup_ins + n_tup_del + n_tup_upd + n_tup_hot_upd as write_activty
FROM pg_stat_user_tables
WHERE seq_scan > 1000
AND seq_scan > ( idx_scan / 10 )
AND pg_relation_size(relid) > ( 16000000 )
ORDER BY pg_relation_size(relid) desc;