-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add queries admin tool. #699
Open
Andrei-Dolgolev
wants to merge
9
commits into
main
Choose a base branch
from
queries-admin-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5be572b
Add queries admin tool.
365f4d6
Merge branch 'main' into queries-admin-cli
b49d29b
Add copy command for queries cli.
40fa8cc
Mypy fixes.
9ceabc7
Merge branch 'main' into queries-admin-cli
234d0a9
Add tags of copied entry.
55457a4
Merge branch 'main' into queries-admin-cli
ff80c36
Add chenges temp state.
4973106
Add fix.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
""" | ||
Utilities for managing subscription type resources for a Moonstream application. | ||
""" | ||
import argparse | ||
import json | ||
|
||
from bugout.data import BugoutResources | ||
from bugout.exceptions import BugoutResponseException | ||
from moonstream.client import Moonstream # type: ignore | ||
import logging | ||
from typing import Dict, Any | ||
|
||
from ..data import BUGOUT_RESOURCE_QUERY_RESOLVER | ||
from ..settings import ( | ||
BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
MOONSTREAM_QUERIES_JOURNAL_ID, | ||
) | ||
from ..settings import bugout_client as bc | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def ensure_queries_tags(args: argparse.Namespace) -> None: | ||
|
||
""" | ||
Check all queries resources and check if they entry have all required tags. | ||
""" | ||
params = {"type": BUGOUT_RESOURCE_QUERY_RESOLVER} | ||
resources: BugoutResources = bc.list_resources( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, params=params | ||
) | ||
|
||
for resource in resources.resources: | ||
|
||
if "entry_id" not in resource.resource_data: | ||
print(f"Missing entry_id for {resource.id}") | ||
continue | ||
if "name" not in resource.resource_data: | ||
print(f"Missing name for {resource.id}") | ||
continue | ||
if "type" not in resource.resource_data: | ||
print(f"Missing type for {resource.id}") | ||
continue | ||
if "user" not in resource.resource_data: | ||
print(f"Missing user for {resource.id}") | ||
continue | ||
if "user_id" not in resource.resource_data: | ||
print(f"Missing user_id for {resource.id}") | ||
continue | ||
|
||
# get entry | ||
try: | ||
entry = bc.get_entry( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
entry_id=resource.resource_data["entry_id"], | ||
journal_id=MOONSTREAM_QUERIES_JOURNAL_ID, | ||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
) | ||
|
||
except BugoutResponseException as e: | ||
print(f"Error getting entry {resource.resource_data['entry_id']} err: {e}") | ||
continue | ||
|
||
print( | ||
f"Entry {entry.id} {entry.title} {entry.journal_url} user: {resource.resource_data['user']}" | ||
) | ||
required_tags = ["user_name", "query_name", "user_id", "query_id"] | ||
for tag in entry.tags: | ||
|
||
tag_prefix = tag.split(":")[0] | ||
|
||
print(tag_prefix) | ||
if tag_prefix in required_tags: | ||
required_tags.remove(tag_prefix) | ||
|
||
print(f"Missing tags for {resource.resource_data['entry_id']}: {required_tags}") | ||
|
||
tags_for_update = [] | ||
if len(required_tags) > 0: | ||
|
||
for required_tag in required_tags: | ||
if required_tag == "user_name": | ||
tag_value = resource.resource_data["user"] | ||
elif required_tag == "query_name": | ||
tag_value = resource.resource_data["name"] | ||
elif required_tag == "user_id": | ||
tag_value = resource.resource_data["user_id"] | ||
elif required_tag == "query_id": | ||
tag_value = resource.resource_data["entry_id"] | ||
|
||
tag = f"{required_tag}:{tag_value}" | ||
tags_for_update.append(tag) | ||
|
||
bc.update_tags( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
entry_id=resource.resource_data["entry_id"], | ||
journal_id=MOONSTREAM_QUERIES_JOURNAL_ID, | ||
tags=tags_for_update, | ||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
) | ||
|
||
|
||
def get_users(args: argparse.Namespace) -> Dict[str, Any]: | ||
""" | ||
Return all users with queries count. | ||
""" | ||
|
||
params = {"type": BUGOUT_RESOURCE_QUERY_RESOLVER} | ||
resources: BugoutResources = bc.list_resources( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, params=params | ||
) | ||
|
||
users = {} | ||
for resource in resources.resources: | ||
user_id = resource.resource_data["user_id"] | ||
if user_id not in users: | ||
users[user_id] = { | ||
"user_id": user_id, | ||
"user_name": resource.resource_data["user"], | ||
"queries_count": 1, | ||
} | ||
else: | ||
users[user_id]["queries_count"] += 1 | ||
|
||
logger.info(f"Users: {json.dumps(users, indent=4)}") | ||
|
||
return users | ||
|
||
|
||
def get_user_queries(args: argparse.Namespace) -> Dict[str, Any]: | ||
""" | ||
Return all queries for user. | ||
""" | ||
|
||
params = {"type": BUGOUT_RESOURCE_QUERY_RESOLVER} | ||
resources: BugoutResources = bc.list_resources( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, params=params | ||
) | ||
|
||
queries = {} | ||
|
||
for resource in resources.resources: | ||
user_id = resource.resource_data["user_id"] | ||
if user_id == args.user_id: | ||
queries[resource.resource_data["entry_id"]] = { | ||
"query_name": resource.resource_data["name"] | ||
} | ||
|
||
# get entries | ||
for query_id in queries: | ||
entry = bc.get_entry( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
entry_id=query_id, | ||
journal_id=MOONSTREAM_QUERIES_JOURNAL_ID, | ||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
) | ||
|
||
queries[query_id]["entry"] = entry | ||
|
||
if "preapprove" in entry.tags: | ||
queries[query_id]["status"] = "preapprove" | ||
elif "approved" in entry.tags: | ||
queries[query_id]["status"] = "approved" | ||
|
||
for query_id in queries: | ||
print( | ||
f"{query_id} name: {queries[query_id]['entry'].title} status: {queries[query_id]['status']}" | ||
) | ||
|
||
return queries | ||
|
||
|
||
def copy_queries(args: argparse.Namespace) -> None: | ||
""" | ||
Copy all queries from one user to another. | ||
""" | ||
|
||
from_user_id = args.from_user_id | ||
|
||
to_user_token = args.to_user_token | ||
|
||
params = {"type": BUGOUT_RESOURCE_QUERY_RESOLVER} | ||
resources: BugoutResources = bc.list_resources( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, params=params | ||
) | ||
|
||
queries = {} | ||
for resource in resources.resources: | ||
user_id = resource.resource_data["user_id"] | ||
if user_id == from_user_id: | ||
queries[resource.resource_data["entry_id"]] = { | ||
"query_id": resource.resource_data["entry_id"], | ||
"query_name": resource.resource_data["name"], | ||
"user_id": user_id, | ||
"user_name": resource.resource_data["user"], | ||
} | ||
|
||
# get entries | ||
for query_id in queries: | ||
entry = bc.get_entry( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
entry_id=query_id, | ||
journal_id=MOONSTREAM_QUERIES_JOURNAL_ID, | ||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
) | ||
|
||
queries[query_id]["entry"] = entry | ||
|
||
client = Moonstream() | ||
|
||
for query_id, query in queries.items(): | ||
|
||
# create query via bugout client | ||
try: | ||
created_query = client.create_query( | ||
token=to_user_token, | ||
name=query["query_name"], | ||
query=query["entry"].content, | ||
) | ||
|
||
except Exception as e: | ||
logger.error(f"Error getting entry {query_id} err: {e}") | ||
continue | ||
|
||
logger.info(f"Created query {created_query.id} {created_query.name}") | ||
|
||
if ( | ||
"preapprove" not in query["entry"].tags | ||
and "approved" in query["entry"].tags | ||
): | ||
# Delete preapprove tag | ||
|
||
try: | ||
bc.delete_tag( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
entry_id=created_query.id, | ||
journal_id=MOONSTREAM_QUERIES_JOURNAL_ID, | ||
tag="preapprove", | ||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
) | ||
except BugoutResponseException as e: | ||
logger.error(f"Error in applind tags to query entry: {str(e)}") | ||
continue | ||
except Exception as e: | ||
logger.error( | ||
f"Error in applind tags to query entry: {str(e)} unknown error" | ||
) | ||
continue | ||
|
||
print(f"Delete preapprove tag for {created_query.id}") | ||
|
||
try: | ||
bc.create_tags( | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
entry_id=created_query.id, | ||
journal_id=MOONSTREAM_QUERIES_JOURNAL_ID, | ||
tags=["approved"], | ||
timeout=BUGOUT_REQUEST_TIMEOUT_SECONDS, | ||
) | ||
except BugoutResponseException as e: | ||
logger.error(f"Error in applind tags to query entry: {str(e)}") | ||
continue | ||
except Exception as e: | ||
logger.error( | ||
f"Error in applind tags to query entry: {str(e)} unknown error" | ||
) | ||
continue | ||
|
||
logger.info(f"Add approved tag for {created_query.id}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
Moonstream library and API version. | ||
""" | ||
|
||
MOONSTREAMAPI_VERSION = "0.2.1" | ||
MOONSTREAMAPI_VERSION = "0.2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one suggestion to add in each loop some print with id to understand it is running