Skip to content

Instantly share code, notes, and snippets.

View markrittman's full-sized avatar

Mark Rittman markrittman

View GitHub Profile
@markrittman
markrittman / gist:17a1b2ad994f7fcc8d0fbe855392fe63
Created June 21, 2018 12:14
Script to patch (rebuild using definition in Github) a business view for a client tracking ID
if [ "$#" -ne 4 ]
then
echo "Usage: ./patchbv client_id tracking_id view_name nested_flatted_flag i.e. patchbv 37028 dnata_travel experience false"
exit 1
fi
echo "patching qubit-client-$1 $2 $3"
gcloud auth login mark.rittman@qubit.com
gcloud config set project qubit-client-"$1"
echo "deleting existing objects for $2"
bq rm -f -t qubit-client-"$1":qubit_bi_"$2".qp_bi_"$3"_all_time_up_to_yesterday
@markrittman
markrittman / gist:2bb637c25f73224ed9ab2c3860841f7f
Created June 21, 2018 12:58
check_materialization_logs.sh
if [ "$#" -ne 1 ]
then
echo "Usage: ./check_materialization_logs.sh tracking_id i.e. check_materialization_logs.sh dnata_travel"
exit 1
fi
echo "checking status of tracking_id $1"
gcloud auth login mark.rittman@qubit.com
gcloud config set project qubit-datateam
bq query "SELECT * from [qubit-datateam:livetap.bi_data_completeness_streamed_log] where meta_trackingId = '"${1}"' and ts = (select max(ts) from livetap.bi_data_completeness_streamed_log where meta_trackingId = '"${1}"') order by ts desc"
bq query "SELECT ts,unique_id, tracking_id, processed_date, status FROM [qubit-datateam:livetap.bi_data_materialisation_streamed_log] WHERE tracking_id = '"${1}"' order by ts desc limit 15"
@markrittman
markrittman / transaction_sequence.txt
Last active February 16, 2020 14:36
Additions to Standard Qubit Looker Block for Merchandising Analytics
##Additional LookML view (transaction_sequence.view.lkml)##
view: transaction_sequence {
view_label: "Product Interactions and Sales"
derived_table: {
sql: WITH view AS (SELECT
* EXCEPT(event_number)
FROM
(SELECT
TIMESTAMP_ADD(meta_serverTs, INTERVAL 0 MICROSECOND) AS property_event_ts,
@markrittman
markrittman / segment_pages_sessionized_with_stats.sql
Last active March 27, 2020 08:24
SQL script for use with Redshift as a Segment warehouse destination. Aggregates page views into user sessions with 60 seconds cut-off and adds count of page views in session, time between sessions, bounced session flag, session length and truncates session start time to the hour
SELECT
date_trunc('hours'::text, session_start_ts) AS session_start_hour,
session_start_ts,
date_diff ('seconds'::text,
pg_catalog.lead(session_start_ts, 1) OVER (PARTITION BY user_id ORDER BY session_start_ts DESC),
session_start_ts) AS secs_between_sessions,
user_id,
global_session_id,
user_session_id,
page_views_in_session,
@markrittman
markrittman / gist:70b141870f9bca8815fa8f8c32f7ee12
Created July 21, 2020 22:12
Actuals vs. Target Calculations for Shopify data in Redshift
WITH
actuals AS (
SELECT
shopify_shop,
CASE
WHEN billing_address__country_code = 'GB' THEN 'UK'
WHEN billing_address__country_code = 'US' THEN 'US'
ELSE
'RoW'
END
@markrittman
markrittman / segment_pageview_parser.sql
Created October 8, 2020 13:18
Simple Segment Pages row parser for generating page category, product name size and colour, channel and source for eCommerce site
with page_views as (
SELECT *,
case when split(context_page_path,'/')[safe_offset(1)] = 'products' then 'Product Category Page'
when split(context_page_path,'/')[safe_offset(1)] = 'shop-all' then 'Product Details Page'
when split(context_page_path,'/')[safe_offset(1)] = 'search-products' then 'Search Page'
else 'Home Page' end as page_type,
case when split(context_page_path,'/')[safe_offset(1)] = 'shop-all' then
replace(replace(replace(replace(replace(split(split(context_page_path,'/')[safe_offset(2)],'_')[safe_offset(0)],
'-white',''),'-blush-pink',''),'-black',''),'-black-black',''),'-',' ') end as product_name,
case when split(context_page_path,'/')[safe_offset(1)] = 'shop-all' then
@markrittman
markrittman / looker_query_history.sql
Created February 10, 2021 22:49
BiqQuery StandardSQL query to return Looker query history using Stitch Looker (v1) Integration (https://www.stitchdata.com/docs/integrations/saas/looker)
WITH
history AS (
SELECT
*
FROM (
SELECT
h.query_id,
h.history_created_date AS query_ts,
h.user_id AS user_id,
h.history_query_run_count AS query_count,
@markrittman
markrittman / list_dataset_partitioning.sql
Created June 24, 2021 20:22
List all datasets with partitioned tables, broken-down by partitioning type
SELECT
TABLE_SCHEMA,
count(distinct table_name) as table_count,
CASE
WHEN IS_SYSTEM_DEFINED = 'YES' THEN 'Ingestion-Time'
WHEN data_type = 'TIMESTAMP' THEN concat('Time-unit column')
WHEN data_type = 'INT64' THEN concat('Integer Range')
END
AS partitioning_type
FROM
https://CLIENT_DOMAIN.looker.com/explore/system__activity/history?fields=look.title,history.completed_week,history.query_run_count,history.average_runtime&pivots=history.completed_week&fill_fields=history.completed_week&f[history.completed_week]=4+weeks&f[look.title]=-EMPTY&sorts=history.query_run_count+desc+0,history.completed_week&limit=500&vis=%7B%22show_view_names%22%3Afalse%2C%22show_row_numbers%22%3Atrue%2C%22transpose%22%3Afalse%2C%22truncate_text%22%3Atrue%2C%22hide_totals%22%3Afalse%2C%22hide_row_totals%22%3Afalse%2C%22size_to_fit%22%3Atrue%2C%22table_theme%22%3A%22white%22%2C%22limit_displayed_rows%22%3Afalse%2C%22enable_conditional_formatting%22%3Atrue%2C%22header_text_alignment%22%3A%22left%22%2C%22header_font_size%22%3A%2212%22%2C%22rows_font_size%22%3A%2212%22%2C%22conditional_formatting_include_totals%22%3Afalse%2C%22conditional_formatting_include_nulls%22%3Afalse%2C%22show_sql_query_menu_options%22%3Afalse%2C%22show_totals%22%3Atrue%2C%22show_row_totals%22%3Atrue%2C%22series_cell_visualization
https://LOOKER_DOMAIN.looker.com/explore/system__activity/history?fields=history.dashboard_run_count,history.average_runtime,history.completed_week,dashboard.title&pivots=history.completed_week&fill_fields=history.completed_week&f[history.dashboard_session]=-NULL&f[history.completed_week]=4+weeks&f[dashboard.title]=-NULL&sorts=history.completed_week+desc,impact+desc+1&limit=500&vis=%7B%22show_view_names%22%3Afalse%2C%22show_row_numbers%22%3Atrue%2C%22transpose%22%3Afalse%2C%22truncate_text%22%3Atrue%2C%22hide_totals%22%3Afalse%2C%22hide_row_totals%22%3Afalse%2C%22size_to_fit%22%3Atrue%2C%22table_theme%22%3A%22white%22%2C%22limit_displayed_rows%22%3Afalse%2C%22enable_conditional_formatting%22%3Atrue%2C%22header_text_alignment%22%3A%22left%22%2C%22header_font_size%22%3A%2212%22%2C%22rows_font_size%22%3A%2212%22%2C%22conditional_formatting_include_totals%22%3Afalse%2C%22conditional_formatting_include_nulls%22%3Afalse%2C%22color_application%22%3A%7B%22collection_id%22%3A%22legacy%22%2C%22palette_id%22%3A%22looker