2021-12-16 13:26:04 +00:00
|
|
|
import json
|
2021-10-26 16:07:33 +00:00
|
|
|
import logging
|
2023-07-31 15:37:59 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Union, cast
|
2021-10-26 16:07:33 +00:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
import boto3 # type: ignore
|
2023-01-26 15:19:25 +00:00
|
|
|
import requests # type: ignore
|
2023-07-31 14:19:22 +00:00
|
|
|
from bugout.data import BugoutResource, BugoutResources, BugoutSearchResultAsEntity
|
2021-10-26 16:07:33 +00:00
|
|
|
from bugout.exceptions import BugoutResponseException
|
2022-01-17 12:50:31 +00:00
|
|
|
from fastapi import APIRouter, Body, Path, Query, Request
|
2021-10-26 16:07:33 +00:00
|
|
|
|
2021-12-16 13:26:04 +00:00
|
|
|
from .. import actions, data
|
2021-10-26 16:07:33 +00:00
|
|
|
from ..middleware import MoonstreamHTTPException
|
|
|
|
from ..reporter import reporter
|
|
|
|
from ..settings import (
|
2021-10-26 16:38:41 +00:00
|
|
|
BUGOUT_REQUEST_TIMEOUT_SECONDS,
|
2023-07-31 12:32:19 +00:00
|
|
|
BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION,
|
2023-04-20 12:55:22 +00:00
|
|
|
MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
2021-12-16 13:26:04 +00:00
|
|
|
MOONSTREAM_APPLICATION_ID,
|
2022-01-17 13:16:31 +00:00
|
|
|
MOONSTREAM_CRAWLERS_SERVER_PORT,
|
2023-07-31 12:32:19 +00:00
|
|
|
MOONSTREAM_CRAWLERS_SERVER_URL,
|
2021-11-14 07:18:23 +00:00
|
|
|
MOONSTREAM_S3_SMARTCONTRACTS_ABI_BUCKET,
|
|
|
|
MOONSTREAM_S3_SMARTCONTRACTS_ABI_PREFIX,
|
2021-10-26 16:07:33 +00:00
|
|
|
)
|
2023-07-31 12:32:19 +00:00
|
|
|
from ..settings import bugout_client as bc
|
2021-10-26 16:07:33 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
router = APIRouter(
|
|
|
|
prefix="/dashboards",
|
|
|
|
)
|
|
|
|
|
|
|
|
BUGOUT_RESOURCE_TYPE_DASHBOARD = "dashboards"
|
|
|
|
|
|
|
|
BUGOUT_RESOURCE_TYPE_SUBSCRIPTION = "subscription"
|
|
|
|
|
|
|
|
|
2021-11-09 16:32:11 +00:00
|
|
|
@router.post("/", tags=["dashboards"], response_model=BugoutResource)
|
2021-10-28 15:48:15 +00:00
|
|
|
async def add_dashboard_handler(
|
2021-12-03 20:53:05 +00:00
|
|
|
request: Request,
|
|
|
|
dashboard: data.DashboardCreate = Body(...),
|
2021-10-26 16:38:41 +00:00
|
|
|
) -> BugoutResource:
|
2021-10-26 16:07:33 +00:00
|
|
|
"""
|
|
|
|
Add subscription to blockchain stream data for user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
token = request.state.token
|
|
|
|
|
|
|
|
user = request.state.user
|
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
subscription_settings = dashboard.subscription_settings
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-07-31 12:32:19 +00:00
|
|
|
# Get user journal (collection) id
|
2021-10-26 16:07:33 +00:00
|
|
|
|
2023-07-31 12:32:19 +00:00
|
|
|
journal_id = actions.get_entity_subscription_journal_id(
|
2023-02-27 10:47:10 +00:00
|
|
|
resource_type=BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION,
|
|
|
|
user_id=user.id,
|
2023-04-20 12:55:22 +00:00
|
|
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-07-31 14:19:22 +00:00
|
|
|
subscriptions_list = bc.search(
|
2023-02-27 10:47:10 +00:00
|
|
|
token=token,
|
2023-07-31 12:32:19 +00:00
|
|
|
journal_id=journal_id,
|
2023-07-31 15:37:59 +00:00
|
|
|
query="tag:type:subscription",
|
2023-02-27 10:47:10 +00:00
|
|
|
limit=1000,
|
2023-07-31 12:32:19 +00:00
|
|
|
representation="entity",
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-02-27 10:47:10 +00:00
|
|
|
# process existing subscriptions with supplied ids
|
2023-07-31 15:37:59 +00:00
|
|
|
available_subscriptions_ids: Dict[Union[UUID, str], BugoutSearchResultAsEntity]
|
|
|
|
for result in subscriptions_list.results:
|
|
|
|
entity = cast(BugoutSearchResultAsEntity, result)
|
|
|
|
entity_url_list = entity.entity_url.split("/")
|
|
|
|
subscription_id = entity_url_list[len(entity_url_list) - 1]
|
|
|
|
available_subscriptions_ids[subscription_id] = entity
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
for dashboard_subscription in subscription_settings:
|
2023-02-27 10:47:10 +00:00
|
|
|
if dashboard_subscription.subscription_id in available_subscriptions_ids.keys():
|
|
|
|
if (
|
2023-04-25 13:49:01 +00:00
|
|
|
available_subscriptions_ids[
|
|
|
|
dashboard_subscription.subscription_id
|
|
|
|
].secondary_fields.get("abi")
|
2023-02-27 10:47:10 +00:00
|
|
|
is None
|
|
|
|
):
|
2021-11-01 09:04:49 +00:00
|
|
|
logger.error(
|
|
|
|
f"Error on dashboard resource {dashboard_subscription.subscription_id} does not have an abi"
|
|
|
|
)
|
|
|
|
raise MoonstreamHTTPException(
|
|
|
|
status_code=404,
|
|
|
|
detail=f"Error on dashboard resource {dashboard_subscription.subscription_id} does not have an abi",
|
|
|
|
)
|
2021-11-09 16:32:11 +00:00
|
|
|
|
2023-02-27 10:47:10 +00:00
|
|
|
abi = json.loads(
|
2023-04-25 13:49:01 +00:00
|
|
|
available_subscriptions_ids[
|
|
|
|
dashboard_subscription.subscription_id
|
|
|
|
].secondary_fields["abi"]
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
2021-11-01 09:04:49 +00:00
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
actions.dashboards_abi_validation(
|
2023-02-27 10:47:10 +00:00
|
|
|
dashboard_subscription,
|
|
|
|
abi,
|
2021-10-28 15:48:15 +00:00
|
|
|
)
|
2021-10-26 16:07:33 +00:00
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
else:
|
|
|
|
logger.error(
|
2021-11-13 08:42:50 +00:00
|
|
|
f"Error subscription_id: {str(dashboard_subscription.subscription_id)} not exists."
|
2021-10-28 15:48:15 +00:00
|
|
|
)
|
|
|
|
raise MoonstreamHTTPException(status_code=404)
|
2021-10-26 16:07:33 +00:00
|
|
|
|
|
|
|
dashboard_resource = data.DashboardResource(
|
|
|
|
type=BUGOUT_RESOURCE_TYPE_DASHBOARD,
|
2021-11-09 16:32:11 +00:00
|
|
|
user_id=str(user.id),
|
|
|
|
name=dashboard.name,
|
2021-12-02 20:02:03 +00:00
|
|
|
subscription_settings=subscription_settings,
|
2021-10-26 16:07:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2021-11-14 08:38:57 +00:00
|
|
|
# json.loads(dashboard_resource.json())
|
|
|
|
# Necessary because the UUIDs inside dashboard_resources do not get serialized into string if we directly convert to ".dict()"
|
2021-10-26 16:07:33 +00:00
|
|
|
resource: BugoutResource = bc.create_resource(
|
|
|
|
token=token,
|
|
|
|
application_id=MOONSTREAM_APPLICATION_ID,
|
2021-11-14 08:38:57 +00:00
|
|
|
resource_data=json.loads(dashboard_resource.json()),
|
2021-10-26 16:07:33 +00:00
|
|
|
)
|
|
|
|
except BugoutResponseException as e:
|
2021-11-14 07:18:23 +00:00
|
|
|
logger.error(f"Error creating dashboard resource: {str(e)}")
|
2021-10-26 16:07:33 +00:00
|
|
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
except Exception as e:
|
2021-11-14 07:18:23 +00:00
|
|
|
logger.error(f"Error creating dashboard resource: {str(e)}")
|
2021-10-26 16:07:33 +00:00
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
|
|
|
return resource
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete(
|
|
|
|
"/{dashboard_id}",
|
|
|
|
tags=["subscriptions"],
|
2021-11-14 05:26:51 +00:00
|
|
|
response_model=BugoutResource,
|
2021-10-26 16:07:33 +00:00
|
|
|
)
|
2023-07-31 12:32:19 +00:00
|
|
|
async def delete_subscription_handler(request: Request, dashboard_id: str = Path(...)):
|
2021-10-26 16:07:33 +00:00
|
|
|
"""
|
|
|
|
Delete subscriptions.
|
|
|
|
"""
|
|
|
|
token = request.state.token
|
|
|
|
try:
|
|
|
|
deleted_resource = bc.delete_resource(token=token, resource_id=dashboard_id)
|
|
|
|
except BugoutResponseException as e:
|
|
|
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error deleting subscription: {str(e)}")
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
|
|
|
return deleted_resource
|
|
|
|
|
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
@router.get("/", tags=["dashboards"], response_model=BugoutResources)
|
2021-10-26 16:07:33 +00:00
|
|
|
async def get_dashboards_handler(
|
2021-11-01 14:20:53 +00:00
|
|
|
request: Request,
|
|
|
|
limit: int = Query(10),
|
|
|
|
offset: int = Query(0),
|
2021-10-28 15:48:15 +00:00
|
|
|
) -> BugoutResources:
|
2021-10-26 16:07:33 +00:00
|
|
|
"""
|
|
|
|
Get user's subscriptions.
|
|
|
|
"""
|
|
|
|
token = request.state.token
|
|
|
|
params = {
|
|
|
|
"type": BUGOUT_RESOURCE_TYPE_DASHBOARD,
|
|
|
|
"user_id": str(request.state.user.id),
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
resources: BugoutResources = bc.list_resources(token=token, params=params)
|
|
|
|
except BugoutResponseException as e:
|
|
|
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
2023-07-18 11:14:22 +00:00
|
|
|
f"Error listing subscriptions for user ({request.user.id}), error: {str(e)}"
|
2021-10-26 16:07:33 +00:00
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
return resources
|
2021-10-26 16:07:33 +00:00
|
|
|
|
|
|
|
|
2023-07-31 12:32:19 +00:00
|
|
|
@router.get("/{dashboard_id}", tags=["dashboards"], response_model=BugoutResource)
|
2021-11-01 12:28:08 +00:00
|
|
|
async def get_dashboard_handler(
|
2023-07-31 12:32:19 +00:00
|
|
|
request: Request, dashboard_id: UUID = Path(...)
|
2021-11-01 12:28:08 +00:00
|
|
|
) -> BugoutResource:
|
2021-10-26 16:07:33 +00:00
|
|
|
"""
|
|
|
|
Get user's subscriptions.
|
|
|
|
"""
|
|
|
|
token = request.state.token
|
2021-10-26 16:38:41 +00:00
|
|
|
|
2021-10-26 16:07:33 +00:00
|
|
|
try:
|
2021-10-26 16:38:41 +00:00
|
|
|
resource: BugoutResource = bc.get_resource(
|
|
|
|
token=token,
|
2023-07-31 12:32:19 +00:00
|
|
|
resource_id=dashboard_id,
|
2021-10-26 16:38:41 +00:00
|
|
|
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
|
|
|
|
)
|
2021-10-26 16:07:33 +00:00
|
|
|
except BugoutResponseException as e:
|
|
|
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
2023-07-18 11:14:22 +00:00
|
|
|
f"Error listing subscriptions for user ({request.user.id}), error: {str(e)}"
|
2021-10-26 16:07:33 +00:00
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
2021-11-01 12:28:08 +00:00
|
|
|
return resource
|
2021-10-26 16:07:33 +00:00
|
|
|
|
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
@router.put("/{dashboard_id}", tags=["dashboards"], response_model=BugoutResource)
|
|
|
|
async def update_dashboard_handler(
|
2021-12-03 20:53:05 +00:00
|
|
|
request: Request,
|
2023-07-31 12:32:19 +00:00
|
|
|
dashboard_id: str = Path(...),
|
2021-12-03 20:53:05 +00:00
|
|
|
dashboard: data.DashboardUpdate = Body(...),
|
2021-10-28 15:48:15 +00:00
|
|
|
) -> BugoutResource:
|
|
|
|
"""
|
|
|
|
Update dashboards mainly fully overwrite name and subscription metadata
|
|
|
|
"""
|
|
|
|
|
|
|
|
token = request.state.token
|
|
|
|
|
|
|
|
user = request.state.user
|
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
subscription_settings = dashboard.subscription_settings
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-07-31 12:32:19 +00:00
|
|
|
# Get user journal (collection) id
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-07-31 12:32:19 +00:00
|
|
|
journal_id = actions.get_entity_subscription_journal_id(
|
2023-02-27 10:47:10 +00:00
|
|
|
resource_type=BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION,
|
|
|
|
user_id=user.id,
|
2023-04-20 12:55:22 +00:00
|
|
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
|
|
|
|
2023-07-31 14:19:22 +00:00
|
|
|
subscriptions_list = bc.search(
|
2023-02-27 10:47:10 +00:00
|
|
|
token=token,
|
2023-07-31 12:32:19 +00:00
|
|
|
journal_id=journal_id,
|
2023-07-31 15:37:59 +00:00
|
|
|
query="tag:type:subscription",
|
2023-02-27 10:47:10 +00:00
|
|
|
limit=1000,
|
2023-07-31 12:32:19 +00:00
|
|
|
representation="entity",
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-07-31 15:37:59 +00:00
|
|
|
available_subscriptions_ids: Dict[Union[UUID, str], BugoutSearchResultAsEntity]
|
|
|
|
for result in subscriptions_list.results:
|
|
|
|
entity = cast(BugoutSearchResultAsEntity, result)
|
|
|
|
entity_url_list = entity.entity_url.split("/")
|
|
|
|
subscription_id = entity_url_list[len(entity_url_list) - 1]
|
|
|
|
available_subscriptions_ids[subscription_id] = entity
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
for dashboard_subscription in subscription_settings:
|
2023-02-27 10:47:10 +00:00
|
|
|
if dashboard_subscription.subscription_id in available_subscriptions_ids:
|
|
|
|
if (
|
2023-04-25 13:49:01 +00:00
|
|
|
available_subscriptions_ids[
|
|
|
|
dashboard_subscription.subscription_id
|
|
|
|
].secondary_fields.get("abi")
|
2023-02-27 10:47:10 +00:00
|
|
|
is None
|
|
|
|
):
|
2021-11-01 12:04:28 +00:00
|
|
|
logger.error(
|
|
|
|
f"Error on dashboard resource {dashboard_subscription.subscription_id} does not have an abi"
|
|
|
|
)
|
|
|
|
raise MoonstreamHTTPException(
|
|
|
|
status_code=404,
|
|
|
|
detail=f"Error on dashboard resource {dashboard_subscription.subscription_id} does not have an abi",
|
|
|
|
)
|
2023-07-31 16:00:59 +00:00
|
|
|
abi_raw = available_subscriptions_ids[
|
|
|
|
dashboard_subscription.subscription_id
|
|
|
|
].secondary_fields.get("abi")
|
|
|
|
abi = json.loads(abi_raw if abi_raw is not None else "")
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2023-02-27 10:47:10 +00:00
|
|
|
actions.dashboards_abi_validation(dashboard_subscription, abi)
|
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
f"Error subscription_id: {dashboard_subscription.subscription_id} not exists."
|
|
|
|
)
|
|
|
|
raise MoonstreamHTTPException(status_code=404)
|
|
|
|
|
|
|
|
dashboard_resource: Dict[str, Any] = {}
|
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
if subscription_settings:
|
|
|
|
dashboard_resource["subscription_settings"] = json.loads(dashboard.json())[
|
|
|
|
"subscription_settings"
|
2021-11-24 19:37:30 +00:00
|
|
|
]
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2021-11-24 14:11:36 +00:00
|
|
|
if dashboard.name is not None:
|
|
|
|
dashboard_resource["name"] = dashboard.name
|
2021-10-28 15:48:15 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
resource: BugoutResource = bc.update_resource(
|
|
|
|
token=token,
|
|
|
|
resource_id=dashboard_id,
|
2021-11-24 16:28:44 +00:00
|
|
|
resource_data=data.SubscriptionUpdate(update=dashboard_resource).dict(),
|
2021-10-28 15:48:15 +00:00
|
|
|
)
|
|
|
|
except BugoutResponseException as e:
|
|
|
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
except Exception as e:
|
2021-11-24 16:28:44 +00:00
|
|
|
logger.error(f"Error updating subscription resource: {str(e)}")
|
2021-10-28 15:48:15 +00:00
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
|
|
|
return resource
|
2021-11-09 15:13:36 +00:00
|
|
|
|
|
|
|
|
2021-11-11 14:10:07 +00:00
|
|
|
@router.get("/{dashboard_id}/stats", tags=["dashboards"])
|
2021-11-09 15:13:36 +00:00
|
|
|
async def get_dashboard_data_links_handler(
|
2023-07-31 12:32:19 +00:00
|
|
|
request: Request, dashboard_id: str = Path(...)
|
2023-02-27 10:47:10 +00:00
|
|
|
) -> Dict[Union[UUID, str], Any]:
|
2021-11-09 15:13:36 +00:00
|
|
|
"""
|
2023-02-27 10:47:10 +00:00
|
|
|
Get s3 presign urls for dashboard grafics
|
2021-11-09 15:13:36 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
token = request.state.token
|
|
|
|
|
|
|
|
user = request.state.user
|
|
|
|
|
|
|
|
try:
|
|
|
|
dashboard_resource: BugoutResource = bc.get_resource(
|
|
|
|
token=token, resource_id=dashboard_id
|
|
|
|
)
|
|
|
|
except BugoutResponseException as e:
|
|
|
|
raise MoonstreamHTTPException(status_code=e.status_code, detail=e.detail)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
2023-07-18 11:14:22 +00:00
|
|
|
f"Error listing subscriptions for user ({request.user.id}), error: {str(e)}"
|
2021-11-09 15:13:36 +00:00
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
|
|
|
s3_client = boto3.client("s3")
|
|
|
|
|
|
|
|
# get subscriptions
|
|
|
|
|
2023-07-31 12:32:19 +00:00
|
|
|
journal_id = actions.get_entity_subscription_journal_id(
|
2023-02-27 10:47:10 +00:00
|
|
|
resource_type=BUGOUT_RESOURCE_TYPE_ENTITY_SUBSCRIPTION,
|
|
|
|
user_id=user.id,
|
2023-04-20 12:55:22 +00:00
|
|
|
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
|
|
|
|
2023-07-31 14:19:22 +00:00
|
|
|
subscriptions_list = bc.search(
|
2023-02-27 10:47:10 +00:00
|
|
|
token=token,
|
2023-07-31 12:32:19 +00:00
|
|
|
journal_id=journal_id,
|
2023-07-31 15:37:59 +00:00
|
|
|
query="tag:type:subscription",
|
2023-02-27 10:47:10 +00:00
|
|
|
limit=1000,
|
2023-07-31 12:32:19 +00:00
|
|
|
representation="entity",
|
2023-02-27 10:47:10 +00:00
|
|
|
)
|
|
|
|
|
2023-07-31 15:37:59 +00:00
|
|
|
# filter out dashboards
|
2021-11-09 15:13:36 +00:00
|
|
|
|
2021-11-09 16:32:11 +00:00
|
|
|
subscriptions_ids = [
|
2023-02-27 10:47:10 +00:00
|
|
|
subscription_meta["subscription_id"]
|
2021-11-09 16:32:11 +00:00
|
|
|
for subscription_meta in dashboard_resource.resource_data[
|
2021-12-02 21:06:39 +00:00
|
|
|
"subscription_settings"
|
2021-11-09 16:32:11 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2023-07-31 15:37:59 +00:00
|
|
|
dashboard_subscriptions: Dict[Union[UUID, str], BugoutSearchResultAsEntity]
|
|
|
|
for result in subscriptions_list.results:
|
|
|
|
entity = cast(BugoutSearchResultAsEntity, result)
|
|
|
|
entity_url_list = entity.entity_url.split("/")
|
|
|
|
subscription_id = entity_url_list[len(entity_url_list) - 1]
|
2023-07-31 15:40:21 +00:00
|
|
|
if str(subscription_id) in subscriptions_ids:
|
|
|
|
dashboard_subscriptions[subscription_id] = entity
|
2023-02-27 10:47:10 +00:00
|
|
|
|
2021-11-09 15:13:36 +00:00
|
|
|
# generate s3 links
|
|
|
|
|
|
|
|
s3_client = boto3.client("s3")
|
|
|
|
|
2023-02-27 10:47:10 +00:00
|
|
|
stats: Dict[Union[str, UUID], Dict[str, Any]] = {}
|
2021-11-09 15:13:36 +00:00
|
|
|
|
2023-02-27 10:47:10 +00:00
|
|
|
for id, subscription in dashboard_subscriptions.items():
|
2021-11-09 15:13:36 +00:00
|
|
|
available_timescales = [timescale.value for timescale in data.TimeScale]
|
2023-02-27 10:47:10 +00:00
|
|
|
stats[id] = {}
|
|
|
|
|
2023-04-25 13:49:01 +00:00
|
|
|
for fields in subscription.required_fields:
|
2023-02-27 10:47:10 +00:00
|
|
|
if "subscription_type_id" in fields:
|
|
|
|
subscription_type_id = fields["subscription_type_id"]
|
|
|
|
|
2021-11-09 15:13:36 +00:00
|
|
|
for timescale in available_timescales:
|
|
|
|
try:
|
2023-02-27 10:47:10 +00:00
|
|
|
result_key = f"{MOONSTREAM_S3_SMARTCONTRACTS_ABI_PREFIX}/{actions.blockchain_by_subscription_id[subscription_type_id]}/contracts_data/{subscription.address}/{dashboard_id}/v1/{timescale}.json"
|
2021-11-09 15:13:36 +00:00
|
|
|
stats_presigned_url = s3_client.generate_presigned_url(
|
|
|
|
"get_object",
|
2021-11-14 07:18:23 +00:00
|
|
|
Params={
|
|
|
|
"Bucket": MOONSTREAM_S3_SMARTCONTRACTS_ABI_BUCKET,
|
|
|
|
"Key": result_key,
|
|
|
|
},
|
2021-11-09 15:13:36 +00:00
|
|
|
ExpiresIn=300,
|
|
|
|
HttpMethod="GET",
|
|
|
|
)
|
2023-02-27 10:47:10 +00:00
|
|
|
stats[id][timescale] = {"url": stats_presigned_url}
|
2021-11-09 15:13:36 +00:00
|
|
|
except Exception as err:
|
|
|
|
logger.warning(
|
2023-04-25 13:49:01 +00:00
|
|
|
f"Can't generate S3 presigned url in stats endpoint for Bucket:{MOONSTREAM_S3_SMARTCONTRACTS_ABI_BUCKET}, get error:{err}"
|
2021-11-09 15:13:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return stats
|
2021-12-16 13:26:04 +00:00
|
|
|
|
|
|
|
|
2021-12-21 15:23:26 +00:00
|
|
|
@router.post("/{dashboard_id}/stats_update", tags=["dashboards"])
|
2021-12-16 13:26:04 +00:00
|
|
|
async def update_dashbord_data_handler(
|
2022-01-17 12:50:31 +00:00
|
|
|
request: Request,
|
|
|
|
dashboard_id: str = Path(...),
|
|
|
|
updatestats: data.UpdateStats = Body(...),
|
2021-12-16 13:26:04 +00:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Return journal statistics
|
|
|
|
journal.read permission required.
|
|
|
|
"""
|
|
|
|
|
|
|
|
token = request.state.token
|
2023-04-25 13:49:01 +00:00
|
|
|
user = request.state.user
|
2021-12-16 13:26:04 +00:00
|
|
|
|
2021-12-21 15:23:26 +00:00
|
|
|
responce = requests.post(
|
2022-01-17 13:16:31 +00:00
|
|
|
f"{MOONSTREAM_CRAWLERS_SERVER_URL}:{MOONSTREAM_CRAWLERS_SERVER_PORT}/jobs/stats_update",
|
2021-12-21 15:23:26 +00:00
|
|
|
json={
|
2021-12-16 13:26:04 +00:00
|
|
|
"dashboard_id": dashboard_id,
|
2021-12-21 15:23:26 +00:00
|
|
|
"timescales": updatestats.timescales,
|
2021-12-16 13:26:04 +00:00
|
|
|
"token": token,
|
2023-04-25 13:49:01 +00:00
|
|
|
"user_id": str(user.id),
|
2021-12-16 13:26:04 +00:00
|
|
|
},
|
|
|
|
)
|
2021-12-21 15:23:26 +00:00
|
|
|
if responce.status_code != 200:
|
|
|
|
raise MoonstreamHTTPException(
|
|
|
|
status_code=responce.status_code,
|
|
|
|
detail="Task for start generate stats failed.",
|
|
|
|
)
|
|
|
|
return responce.json()
|