Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leftmove authored Jun 28, 2024
1 parent 2c8cab9 commit a42d281
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 61 deletions.
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MONGO_BACKUP_URL = "1LT4xiFJkh6YlAPQDcov8YIKqcvevFlEE"

REDIS_SERVER_URL = "${SERVER}"
REDIS_PORT = 6379
REDIS_PASSWORD = "***********"

MEILI_SERVER_URL = "http://${SERVER}:7700"
MEILI_MASTER_KEY = "***********"
Expand Down
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ example.json
.env
*.env
.env.*
**.env

.venv
development.test.py
Expand Down
60 changes: 30 additions & 30 deletions backend/routers/filer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,32 @@ def create_filer(cik, sec_data):
create_historical(cik, company, stamp)


@router.get(
"/query",
tags=["filers"],
status_code=201,
)
async def query_filer(cik: str):
filer = database.find_filer(cik)
if not filer:
try:
sec_data = sec_filer_search(cik)
except Exception as e:
logging.error(e)
raise HTTPException(404, detail="CIK not found.")

if production_environment:
worker.create_filer.delay(cik, sec_data)
else:
create_filer(cik, sec_data)

res = {"description": "Filer creation started."}
else:
res = update_filer(filer)

return res


def update_filer(company):
cik = company["cik"]
time = datetime.now().timestamp()
Expand Down Expand Up @@ -233,32 +259,6 @@ def update_filer(company):
return {"description": "Filer update started."}


@router.get(
"/query",
tags=["filers"],
status_code=201,
)
async def query_filer(cik: str):
filer = database.find_filer(cik)
if not filer:
try:
sec_data = sec_filer_search(cik)
except Exception as e:
logging.error(e)
raise HTTPException(404, detail="CIK not found.")

if production_environment:
worker.create_filer.delay(cik, sec_data)
else:
create_filer(cik, sec_data)

res = {"description": "Filer creation started."}
else:
res = update_filer(filer)

return res


@router.get("/rollback", tags=["filers"], status_code=201, include_in_schema=False)
async def rollback_filer(cik: str, password: str):
filer = database.find_filer(cik, {"last_report": 1})
Expand All @@ -280,9 +280,9 @@ async def rollback_filer(cik: str, password: str):
filing_stock, filings
)

filings[access_number]["stocks"][cusip]["first_appearance"] = (
first_appearance
)
filings[access_number]["stocks"][cusip][
"first_appearance"
] = first_appearance
filings[access_number]["stocks"][cusip]["last_appearance"] = last_appearance

filings_sorted = sorted(
Expand All @@ -300,7 +300,7 @@ async def rollback_filer(cik: str, password: str):

start = datetime.now().timestamp()
stamp = {"name": filer["name"], "start": start}

if production_environment:
worker.create_historical.delay(cik, filer, stamp)
else:
Expand Down
78 changes: 54 additions & 24 deletions backend/routers/lib/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,30 @@ def serialize_stock(local_stock, global_stock):
def serialize_local(
local_stock,
global_stock,
sold,
first_appearance,
last_appearance,
filings,
portfolio_percentage,
ownership_percentage,
):
buy_float = filings[first_appearance]["report_date"]

sold = local_stock["sold"]
records = local_stock["records"]
prices = local_stock["prices"]
ratios = local_stock["ratios"]

first_appearance = records["first_appearance"]
last_appearance = records["last_appearance"]
portfolio_percentage = ratios["portfolio_percent"]
ownership_percentage = ratios["ownership_percent"]

buy_price = prices["buy"]
buy_float = buy_price["time"]
buy_date = datetime.fromtimestamp(buy_float)
buy_date_str = f"Q{(buy_date.month-1)//3+1} {buy_date.year}"
buy_series = buy_price["series"]

sold_float = filings[last_appearance]["report_date"] if sold else "NA"
sold_price = prices["sold"]
sold_float = sold_price["time"] if sold else "NA"
sold_date = datetime.fromtimestamp(sold_float) if sold else "NA"
sold_date_str = f"Q{(sold_date.month-1)//3+1} {sold_date.year}" if sold else "NA"
sold_series = sold_price["series"] if sold else "NA"

portfolio_percentage_str = (
"{:.2f}".format(round(portfolio_percentage, 4))
Expand All @@ -223,16 +233,28 @@ def serialize_local(
return {
**serialized_global,
"sold": sold,
"portfolio_percent": portfolio_percentage,
"portfolio_str": portfolio_percentage_str,
"ownership_percent": ownership_percentage,
"ownership_str": ownership_percentage_str,
"first_appearance": first_appearance,
"last_appearance": last_appearance,
"buy_time": buy_float,
"buy_str": buy_date_str,
"sold_time": sold_float,
"sold_str": sold_date_str,
"ratios": {
"portfolio_percent": portfolio_percentage,
"portfolio_str": portfolio_percentage_str,
"ownership_percent": ownership_percentage,
"ownership_str": ownership_percentage_str,
},
"records": {
"first_appearance": first_appearance,
"last_appearance": last_appearance,
},
"prices": {
"buy": {
"time": buy_float,
"time_str": buy_date_str,
"series": buy_series,
},
"sold": {
"time": sold_float,
"time_str": sold_date_str,
"series": sold_series,
},
},
}


Expand Down Expand Up @@ -331,8 +353,8 @@ def analyze_timeseries(cik, local_stock, global_stock, filings):
update_timeseries = False

sold = local_stock["sold"]
first_appearance = local_stock["first_appearance"]
last_appearance = local_stock["last_appearance"]
first_appearance = local_stock["records"]["first_appearance"]
last_appearance = local_stock["records"]["last_appearance"]
buy_time = filings[first_appearance]["report_date"]
sold_time = filings[last_appearance]["report_date"] if sold else "NA"

Expand Down Expand Up @@ -381,7 +403,14 @@ def analyze_filings(cik, filings, last_report):
first_appearance, last_appearance = analyze_report(
local_stock, filings_sorted
)
records = {
"first_appearance": first_appearance,
"last_appearance": last_appearance,
}
local_stock["records"] = records

sold = False if last_appearance == last_report else False
local_stock["sold"] = sold

found_stock = stock_cache.get(cusip)
if not found_stock:
Expand All @@ -395,6 +424,11 @@ def analyze_filings(cik, filings, last_report):
portfolio_percentage, ownership_percentage = analyze_value(
local_stock, found_stock, total_value
)
ratios = {
"portfolio_percent": portfolio_percentage,
"ownership_percent": ownership_percentage,
}
local_stock["ratios"] = ratios

if found_stock.get("prices"):
prices = found_stock["prices"]
Expand All @@ -404,17 +438,13 @@ def analyze_filings(cik, filings, last_report):
)
prices = {"buy": buy_stamp, "sold": sold_stamp}
stock_cache[cusip]["prices"] = prices

local_stock["prices"] = prices

filing_stock = serialize_local(
local_stock,
found_stock,
sold,
first_appearance,
last_appearance,
filings_map,
portfolio_percentage,
ownership_percentage,
)

if is_updated:
Expand Down
5 changes: 3 additions & 2 deletions backend/routers/lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

REDIS_SERVER_URL = os.environ["REDIS_SERVER_URL"]
REDIS_PORT = int(os.environ.get("REDIS_PORT", 14640))
REDIS_PASSWORD = os.environ["REDIS_PASSWORD"]

store = redis.Redis(
host="***REMOVED***",
host=REDIS_SERVER_URL,
port=14300,
password="***REMOVED***",
password=REDIS_PASSWORD,
decode_responses=True,
)

Expand Down
7 changes: 4 additions & 3 deletions backend/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
MEILI_MASTER_KEY = os.environ["MEILI_MASTER_KEY"]
REDIS_SERVER_URL = os.environ["REDIS_SERVER_URL"]
REDIS_PORT = int(os.environ.get("REDIS_PORT", 14640))
REDIS_PASSWORD = os.environ["REDIS_PASSWORD"]
DEBUG_CIK = os.environ.get("DEBUG_CIK", "")
SENTRY_DSN = os.environ.get("SENTRY_DSN", "")
TELEMETRY = bool(os.environ.get("TELEMETRY", False))
Expand Down Expand Up @@ -131,9 +132,9 @@ def initialize():
companies_index = search.index("companies")

store = redis.Redis(
host="***REMOVED***",
port=14300,
password="***REMOVED***",
host=REDIS_SERVER_URL,
port=REDIS_PORT,
password=REDIS_PASSWORD,
decode_responses=True,
)

Expand Down
2 changes: 1 addition & 1 deletion backend/static/popular.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["1067983", "1649339", "1541617", "1336528", "1709323", "1536411", "1037389", "1791786", "1350694", "1167483", "1037389"]
["1067983", "1649339", "1541617", "1336528", "1709323", "1536411", "1037389", "1791786", "1350694", "1167483"]
13 changes: 12 additions & 1 deletion backend/static/statistics.json
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{"latest": {"count": 201, "total": 4645.60446190834, "average": 23.11246000949423}, "historical": {"count": 195, "total": 34689.67751979828, "average": 177.89578215281168}}
{
"latest": {
"count": 202,
"total": 4788.016959905624,
"average": 23.703054256958538
},
"historical": {
"count": 195,
"total": 34689.67751979828,
"average": 177.89578215281168
}
}

0 comments on commit a42d281

Please sign in to comment.