Skip to content

Instantly share code, notes, and snippets.

View trojanh's full-sized avatar

Rajan Tiwari trojanh

  • Mumbai
View GitHub Profile
@trojanh
trojanh / gist:bbdc466a9134ec5d72592c919985665f
Created July 1, 2021 07:41
Grafana Prometheus Nodejs Integration Steps using prom-client
# Pre-requisite
Should have node >=12
# Install
npm i prom-client
# Setup
//https://docs.mongodb.com/v3.2/reference/method/db.collection.bulkWrite
db.enemies.bulkWrite(
[
{ updateMany :
{
"filter" : { "rating" : { $gte : 3} },
"update" : { $inc : { "encounter" : 0.1 } }
},
},
@trojanh
trojanh / Mongodb Aggregate with conditional groupBy and projections
Last active April 13, 2021 15:05
Mongodb Aggregate with conditional groupBy and projections
db.getCollection('panData').aggregate([
{ $match: { createdAt: { $gte: '2020-04-01T18:30:00.000Z' } } },
{
$group: {
_id: { partner: '$productCode', project: '$source' },
project: { $sum: { $cond: [{ $eq: ['$cached', true] }, 1, 0] } },
apiHits: { $sum: { $cond: [{ $eq: ['$cached', true] }, 1, 0] } },
dbHits: { $sum: { $cond: [{ $eq: ['$cached', false] }, 1, 0] } },
hits: { $sum: 1 }
}
@trojanh
trojanh / Mongodb Aggregate Facet queries
Last active August 20, 2022 09:36
Mongodb example for aggregate pipeline with facet, lookup, group, project and match queries
/*
* This is an example of multi stage facet query in MongoDb to cover few possible combinations of query
* Facets allows us to write multiple indepent queries to get facet result which is usually used for obtaining stats from DB
* or tags for products or blogs with product count, category count from products list with product count etc.
*/
db.getCollection('applications').aggregate([
{
$match: {
partner: { $in: [ObjectId("5e8ac6806a99770011f2f38b")] },
@trojanh
trojanh / Issues Setting Up Jest in Nodejs.md
Last active March 2, 2023 23:31
Issues Setting Up Jest, Nock, Supertest for Testing Nodejs with External API call

To prevent Supertest from throwing following error

port already in use

Add following line in app.js or main express server file

  if (NODE_ENV !== 'test') {
      await app.listen(PORT)
  }
// deepClone.js
x = { a: 1, b: 2, z: { c: 3, d: 4, y: { e: 5 } } }
const deepClone = (obj) => {
if (typeof obj === "object" && obj !== null) {
return Object
.keys(obj)
.map((key) => {
return {
1. brew install postgres
2. /usr/local/opt/postgres/bin/createuser -s postgres
Step to create default user
@trojanh
trojanh / ghost-finder.js
Last active November 29, 2019 11:42
Customized ghost-finder to add custom css for tags and authors
/*
This is an example on how I customized the ghostfinder ghost pulgin i.e https://github.com/electronthemes/ghost-finder
to show all tags and authors along with custom css for them.
*/
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
@trojanh
trojanh / delete_git_submodule.md
Created November 26, 2019 11:46 — forked from myusuf3/delete_git_submodule.md
How effectively delete a git submodule.

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m "Removed submodule "
  • Delete the now untracked submodule files rm -rf path_to_submodule
@trojanh
trojanh / Transformer.ex
Last active March 16, 2022 07:35
Create Image thumbnail using ImageMagick in Elixir
defmodule Image.Transformer do
def transform(original_file, operation \\ "convert") do
thumb_path = generate_thumb_file(original_file)
System.cmd(operation, operation_commands(original_file_path, thumb_path))
thumb_path
end
defp generate_thumb_file(original_file) do