forked from codemonger-io/codemonger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
9 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/build | ||
*.egg-info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build-system] | ||
requires = ["setuptools", "wheel"] | ||
build-backend = "setuptools.build_meta" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/data_api.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/exceptions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/tables.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}); | ||
} | ||
} |