Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Badges #100

Draft
wants to merge 73 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
ae8e84d
🚀 crowdaction hex refactoring init
edreinoso Feb 5, 2022
ac8387f
🚧 dynamodb manager repository, pt1: init
edreinoso Feb 5, 2022
936fb1e
badges on crowdactions and profiles
rubenhorn Feb 20, 2022
7ae52c7
⚡️ launching own sam config
edreinoso Feb 25, 2022
f0656c4
Merge branch 'development' into feat/gh-73/crowdactions
edreinoso Feb 25, 2022
1eae809
Merge branch 'development' into feat/gh-73/crowdactions
edreinoso Feb 25, 2022
9963992
🚧 [crowdaction] deployed first function on to aws
edreinoso Feb 26, 2022
605e564
🚧 [crowdaction] removing extra copy of dynamo repository
edreinoso Feb 26, 2022
0423b32
🚧 [crowdaction] init model and validation on request
edreinoso Feb 26, 2022
d7daae2
🚧 [crowdaction] changed the model function names
edreinoso Mar 4, 2022
e684acf
🚧 [crowdaction] working on internal and handler
edreinoso Mar 9, 2022
32666f8
🚧 [crowdaction] simple version of GET request by ID
edreinoso Mar 10, 2022
c25eec9
🚧 [crowdaction] testing GET request by ID
edreinoso Mar 11, 2022
bae64a5
🚧 [crowdaction] creating external function to GET crowdaction by ID
edreinoso Mar 11, 2022
3d5c1a5
🚧 [crowdaction] including query function in ddb repository
edreinoso Mar 19, 2022
33a7ab1
🚧 [crowdaction] initial code base for GET crowdactions by status
edreinoso Mar 19, 2022
b555b09
🚧 [crowdaction] cleaning up, commitment options
edreinoso Mar 19, 2022
841b968
🚧 [crowdaction] including the mock repository for dynamo and crowdaction
edreinoso Mar 20, 2022
483e006
🚧 [crowdaction] modifying the mock repository to include GetCrowdacti…
edreinoso Mar 20, 2022
9ce295a
🚧 [crowdaction] removed unnecessary comments, per the feedback on pul…
edreinoso Mar 20, 2022
8c5c280
[pull request] removing samconfig file from history
edreinoso Mar 23, 2022
99f52ef
[pull request] ignoring samconfig file as per the pull request
edreinoso Mar 23, 2022
4dc7789
[pull request] modified naming for the internal interface
edreinoso Mar 23, 2022
18aa963
[pull request] getting rid of useless else statement
edreinoso Mar 23, 2022
8fb01c0
[pull request] removing dynamodb dependencies in logic
edreinoso Mar 24, 2022
a47a23b
[pull request] including mock repository and crowdaction test
edreinoso Mar 24, 2022
39c7417
[pull request] cleaning up some unnecessary lines
edreinoso Mar 24, 2022
525b2b4
[pull request] separating repository concerns
edreinoso Mar 28, 2022
595b737
[pull request] modifying test cases with new repository structure
edreinoso Mar 28, 2022
98f45af
[pull request] changing dynamo repository call from crowdaction main …
edreinoso Mar 28, 2022
45cb593
[pull request] changing parameters to pass unit test case
edreinoso Mar 28, 2022
93138e2
[pull request] changing crowdactionID from test case
edreinoso Mar 28, 2022
f03c63a
[pull request] changing method call for crowdaction test
edreinoso Mar 28, 2022
8449139
[pull request] fixing parameter issue in crowdaction_test
edreinoso Mar 29, 2022
14f671a
[pull request] testing issue with the args.get(x) call in mock/dynamo…
edreinoso Mar 29, 2022
09ed55a
[pull request] including Mock in each of the function calls from the …
edreinoso Mar 29, 2022
589a25c
[pull request] modifying test cases for crowdaction
edreinoso Mar 29, 2022
72c469f
[pull request] changes done based on feedback
edreinoso Mar 23, 2022
11d5e7f
Merge branch 'feat/gh-73/crowdactions' of github.com:CollActionteam/c…
edreinoso Mar 29, 2022
4c3e35e
[pull request] removing unnecessary hello world
edreinoso Apr 2, 2022
4c44370
[pull request] removing some unnecessary comments
edreinoso Apr 2, 2022
77bed7e
pointing system for the commitments in crowdactions
edreinoso Apr 8, 2022
9858c8b
Merge branch 'development' of github.com:CollActionteam/collaction_ba…
edreinoso Apr 8, 2022
4459c00
Merge branch 'feat/gh-73/crowdactions' of github.com:CollActionteam/c…
edreinoso Apr 8, 2022
aa80c90
Merge remote-tracking branch 'origin/feat/gh-111/badge_threshold' int…
edreinoso Jun 11, 2022
17cd6fb
Squashed commit of the following:
edreinoso Jun 11, 2022
ffa947c
cleaning up the aws crowdaction repository
edreinoso Jun 11, 2022
50a9d43
functions to handle the badge logic
edreinoso Jun 11, 2022
64753c4
test script in python for the badge logic
edreinoso Jun 11, 2022
cca61ca
organisation for retaking the badge logic
edreinoso Jun 11, 2022
23432b9
initial tree recursion
edreinoso Jun 13, 2022
d40a02a
optimised the tree recursion
edreinoso Jun 14, 2022
a162571
included the points for commitments
edreinoso Jun 14, 2022
1f4a2df
querying table for participants
edreinoso Jun 14, 2022
979ddb4
initial participation logic comparison
edreinoso Jun 14, 2022
f1bb459
badge point user mapping
edreinoso Jun 15, 2022
9b44401
badge point ddb table update
edreinoso Jun 15, 2022
8551381
separation of concerns in ddb operations
edreinoso Jun 15, 2022
b77847f
minor changes to the badge test script
edreinoso Jun 16, 2022
6d33f48
minor change for point calculation system
edreinoso Jun 16, 2022
70f9937
minor changes to test badge award
edreinoso Jun 16, 2022
c3269e1
small changes to the badge award script
edreinoso Jun 18, 2022
7abd4a3
modified test script
edreinoso Jun 21, 2022
19e9d93
passed category and subcategory to the create class
edreinoso Jun 21, 2022
c78179b
class and function changes in the test class
edreinoso Jun 21, 2022
ea34179
updated the eventbus for badge award
edreinoso Jun 21, 2022
8156a80
minor changes to the api.py test script
edreinoso Jun 21, 2022
8659ee7
ignoring cached python files in test directory
edreinoso Jun 22, 2022
0edc755
dynamic commitment in test script
edreinoso Jun 22, 2022
2f647f2
fixed typos with the commitments
edreinoso Jun 22, 2022
0b23bc5
created delete eventbridge rule file
edreinoso Jun 22, 2022
8f65e11
modified the commitment level in test script
edreinoso Jun 23, 2022
8ccbf6f
changed id to remove target from the rule
edreinoso Jun 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ development, master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ development ]
schedule:
- cron: '0 8 * * 1'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go', 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ _ignored/
.vscode/launch.json
.vscode/extensions.json
*.code-workspace

*.gitconfig
# VisualStudioCode Patch
# Ignore all local history of files
.history
.ionide

# MacOS
.DS_Store
.idea
.idea

samconfig.yaml
scripts/badges/__pycache__/create.cpython-39.pyc
73 changes: 73 additions & 0 deletions badges/dynamodb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import boto3
d_client = boto3.client('dynamodb')


class ddb_ops():
"""Get Item"""

def get_item(self, table, crowdaction_id):
self.table = table
self.crowdaction_id = crowdaction_id

res = d_client.get_item(
TableName=table,
Key={
'pk': {'S': 'act'},
'sk': {'S': crowdaction_id}
}
)
return res

"""Query Items"""

def query(self, table, crowdaction_id):
self.table = table
self.crowdaction_id = crowdaction_id

res = d_client.query(
TableName=table,
IndexName='invertedIndex',
KeyConditionExpression="sk = :sk",
ExpressionAttributeValues={
':sk': {'S': f'prt#act#{crowdaction_id}'}
},
)
return res

"""Update Items"""

def update(self, table, usr_id, reward, crowdaction_id):
self.table = table
self.usr_id = usr_id
self.reward = reward
self.crowdaction_id = crowdaction_id

print("update", table, usr_id, reward, crowdaction_id)

d_client.update_item(
TableName=table,
Key={
'userid': {
'S': usr_id,
}
},
AttributeUpdates={
'reward': {
'Value': {
'L': [
{
"M": {
"award": {
"S": reward
},
"crowdactionID": {
"S": crowdaction_id
}
}
},
],
},
'Action': 'ADD' # this operations still pending
}
},
)
132 changes: 132 additions & 0 deletions badges/eventbus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# This function should be responsible for point system calculation
# as well as the event rule deletion from the bus once the crowdaction
# terminates

import json
import logging
import boto3
import os
from dynamodb import *

e_client = boto3.client('events')
l_client = boto3.client('lambda')

ddb = ddb_ops()

commit_dict = {} # this may be global for now


def compute_badge_award(points, reward_list):
"""
There is an assumption about the order
of the reward_list. This is taken into
account a descending order
"""
if int(points) >= int(reward_list[3]):
return "Diamond"
elif int(points) >= int(reward_list[2]):
return "Golden"
elif int(points) >= int(reward_list[1]):
return "Silver"
elif int(points) >= int(reward_list[0]):
return "Bronze"
else:
return "No reward"


def tree_recursion(tree):
for i in range(0, len(tree)):
t = tree[i]['M']
commit_key = t['id']['S']
commit_points = t['points']['N']
commit_dict[commit_key] = commit_points
if 'requires' in t:
tree_recursion(t['requires']['L'])


def lambda_handler(event, context):
badge_reward_list = []

target_name = 'lambda_cron_test_end_date'
single_table = 'collaction-dev-edreinoso-SingleTable-BAXICTFSQ4WV'
profile_table = 'collaction-dev-edreinoso-ProfileTable-XQEJJNBK6UUY'
crowdaction_id = event['resources'][0].split(
'/')[1].replace('_', '#') # prod
print('Lambda Crontab!', crowdaction_id)

"""
POINT CALCULATION LOGIC
"""

# 1. fetch the badge scale for crowdaction ✅
badge_scale = ddb.get_item(single_table, crowdaction_id)

tree = badge_scale['Item']['commitment_options']['L']
for reward in badge_scale['Item']['badges']['L']:
badge_reward_list.append(reward['N'])
# print(badge_reward_list) # verfying the badge reward list

# 2. restructure the tree to a dictionary ✅
tree_recursion(tree)
print(commit_dict) # verifying the dictionary convertion

# 3. go through all participants ✅
participant_list = ddb.query(single_table, crowdaction_id)
print(participant_list)

# 4. map user commitment level ✅
user_prt_list = [] # list required to store individual participations
for i in range(0, len(participant_list['Items'])):
prt_details = participant_list['Items'][i]
usr_id = prt_details['userID']['S']
prt_lvl = prt_details['commitments']['L']
usr_prt_counter = 0
for n in range(0, len(prt_lvl)):
usr_prt_counter += int(commit_dict[prt_lvl[n]['S']])
usr_obj = {
"userid": usr_id,
"prt": prt_lvl,
"points": usr_prt_counter
}
print(usr_obj)
# if prt_lvl in commit_dict: # would I be assuming that a user would always have a participation
usr_obj['badge'] = compute_badge_award(
usr_prt_counter, badge_reward_list)
user_prt_list.append(usr_obj)

print(user_prt_list)

# 5. award badge ✅
for usr in user_prt_list:
ddb.update(
profile_table, usr['userid'], usr['badge'], crowdaction_id)

"""
CLEANING UP TARGETS AND EVENTS
"""

crowdaction_id_e = crowdaction_id.replace('#', '_')

# 6. delete permission ✅
l_client.remove_permission(
FunctionName=target_name,
StatementId=crowdaction_id_e,
)

# 7. delete targets ✅
e_client.remove_targets(
Rule=crowdaction_id_e,
Ids=[
crowdaction_id_e,
],
)

# 8. delete event ✅
e_client.delete_rule(
Name=crowdaction_id_e,
)

return {
'statusCode': 200,
'body': json.dumps('Crowdaction Ended!')
}
84 changes: 84 additions & 0 deletions badges/trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
This function should be responsible for keeping track of the end date
of the crowdaction, to record it in the event bus for later point
system calculation
"""

from datetime import datetime
import logging
import boto3
import json
import random
import string

e_client = boto3.client('events')
l_client = boto3.client('lambda')


def lambda_handler(event, context):
if(event['Records'][0]['eventName'] == 'INSERT'):
record = event['Records'][0]['dynamodb']['NewImage']

# TODO: test this!
# checking for records that are only for crowdactions
if (record['pk']['S'] != "act"):
return {
'statusCode': 200,
'body': json.dumps('Event is not crowdaction')
}

target_arn = 'arn:aws:lambda:eu-central-1:156764677614:function:lambda_cron_test_end_date'
target_name = 'lambda_cron_test_end_date'
action = 'lambda:InvokeFunction'

title = record['title']['S']
description = record['description']['S']
date_end = record['date_end']['S']
crowdaction_id = record['sk']['S'].replace('#', '_')
# may have to handle date exception
date_end_expr = datetime_to_cron(
datetime.strptime(date_end, "%Y-%m-%d %H:%M:%S"))

# PUT TARGET
e_client.put_rule(
Name=crowdaction_id,
ScheduleExpression=date_end_expr,
State='ENABLED',
Description='event rule for ' + crowdaction_id,
)

# PUT TARGET
e_client.put_targets(
Rule=crowdaction_id,
Targets=[
{
'Id': crowdaction_id,
'Arn': target_arn,
},
]
)

# ADD PERMISSIONS
l_client.add_permission(
FunctionName=target_name,
StatementId=crowdaction_id,
Action=action,
Principal='events.amazonaws.com',
SourceArn='arn:aws:events:eu-central-1:156764677614:rule/'+crowdaction_id,
)

print('rule has been placed on the bus')

return {
'statusCode': 200,
'body': json.dumps('End day has been scheduled!')
}


"""
Function that would convert datetime into cronjobs
"""


def datetime_to_cron(dt):
return f"cron({dt.minute} {dt.hour} {dt.day} {dt.month} ? {dt.year})"
Loading