Add prune feature to cli to remove duplicated entries (#28)

* Add prune feature to cli to remove duplicated entries as a workaround to https://github.com/decompals/frogress/issues/27

usage: cli.py prune [-h] project version

positional arguments:
  project     the project for which to prune the entries
  version     the slug for the version

optional arguments:
  -h, --help  show this help message and exit

* Reformat cli.py with Black

* Improve help info for new added prune option

It is used to fix duplicates caused by https://github.com/decompals/frogress/issues/27
This commit is contained in:
laqieer 2023-04-04 00:46:29 +08:00 committed by GitHub
parent 31616f9132
commit 632e710acd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 98 additions and 0 deletions

98
cli.py
View File

@ -83,6 +83,93 @@ def delete_category(args: argparse.Namespace) -> None:
print(response.text)
def prune_entries(args: argparse.Namespace) -> None:
# Get entries
url = f"{domain}/data/{args.project}/{args.version}?mode=all"
debug("GET " + url)
response = requests.get(url)
print(response.text)
categories = response.json().get(args.project, {}).get(args.version, {})
if len(categories) == 0:
return
# Filter entries
filtered = {}
for category, entries in categories.items():
if len(entries) == 0:
continue
for entry in entries:
if entry["git_hash"] not in filtered:
filtered[entry["git_hash"]] = {
"git_hash": entry["git_hash"],
"timestamp": entry["timestamp"],
"categories": {},
}
if category not in filtered[entry["git_hash"]]["categories"]:
filtered[entry["git_hash"]]["categories"][category] = entry["measures"]
else:
for measure, value in entry["measures"].items():
if (
measure
not in filtered[entry["git_hash"]]["categories"][category]
):
filtered[entry["git_hash"]]["categories"][category][
measure
] = value
entries = list(filtered.values())
# Clear entries
for category in categories.keys():
# Delete categories
url = f"{domain}/projects/{args.project}/{args.version}/{category}/"
data = {"api_key": api_key}
debug("DELETE " + url)
response = requests.delete(url, json=data)
print(response.text)
# Recreate categories
data["name"] = category
debug("POST " + url)
response = requests.post(url, json=data)
print(response.text)
# Upload entries
url = f"{domain}/data/{args.project}/{args.version}/"
data = {"api_key": api_key, "entries": entries}
debug("POST " + url)
response = requests.post(url, json=data)
print(response.status_code, response.text)
# Check entries
url = f"{domain}/data/{args.project}/{args.version}?mode=all"
debug("GET " + url)
response = requests.get(url)
print(response.text)
def main() -> None:
parser = argparse.ArgumentParser()
@ -148,6 +235,17 @@ def main() -> None:
delete_category_parser.add_argument("slug", help="the slug for the category")
delete_category_parser.set_defaults(func=delete_category)
# Prune entries
prune_parser = subparsers.add_parser(
"prune",
help="prune entries to remove duplicates",
)
prune_parser.add_argument(
"project", help="the project for which to prune duplicated entries"
)
prune_parser.add_argument("version", help="the slug for the version")
prune_parser.set_defaults(func=prune_entries)
args = parser.parse_args()
args.func(args)