Skip to content

Instantly share code, notes, and snippets.

View raymyers's full-sized avatar
🌳
Automating!

Ray Myers raymyers

🌳
Automating!
View GitHub Profile
@raymyers
raymyers / trimmedMean.js
Created February 14, 2020 16:15
Trimmed Mean and Median in ES6.
const mean = (values) => {
let sum = values.reduce((previous, current) => current += previous);
return sum / values.length;
};
const median = (arr) => {
const mid = Math.floor(arr.length / 2);
const sorted = [...arr].sort((a, b) => a - b);
return arr.length % 2 == 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
};
@raymyers
raymyers / index.html
Last active January 29, 2020 07:42
Using diff2html on output of 'diff -u'
<html>
<head>
<!-- CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css" />
<link rel="stylesheet" type="text/css" href="vendor-js/diff2html-3.0.0/bundles/css/diff2html.min.css" />
<!-- Javascripts -->
<script type="text/javascript" src="vendor-js/diff2html-3.0.0/bundles/js/diff2html-ui.min.js"></script>
<script type="text/javascript" src="vendor-js/diff2html-3.0.0/bundles/js/diff2html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
@raymyers
raymyers / AstWalker.java
Created January 7, 2020 23:37
Abstract Tree Walker using Type-safe Heterogeneous Container pattern.
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import java.util.Collection;
import java.util.function.Consumer;
public class AstWalker {
private final Multimap<Class<?>, Consumer<?>> enterNodeListeners = MultimapBuilder.linkedHashKeys().arrayListValues().build();
private final Multimap<Class<?>, Consumer<?>> exitNodeListeners = MultimapBuilder.linkedHashKeys().arrayListValues().build();
@raymyers
raymyers / handler.py
Last active July 17, 2019 19:28
Shutdown EC2 instances off hours with AWS Lambda + Serverless framework + Python
import boto3
region = 'us-east-1'
def find_instance_ids(ec2):
response = ec2.describe_instances(Filters=[
{'Name': "tag:StopOffHours", 'Values': ["yes"]}
])
return [
instance['InstanceId']
for reservation in response['Reservations']
@raymyers
raymyers / schedule_monitor_tasks.rb
Created June 13, 2016 20:25
Scheduling periodic monitoring tasks with Rufus and Librato
unless self.class.const_defined?('SCHEDULER')
SCHEDULER = Rufus::Scheduler.new(:lockfile => ".rufus-scheduler.lock")
unless SCHEDULER.down?
SCHEDULER.every('60s', :first_in => '10s') do
active_count = DASHBOARD.find_recent_users(10.minutes.ago).count()
# Rails.logger.info "======== active users #{active_count}"
Librato.measure('users.active', active_count)
end
SCHEDULER.every('60s', :first_in => '10s') do
@raymyers
raymyers / google-drive-example.rb
Created December 18, 2015 19:55
Example Google Drive ruby API usage with google-api-client version 0.9.pre4 authenticating with a service account
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "#{ENV['HOME']}/google-service-account.json"
scopes = ['https://www.googleapis.com/auth/drive']
drive = Google::Apis::DriveV2::DriveService.new
auth_client = Google::Auth.get_application_default(scopes).dup
auth_client.sub = 'user@mydomain.com'
drive.authorization = auth_client
report_folders = drive.list_files(q: "title = 'Reports'")
raise "Expected 1 folder called 'Reports', found #{report_folders.items.count}" if report_folders.items.count != 1
parent_id = report_folders.items[0]
@raymyers
raymyers / blacklist.js
Created February 19, 2015 16:15
angular directive to validate with a blacklist
angular.module('myapp.directive.blacklist', []).
directive('maBlacklist', [
function() {
return {
restrict: 'A',
require: 'ngModel',
scope: {'blacklist':'=gsBlacklist'},
link: function($scope, $elem, $attrs, modelCtrl) {
var check = function(value) {
@raymyers
raymyers / drain_port.sh
Last active July 21, 2022 19:29
Bash script for connection drain. Uses netstat to repeatedly count established connections
port=8080
max_tries=10
seconds_to_wait=3
echo "Waiting for traffic on port $port to stop..."
for (( tries=1; tries<=$max_tries; tries++ )); do
# Netstat invocation tested on Ubuntu. Check your distro for differences.
connections=$(netstat -ant | grep ":$port.*:.*ESTABLISHED" | wc -l)
echo " $connections connections remaining. Try $tries/$max_tries."
if [ $connections -eq 0 ]; then
exit 0
@raymyers
raymyers / ShellSplitter.java
Last active April 7, 2024 19:11
Simple Java program to tokenize string as a shell would - similar to shlex in Python. Not checked for POSIX compliance yet - feel free to comment with edge cases that could be fixed. This is my original work, free to reuse. Created for Stack Overflow question: https://stackoverflow.com/questions/1082953/shlex-alternative-for-java/20725050:
package com.cadrlife;
import java.util.ArrayList;
import java.util.List;
public class ShellSplitter {
public List<String> shellSplit(CharSequence string) {
List<String> tokens = new ArrayList<String>();
boolean escaping = false;
char quoteChar = ' ';
@raymyers
raymyers / iptables_port_switching.sh
Created September 13, 2013 19:07
Iptables commands supporting hot deploy by transferring traffic between app servers
sudo iptables -t nat -N hot-deploy
sudo iptables -t nat -A PREROUTING -j hot-deploy
# Enable first server
sudo iptables -t nat -A hot-deploy -p tcp --dport 80 -j REDIRECT --to-ports 8080
# Enable second
sudo iptables -t nat -A hot-deploy -p tcp --dport 80 -j REDIRECT --to-ports 8081 && sudo iptables -t nat -D hot-deploy 1
# Back to first