Add Bugout integration.

pull/1045/head
Andrey 2024-04-02 18:16:01 +03:00
rodzic d22606508e
commit 4a41852bea
3 zmienionych plików z 149 dodań i 1 usunięć

Wyświetl plik

@ -16,8 +16,10 @@ from moonstreamdb.db import SessionLocal
from ..settings import (
BUGOUT_BROOD_URL,
BUGOUT_SPIRE_URL,
MOONSTREAM_ADMIN_ACCESS_TOKEN,
MOONSTREAM_APPLICATION_ID,
MOONSTREAM_MOONWORM_TASKS_JOURNAL,
MOONSTREAM_USAGE_REPORTS_JOURNAL_ID,
)
from ..web3_provider import yield_web3_provider
@ -265,6 +267,17 @@ def generate_usage_handler(args: argparse.Namespace) -> None:
contracts=args.contracts,
)
if MOONSTREAM_USAGE_REPORTS_JOURNAL_ID is not None:
usage.push_report_to_bugout_journal(
name=args.name,
user_id=args.user_id,
month=args.month,
journal_id=MOONSTREAM_USAGE_REPORTS_JOURNAL_ID,
report=usage_info,
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
)
if args.output is not None:
# create path if not exists
@ -578,6 +591,14 @@ This CLI is configured to work with the following API URLs:
type=str,
help="User token for which to generate usage (not implemented yet - use user-token instead)",
)
generate_usage_parser.add_argument(
"--name",
type=str,
help="Name of the user for which to generate usage",
default="",
)
generate_usage_parser.add_argument(
"--contracts",
required=False,

Wyświetl plik

@ -14,6 +14,7 @@ from ..settings import (
BUGOUT_REQUEST_TIMEOUT_SECONDS,
MOONSTREAM_PUBLIC_QUERIES_DATA_ACCESS_TOKEN,
MOONSTREAM_LEADERBOARD_GENERATOR_JOURNAL_ID,
MOONSTREAM_USAGE_REPORTS_JOURNAL_ID,
)
from ..data import BUGOUT_RESOURCE_QUERY_RESOLVER
@ -101,13 +102,33 @@ def generate_leaderboard_owners(
Get list of all leaderboard and add owners to it.
"""
leaderboards_cache = {}
leaderboard_owners = []
### Get leaderboard owners cache entry
try:
entries = get_all_entries_from_search(
journal_id=MOONSTREAM_USAGE_REPORTS_JOURNAL_ID,
search_query=f"tag:leaderboard_owners tag:cache",
limit=100,
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
)
except Exception as e:
logger.error(f"Error getting leaderboard_owners_cache entry: {e}")
entries = []
if len(entries) > 0:
leaderboards_cache = json.loads(entries[0].content)
for leaderboard in leaderboards:
# breakpoint()
leaderboard_id = leaderboard.resource_data["leaderboard_id"]
resource_id = leaderboard.id
if leaderboard_id in leaderboards_cache:
leaderboard_owners.append(leaderboards_cache[leaderboard_id])
continue
holders: BugoutResourceHolders = bc.get_resource_holders(
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
resource_id=resource_id,
@ -136,6 +157,52 @@ def generate_leaderboard_owners(
}
)
leaderboards_cache[leaderboard_id] = {
"leaderboard_id": leaderboard_id,
"owner": str(owner),
"resource_id": str(resource_id),
"created_at": str(leaderboard.created_at),
"updated_at": str(leaderboard.updated_at),
}
### update cache
if len(entries) > 0:
try:
bc.update_entry_content(
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
entry_id=entries[0].entry_url.split("/")[-1],
content=json.dumps(leaderboards_cache),
title=entries[0].title,
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
)
except Exception as e:
logger.error(
f"Error updating leaderboard_owners_cache entry: {e} continue..."
)
else:
title = "leaderboard_owners_cache"
tags = [
"leaderboard_owners",
"cache",
]
try:
report_entry = bc.create_entry(
token=MOONSTREAM_ADMIN_ACCESS_TOKEN,
journal_id=MOONSTREAM_USAGE_REPORTS_JOURNAL_ID,
title=title,
content=json.dumps(leaderboards_cache),
tags=tags,
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
)
except Exception as e:
logger.error(
f"Error creating leaderboard_owners_cache entry: {e} continue..."
)
return leaderboard_owners
@ -316,3 +383,58 @@ def collect_usage_information(
"leaderboards_amount": len(leaderboards),
"contracts": contract_data,
}
def push_report_to_bugout_journal(
name: str,
user_id: str,
month: str,
journal_id: str,
report: Dict[str, Any],
token: str,
) -> None:
"""
Push report to bugout journal.
"""
### search by month if entry already exists
entries = get_all_entries_from_search(
journal_id=journal_id,
search_query=f"tag:month:{month} tag:report user_id:{user_id}",
limit=100,
token=token,
)
if len(entries) > 0:
entry = entries[0]
bc.update_entry_content(
token=token,
entry_id=entry.entry_url.split("/")[-1],
content=json.dumps(report),
title=entry.title,
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
)
logger.info("Report entry updated: %s", entry.entry_url)
else:
title = f"{name} - {month} - {user_id}"
tags = [
f"month:{month}",
"report",
f"user_id:{user_id}",
]
report_entry = bc.create_entry(
token=token,
journal_id=journal_id,
title=title,
content=json.dumps(report),
tags=tags,
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS,
)
logger.info("Report entry created: %s", report_entry.id)

Wyświetl plik

@ -320,3 +320,8 @@ if MOONSTREAM_LEADERBOARD_GENERATOR_JOURNAL_ID == "":
raise ValueError(
"MOONSTREAM_LEADERBOARD_GENERATOR_JOURNAL_ID environment variable must be set"
)
MOONSTREAM_USAGE_REPORTS_JOURNAL_ID = os.environ.get(
"MOONSTREAM_USAGE_REPORTS_JOURNAL_ID", ""
)