Skip to content

Instantly share code, notes, and snippets.

View afruzan's full-sized avatar
😎
I may be slow to respond.

afruzan afruzan

😎
I may be slow to respond.
View GitHub Profile
@afruzan
afruzan / DatabaseCleanupHints.md
Created May 24, 2023 06:36
hints about database cleanup job to delete old records from time-series table

اولا که بهترین روش که ایده خودم هست استفاده از partitioning در دیتابیس هایی که ممکن باشه هست ولی اگر ممکن نبود (مانند sqlite) باید به کارایی دقت زیادی کرد. در این موارد اگر جدول مربوطه فایل های وابسته حجیمی داشته باشه یه راهکار اینه ابتدا از حذف منطقی برای حذف کردن فایل های و تصاویر وابسته استفاده کرد و در فواصل زمانی بیشتر زمانی که کار سیستم سبک تره حذف واقعی انجام داد.

به هر حال برای انجام فرآیند حذف موارد زیر مهم اند:

If your query affects 5000 rows or more in the same table, that table gets locked during the operation. This is standard SQL Server behavior. Basically every DELETE causes a lock on that row and every 5000 row locks on the same table cause a Lock Escalation from row to table.

declare @MoreRowsToDelete bit
@afruzan
afruzan / pivotize.ts
Last active April 17, 2023 18:08
Convert flat array of items to Pivot Table data in JavaScript
// convert flat array of items to pivot table data
// uses linq.js
import Enumerable from 'linq';
export interface Column {
columnName: string;
sourceField: string;
sourceValue?: string;
isTotal?: boolean;
@afruzan
afruzan / LocalEventBus
Created February 9, 2023 15:48
C# thread safe local Event Bus
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
namespace Afruzan.Shared.EventBus;
public class LocalEventBus : IEventBus
{
public LocalEventBus()
{
@afruzan
afruzan / JS-LINQ.js
Created February 5, 2022 17:06 — forked from DanDiplo/JS-LINQ.js
JavaScript equivalents of some common C# LINQ methods. To help me remember!
// JS array equivalents to C# LINQ methods - by Dan B.
// Here's a simple array of "person" objects
var people = [
{ name: "John", age: 20 },
{ name: "Mary", age: 35 },
{ name: "Arthur", age: 78 },
{ name: "Mike", age: 27 },
{ name: "Judy", age: 42 },
{ name: "Tim", age: 8 }
@afruzan
afruzan / retry-policy.ts
Last active April 17, 2023 14:21
JavaScript promise retry policy and delay with cancellation token
export class RetryPolicy<T> {
constructor(
private action: () => Promise<T>,
private retryDelays: number[]) {
}
private retryNumber;
@afruzan
afruzan / signalr-hub-base.ts
Last active April 23, 2023 15:44
typescript signalr client hub base class that provides auth token and rxjs observable pattern.
import { HubConnection, HubConnectionBuilder, IHttpConnectionOptions, LogLevel, MessageHeaders } from '@microsoft/signalr';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { AuthService } from '../auth.service';
import { RetryCanceledError, RetryPolicy } from './retry-policy';
export abstract class SignalRHubBase {
constructor(private authService: AuthService) {
@afruzan
afruzan / SignalRHubsAuthExtentions.cs
Last active October 4, 2021 17:03
supporting .net core auth in signalr hubs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace SignalRWebApp
// a query on db without any think on database data structure:
// (NullIfEmpty is my function on T[].)
var shardsBounds = context.Storages
.Select(i => new ShardBounds
{
StorageCode = i.StorageCode,
StartDateTime = i.StartDateTime,
EndDateTime = i.EndDateTime,
PlateReaderIds = i.StoragePlateReaders.Select(i => i.PlateReaderId).ToArray().NullIfEmpty(),
});
@afruzan
afruzan / IranOfficialDateAndTimeInSQLServer.sql
Last active March 18, 2021 12:24
Iran Official Date And Time (Jalali calendar - Tehran TimeZone) Solution in SQL raw and linq query (ef core 5).
declare @myutcdate as datetime2 = CAST('2021-03-17 22:00' AS datetime2);
select
FORMAT(SWITCHOFFSET(@myutcdate, '+03:30'), 'yyyy-MM-dd', 'fa') as 'get iran date',
FORMAT(SWITCHOFFSET(@myutcdate, '+03:30'), 'yyyy-MM', 'fa') as 'get iran year-month',
CAST(FORMAT(SWITCHOFFSET(@myutcdate, '+03:30'), 'dd', 'fa') as int) as 'get iran day',
CAST(FORMAT(@myutcdate, 'dd', 'fa') as int) as 'invalid time-zone results in invalid date.'
@afruzan
afruzan / AddFileLoggerSample.sc
Created January 28, 2021 16:28
asp.net core (tested on 5) sample for simple and light and also powerful file logger provider.
public class Program
{
private static DateTime _startupTimestamp;
public static void Main(string[] args)
{
_startupTimestamp = DateTime.Now;
CreateHostBuilder(args).Build().Run();
}