Skip to content

Instantly share code, notes, and snippets.

@lewish
lewish / BUILD
Created February 15, 2023 17:23
protobuf-ts bazel rules
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")
load("@npm//:@protobuf-ts/plugin/package_json.bzl", protobuf_ts_plugin = "bin")
package(default_visibility = ["//visibility:public"])
protobuf_ts_plugin.protoc_gen_ts_binary(
name = "protoc_gen_ts",
)
proto_plugin(
@lewish
lewish / sessionized_segment_data.sql
Last active September 19, 2019 13:29
Sessionized segment tracks & pages (BigQuery)
/*
This query creates a combined view of tracks and pages from segment data.
Sessions are computed by finding the any session start timestamp denoted by an activity gap of 30 minutes.
Session IDs are assigned to each track or page record using a combination of the session index, user ID, and date.
*/
WITH
with_session_starts AS (
SELECT
*,
COALESCE( (UNIX_MILLIS(timestamp) - UNIX_MILLIS(LAG(timestamp) OVER (PARTITION BY user_id ORDER BY timestamp ASC)))/(1000*60) >= 30,
/*
* Build a sample from the given values array with replacement.
*/
function sample(values) {
var sampleValues = [];
for (let i = 0; i < values.length; i++) {
sampleValues.push(values[Math.floor(Math.random() * values.length)]);
}
return sampleValues;
}