Skip to content

Commit

Permalink
Merge pull request #34 from mxmehl/better-summary
Browse files Browse the repository at this point in the history
Improve statistics output
  • Loading branch information
mxmehl authored Jun 12, 2024
2 parents f420b88 + 8da4d50 commit 54eef53
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ jobs:
sed -i "s|__INWX_OTE_PASS__|${{ secrets.INWX_OTE_PASS }}|" /home/runner/.config/inwx-dns-recordmaster/config.toml
- name: Run with first records configuration
run: poetry run inwx-dnsrm sync -c tests/records/
run: poetry run inwx-dnsrm sync -c tests/records1/

- name: Run with second records configuration
run: |
cp tests/records/test-mehl.mx.yaml.changes tests/records/test-mehl.mx.yaml
poetry run inwx-dnsrm sync -c tests/records/
run: poetry run inwx-dnsrm sync -c tests/records2/

- name: Convert the remote records to YAML
run: |
Expand Down
50 changes: 48 additions & 2 deletions recordmaster/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ def import_records(self, data: dict, domain: str = "", root: str = ""):


@dataclass
class DomainStats:
class DomainStats: # pylint: disable=too-many-instance-attributes
"""Dataclass holding general statistics about the handling of a domain"""

domainname: str = ""
total_remote: int = 0
updated: int = 0
added: int = 0
Expand All @@ -77,10 +78,11 @@ class DomainStats:
def stats_calc(self, domainname: str) -> None:
"""Calculate the number of unchanged records based on updates and removals"""

self.domainname = domainname
self.unchanged = self.total_remote - self.updated - self.deleted
self.changed = self.updated + self.added + self.deleted

logging.info(
logging.debug(
"[%s] Domain synchronised with %s changes: %s updated, %s added, %s deleted. "
"%s ignored, %s unchanged",
domainname,
Expand All @@ -92,6 +94,50 @@ def stats_calc(self, domainname: str) -> None:
self.unchanged,
)

def dc2dict(self) -> dict:
"""Return dataclass as dict"""
return {
self.domainname: {
"changed": self.changed,
"updated": self.updated,
"added": self.added,
"deleted": self.deleted,
"ignored": self.ignored,
"unchanged": self.unchanged,
}
}


@dataclass
class DomainStatsSummary:
"""Dataclass holding statistics about the handling of all domains"""

stats: dict = field(default_factory=dict)

def print_summary(self):
"""Print summary of all stats"""
changed = [domain for domain, stats in self.stats.items() if stats.get("changed", -1) > 0]

if not changed:
logging.info(
"SUMMARY: No changes were made in any of the %s handled domains", len(self.stats)
)
else:
changestats = []
for domain in changed:
stats = self.stats[domain]
changestats.append(
f"- {domain}: {stats['changed']} changes. {stats['updated']} updated, "
f"{stats['added']} added, {stats['deleted']} deleted. "
f"{stats['ignored']} ignored, {stats['unchanged']} unchanged"
)
logging.info(
"SUMMARY: Changes were made in %s of the %s handled domains: \n%s",
len(changed),
len(self.stats),
"\n".join(changestats),
)


@dataclass
class Domain:
Expand Down
12 changes: 9 additions & 3 deletions recordmaster/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from . import DEFAULT_OPTIONS, __version__, configure_logger
from ._api import api_login
from ._data import Domain, cache_data, convert_punycode
from ._data import Domain, DomainStatsSummary, cache_data, convert_punycode
from ._get_records import (
check_local_records_config,
combine_local_records,
Expand Down Expand Up @@ -112,7 +112,7 @@
parser.add_argument("--version", action="version", version="%(prog)s " + __version__)


def sync(
def sync( # pylint: disable=too-many-locals
# pylint: disable=too-many-arguments, dangerous-default-value
api: ApiClient,
dns_config: str,
Expand Down Expand Up @@ -144,6 +144,9 @@ def sync(
# Load domain records configuration files
records_files = find_valid_local_records_files(dns_config)

# Create empty Stats Summary dataclass
statssummary = DomainStatsSummary()

# Normal procedure
for domainname, records in combine_local_records(records_files).items():
# If `-d`/`--domain` given, skip all other domains
Expand Down Expand Up @@ -226,8 +229,11 @@ def sync(
len(unmatched_remote),
)

# Finally, show stats about this domain
# Finally, gather stats about this domain
domain.stats.stats_calc(domain.name)
statssummary.stats.update(domain.stats.dc2dict())

statssummary.print_summary()


def convert(
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 54eef53

Please sign in to comment.