Skip to content

Commit

Permalink
we have report
Browse files Browse the repository at this point in the history
  • Loading branch information
sindrig committed Jun 2, 2024
1 parent 5387863 commit 9e5640f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 156 deletions.
Empty file removed clock-api/match-list/__init__.py
Empty file.
88 changes: 0 additions & 88 deletions clock-api/match-list/app.py

This file was deleted.

2 changes: 0 additions & 2 deletions clock-api/match-list/requirements.txt

This file was deleted.

42 changes: 12 additions & 30 deletions clock-api/match-report-admin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import bs4
import requests
from src.client import ksi_client
from src.models import Error, Match, MatchListMatch, Team
from src.models import Error, Match, MatchListMatch, MatchReport, Team


class EnhancedJSONEncoder(json.JSONEncoder):
Expand All @@ -31,46 +31,28 @@ def default(self, o):
# }


def clock_main(query, context):
def get_report(query: dict):
date = get_date(query)
try:
home_team = int(query["homeTeam"])
away_team = int(query["awayTeam"])
pitch_id = int(query["pitchId"])
match_id = int(query["matchId"])
except KeyError:
return {
"error": {
"key": "BAD_INPUT",
"text": "homeTeam or awayTeam missing: {query}",
"text": "matchId missing: {query}",
}
}
except ValueError:
return {
"error": {
"key": "BAD_INPUT",
"text": "homeTeam and awayTeam must be integers",
"text": "matchId must be integers",
}
}
pitch_id = None
matches = ksi_client.get_matches(home_team, away_team, date, pitch_id)
if not matches:
return no_match_found(home_team, away_team)
elif isinstance(matches, dict):
if "error" in matches:
return matches
raise ValueError(f"Unknown matches {matches}")
return {
"matches": [
{
"group": match.group,
"starts": match.starts,
"id": match.match_id,
"home": match.home_team,
"away": match.away_team,
}
for match in matches
]
}
players = ksi_client.get_players(match_id=match_id)
if isinstance(players, Error):
return players
return MatchReport(players=players)


def get_date(query):
Expand Down Expand Up @@ -175,16 +157,16 @@ def lambda_handler(json_input, context):
case "get-matches":
return respond(200, {"matches": list(get_matches(query))})
case "get-report":
return respond(200, clock_main(query, context))
return respond(200, get_report(query))
return respond(400, {"error": "Action not found"})


if __name__ == "__main__":
print(
lambda_handler(
# {"queryStringParameters": {"location": 2621}},
{"queryStringParameters": {"location": "102", "action": "get-matches"}},
# {"queryStringParameters": {"matchId": "102", "action": "get-report"}},
# {"queryStringParameters": {"location": "102", "action": "get-matches"}},
{"queryStringParameters": {"matchId": "638172", "action": "get-report"}},
{},
)
)
10 changes: 5 additions & 5 deletions clock-api/match-report-admin/src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def get_matches(
)
return matches

def get_players(self, match_id) -> dict[str, list[Player]] | Error:
def get_players(self, match_id) -> dict[int, list[Player]] | Error:
game = self.client.service.LeikurLeikmenn(LeikurNumer=match_id)
result: dict[str, list[Player]] = defaultdict(list)
captains = {}
result: dict[int, list[Player]] = defaultdict(list)
captains: dict[str, Player] = {}
if not game.ArrayLeikurLeikmenn:
return Error(
{
Expand All @@ -102,9 +102,9 @@ def get_players(self, match_id) -> dict[str, list[Player]] | Error:
game.ArrayLeikurLeikmenn.LeikurLeikmenn,
key=player_sort_key,
):
club_id = str(player.FelagNumer)
club_id = player.FelagNumer
player_dict = self.get_player(player)
if club_id in captains and captains[club_id]["number"] < player_dict.number:
if club_id in captains and captains[club_id].number < player_dict.number:
result[club_id].append(captains[club_id])
del captains[club_id]
if player_dict.role == "Fyrirliði":
Expand Down
6 changes: 6 additions & 0 deletions clock-api/match-report-admin/src/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
import datetime
import typing


@dataclasses.dataclass
Expand Down Expand Up @@ -43,3 +44,8 @@ class Person:
@dataclasses.dataclass
class Player(Person):
number: int


@dataclasses.dataclass
class MatchReport:
players: dict[int, list[Player]]
31 changes: 0 additions & 31 deletions infra/api.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ module "api_gateway" {
timeout_milliseconds = 12000
}

"ANY /match-list" = {
lambda_arn = module.match-list.lambda_function_arn
payload_format_version = "2.0"
timeout_milliseconds = 12000
}

"ANY /match-report/v2" = {
lambda_arn = module.match-report-admin.lambda_function_arn
payload_format_version = "2.0"
Expand Down Expand Up @@ -174,31 +168,6 @@ module "weather" {
}
}


module "match-list" {
source = "terraform-aws-modules/lambda/aws"
version = "7.4.0"

function_name = "${random_pet.this.id}-match-list"
description = "Match lists"
handler = "app.lambda_handler"
runtime = "python3.12"

publish = true

timeout = 20

build_in_docker = true
source_path = "${path.module}/../clock-api/match-list"

allowed_triggers = {
AllowExecutionFromAPIGateway = {
service = "apigateway"
source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*"
}
}
}

module "match-report-admin" {
source = "terraform-aws-modules/lambda/aws"
version = "7.4.0"
Expand Down

0 comments on commit 9e5640f

Please sign in to comment.