Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes#355: [Pdr acc] CORS, Debug Mode, the first day issue on the calculation #358

Merged
merged 24 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
issue296: missing enforce_types are added
  • Loading branch information
kdetry committed Nov 8, 2023
commit 1c64a196002a7de6d94551ac1a135dc42b0079c7
16 changes: 10 additions & 6 deletions pdr_backend/accuracy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from datetime import datetime, timedelta
from typing import Tuple
from enforce_typing import enforce_types
from flask import Flask, jsonify

from pdr_backend.util.subgraph_predictions import get_all_contract_ids_by_owner
Expand All @@ -12,6 +13,7 @@
JSON_FILE_PATH = "pdr_backend/accuracy/output/accuracy_data.json"


@enforce_types
def calculate_timeframe_timestamps(contract_timeframe: str) -> Tuple[int, int]:
"""
Calculates and returns a tuple of Unix timestamps for a start and end time
Expand All @@ -29,18 +31,19 @@ def calculate_timeframe_timestamps(contract_timeframe: str) -> Tuple[int, int]:

end_ts = int(datetime.utcnow().timestamp())
time_delta = (
# timedelta(weeks=2)
# if contract_timeframe == "5m"
# else timedelta(weeks=4)
timedelta(days=1)
timedelta(weeks=2)
if contract_timeframe == "5m"
else timedelta(days=1)
else timedelta(weeks=4)
# timedelta(days=1)
# if contract_timeframe == "5m"
# else timedelta(days=1)
)
start_ts = int((datetime.utcnow() - time_delta).timestamp())

return start_ts, end_ts


@enforce_types
def save_statistics_to_file():
"""
Periodically fetches and saves statistical data to a JSON file.
Expand Down Expand Up @@ -93,7 +96,7 @@ def save_statistics_to_file():
)

start_ts_param, end_ts_param = calculate_timeframe_timestamps(
seconds_per_epoch
statistic_type["alias"]
)

contract_ids = [contract["id"] for contract in contracts]
Expand All @@ -120,6 +123,7 @@ def save_statistics_to_file():
threading.Event().wait(300) # Wait for 5 minutes (300 seconds)


@enforce_types
@app.route("/statistics", methods=["GET"])
def serve_statistics_from_file():
"""
Expand Down
4 changes: 4 additions & 0 deletions pdr_backend/util/test/test_predictor_stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Set
from enforce_typing import enforce_types

from pdr_backend.util.predictoor_stats import (
aggregate_prediction_statistics,
Expand Down Expand Up @@ -36,6 +37,7 @@
]


@enforce_types
def test_aggregate_prediction_statistics():
stats, correct_predictions = aggregate_prediction_statistics(sample_predictions)
assert isinstance(stats, dict)
Expand All @@ -44,6 +46,7 @@ def test_aggregate_prediction_statistics():
assert correct_predictions == 1 # Adjust based on your sample data


@enforce_types
def test_get_endpoint_statistics():
accuracy, pair_timeframe_stats, predictoor_stats = get_endpoint_statistics(
sample_predictions
Expand Down Expand Up @@ -95,6 +98,7 @@ def test_get_endpoint_statistics():
assert len(predictoor_stat["details"]) == 1


@enforce_types
def test_get_cli_statistics(capsys):
get_cli_statistics(sample_predictions)
captured = capsys.readouterr()
Expand Down
5 changes: 4 additions & 1 deletion pdr_backend/util/test/test_subgraph_predictions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from unittest.mock import patch
from enforce_typing import enforce_types
from pdr_backend.util.subgraph_predictions import (
fetch_filtered_predictions,
get_all_contract_ids_by_owner,
Expand Down Expand Up @@ -90,6 +90,7 @@
}


@enforce_types
@patch("pdr_backend.util.subgraph_predictions.query_subgraph")
def test_fetch_filtered_predictions(mock_query_subgraph):
mock_query_subgraph.side_effect = [
Expand All @@ -113,6 +114,7 @@ def test_fetch_filtered_predictions(mock_query_subgraph):
assert mock_query_subgraph.call_count == 2


@enforce_types
@patch(
"pdr_backend.util.subgraph_predictions.query_subgraph",
return_value=MOCK_CONTRACTS_RESPONSE,
Expand All @@ -130,6 +132,7 @@ def test_get_all_contract_ids_by_owner(
mock_query_subgraph.assert_called_once()


@enforce_types
@patch(
"pdr_backend.util.subgraph_predictions.query_subgraph",
return_value=MOCK_CONTRACT_DETAILS_RESPONSE,
Expand Down
8 changes: 8 additions & 0 deletions pdr_backend/util/test/test_subgraph_slot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.mock import patch
from dataclasses import asdict
from typing import Dict
from enforce_typing import enforce_types

from pdr_backend.util.subgraph_slot import (
get_predict_slots_query,
Expand All @@ -23,6 +24,7 @@
)


@enforce_types
def test_get_predict_slots_query():
# Test the get_predict_slots_query function with expected inputs and outputs
query = get_predict_slots_query(
Expand Down Expand Up @@ -58,6 +60,7 @@ def test_get_predict_slots_query():
}


@enforce_types
@patch("pdr_backend.util.subgraph_slot.query_subgraph")
def test_get_slots(mock_query_subgraph):
# Configure the mock to return a full page of results on the first call,
Expand Down Expand Up @@ -88,6 +91,7 @@ def test_get_slots(mock_query_subgraph):
assert result_slots[0].id == "0xAsset-12345"


@enforce_types
def test_calculate_prediction_prediction_result():
# Test the calculate_prediction_prediction_result function with expected inputs
result = calculate_prediction_prediction_result(150.0, 100.0)
Expand All @@ -97,6 +101,7 @@ def test_calculate_prediction_prediction_result():
assert not result["direction"]


@enforce_types
def test_process_single_slot():
# Test the process_single_slot function
(
Expand All @@ -114,6 +119,7 @@ def test_process_single_slot():
assert slots_evaluated == 1


@enforce_types
def test_aggregate_statistics():
# Test the aggregate_statistics function
(
Expand All @@ -130,6 +136,7 @@ def test_aggregate_statistics():
assert total_slots_evaluated == 1


@enforce_types
@patch("pdr_backend.util.subgraph_slot.fetch_slots_for_all_assets")
def test_calculate_statistics_for_all_assets(mock_fetch_slots):
# Set up the mock to return a predetermined value
Expand All @@ -144,6 +151,7 @@ def test_calculate_statistics_for_all_assets(mock_fetch_slots):
mock_fetch_slots.assert_called_once_with(["0xAsset"], 1000, 2000, "mainnet")


@enforce_types
@patch(
"pdr_backend.util.subgraph_slot.query_subgraph", return_value=MOCK_QUERY_RESPONSE
)
Expand Down
Loading