Skip to content

Instantly share code, notes, and snippets.

View colinfwren's full-sized avatar

Colin Wren colinfwren

View GitHub Profile
@colinfwren
colinfwren / xXx_1337_haxXx0r.js
Created August 21, 2018 20:11
Script to get events from JSON and put them on page
var getFormattedDate = function(distance){
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
return days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}
var container = $('#demo');
@colinfwren
colinfwren / safely_get_env_var.js
Last active February 6, 2019 20:30
Safely get Environment Variables
const getEnvVar = (key, safeDefault) => {
if (Object.prototype.hasOwnProperty.call(process.env, key) && !(typeof (process.env[key]) === 'undefined')) {
return process.env[key];
}
return safeDefault;
};
// Use it like
const config = {
serverUrl: getEnvVar('SERVER_URL', 'http://localhost:8080')
@colinfwren
colinfwren / example-controller.js
Created February 6, 2019 21:39
Example controller
server.post('/test-runs', async (req, res, next) => {
try{
console.log('Kicking off test run');
exec('npm test', (error, stdout, stderr) => {
if (error) {
console.error(`Error running tests: ${error.message}`);
}
console.log(stdout);
console.error(stderr);
});
@colinfwren
colinfwren / couchbase_reporter_for_jest.js
Created February 6, 2019 23:22
Couchbase report for jest
var couchbase = require('couchbase');
class CouchbaseReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
const cluster = new couchbase.Cluster(`couchbase://127.0.0.1`);
cluster.authenticate('genericUser', 'dontStealMe');
this.bucket = cluster.openBucket('default');
}
#!/usr/bin/env bash
until $(curl --output /dev/null --silent --head --fail http://$HOST_IP:8080/test-runs/$TEST_RUN_ID) ; do
echo "Still waiting for Schema Registry to become available";
sleep 5;
done;
@colinfwren
colinfwren / polling_kafka_queue_for_messages.js
Created February 10, 2019 22:27
A recursive function to check that a Kafka queue has got a message
/**
* Sleep for the specified number of seconds
*
* @param {number} seconds - Number of seconds to sleep for
* @returns {Promise}
*/
const sleep = async (seconds) => {
return new Promise(resolve => {
setTimeout(resolve, (seconds * 1000));
});
@colinfwren
colinfwren / Makefile
Created February 10, 2019 22:32
Getting the host's IP in a Makefile to populate into a docker-compose file
#!/usr/bin/make
run:
HOST_IP=$(shell ifconfig en0 | grep inet | grep -v inet6 | awk '{print $$2}') docker-compose -f confluent.yml up -d
# In your docker-compose file you can use something like:
# version: '3'
# services:
# zookeeper:
# image: confluentinc/cp-zookeeper:5.1.0
@colinfwren
colinfwren / using_kafka_mirror_maker.sh
Created February 10, 2019 23:13
Using Kafka Mirror Maker
# Example contents of consumer.config
bootstrap-servers=[KAFKA HOST]
exclude.internal.topics=true
group.id=mirror-maker
auto.offset.reset=latest
# Example contents of producer.config
bootstrap-servers=[KAFKA HOST]
acks=-1
batch.size=100
export const setMock = jest.fn();
export const getMock = jest.fn();
const mock = jest.fn().mockImplementation(() => {
return {
set: setMock,
get: getMock
};
});
module.exports = {
testEnvironment: 'node',
silent: false,
reporters: [
'default',
'couchbaseReporter'
]
};