Skip to content

Commit

Permalink
feat(cdk-ops): add libdatawarehouse
Browse files Browse the repository at this point in the history
- Introduces a new Lambda layer `lambda/libdatawarehouse` that provides
  utilities to handle the data warehouse for CloudFront access logs. A
  new CDK construct `LibdatawarehouseLayer` provisions it.

issue codemonger-io#30
  • Loading branch information
kikuomax committed Oct 8, 2022
1 parent 268279f commit 570d151
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cdk-ops/lambda/libdatawarehouse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
*.egg-info
3 changes: 3 additions & 0 deletions cdk-ops/lambda/libdatawarehouse/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
13 changes: 13 additions & 0 deletions cdk-ops/lambda/libdatawarehouse/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[metadata]
name = libdatawarehouse
version = attr: libdatawarehouse.VERSION
description = Library for Codemonger Data Warehouse

[options]
packages =
libdatawarehouse
package_dir =
= src

[options.package_data]
libdatawarehouse = *.pyi, py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

"""Library for Codemonger Data Warehouse.
"""

VERSION = '0.1.0'

ACCESS_LOGS_DATABASE_NAME = 'access_logs'
37 changes: 37 additions & 0 deletions cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/data_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

"""Provides utilities to access the Redshift Data API.
"""

import time
from typing import Dict, Optional, Tuple


RUNNING_STATUSES = ['SUBMITTED', 'PICKED', 'STARTED']


def wait_for_results(
client,
statement_id: str,
polling_interval: float = 0.05,
polling_timeout: int = round(300 / 0.05),
) -> Tuple[Optional[str], Dict]:
"""Waits for a given statement to finish.
:param RedshiftDataAPIService.Client client: Redshift Data API client.
:param float polling_interval: interval in seconds between two consecutive
pollings.
:param int polling_timeout: timeout represented as the number of pollings.
"""
polling_counter = 0
while True:
res = client.describe_statement(Id=statement_id)
status = res['Status']
if status not in RUNNING_STATUSES:
return status, res
polling_counter += 1
if polling_counter >= polling_timeout:
return None, res
time.sleep(polling_interval)
22 changes: 22 additions & 0 deletions cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-

"""Common exceptions.
"""

class DataWarehouseException(Exception):
"""Base exception raised when a data warehouse operation fails.
"""
def __init__(self, message: str):
"""Initializes with a message.
"""
self.message = message


def __str__(self) -> str:
classname = type(self).__name__
return f'{classname}({self.message})'


def __repr__(self) -> str:
classname = type(self).__name__
return f'{classname}({repr(self.message)})'
Empty file.
16 changes: 16 additions & 0 deletions cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

"""Tables in the data warehouse.
"""

ACCESS_LOG_TABLE_NAME = 'access_log'

REFERER_TABLE_NAME = 'referer'

PAGE_TABLE_NAME = 'page'

USER_AGENT_TABLE_NAME = 'user_agent'

EDGE_LOCATION_TABLE_NAME = 'edge_location'

RESULT_TYPE_TABLE_NAME = 'result_type'
20 changes: 20 additions & 0 deletions cdk-ops/lib/libdatawarehouse-layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as path from 'path';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { Construct } from 'constructs';

import { PythonLibraryLayer } from 'cdk2-python-library-layer';

/** CDK construct that provisions a Lambda layer of `libdatawarehouse`. */
export class LibdatawarehouseLayer extends Construct {
/** Lambda layer of `libdatawarehouse`. */
readonly layer: lambda.ILayerVersion;

constructor(scope: Construct, id: string) {
super(scope, id);

this.layer = new PythonLibraryLayer(this, 'Layer', {
runtime: lambda.Runtime.PYTHON_3_8,
entry: path.join('lambda', 'libdatawarehouse'),
});
}
}

0 comments on commit 570d151

Please sign in to comment.