Skip to content

Instantly share code, notes, and snippets.

@defkode
defkode / solid_errors_active_job.rb
Created May 9, 2024 05:53
SolidErrors - ActiveJob integration
# catches errors from any ActiveJob::Base inherited classes like ApplicationJob
# and often overlooked ActionMailer::MailDeliveryJob which is wrapper for i.e: YourMailer.deliver_later
module SolidErrors::ActiveJob
extend ActiveSupport::Concern
included do
around_perform do |job, block|
Rails.error.record(StandardError, context: {job: job.class.name, job_id: job.job_id}) do
block.call
end
// Converts Excel Column name to zero-based index
// A => 0, B => 1, Z => 25 AA => 26).
function excelColumnNameToInteger(columnName) {
if !(/^[A-z]+$/.test(columnName)) {
let result = 0;
for (let i = 0; i < columnName.length; i++) {
result += (columnName.charCodeAt(i) - 64) * Math.pow(26, columnName.length - i - 1);
}
return result - 1 // shift the result by 1 to get zero-based numbering;
@defkode
defkode / with_retries.rb
Created November 16, 2023 16:53
Retry n times on specified exceptions
# USAGE: retry 3 times on specified errors and wait 5s between retries
# when maximum retries is run then raise exception
# with_retries(on: [Errno::EHOSTUNREACH], tries: 3, interval: 5) do
# Net::HTTP.get('example.com', '/index.html')
# end
module WithRetries
def with_retries(on: [StandardError], tries: 3, interval: 0.5)
interval = 0 if Rails.env.test? # speed up tests
@defkode
defkode / betterstack-logs-rails-lograge.sql
Last active August 4, 2023 15:17
Top 10 outliers by DB time, request durations and allocations (Logs by BetterStack)
-- SQL: https://clickhouse.com/docs/en/sql-reference
-- top db
SELECT endpoint, AVG(db) AS average_db, MIN(db) AS min_db, MAX(db) AS max_db, COUNT(*) AS count FROM (
SELECT
CONCAT(
JSON_VALUE(json, '$.message.controller'),
'#',
JSON_VALUE(json, '$.message.action')
) AS endpoint,
simpleJSONExtractFloat(message, 'db') / 1000 as db
@defkode
defkode / copy-image-from-clipboard.js
Created May 28, 2022 07:05
Copy image from clipboard and display on canvas
// app.js
import React from 'react';
class App extends React.Component {
componentDidMount() {
const retrieveImageFromClipboardAsBlob = (pasteEvent, callback) => {
if(pasteEvent.clipboardData === false){
if(typeof(callback) === "function"){
@defkode
defkode / heroku-review-apps-create.yml
Created May 3, 2022 09:49
Github Actions for Automatic Creation/Update Heroku review apps without connecting Github to Heroku and without app.json configuration
# please set up following github secrets in your repo:
# HEROKU_PIPELINE_NAME (https://devcenter.heroku.com/articles/pipelines#creating-pipelines)
# HEROKU_USER (your heroku username/email)
# HEROKU_API_KEY (use: 'heroku authorizations:create' for creating separate api_key for this purpose)
name: Heroku Review App Create
on:
pull_request:
types: opened
FROM alpine:latest
RUN apk add \
py3-pip \
py3-pillow \
py3-cffi \
py3-brotli \
gcc \
musl-dev \
python3-dev \
FROM debian:buster
RUN apt-get update -qq && apt-get install -qqy \
build-essential \
python3-pip \
python3-cffi \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libgdk-pixbuf2.0-0
@defkode
defkode / flatten_json.coffee
Created July 19, 2013 12:58
flatten JSON
window.flattenJSON = (obj) ->
flattened = {}
join = (path, key) ->
(if path is "" then "" else path + "_") + key
flattenArray = (arr, path) ->
flattenElement(arr, path, i) for i in [0...arr.length]
flattenObject = (obj, path) ->
@defkode
defkode / machine_readable_code_validation.rb
Created November 16, 2011 09:42
machine_readable_code_validation
# TPD<<T220001293<<<<<<<<<<<<<<<
# 6408125<2010315D<<<<<<<<<<<<<4
# MUSTERMAN<<ERIKA<<<<<<<<<<<<<<
class PersonalId
attr_reader :code, :data
# 9 alphanumeric
def initialize(machinecode = "TPD<<2406055684D<<<<<<<<<<<<<<6810203<0705109D<<<<<<<<<<<<<6MUSTERMAN<<ERIKA<<<<<<<<<<<<<<")
@code = machinecode