Skip to content

Instantly share code, notes, and snippets.

View vlasky's full-sized avatar

Vlad Lasky vlasky

  • Sydney, Australia
View GitHub Profile
@vlasky
vlasky / calculate_median_in_mysql_8_using_window_function_row_number.md
Last active June 23, 2023 02:02
How to calculate the median in MySQL 8.0+ using the window function ROW_NUMBER()

During a collaborative session with ChatGPT, I came up with a modern MySQL query to calculate the median value of a set of data. It leverages the window function ROW_NUMBER() introduced in MySQL 8.0. For this reason, it won't run on MySQL 5.7 or older versions.

The median is an extremely useful metric, especially when analyzing data sets representing response times, latencies or error rates. Unlike the average, it is resistant to being skewed by outliers.

To get the median of a data set, we need to sort the data and take the value of the middle row (if the number of rows is odd), or the average of the two middle numbers (if the number of rows is even).

All the median calculation MySQL queries I found online seemed crude, relying on user-defined variables, string concatenation via GROUP_CONCAT() and/or multiple subqueries. I find this approach much cleaner.

As of June 2023, [MariaDB is currently the only fork of MySQL that has native MEDIAN() and PERCENTILE_CONT() functions](https://mariadb.com/kb/en/media

@vlasky
vlasky / cron.js
Created May 27, 2020 10:09
Meteor demo code for running jobs synchronized across multiple processes. It is based on percolate:synced-cron, but modified to use MySQL. Files must be placed in the Meteor server folder and all required atmosphere & NPM dependencies installed.
// A package for running jobs synchronized across multiple processes
// Based on percolate:synced-cron, but modified to use MySQL
// https://github.com/percolatestudio/meteor-synced-cron
/*You must create a cron_history table in your MySQL database with the following schema:
CREATE TABLE `cron_history` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`intendedAt` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
@vlasky
vlasky / gzcompress_meteor_assets.sh
Last active March 21, 2018 10:32
This is a Linux shell (bash) script that gzip-compresses the static assets contained within a production build of a Meteor webapp. It has been tested under CentOS 7. This script is intended to be used when Nginx is running as the web server and the 'gzip_static' configuration option is enabled. When the client supports gzip compression, Nginx wi…
#!/bin/sh
# Files with the following extensions will be compressed with gzip
GZFILETYPES=( "*.html" "*.css" "*.js" "*.ico" "*.xml" "*.json" "*.svg" "*.otf" "*.ttf" "*.eot" )
# The base directory of the Meteor production bundle assets that are served to the client
DIRECTORIES="/var/www/meteor/production/bundle/programs/web.browser/"
# Files smaller than this size (in bytes) will NOT be compressed as the gzip overhead usually makes them larger
GZMINSIZE=150
for currentDir in $DIRECTORIES; do
for f in "${GZFILETYPES[@]}"; do
files="$(find $currentDir -iname "$f")";
@vlasky
vlasky / disable_transparent_huge_pages.service
Created March 6, 2018 06:31
Systemd startup script to automatically disable Transparent Huge Pages (THP) under Linux. This must be done before launching MySQL with the TokuDB storage engine or before launching MongoDB.
# Copy this file to /etc/systemd/system/
# Then run:
# systemctl enable disable_transparent_huge_pages.service
# systemctl start disable_transparent_huge_pages.service
[Unit]
Description=Disable transparent huge pages
[Service]
Type=oneshot
ExecStart=/bin/sh -c "/usr/bin/echo never | /usr/bin/tee /sys/kernel/mm/transparent_hugepage/enabled"
@vlasky
vlasky / point_in_polygon_using_winding_number.js
Last active March 19, 2024 04:58
JavaScript implementation of winding number algorithm to determine whether a point is inside a polygon
//JavaScript implementation of winding number algorithm to determine whether a point is inside a polygon
//Based on C++ implementation of wn_PnPoly() published on http://geomalgorithms.com/a03-_inclusion.html
function pointInPolygon(point, vs) {
const x = point[0], y = point[1];
let wn = 0;
for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
let xi = vs[i][0], yi = vs[i][1];
let xj = vs[j][0], yj = vs[j][1];
@vlasky
vlasky / debugeventemitter.js
Last active May 30, 2020 11:04
Debugging code to help track down events that cause EventEmitter memory leaks in Node.js
//Is Node.js reporting warning messages like this?:
//
// "Warning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit"
//
//The following code will intercept calls to addListener() and on() and print the type of event and generate an Error exception
//With a stack trace to help you find the cause
var EventEmitter = require('events').EventEmitter;
const originalAddListener = EventEmitter.prototype.addListener;