Skip to content

Instantly share code, notes, and snippets.

View mrsarm's full-sized avatar
🏠
Working from home

Mariano Ruiz mrsarm

🏠
Working from home
View GitHub Profile
@mrsarm
mrsarm / github-file.sh
Last active April 19, 2024 22:25
github-file.sh: GitHub Action script to get a file from a repo/branch, otherwise fallback to another branch.
#!/usr/bin/env sh
# Get a file from a GitHub repository, trying first to download it
# from a branch passed by argument, if the file doesn't exist,
# fallback to another branch version.
#
# Script to be called from Github Actions as following:
#
# - name: Get GitHub downloader
# run: wget https://gist.github.com/mrsarm/95279381f3d8bf4269499fb437888e2c/raw/github-file.sh
@mrsarm
mrsarm / main.rs
Last active March 1, 2024 22:58
vector_threads
/// Multi-thread example in Rust of iterating a vector as input data in chunks,
/// using threads in a safe way, using the threads and the channels APIs, and
/// the `Arc` type.
///
/// ## Details
///
/// Spawn MAX_NTHREADS number of threads, then from each thread borrow a slice
/// of the vector data, and from each string inside
/// the slice, calculate a new string and publish it into a channel, that
/// can then be listened by a receiver from the main thread, while the
@mrsarm
mrsarm / promise-concurrency-limit.ts
Created June 16, 2023 01:46
promise-concurrency-limit.ts: Execute concurrently the asynchronous functions passed, with a limit in concurrency
/**
* Execute concurrently the asynchronous functions `fns` passed, but
* not more than `maxConcurrency` at the same time. All functions
* return the same type T, an a promise with T[] inside is returned.
*/
const execConcurrent = async<T> (
fns: (() => Promise<T>)[],
maxConcurrency: number,
): Promise<T[]> => {
const res: T[] = [];
@mrsarm
mrsarm / drop-db-connections-opened.sql
Last active June 15, 2023 15:26
drop-db-connections-opened.sql: Delete Postgres Database with connections opened
-- Delete Database with connections opened
-- Postgres older than 13 don't support FORCE option, in this case pg_terminate_backend() is used:
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'dbname';
DROP DATABASE dbname;
@mrsarm
mrsarm / video2mp4.sh
Created March 12, 2023 14:08
Convert what ever video file to a compatible MP4 file that works on most devices.
#!/usr/bin/env bash
if [ "$1" == "-h" -o "$1" == "--help" -o "$#" == "0" ]
then
echo "Convert what ever video file to a compatible MP4 file"
echo "that works on most devices."
echo
echo "Usage: video2mp4.sh [FILE]"
exit
fi

Install Python from sources

On Ubuntu or compatible:

sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev libffi-dev lzma liblzma-dev

Download Python source and install as normal user with (replace prefix path, e.g. /opt/py/python3.11):

@mrsarm
mrsarm / utils-db-postgresql.sql
Created November 22, 2022 15:38
Postgres DB utils SQL scripts
-- DB size in bytes
SELECT pg_size_pretty( pg_database_size('dbname') );
-- Table size in bytes
SELECT pg_size_pretty( pg_total_relation_size('tablename') );
-- All table sizes sorted by size desc
SELECT table_name, pg_size_pretty( pg_relation_size(quote_ident(table_name)) )
FROM information_schema.tables
@mrsarm
mrsarm / docker-compose-services.sh
Created October 17, 2022 12:32
docker-compose-services: list all services found in the docker-compose.yml file
#!/usr/bin/env bash
if [ "$1" == "-h" -o "$1" == "--help" ]; then
echo 'docker-compose-services: list all services found in the docker-compose.yml file'
exit
fi
cat docker-compose.y*ml | grep -oP '(?<=^ )(\w|-)+(?=:)'
@mrsarm
mrsarm / taillog.sh
Last active May 19, 2023 15:51
taillog: tail FILE in --follow mode highlighting with colors the severity
#!/usr/bin/env bash
#
# taillog: tail FILE highlighting with colors the severity.
#
# Based in the script found at https://serverfault.com/a/1022806/256983
#
# TODO: check whether the syntax "\<MESSAGE\>" offers better compatibility than "\bMESSAGE\b" to match whole words only (Mac OS)
if [ "$1" = "-h" -o "$1" = "--help" ]
then
@mrsarm
mrsarm / psql-output-json.sql
Created January 17, 2022 21:12
psql-output-json.sql: Output SQL results from Psql into a JSON file (JSON Line file)
# \t\a\o /tmp/out.json
--Tuples only is off.
--Output format is aligned.
SELECT row_to_json(r) FROM my_table AS r;
# \t\a\o
--Tuples only is off.
--Output format is aligned.