Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
leandrosilva / fts.sql
Created October 6, 2022 20:47
Full Text Search "sort of" with prefix, infix, fuzzy, and similarity on the Almighty Postgres
SELECT
pos,
sim,
p.*
FROM
produtos AS p,
NULLIF(POSITION('seme' IN LOWER(COALESCE(p.gtin, '') || ' ' || COALESCE(p.descricao, ''))), 0) AS pos,
NULLIF(SIMILARITY(LOWER(COALESCE(p.gtin, '') || ' ' || COALESCE(p.descricao, '')), 'seme'), 0) AS sim
WHERE
(LOWER(COALESCE(p.gtin, '') || ' ' || COALESCE(p.descricao, ''))) LIKE 'seme%'
@leandrosilva
leandrosilva / parallel_for.cs
Last active November 8, 2021 21:20
Source code for the article "Tudo ao mesmo tempo agora com C#" originally published at https://medium.com/pricefy-labs
private static void ParallelFor()
{
var numberOfIterations = 1000;
var min = 0;
var max = 100_000_000;
var random = new Random();
var breakIndex = random.Next(1, numberOfIterations);
@leandrosilva
leandrosilva / EFLazyLoadingExtensions.cs
Created May 19, 2021 23:05
Extension method to lazy loading related fields of an entity
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace CodeZone.EntityFramework.Extensions
{
public static class EFLazyLoadingExtensions
{
public static void Load<TEntity, TRelated>(this TEntity entity, Expression<Func<TEntity, TRelated>> relatedSelector)
where TRelated : class
@leandrosilva
leandrosilva / wkhtmltopdf.v
Created December 31, 2020 15:14
My contribution to the V programming language - yet another example of C interop
import os
// Example of C interop for a very handy task.
//
// wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to
// render HTML into PDF and various image formats using the Qt WebKit rendering
// engine. These run entirely "headless" and do not require a display or display
// service.
//
// https://github.com/wkhtmltopdf/wkhtmltopdf
@leandrosilva
leandrosilva / Program.cs
Last active May 19, 2021 23:07
pinvoke-wkhtmltox - lab with C# .NET Core and wkhtmltox C library
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace pinvoke_wkhtmltox
{
class Program
{
static void Main(string[] args)
@leandrosilva
leandrosilva / get_output.py
Created April 17, 2020 22:14
wkpything - lab with python and wkhtmltox C library
import sys
import ctypes
class wkhtmltopdf_global_settings(ctypes.Structure):
_fields_ = []
class wkhtmltopdf_object_settings(ctypes.Structure):
_fields_ = []
class wkhtmltopdf_converter(ctypes.Structure):
@leandrosilva
leandrosilva / quick_and_dirty_parsedate.js
Last active September 30, 2019 18:32
Quick and dirty JS parsing date
function parseDate(sdatetime) {
let dateTime = sdatetime.split(' ');
let sdate = dateTime[0];
let date = sdate.split('/');
let stime = dateTime[1];
let time = stime.split(':');
return new Date(Date.UTC(date[2], date[1]-1, date[0], time[0], time[1], time[2], 0));
}
@leandrosilva
leandrosilva / ec2_instances.ps1
Created September 4, 2019 21:28
PowerShell script to quick list running EC2 instances based on a name pattern
param (
[string] $NamePattern = "Dummy-web-app-*",
[string] $Status = "Running"
)
function Get-InstanceStatus () {
$res = $(aws ec2 describe-instances --region us-east-1 --filters "Name=tag:Name,Values=$NamePattern")
$json = $res | ConvertFrom-Json
$instances = $json.Reservations.Instances `
| Where-Object { $_.State.Name -eq $Status } `
@leandrosilva
leandrosilva / ec2_web_heater.ps1
Last active September 4, 2019 21:26
PowerShell script to fire some URLs to get them warmed up
param (
[string] $NamePattern = "Dummy-web-app-*",
[int] $Iterations = 2
)
function Get-WebInstances () {
$res = $(aws ec2 describe-instances --region us-east-1 --filters "Name=tag:Name,Values=$NamePattern")
$json = $res | ConvertFrom-Json
$instances = $json.Reservations.Instances `
| Where-Object { $_.State.Name -eq "Running" } `
@leandrosilva
leandrosilva / usage_sample_es.ps1
Last active September 9, 2019 12:17
Two small samples on how to use mod_s3.psm1 and mod_eslog.psm1
#################################
# SENDING LOG TO ELASTIC SEARCH #
#################################
Import-Module ($PSScriptRoot + "\mod_eslog.psm1") -Force
Send-InstallationLog -Version $remoteVersion
"Current version has been installed."