Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 04:56 (UTC -07:00)
View GitHub Profile
@StevenJL
StevenJL / psql_kill_long_running.sql
Last active August 31, 2021 16:31
Psql Kill Long Running Queries
/*
Kill long-running read-only queries
*/
SELECT
application_name,
pg_cancel_backend(pid), /* returns boolean indicating whether or not query was killed */
state,
age(clock_timestamp(), query_start),
usename,
query
@StevenJL
StevenJL / how_many_running_longer.sql
Created January 23, 2021 08:03
How many queries running longer
SELECT count(*)
FROM pg_stat_activity
WHERE state != 'idle'
AND query_start < (NOW() - INTERVAL '60 seconds');
@StevenJL
StevenJL / psql_oldest_query.sql
Last active January 23, 2021 07:53
Psql Oldest Running Query
SELECT application_name,
pid,
state,
age(clock_timestamp(), query_start), /* How long has the query has been running in h:m:s */
usename,
query /* The query text */
FROM pg_stat_activity
WHERE state != 'idle'
AND query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start asc;
@StevenJL
StevenJL / jwt_sensitive.rb
Last active December 23, 2020 00:04
JWT with sensitive information
require "jwt"
SIGNING_SECRET = 'jwtsigningsecret'
ALGORITHM = 'HS512'
# A payload with sensitive information
payload_sensitive = { social_security_number: "555-44-7777" }
# The token has been encoded, but not encrypted
token_with_sensitive = JWT.encode payload_sensitive, SIGNING_SECRET, ALGORITHM
@StevenJL
StevenJL / jwt_invalid_sig.rb
Last active December 22, 2020 23:56
JWT Invalid Signature
require "jwt"
SIGNING_SECRET = 'jwtsigningsecret'
ALGORITHM = 'HS512'
# Suppose a sender wants to send the following payload
transfer_50_payload = { transfer_amt: 50 }
token_legit = JWT.encode payload, SIGNING_SECRET, ALGORITHM
# eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ0cmFuc2Zlcl9hbXQiOiI1MCJ9.tf8g0nTprdIzoqLVrjLJS5bYFysiLYal8o4OeBbOuebU1UxGdKYhtADLD2oLzO_P2QmZBFqsF7uAAaN3DvYYAw
@StevenJL
StevenJL / applyMiddleware.js
Created July 21, 2019 08:24
Apply Middleware
export default function applyMiddleware(...middlewares) {
return createStore => (...args) => {
const store = createStore(...args)
let dispatch = () => {
throw new Error(
'Dispatching while constructing your middleware is not allowed. ' +
'Other middleware would not be applied to this dispatch.'
)
}
@StevenJL
StevenJL / applying_redux_middleware.js
Last active July 21, 2019 08:23
Applying Redux Middleware
import { createStore, applyMiddleware } from 'redux';
import someMiddleware from 'some-redux-middleware';
import rootReducer from './reducers/index';
const store = createStore(rootReducer, applyMiddleware(someMiddleware));
class SomeForm extends React.Component {
handleFirstNameChange = (event) => {
this.setState({firstName: event.currentTarget.value});
console.log(this.state.firstName)
}
render () {
return (
<div>
// form_container.js
import { connect } from 'react-redux';
import { ReduxControlledComponent } from './components';
const mapStateToProps = (state) => {
return {
name: state.name,
email: state.email,
password: state.password
}
class ButtonComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
message: 'I do not need to be manually binded!',
}
}
// this is now defined using the arrow function syntax
handleClick = () => {