Skip to content

Instantly share code, notes, and snippets.

View robcowie's full-sized avatar

Rob Cowie robcowie

  • Recycleye
  • Leeds/London, United Kingdom
View GitHub Profile
@robcowie
robcowie / dag_duration.sql
Created May 4, 2020 13:08
Airflow Task & DAG Duration Queries
-- duration of task instances
select
date(execution_date) as dt,
avg(duration) as avg_duration,
min(duration) as min_duration,
max(duration) as max_duration
from
task_instance
group by
date(execution_date)
@robcowie
robcowie / random_strings.sql
Created April 30, 2020 10:44
Generate random strings in Postgresql
-- Fixed-length ascii strings
CREATE OR REPLACE FUNCTION pg_temp.random_string(int) RETURNS text
AS $$ SELECT
array_to_string(ARRAY(SELECT chr(ascii('B') + round(random() * 25)::integer)
FROM
generate_series(1, $1)), '') $$
LANGUAGE sql;
select pg_temp.random_string(8);
@robcowie
robcowie / gcloud_configurations.md
Created April 20, 2020 22:27
gcloud named configurations

Manage Local GCP Config

To see help for gcloud named configurations

gcloud topic configurations

To list & describe configurations

@robcowie
robcowie / postgresql_management_queries.sql
Last active May 22, 2021 16:28
Useful Postgresql Management Queries
-----------------------------------------------------------------------------------------------------------------------------------
-- USEFUL POSTGRESQL MANAGEMENT QUERIES --
-- See https://medium.com/compass-true-north/dealing-with-significant-postgres-database-bloat-what-are-your-options-a6c1814a03a5 --
-----------------------------------------------------------------------------------------------------------------------------------
-- SHOW RUNNING QUERIES
SELECT
state,
pid,
@robcowie
robcowie / fix_unicode.py
Created March 22, 2020 15:41
Fix unicode. Python.
# -*- coding: utf-8 -*-
"""OLD AND UNTESTED"""
import unicodedata
def fix_bad_unicode(text):
u"""
Something you will find all over the place, in real-world text, is text
that's mistakenly encoded as utf-8, decoded in some ugly format like
@robcowie
robcowie / pg_vacuuming_queries.md
Last active February 18, 2020 13:43
PostgreSQL vacuuming monitoring queries

Postgresql Vacuuming

To monitor progress of autovacuum

SELECT
    heap_blks_scanned/cast(heap_blks_total as numeric) * 100 as heap_blks_percent,
    progress.*,
    activity.query
FROM
@robcowie
robcowie / marketplace.py
Created November 4, 2019 15:15
Amazon marketplaces with api endpoints
"""Amazon Marketplace and Region Info.
* [MWS docs](https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html)
* [Advertising API docs](https://advertising.amazon.com/API/docs/en-us/get-started/how-to-use-api)
"""
from dataclasses import dataclass
@dataclass
@robcowie
robcowie / circleci.yml
Created October 25, 2019 16:41
Example circleci config for running tests with a postgresql DB
version: 2.1
jobs:
test-cf-audit-report-data:
docker:
- image: circleci/python:3.7.1
- image: circleci/postgres:11.4-alpine-ram
environment:
POSTGRES_USER: user
POSTGRES_DB: test
@robcowie
robcowie / install_lsp_sublime.md
Last active August 29, 2022 19:19
Install and configure the python language server for Sublime Text 3 (using the LSP plugin)

Install and configure Python LSP & Sublime Text 3

Install the langauge server

This installs the python server provided by Palantir.

Microsoft also provide the C# server which requires the .net runtime.

pipenv install 'python-language-server[all]'
@robcowie
robcowie / sqlalchemy_create_statement.py
Last active August 30, 2019 09:41
Print create SQL statement for tales and indices defined with SQLAlchemy
from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy.schema import CreateIndex, CreateTable
metadata = MetaData()
engine = create_engine('')
def create_table_statement(connection, table):
stmts = []