Skip to content

Instantly share code, notes, and snippets.

View gsscoder's full-sized avatar
💭
obsessive coding disorder

coder (π³) gsscoder

💭
obsessive coding disorder
View GitHub Profile
@gsscoder
gsscoder / BubbleSort.sol
Created February 3, 2023 13:49
Bubble sort implementation in Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract BubbleSort {
function sort(uint[] memory array) public pure returns (uint[] memory) {
bool swapped;
for (uint i = 1; i < array.length; i++) {
swapped = false;
@gsscoder
gsscoder / ImageUtil.cs
Last active May 30, 2022 14:19
C# image utilities
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
public static class ImageUtil
{
public static Image Resize(this Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
@gsscoder
gsscoder / EnumerableExtensions_Partition.cs
Created December 10, 2021 11:16
C# extension method to partition sequences by a value
using System;
using System.Collections.Generic;
using System.Linq;
using SharpX.Extensions;
static class EnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> Partition<T, TKey>(this IEnumerable<T> source, Func<T, TKey> selector)
{
var result = new List<IEnumerable<T>>();
@gsscoder
gsscoder / ExportJsonARMTemplate.cs
Last active December 9, 2021 07:59
C# sample that demonstrates programmatic JSON AzureRM template export
// requires Microsoft.IdentityModel.Clients.ActiveDirectory 5.29
// Microsoft.Azure.Management.ResourceManager 3.15.1-preview
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
@gsscoder
gsscoder / JsonUtil_Prettify.cs
Last active March 25, 2021 07:36
C# helper method to pretty print JSON (ILogger friendly)
// based on: https://gist.github.com/FrankHu-MSFT/b6750185b19fd4ada4ba36b099985813
// can removes \r to fit in a single log line when using ILogger
using System.IO;
using Newtonsoft.Json;
static class JsonUtil
{
public static string Prettify(string json, bool stripLineFeed = false)
{
using var stringReader = new StringReader(json);
@gsscoder
gsscoder / AvailabilityCanaryFunction.cs
Last active March 19, 2021 06:56
Simple C# availability canary Function App
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
namespace HealthTest2
{
@gsscoder
gsscoder / ChaosHealthCheck.cs
Created March 18, 2021 16:56
Basic IHealthCheck implementation that injects chaos
using System;
using System.Threading;
using System.Threading.Tasks;
using Polly.Contrib.Simmy;
using Polly.Contrib.Simmy.Outcomes;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using CSharpx;
sealed class ChaosHealthCheck : IHealthCheck
{
@gsscoder
gsscoder / checksum.bat
Last active March 13, 2021 06:59
Batch script to compare a file SHA256 checksum to a given one
@echo off
setlocal enabledelayedexpansion
::Batch script to compare a file SHA256 checksum to a given one.
::Usage: checksum [FILE] [VALUE]
set filepath=%1
set checksum=%2
set idx=0
@gsscoder
gsscoder / query_appinsights_rest.ps1
Last active March 12, 2021 13:24
PowerShell code to query Azure App Insights using REST API
$aiAppId = '00000000-0000-0000-0000-000000000000'
$aiApiKey = 'ehzgdrES4kBJNCuRUzcBfkuka5CGWkAr4hyhJ6y3'
$result = Invoke-RestMethod -Uri "https://api.applicationinsights.io/v1/apps/$aiAppId/query" `
-Method Post -UseBasicParsing `
-Headers @{
'x-api-key' = $aiApiKey
'content-type' = 'application/json'
} `
-Body '{"query": "traces | where timestamp <= ago(1m) | limit 10"}'
@gsscoder
gsscoder / chaos3.fsx
Last active February 27, 2021 14:06
Simple F# function that returns an asynchronous function that may inject chaos
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos3.fsx
// latency test
// 0.490776
open System
open System.Threading
let chaosAsync (name:string) (shouldChaosAsync:unit -> Async<bool>) (chaosAsync:unit -> Async<unit>) =
fun (serviceAsync:unit -> Async<'t>) ->
async {