2021-12-16 13:26:04 +00:00
|
|
|
import json
|
2021-10-26 16:07:33 +00:00
|
|
|
import logging
|
2021-10-28 15:48:15 +00:00
|
|
|
from os import read
|
2021-12-16 13:26:04 +00:00
|
|
|
from typing import Any, Dict, List, Optional
|
2021-10-26 16:07:33 +00:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
import boto3 # type: ignore
|
2021-12-16 13:26:04 +00:00
|
|
|
import requests
|
2021-10-26 16:07:33 +00:00
|
|
|
from bugout.data import BugoutResource, BugoutResources
|
|
|
|
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,
|
2021-12-16 13:26:04 +00:00
|
|
|
MOONSTREAM_APPLICATION_ID,
|
2022-01-17 12:56:47 +00:00
|
|
|
MOONSTREAM_CRAWLERS_SERVER_URL,
|
2022-01-17 13:16:31 +00:00
|
|
|
MOONSTREAM_CRAWLERS_SERVER_PORT,
|
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
|
|
|
)
|
2021-12-16 13:26:04 +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
|
|
|
|
2021-10-26 16:07:33 +00:00
|
|
|
# Get all user subscriptions
|
|
|
|
params = {
|
|
|
|
"type": BUGOUT_RESOURCE_TYPE_SUBSCRIPTION,
|
|
|
|
"user_id": str(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(
|
|
|
|
f"Error listing subscriptions for user ({request.user.id}) with token ({request.state.token}), error: {str(e)}"
|
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
# process existing subscriptions with supplied ids
|
|
|
|
|
|
|
|
s3_client = boto3.client("s3")
|
|
|
|
|
2021-12-03 20:53:05 +00:00
|
|
|
available_subscriptions: Dict[UUID, Dict[str, Any]] = {
|
2021-11-14 08:38:57 +00:00
|
|
|
resource.id: resource.resource_data for resource in resources.resources
|
2021-10-28 15:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
for dashboard_subscription in subscription_settings:
|
2021-11-09 16:32:11 +00:00
|
|
|
if dashboard_subscription.subscription_id in available_subscriptions.keys():
|
2021-10-28 15:48:15 +00:00
|
|
|
|
|
|
|
# TODO(Andrey): Add some dedublication for get object from s3 for repeated subscription_id
|
|
|
|
|
2021-11-01 09:04:49 +00:00
|
|
|
bucket = available_subscriptions[dashboard_subscription.subscription_id][
|
|
|
|
"bucket"
|
|
|
|
]
|
2021-11-09 16:32:11 +00:00
|
|
|
key = available_subscriptions[dashboard_subscription.subscription_id][
|
|
|
|
"s3_path"
|
2021-11-01 09:04:49 +00:00
|
|
|
]
|
|
|
|
|
2021-11-09 16:32:11 +00:00
|
|
|
if bucket is None or key 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
|
|
|
s3_path = f"s3://{bucket}/{key}"
|
|
|
|
|
2021-11-01 09:04:49 +00:00
|
|
|
try:
|
|
|
|
|
|
|
|
response = s3_client.get_object(
|
|
|
|
Bucket=bucket,
|
2021-11-09 16:32:11 +00:00
|
|
|
Key=key,
|
2021-11-01 09:04:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
except s3_client.exceptions.NoSuchKey as e:
|
|
|
|
logger.error(
|
2021-11-13 08:42:50 +00:00
|
|
|
f"Error getting Abi for subscription {str(dashboard_subscription.subscription_id)} S3 {s3_path} does not exist : {str(e)}"
|
2021-11-01 09:04:49 +00:00
|
|
|
)
|
|
|
|
raise MoonstreamHTTPException(
|
|
|
|
status_code=500,
|
|
|
|
internal_error=e,
|
2021-11-13 08:42:50 +00:00
|
|
|
detail=f"We can't access the abi for subscription with id:{str(dashboard_subscription.subscription_id)}.",
|
2021-11-01 09:04:49 +00:00
|
|
|
)
|
|
|
|
|
2021-11-09 16:32:11 +00:00
|
|
|
abi = json.loads(response["Body"].read())
|
2021-11-01 09:04:49 +00:00
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
actions.dashboards_abi_validation(
|
2021-11-01 09:04:49 +00:00
|
|
|
dashboard_subscription, abi, s3_path=s3_path
|
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
|
|
|
)
|
|
|
|
async def delete_subscription_handler(request: Request, dashboard_id: str):
|
|
|
|
"""
|
|
|
|
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(
|
|
|
|
f"Error listing subscriptions for user ({request.user.id}) with token ({request.state.token}), error: {str(e)}"
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-11-01 12:28:08 +00:00
|
|
|
@router.get("/{dashboarsd_id}", tags=["dashboards"], response_model=BugoutResource)
|
|
|
|
async def get_dashboard_handler(
|
|
|
|
request: Request, dashboarsd_id: UUID
|
|
|
|
) -> 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,
|
|
|
|
resource_id=dashboarsd_id,
|
|
|
|
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(
|
|
|
|
f"Error listing subscriptions for user ({request.user.id}) with token ({request.state.token}), error: {str(e)}"
|
|
|
|
)
|
|
|
|
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,
|
|
|
|
dashboard_id: str,
|
|
|
|
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
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": BUGOUT_RESOURCE_TYPE_SUBSCRIPTION,
|
|
|
|
"user_id": str(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(
|
|
|
|
f"Error listing subscriptions for user ({request.user.id}) with token ({request.state.token}), error: {str(e)}"
|
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
|
|
|
s3_client = boto3.client("s3")
|
|
|
|
|
|
|
|
available_subscriptions = {
|
|
|
|
resource.id: resource.resource_data for resource in resources.resources
|
|
|
|
}
|
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
for dashboard_subscription in subscription_settings:
|
2021-11-09 16:32:11 +00:00
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
if dashboard_subscription.subscription_id in available_subscriptions:
|
|
|
|
|
2021-11-01 12:04:28 +00:00
|
|
|
# TODO(Andrey): Add some dedublication for get object from s3 for repeated subscription_id
|
|
|
|
|
|
|
|
bucket = available_subscriptions[dashboard_subscription.subscription_id][
|
|
|
|
"bucket"
|
|
|
|
]
|
|
|
|
abi_path = available_subscriptions[dashboard_subscription.subscription_id][
|
2021-11-24 19:37:30 +00:00
|
|
|
"s3_path"
|
2021-11-01 12:04:28 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if bucket is None or abi_path is None:
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
s3_path = f"s3://{bucket}/{abi_path}"
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = s3_client.get_object(
|
|
|
|
Bucket=bucket,
|
|
|
|
Key=abi_path,
|
|
|
|
)
|
|
|
|
|
|
|
|
except s3_client.exceptions.NoSuchKey as e:
|
|
|
|
logger.error(
|
|
|
|
f"Error getting Abi for subscription {dashboard_subscription.subscription_id} S3 {s3_path} does not exist : {str(e)}"
|
|
|
|
)
|
|
|
|
raise MoonstreamHTTPException(
|
|
|
|
status_code=500,
|
|
|
|
internal_error=e,
|
|
|
|
detail=f"We can't access the abi for subscription with id:{dashboard_subscription.subscription_id}.",
|
|
|
|
)
|
2021-11-24 19:37:30 +00:00
|
|
|
abi = json.loads(response["Body"].read())
|
2021-11-01 12:04:28 +00:00
|
|
|
|
2021-10-28 15:48:15 +00:00
|
|
|
actions.dashboards_abi_validation(
|
2021-11-01 12:04:28 +00:00
|
|
|
dashboard_subscription, abi, s3_path=s3_path
|
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:
|
2021-10-28 15:48:15 +00:00
|
|
|
|
2021-12-02 20:02:03 +00:00
|
|
|
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(
|
|
|
|
request: Request, dashboard_id: str
|
2021-11-13 08:42:50 +00:00
|
|
|
) -> Dict[UUID, Any]:
|
2021-11-09 15:13:36 +00:00
|
|
|
"""
|
2021-12-16 13:26:04 +00:00
|
|
|
Get s3 presign urls for dshaboard 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(
|
|
|
|
f"Error listing subscriptions for user ({request.user.id}) with token ({request.state.token}), error: {str(e)}"
|
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
|
|
|
|
s3_client = boto3.client("s3")
|
|
|
|
|
|
|
|
# get subscriptions
|
|
|
|
|
|
|
|
params = {
|
|
|
|
"type": BUGOUT_RESOURCE_TYPE_SUBSCRIPTION,
|
|
|
|
"user_id": str(user.id),
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
subscription_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(
|
|
|
|
f"Error listing subscriptions for user ({request.user.id}) with token ({request.state.token}), error: {str(e)}"
|
|
|
|
)
|
|
|
|
reporter.error_report(e)
|
|
|
|
raise MoonstreamHTTPException(status_code=500, internal_error=e)
|
|
|
|
# filter out dasboards
|
|
|
|
|
2021-11-09 16:32:11 +00:00
|
|
|
subscriptions_ids = [
|
|
|
|
UUID(subscription_meta["subscription_id"])
|
|
|
|
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
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2021-11-09 15:13:36 +00:00
|
|
|
dashboard_subscriptions = [
|
|
|
|
subscription
|
|
|
|
for subscription in subscription_resources.resources
|
2021-11-09 16:32:11 +00:00
|
|
|
if subscription.id in subscriptions_ids
|
2021-11-09 15:13:36 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# generate s3 links
|
|
|
|
|
|
|
|
s3_client = boto3.client("s3")
|
|
|
|
|
2021-11-13 08:42:50 +00:00
|
|
|
stats: Dict[UUID, Any] = {}
|
2021-11-09 15:13:36 +00:00
|
|
|
|
|
|
|
for subscription in dashboard_subscriptions:
|
|
|
|
|
|
|
|
available_timescales = [timescale.value for timescale in data.TimeScale]
|
|
|
|
stats[subscription.id] = {}
|
|
|
|
for timescale in available_timescales:
|
|
|
|
try:
|
2022-05-25 13:47:51 +00:00
|
|
|
result_key = f'{MOONSTREAM_S3_SMARTCONTRACTS_ABI_PREFIX}/{actions.blockchain_by_subscription_id[subscription.resource_data["subscription_type_id"]]}/contracts_data/{subscription.resource_data["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",
|
|
|
|
)
|
2022-01-10 10:46:46 +00:00
|
|
|
stats[subscription.id][timescale] = {"url": stats_presigned_url}
|
2021-11-09 15:13:36 +00:00
|
|
|
except Exception as err:
|
|
|
|
logger.warning(
|
2021-11-14 07:18:23 +00:00
|
|
|
f"Can't generate S3 presigned url in stats endpoint for Bucket:{MOONSTREAM_S3_SMARTCONTRACTS_ABI_BUCKET}, Key:{result_key} 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
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
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()
|