Skip to content

Instantly share code, notes, and snippets.

View we4tech's full-sized avatar

Hossain Khan we4tech

View GitHub Profile
@we4tech
we4tech / es_result_transformer.js
Created April 9, 2014 09:01
ElasticSearch result transformer example
var data = [],
now = new Date().getTime();
root.hits.hits.forEach(function(hit) {
var d = new Date(),
ts = hit.fields.expired_at_de * 1000;
d.setTime(ts);
var diffInHours = (now - ts) / 1000.0 / 60.0 / 60.0;
@we4tech
we4tech / _your_model.rb
Created June 20, 2012 03:49
Fix 1 error(s) on assignment of multiparameter attributes for dynamic column or encapsulated column accessor
# Put it on your model instance
def column_for_attribute(name)
# Put your accessor name, in my case :value
if :value == name.to_sym
self.class.columns_hash[self.field.value_column.to_s]
else
self.class.columns_hash[name.to_s]
end
end
@we4tech
we4tech / alter-owner.sql
Last active May 5, 2022 19:51
PostgresSQL alter owner for all tables
-- Geneate query
SELECT format('alter table %s owner to root;', tablename) FROM pg_tables WHERE schemaname = 'public'
-- On psql execute \gexec to eval all above queries.
\gexec
-- Generate query for sequences
select format('alter sequence %s owner to root;', sequencename) FROM pg_sequences WHERE schemaname = 'public';
'shun
'slid
'twas
2,4-d
30-30
a-cry
a-day
a-hey
a-one
a-row
@we4tech
we4tech / case.go
Last active October 23, 2020 15:09
Golang test case vs return
package envars
import "errors"
var (
ErrorVaultSecretNotFound2 = errors.New("vault secret not found")
ErrorCannotFindValue2 = errors.New("can not find value")
)
func IsNotFoundError2(err error) bool {
@we4tech
we4tech / iptables_rule.sh
Created May 4, 2020 02:07
Iptables to forward egress (outbound) request to another IP and port
##
# Redirect all outbound requests from port 80 to a specific IP and port.
#
iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination $DEST_SERVER:$DEST_PORT
@we4tech
we4tech / yaml_preserved_string_quote.rb
Created November 4, 2019 19:29
How to use double quoted a string node
class YAMLPreservedStringQuote
def initialize(*doublequoted_nodes)
@doublequoted_nodes = doublequoted_nodes
end
def load(str)
ast = YAML.parse_stream(StringIO.new(str))
ast.grep(Psych::Nodes::Mapping).each do |node|
node.children.each_slice(2) do |key, value|
@we4tech
we4tech / total_mem_usages_by_unicorn.sh
Created October 30, 2019 19:14
Capture total memory usages by all unicorn process
ps axu | grep "unicorn" | grep -v "grep" | awk '{split($0,a," "); s+=a[6]} END {print s}' | pbcopy
@we4tech
we4tech / parse_new_relic_insides_data.rb
Created February 9, 2018 19:51
New Relic Insides Data Parser
#!/usr/bin/env ruby
#
# frozen_string_literal: true
require 'byebug'
require 'json'
require 'csv'
module NewRelicInsides
class Parser
@we4tech
we4tech / run_docker.go
Created June 9, 2019 03:51
Use docker client to handle STDIN and STDOUT from a running container
package main
import (
"bufio"
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"io"