Skip to content

Commit d425572

Browse files
author
Andrew Brookins
committed
"It works"
1 parent 7638ff9 commit d425572

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

app/main.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33
import logging
44
from datetime import datetime
55
from datetime import timedelta
6+
from datetime import timezone
67
from typing import Dict
78
from typing import Iterable
89
from typing import List
9-
from typing import Optional
1010
from typing import Tuple
1111
from typing import Union
1212

1313
import aioredis
1414
import requests
1515
from aioredis.exceptions import ResponseError
16-
from dateutil.rrule import HOURLY
17-
from dateutil.rrule import rrule
18-
from fastapi import BackgroundTasks
19-
from fastapi import Body
2016
from fastapi import Depends
2117
from fastapi import FastAPI
2218
from pydantic import BaseSettings
@@ -74,7 +70,7 @@ def timeseries_1_hour_price_key(self) -> str:
7470

7571
@prefixed_key
7672
def cache_key(self) -> str:
77-
return f'sentiment:mean:1h:cache'
73+
return f'cache'
7874

7975

8076
class Config(BaseSettings):
@@ -143,13 +139,15 @@ async def get_current_hour_data(keys):
143139
top_of_the_hour = int(
144140
datetime.utcnow().replace(
145141
minute=0,
142+
second=0,
143+
microsecond=0,
146144
).timestamp() * 1000,
147145
)
148146
current_hour_avg_sentiment = await get_hourly_average(ts_sentiment_key, top_of_the_hour)
149147
current_hour_avg_price = await get_hourly_average(ts_price_key, top_of_the_hour)
150148

151149
return {
152-
'time': top_of_the_hour,
150+
'time': datetime.fromtimestamp(top_of_the_hour / 1000, tz=timezone.utc).isoformat(),
153151
'price': current_hour_avg_price,
154152
'sentiment': current_hour_avg_sentiment,
155153
}
@@ -179,7 +177,7 @@ async def set_current_hour_cache(keys: Keys):
179177

180178
# Now that we've ingested raw sentiment data, aggregate it for the current
181179
# hour and cache the result.
182-
return refresh_hourly_cache(keys)
180+
return await refresh_hourly_cache(keys)
183181

184182

185183
@app.get('/refresh')
@@ -197,16 +195,17 @@ async def bitcoin(keys: Keys = Depends(make_keys)):
197195
current_hour_stats_cached = await get_current_hour_cache(keys)
198196

199197
if not current_hour_stats_cached:
200-
await set_current_hour_cache(keys)
198+
current_hour_stats_cached = await set_current_hour_cache(keys)
201199

202200
three_hours_ago_ms = int((now - timedelta(hours=3)).timestamp() * 1000)
203201
sentiment = await redis.execute_command('TS.RANGE', sentiment_1h_key, three_hours_ago_ms, '+')
204202
price = await redis.execute_command('TS.RANGE', price_1h_key, three_hours_ago_ms, '+')
205-
past_hours = [{'price': data[0][1], 'sentiment': data[1][1], 'time': data[0][0]}
206-
for data in zip(price, sentiment)]
207-
current_hour = [current_hour_stats_cached] + past_hours
208-
209-
return current_hour + past_hours
203+
past_hours = [{
204+
'price': data[0][1], 'sentiment': data[1][1],
205+
'time': datetime.fromtimestamp(data[0][0] / 1000, tz=timezone.utc),
206+
}
207+
for data in zip(price, sentiment)]
208+
return past_hours + [current_hour_stats_cached]
210209

211210

212211
async def make_timeseries(key):

0 commit comments

Comments
 (0)