Skip to content

Instantly share code, notes, and snippets.

View ContrastingSounds's full-sized avatar

Jon Walls ContrastingSounds

View GitHub Profile
@ContrastingSounds
ContrastingSounds / EndStateUsageAndCharge.xml
Created January 29, 2018 18:47
Cloud Cruiser workbook for running bespoke monthly charging sql (line 49, EXEC [dbo].[sp_end_state_usage]), and then triggering charge step (line 71)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
<bean id="processInfo" class="com.cloudcruiser.batch.ProcessInfo">
<property name="processName" value="EndStateUsageAndCharge" />
<property name="usageTimeZone" value="Europe/London" />
</bean>
@ContrastingSounds
ContrastingSounds / sp_end_state_usage.xml
Created January 29, 2018 18:45
XML wraparound for the sp_end_state_usage.sql stored procedure, for uploading via web UI.
<?xml version="1.0" encoding="UTF-8"?><procedure description="Zeros out usage measures except for last record of month per resource" name="sp_end_state_usage">
<sqlserver>
<![CDATA[
DELIMITER GO
CREATE PROCEDURE $CCATTRIBUTE(name)
@inSelectDate NCHAR(8)
AS
@ContrastingSounds
ContrastingSounds / sp_end_state_usage.sql
Created January 29, 2018 18:44
Enables a bespoke charging model, so that resources that may change size during the month (e.g. disk size) are charged at their final value for the month (Cloud Cruiser's internal algorithm only directly supports charging for the peak value. )
DELIMITER GO
CREATE PROCEDURE $CCATTRIBUTE(name)
@inSelectDate NCHAR(8)
AS
BEGIN
DECLARE @vStartDate BIGINT;
DECLARE @vEndDate BIGINT;
SET @vStartDate = dbo.cc_date_to_seconds(DATEADD(MONTH, DATEDIFF(MONTH, 0, CAST(@inSelectDate + N' 00:00:00' AS datetime)), 0));
@ContrastingSounds
ContrastingSounds / us_budget_outlays.lkml
Last active January 26, 2018 15:28
Using Liquid templating to simplify a CROSS JOIN in BiqQuery (un-pivoting a table)
view: outlays {
derived_table: {
sql:
SELECT
account_name,
agency_name,
bureau_name,
subfunction_title,
treasury_agency_code,
CAST(REPLACE(outlays.TQ, ",", "") AS INT64) AS tq,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ContrastingSounds
ContrastingSounds / stream_to_bigquery.py
Created January 15, 2018 09:20
Streams data to an existing Google BigQuery table.
import logging
from google.cloud import bigquery
SERVICE_ACCOUNT = '/path/to/credentials/file/<role>-<project>.json'
BQ_DATASET = 'state_data'
client = bigquery.Client.from_service_account_json(SERVICE_ACCOUNT)
dataset = BQ_DATASET
logger = logging.getLogger()
@ContrastingSounds
ContrastingSounds / generic_saving_as_avro.py
Created January 15, 2018 09:14
Stub functions to dynamically create an Avro schema, and save records to file.
import json
import avro.schema
from avro.datafile import DataFileWriter
from avro.io import DatumWriter
STATE_SCHEMA = 'state_schema.avsc'
STATE_RECORDS = 'state_records.avro'
model_dictionary = {
'dimensions': set(),
@ContrastingSounds
ContrastingSounds / State Machine Spreadsheet to PDF Diagrams.ipynb
Last active October 11, 2024 09:42
Generate state machine diagrams automatically from a spreadsheet of states and transitions. Supports Google Sheets and Excel.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ContrastingSounds
ContrastingSounds / DynamicSchemaGeneration.ipynb
Created January 1, 2018 17:24
Shows how to dynamically create a database schema using SQLAlchemy and a table schema stored in a Python dictionary. For completeness, includes creating the original database (in this case using PostgreSQL).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ContrastingSounds
ContrastingSounds / swap_db.sql
Created December 31, 2017 13:12
Swaps databases for testing of SQL Server based applications. Backs up current database, replaces it with a prior version.
-- old: Location to save backup
-- db_name: Name of the SQL Server Database
-- app_admin: Username of the administration login
-- default_password: Default password for a new administration login
-- BACKUP DATABASE TO DEFAULT LOCATION - NOTE: Filename currently hardcoded
BACKUP DATABASE [{{ db_name }}] TO DISK = '{{ old }}' WITH NOFORMAT, NOINIT, NAME = N'{{ db_name }}-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO