Skip to content

Instantly share code, notes, and snippets.

View plcstevens's full-sized avatar

Philip Stevens plcstevens

View GitHub Profile
@plcstevens
plcstevens / postgres_queries_and_commands.sql
Created January 17, 2019 11:45 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@plcstevens
plcstevens / Schema.elm
Created August 20, 2018 15:40
A basic example of trying to create a schema ignorant JSON decoder in ELM
import Dict exposing (Dict)
import Json.Decode as Decode
type SchemaData
= SchemaObject (Dict String SchemaData)
| SchemaList (List SchemaData)
| SchemaString String
| SchemaInt Int
| SchemaBool Bool
| SchemaNull
@plcstevens
plcstevens / controller_example.rb
Last active August 29, 2015 14:26
Include this in controllers and render with your JSON to get pagination URLs matching http://jsonapi.org/format/#fetching-pagination specification. Requires that you use https://github.com/amatsuda/kaminari
class ExamplesController < ApplicationController
def index
@examples = Example.order(id: :desc)
render json: { pagination: paginate(@examples), examples: @examples }
end
private
def paginate(active_relation)
Pagination.for(self, active_relation)
es_alias = ENV.fetch('ALIAS')
hosts = ENV.fetch('HOSTS')
old_index = ENV.fetch('OLD_INDEX')
old_type = ENV.fetch('OLD_TYPE')
new_index = ENV.fetch('NEW_INDEX')
new_type = ENV.fetch('NEW_TYPE')
size = ENV.fetch('SCAN_SIZE', 100)
client = Elasticsearch::Client.new(hosts: hosts)
unless client.indices.exists(index: new_index)
Rehearsal ---------------------------------------------
any? 0.000000 0.000000 0.000000 ( 0.000885)
all? 0.000000 0.000000 0.000000 ( 0.000279)
min block 0.960000 0.000000 0.960000 ( 0.958517)
------------------------------------ total: 0.960000sec
user system total real
any? 0.000000 0.000000 0.000000 ( 0.000886)
all? 0.000000 0.000000 0.000000 ( 0.000297)
min block 0.970000 0.000000 0.970000 ( 0.974703)
@plcstevens
plcstevens / template.rb
Created January 6, 2015 10:51
Benchmark template
require "benchmark"
Benchmark.bm(10000) do |x|
x.report("A") { }
x.report("B") { }
end
@plcstevens
plcstevens / reevoomark_defined.js
Last active August 29, 2015 14:06
This is how we can allow users to call ReevooMark api actions from outside of our callbacks. Please note as this is all asynchronous, ReevooMark is not guaranteed to exist. Therefore this should only be used when the call to ReevooMark occurs some time after page load.
(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//cdn.mark.reevoo.com/assets/reevoo_mark.js';
var s = document.getElementById('reevoomark-loader');
s.parentNode.insertBefore(script, s);
})();
afterReevooMarkLoaded = [function() {
ReevooApi.load('YOUR_TRKREF', function(retailer) {
@plcstevens
plcstevens / reevoomark_observing.js
Last active April 6, 2016 10:44
This is how a client can implement their own callbacks onto our badge actions.
(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://cdn.mark.reevoo.com/assets/reevoo_mark.js';
var s = document.getElementById('reevoomark-loader');
s.parentNode.insertBefore(script, s);
})();
afterReevooMarkLoaded = [function(){
ReevooApi.load('YOUR_TRKREF', function(retailer){
@plcstevens
plcstevens / reevoomark2.js
Last active August 29, 2015 14:06
Possible implementation technique for future JavaScipt library. This would avoid issues with asynchronous vs synchronous method calls and also allow for a very verbose API that would be easily extendable.
(function(url, name){
(window.ReevooMarkObjects = window.ReevooMarkObjects || []).concat([name]);
window[name] = window[name] || function(){
(window[name].queue = window[name].queue || []).push([arguments]);
};
var script = document.createElement("script");
script.async = true;
script.src = url;
@plcstevens
plcstevens / no_pre_compile.rb
Last active August 29, 2015 14:06
Only pre-compile when necessary
# set the locations that we will look for changed assets to determine whether to precompile
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)
# clear the previous precompile task
Rake::Task["deploy:assets:precompile"].clear_actions
class PrecompileRequired < StandardError; end
namespace :deploy do
namespace :assets do
desc "Precompile assets"