Skip to content

Instantly share code, notes, and snippets.

// Extract "GET" parameters from a JS include querystring
function getParams(script_name) {
// Find all script tags
var scripts = document.getElementsByTagName("script");
// Look through them trying to find ourselves
for(var i=0; i<scripts.length; i++) {
if(scripts[i].src.indexOf("/" + script_name) > -1) {
// Get an array of key=value strings of params
var pa = scripts[i].src.split("?").pop().split("&");
@loopj
loopj / passenger-management.rb
Created March 2, 2011 19:23
Checks memory usage of all paseenger child process and kill if grows too large. Additionally kill off long running passengers to prevent memory leak issues (similar to the PassengerMaxRequests config variable in apache's passenger version, but works on bo
#!/usr/bin/env ruby
#
# Passenger management script
# By James Smith http://loopj.com
# Based heavily on a similar script by Jon Bettcher
#
# Check memory usage of all paseenger child process and kill if grows
# too large.
#
@loopj
loopj / mysql-fast-backup-restore.sh
Created March 6, 2011 03:12
Script to create mysql backups which will restore quickly
#!/bin/bash
echo "SET autocommit=0;
SET unique_checks=0;
SET foreign_key_checks=0;" > backup.sql
mysqldump -u myuser --password=mypassword mydatabase >> backup.sql
echo "COMMIT;" >> backup.sql
@loopj
loopj / colorlogcat.py
Created March 11, 2011 20:36
Script to highlight Android's adb logcat output for console
#!/usr/bin/python
'''
Copyright 2009, The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
class MongoHelpers
# Sum over fields
def self.sum(collection, fields, group_by, conditions=nil)
map = <<-EOD
function() {
self = this;
key = {};
if(group_by && group_by.length > 0) {
group_by.forEach(function (g) {
#
# Requirements for js framework
#
# - Cache Page "chrome", javascripts, css, templates in app-cache
# - Optionally
# - Load json data from android/ios client (eg. android Parcelable data)
# - Load json data immediately from cache, if present
# - Load json data from http json endpoint, store to cache
# - Update view elements when associated model data changes (knockout.js?)
@loopj
loopj / redis-cache-example.coffee
Created December 30, 2011 10:47
Redis passthrough cache for node
# Example usage
AvatarCache = new RedisCache
keyPrefix: "userAvatar:"
expiry: 3600
generator: (key, callback) ->
request "https://api.github.com/users/#{key}", (error, response, body) ->
if !error and response.statusCode == 200
user = JSON.parse(body)
generate user.avatar_url
@loopj
loopj / average_timer.rb
Created January 7, 2012 00:32
Rough script for counting how many events per second you are doing, with rolling average
# Usage:
#
# timer = AverageTimer.new
# my_loop do
# # Track an avent
# timer.event
#
# # Print the events per second (averaged over 5 seconds)
# puts timer.events_per_second(5)
#
@loopj
loopj / android-memory.java
Created May 29, 2012 00:51
Android memory metrics
// Device memory data
ActivityManager activityManager = (ActivityManager) androidContext.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
// Runtime data memory numbers are in bytes (dalvik process?)
int processors = runtime.availableProcessors();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
System.out.println(String.format("Runtime: %d processors, %d totalMemory (bytes), %d freeMemory (bytes)", processors, totalMemory, freeMemory));
// Debug data (system wide) memory numbers are in bytes
@loopj
loopj / mongo-functions.js
Created June 8, 2012 18:58
Useful Mongo Shell Functions
function getCurrentOpStats() {
intervals = [1,5,10,30]
waitingForLock = 0;
secsRunningStats = {};
inProg = db.currentOp()["inprog"]
inProg.forEach(function (op) {
if(op["waitingForLock"]) {
waitingForLock += 1;
}