Skip to content

Instantly share code, notes, and snippets.

View sandcastle's full-sized avatar
🍦

Glenn Morton sandcastle

🍦
View GitHub Profile
@sandcastle
sandcastle / stringToColor.js
Created February 22, 2024 03:50
String to color converter
function stringToColor(str) {
// Predefined set of colors
const colors = ['#FF5733', '#33FF57', '#3357FF', '#F833FF', '#FF8333', '#33FFF8'];
// Simple hash function
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
@sandcastle
sandcastle / time_histogram.sql
Last active December 8, 2020 22:42
Time histogram helper function
CREATE OR REPLACE FUNCTION time_histogram(
start_time timestamp,
end_time timestamp
)
RETURNS TABLE(bucket timestamp) AS $$
DECLARE diff_hours int;
BEGIN
diff_hours = abs(extract(epoch from end_time - start_time) / 3600)
raise notice 'diff hours: ', diff_hours;
@sandcastle
sandcastle / posgres_size.sql
Created November 8, 2020 21:34
Postgres table sizes
WITH RECURSIVE pg_inherit(inhrelid, inhparent) AS
(select inhrelid, inhparent
FROM pg_inherits
UNION
SELECT child.inhrelid, parent.inhparent
FROM pg_inherit child, pg_inherits parent
WHERE child.inhparent = parent.inhrelid),
pg_inherit_short AS (SELECT * FROM pg_inherit WHERE inhparent NOT IN (SELECT inhrelid FROM pg_inherit))
SELECT table_schema
, TABLE_NAME
@sandcastle
sandcastle / IsLoopbackOrPrivate.cs
Created April 21, 2020 07:49
Check if loop back is private network or a loopback address...
static bool IsLoopbackOrPrivate([NotNull] IPAddress clientIp)
{
// RFC for private networks:
// http://www.faqs.org/rfcs/rfc1918.html
byte[] bytes = clientIp.GetAddressBytes();
switch(bytes[0])
{
case 10:
return true;
@sandcastle
sandcastle / animals.txt
Last active December 18, 2019 03:08
Animal and colour names for randomly generating anonymous names for users.
Alligator
Anteater
Armadillo
Axolotl
Badger
Bat
Beaver
Buffalo
Camel
Capybara
@sandcastle
sandcastle / name_blacklist.txt
Last active December 18, 2019 02:19
A blacklist of names and words that can be used for user registrations
airhead
allah
altar
anal
anus
aryan
ass
ass lick
asshole
asshole cleaner
@sandcastle
sandcastle / pi-mount.sh
Last active March 4, 2023 21:05
Mount HFS+ USB Drive on a Raspberry Pi
sudo apt-get update
sudo apt-get upgrade -y
# install hfs tools (for macos extended journaled)
sudo apt-get install hfsplus hfsutils hfsprogs
sudo reboot
# get device id
df -h
# example:
@sandcastle
sandcastle / Dockerfile
Created October 21, 2019 12:18
Install .Net 3.0 diagnostic tools in a docker container
# https://github.com/dotnet/diagnostics/issues/573#issuecomment-543886037
# dotnet tools are currently available as part of SDK so we need to create them in an sdk image
# and copy them to our final runtime image
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS tools-install
RUN dotnet tool install --tool-path /dotnetcore-tools dotnet-sos
RUN dotnet tool install --tool-path /dotnetcore-tools dotnet-trace
RUN dotnet tool install --tool-path /dotnetcore-tools dotnet-dump
RUN dotnet tool install --tool-path /dotnetcore-tools dotnet-counters
@sandcastle
sandcastle / postgres_version.sql
Created September 16, 2019 06:40
Determine the version of postgres a statement is run on.
select version();
-- PostgreSQL 9.6.12 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.2.0) 8.2.0, 64-bit
SHOW server_version;
-- 9.6.12
SELECT current_setting('server_version_num');
-- 90612
select current_setting('server_version');
@sandcastle
sandcastle / text_replace.sh
Created August 3, 2018 14:07
Replace text function for shell
function text_replace() {
case "${OSTYPE}" in
darwin*) PLATFORM="OSX" ;;
linux*) PLATFORM="LINUX" ;;
bsd*) PLATFORM="BSD" ;;
*) PLATFORM="UNKNOWN" ;;
esac
if [[ "${PLATFORM}" == "OSX" || "${PLATFORM}" == "BSD" ]]; then
find $1 -type f -name $2 -exec sed -i "" "s/$3/$4/g" {} +