Skip to content
Snippets Groups Projects
Commit 1dfc10fa authored by roadscape's avatar roadscape
Browse files

rename context to ctx; prep for stateless

parent bad1a97d
No related branches found
No related tags found
No related merge requests found
...@@ -40,8 +40,8 @@ http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_following pa ...@@ -40,8 +40,8 @@ http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_following pa
http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_follow_count params:='{"account":"test-safari"}' http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_follow_count params:='{"account":"test-safari"}'
http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_user_feed params:='{"account":"test-safari","skip":0,"limit":20,"context":"test-safari"}' http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_user_feed params:='{"account":"test-safari","skip":0,"limit":20,"ctx":"test-safari"}'
http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_blog_feed params:='{"account":"test-safari","skip":0,"limit":20,"context":"test-safari"}' http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_blog_feed params:='{"account":"test-safari","skip":0,"limit":20,"ctx":"test-safari"}'
http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_related_posts params:='{"account":"test-safari","permlink":"tps-report-4-calm-before-the-storm"}' http -j post http://localhost:8080 jsonrpc=2.0 id=1 method=hive.get_related_posts params:='{"account":"test-safari","permlink":"tps-report-4-calm-before-the-storm"}'
......
...@@ -5,7 +5,6 @@ import logging ...@@ -5,7 +5,6 @@ import logging
from decimal import Decimal from decimal import Decimal
from aiocache import cached from aiocache import cached
from hive.db.adapter import Db from hive.db.adapter import Db
from hive.db.db_state import DbState
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -13,7 +12,13 @@ DB = Db.instance() ...@@ -13,7 +12,13 @@ DB = Db.instance()
async def db_head_state(): async def db_head_state():
"""Status/health check.""" """Status/health check."""
return DbState.status() sql = ("SELECT num, created_at, extract(epoch from created_at) ts "
"FROM hive_blocks ORDER BY num DESC LIMIT 1")
row = DB.query_row(sql)
return dict(db_head_block=row['num'],
db_head_time=str(row['created_at']),
db_head_age=int(time.time() - row['ts']))
# stats methods # stats methods
# ------------- # -------------
...@@ -48,13 +53,13 @@ async def payouts_last_24h(): ...@@ -48,13 +53,13 @@ async def payouts_last_24h():
# discussion apis # discussion apis
# --------------- # ---------------
async def get_blog_feed(account: str, skip: int, limit: int, context: str = None): async def get_blog_feed(account: str, skip: int, limit: int, ctx: str = None):
"""Get a blog feed (posts and reblogs from the specified account)""" """Get a blog feed (posts and reblogs from the specified account)"""
account_id = _get_account_id(account) account_id = _get_account_id(account)
sql = ("SELECT post_id FROM hive_feed_cache WHERE account_id = :account_id " sql = ("SELECT post_id FROM hive_feed_cache WHERE account_id = :account_id "
"ORDER BY created_at DESC LIMIT :limit OFFSET :skip") "ORDER BY created_at DESC LIMIT :limit OFFSET :skip")
post_ids = DB.query_col(sql, account_id=account_id, skip=skip, limit=limit) post_ids = DB.query_col(sql, account_id=account_id, skip=skip, limit=limit)
return _get_posts(post_ids, context) return _get_posts(post_ids, ctx)
async def get_related_posts(account: str, permlink: str): async def get_related_posts(account: str, permlink: str):
...@@ -82,7 +87,7 @@ def _get_account_id(name): ...@@ -82,7 +87,7 @@ def _get_account_id(name):
return DB.query_one("SELECT id FROM hive_accounts WHERE name = :n", n=name) return DB.query_one("SELECT id FROM hive_accounts WHERE name = :n", n=name)
# given an array of post ids, returns full metadata in the same order # given an array of post ids, returns full metadata in the same order
def _get_posts(ids, context=None): def _get_posts(ids, ctx=None):
sql = """ sql = """
SELECT post_id, author, permlink, title, preview, img_url, payout, SELECT post_id, author, permlink, title, preview, img_url, payout,
promoted, created_at, payout_at, is_nsfw, rshares, votes, json promoted, created_at, payout_at, is_nsfw, rshares, votes, json
...@@ -90,21 +95,21 @@ def _get_posts(ids, context=None): ...@@ -90,21 +95,21 @@ def _get_posts(ids, context=None):
""" """
reblogged_ids = [] reblogged_ids = []
if context: if ctx:
reblogged_ids = DB.query_col("SELECT post_id FROM hive_reblogs " reblogged_ids = DB.query_col("SELECT post_id FROM hive_reblogs "
"WHERE account = :a AND post_id IN :ids", "WHERE account = :a AND post_id IN :ids",
a=context, ids=tuple(ids)) a=ctx, ids=tuple(ids))
# key by id so we can return sorted by input order # key by id so we can return sorted by input order
posts_by_id = {} posts_by_id = {}
for row in DB.query_all(sql, ids=tuple(ids)): for row in DB.query_all(sql, ids=tuple(ids)):
obj = dict(row) obj = dict(row)
if context: if ctx:
voters = [csa.split(",")[0] for csa in obj['votes'].split("\n")] voters = [csa.split(",")[0] for csa in obj['votes'].split("\n")]
obj['user_state'] = { obj['user_state'] = {
'reblogged': row['post_id'] in reblogged_ids, 'reblogged': row['post_id'] in reblogged_ids,
'voted': context in voters 'voted': ctx in voters
} }
# TODO: Object of type 'Decimal' is not JSON serializable # TODO: Object of type 'Decimal' is not JSON serializable
......
...@@ -11,6 +11,9 @@ from aiohttp import web ...@@ -11,6 +11,9 @@ from aiohttp import web
from jsonrpcserver import config from jsonrpcserver import config
from jsonrpcserver.async_methods import AsyncMethods from jsonrpcserver.async_methods import AsyncMethods
#from hive.db.adapter import Db
#context = dict(db=Db.instance())
from hive.conf import Conf from hive.conf import Conf
from hive.server.condenser_api import methods as condenser_api from hive.server.condenser_api import methods as condenser_api
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment