Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Trenton Potgieter committed Jun 27, 2023
1 parent e76ccbd commit b65bbd8
Show file tree
Hide file tree
Showing 73 changed files with 28,963 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Environment
*.swp
# package-lock.json
__pycache__
.pytest_cache
.venv
*.egg-info

# Infrastructure
infrastructure/config.yaml

# CDK asset staging directory
.cdk.staging
cdk.out

# macOS
.DS_Store

# PyCharm
.idea/

# pyenv
.python-version

# VSCode
.vscode
.history/
*.vsix

node_modules/
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ Be sure to:
* Change the title in this README
* Edit your repository description on GitHub

---

# Prerequisites
```
docker
npm
nodejs
python
```

# [REQUIRED] Set Configuration

In the `infrastructure` folder there is a `config.yaml.TEMPLATE`. Copy this and rename it to `config.yaml` and make the necessary changes to deploy your solution. You will have to add your own account numbers and regions at a minimum to ensure this works.


To deploy the solution
```
npm run build
npm run deploy.bootstrap
npm run deploy
```
---

## Security

See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
Expand Down
89 changes: 89 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
Copyright 2021 Amazon.com, Inc. and its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
"""

import os
import subprocess
import sys
import argparse


def exit_on_failure(exit_code, msg):
if exit_code != 0:
print(msg)
exit(exit_code)


def change_dir_with_return(dir):
current_dir = os.getcwd()
os.chdir(dir)
return lambda: os.chdir(current_dir)


def build_infrastructure():

return_dir = change_dir_with_return("./infrastructure")

cmd = [sys.executable, "build.py"]
proc = subprocess.run(cmd, stderr=subprocess.STDOUT)
exit_on_failure(proc.returncode, "Infrastructure build failed")

return_dir()


def build_web_app():

return_dir = change_dir_with_return("./web-app")
cmd = [sys.executable, "build.py"]
proc = subprocess.run(cmd, stderr=subprocess.STDOUT)
exit_on_failure(proc.returncode, "Web app build failed")

return_dir()


def build_logic():

return_dir = change_dir_with_return("./buisness-logic")

cmd = [sys.executable, "build.py"]
proc = subprocess.run(cmd, stderr=subprocess.STDOUT)
exit_on_failure(proc.returncode, "Buisness Logic build failed")

return_dir()


def main():
parser = argparse.ArgumentParser(
description="Builds parts or all of the solution. If no arguments are passed then all builds are run"
)
parser.add_argument("--infrastructure",
action="store_true", help="builds infrastructure")
parser.add_argument("--buisness_logic",
action="store_true", help="builds buisness logic")
args = parser.parse_args()

if len(sys.argv) == 1:
# build_web_app()
build_infrastructure()
build_logic()
# needs to be last to ensure the dependencies are built before the CDK deployment can build/run
else:
if args.infrastructure:
build_infrastructure()
if args.buisness_logic:
build_logic()


if __name__ == "__main__":
main()
108 changes: 108 additions & 0 deletions buisness-logic/analytics-processing/app.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
This application generates real-time metrics that are processed by Lambda.
Query outputs should adhere to the schema defined in DESTINATION_STREAM required by the Lambda function that processes output.
Additional in-application streams can be pumped into the DESTINATION_STREAM table for consumption and processing by Lambda.
Refer to the Game Analytics Pipeline Developer Guide for more information.
*/

CREATE STREAM "DESTINATION_STREAM"(
METRIC_NAME VARCHAR(1024),
METRIC_TIMESTAMP BIGINT,
METRIC_UNIT_VALUE_INT BIGINT,
METRIC_UNIT VARCHAR(1024),
DIMENSION_APPLICATION_ID VARCHAR(1024),
DIMENSION_APP_VERSION VARCHAR(1024),
DIMENSION_COUNTRY_ID VARCHAR(1024),
DIMENSION_CURRENCY_TYPE VARCHAR (1024),
DIMENSION_SPELL_ID VARCHAR (1024),
DIMENSION_MISSION_ID VARCHAR (1024),
DIMENSION_ITEM_ID VARCHAR (1024),
OUTPUT_TYPE VARCHAR(1024));

-- Total Events
-- Count of Total Events within period
CREATE OR REPLACE PUMP "TOTAL_EVENTS_PUMP" AS
INSERT INTO "DESTINATION_STREAM" (METRIC_NAME, METRIC_TIMESTAMP, METRIC_UNIT_VALUE_INT, METRIC_UNIT, DIMENSION_APPLICATION_ID, DIMENSION_APP_VERSION, OUTPUT_TYPE)
SELECT STREAM 'TotalEvents', UNIX_TIMESTAMP(TIME_WINDOW), COUNT(distinct_stream.event_id) AS unique_count, 'Count', distinct_stream.application_id, distinct_stream.app_version, 'metrics'
FROM (
SELECT STREAM DISTINCT
rowtime as window_time,
"AnalyticsApp_001"."event_id" as event_id,
"AnalyticsApp_001"."application_id" as application_id,
"AnalyticsApp_001"."app_version" as app_version,
STEP("AnalyticsApp_001".rowtime BY INTERVAL '1' MINUTE) as TIME_WINDOW
FROM "AnalyticsApp_001"
) as distinct_stream
GROUP BY
application_id,
app_version,
TIME_WINDOW,
STEP(distinct_stream.window_time BY INTERVAL '1' MINUTE);

-- Total Logins
-- Count of logins within period
CREATE OR REPLACE PUMP "LOGIN_PUMP" AS
INSERT INTO "DESTINATION_STREAM" (METRIC_NAME, METRIC_TIMESTAMP, METRIC_UNIT_VALUE_INT, METRIC_UNIT, DIMENSION_APPLICATION_ID, DIMENSION_APP_VERSION, OUTPUT_TYPE)
SELECT STREAM 'TotalLogins', UNIX_TIMESTAMP(TIME_WINDOW), COUNT(distinct_stream.login_count) AS unique_count, 'Count', distinct_stream.application_id, distinct_stream.app_version, 'metrics'
FROM (
SELECT STREAM DISTINCT
rowtime as window_time,
"AnalyticsApp_001"."event_id" as login_count,
"AnalyticsApp_001"."application_id" as application_id,
"AnalyticsApp_001"."app_version" as app_version,
STEP("AnalyticsApp_001".rowtime BY INTERVAL '1' MINUTE) as TIME_WINDOW
FROM "AnalyticsApp_001"
WHERE "AnalyticsApp_001"."event_type" = 'login'
) as distinct_stream
GROUP BY
application_id,
app_version,
TIME_WINDOW,
STEP(distinct_stream.window_time BY INTERVAL '1' MINUTE);

-- Knockouts By Spells
-- Get the number of knockouts by each spell used in a knockout in the period
CREATE OR REPLACE PUMP "KNOCKOUTS_BY_SPELL_PUMP" AS
INSERT INTO "DESTINATION_STREAM" (METRIC_NAME, METRIC_TIMESTAMP, METRIC_UNIT_VALUE_INT, METRIC_UNIT, DIMENSION_SPELL_ID, DIMENSION_APPLICATION_ID, DIMENSION_APP_VERSION, OUTPUT_TYPE)
SELECT STREAM 'KnockoutsBySpell', UNIX_TIMESTAMP(TIME_WINDOW), SPELL_COUNT, 'Count', SPELL_ID, application_id, app_version, 'metrics'
FROM (
SELECT STREAM
events."spell_id" as SPELL_ID,
events."application_id" as application_id,
events."app_version" as app_version,
count(*) as SPELL_COUNT,
STEP(events.rowtime BY INTERVAL '1' MINUTE) as TIME_WINDOW
FROM "AnalyticsApp_001" events
WHERE events."spell_id" is not NULL
AND events."event_type" = 'user_knockout'
GROUP BY
STEP (events.ROWTIME BY INTERVAL '1' MINUTE),
events."spell_id",
events."application_id",
events."app_version"
HAVING count(*) > 1
ORDER BY STEP (events.ROWTIME BY INTERVAL '1' MINUTE), SPELL_COUNT desc
);

-- Purchases
-- Get all purchases grouped by country over the period
CREATE OR REPLACE PUMP "PURCHASES_PER_CURRENCY_PUMP" AS
INSERT INTO "DESTINATION_STREAM" (METRIC_NAME, METRIC_TIMESTAMP, METRIC_UNIT_VALUE_INT, METRIC_UNIT, DIMENSION_CURRENCY_TYPE, DIMENSION_APPLICATION_ID, DIMENSION_APP_VERSION, OUTPUT_TYPE)
SELECT 'Purchases', UNIX_TIMESTAMP(TIME_WINDOW), PURCHASE_COUNT, 'Count', CURRENCY_TYPE, application_id, app_version, 'metrics' FROM (
SELECT STREAM
events."currency_type" as CURRENCY_TYPE,
events."application_id" as application_id,
events."app_version" as app_version,
count(*) as PURCHASE_COUNT,
STEP(events.rowtime BY INTERVAL '1' MINUTE) as TIME_WINDOW
FROM "AnalyticsApp_001" events
WHERE events."currency_type" is not NULL
AND events."event_type" = 'iap_transaction'
GROUP BY
STEP (events.ROWTIME BY INTERVAL '1' MINUTE),
events."currency_type",
events."application_id",
events."app_version"
HAVING count(*) > 1
ORDER BY STEP (events.ROWTIME BY INTERVAL '1' MINUTE), PURCHASE_COUNT desc
);
33 changes: 33 additions & 0 deletions buisness-logic/analytics-processing/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Copyright 2021 Amazon.com, Inc. and its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
"""

import os
import subprocess
import sys
import shutil


def exit_on_failure(exit_code, msg):
if exit_code != 0:
print(msg)
exit(exit_code)


dir_path = os.path.dirname(os.path.realpath(__file__))

npm_cmd = shutil.which("npm")
cmd = [npm_cmd, "install", "--prefix", dir_path]
proc = subprocess.run(cmd, stderr=subprocess.STDOUT)
exit_on_failure(proc.returncode, "Web app npm install failed")
37 changes: 37 additions & 0 deletions buisness-logic/analytics-processing/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
'use strict';

/**
* Lib
*/

let lib = require('./lib/process.js');

exports.handler = function(event, context, callback) {
console.log(`Analytics Processing service received event`);

lib
.respond(event)
.then(data => {
return callback(null, data);
})
.catch(err => {
return callback(err, null);
});
};
Loading

0 comments on commit b65bbd8

Please sign in to comment.