Skip to content
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

Better representation of changes in JSON structures in audit logs #1322

Open
nhoening opened this issue Feb 9, 2025 · 0 comments
Open

Better representation of changes in JSON structures in audit logs #1322

nhoening opened this issue Feb 9, 2025 · 0 comments
Assignees
Labels
Milestone

Comments

@nhoening
Copy link
Contributor

nhoening commented Feb 9, 2025

Right now, the string representation of changes can become pretty long (showing both whole before and after dicts), for example.

Updated Field: sensors_to_show, From: [{'title': 'Prices', 'sensors': [58, 70]}, 16, {'title': 'Battttery activity', 'sensors ... e': 'Battttery activity', 'sensors': [5, 4]}, {'title': 'No Title', 'sensors': [15]}, {'title': 'No Title 5', 'sensors': []}]

Note the "..." part, as we have limited space, strings are cut to fit 255 characters.

We have some code in another project using dictdiffer, which we can use for all two existing structures (sensors_to_show, flex_context) and of course later also flex_model:

from dictdiffer import diff

def pretty_diff(result) -> list[str]:
    string = []
    for change_type, key, value in result:
        if isinstance(key, list):
            _key = key[0]
            for k in key[1:]:
                _key += f"[{k}]" if isinstance(k, int) else f".{k}"
            key = _key
        if change_type == "change":
            string.append(f"change {key}: {value[0]} -> {value[1]}")
        elif change_type == "add":
            for v in value:
                key += f"[{v[0]}]" if isinstance(v[0], int) else f".{v[0]}"
                string.append(f"add {key}: {v[1]}")
        elif change_type == "remove":
            for v in value:
                if key:
                    key += f"[{v[0]}]" if isinstance(v[0], int) else f".{v[0]}"
                    string.append(f"remove {key}: {v[1]}")
                else:
                    string.append(f"remove {v[0]}: {v[1]}")
        else:
            string.append(f"{change_type} {key}: {value}")
    return string


def enumerated_dict_diff(d1: dict, d2: dict) -> str:
    result = diff(d1, d2)
    string = ""
    for index, item in enumerate(pretty_diff(result), start=1):
        string += f"{index}. {item}\n"
    return string

This hopefully will look informative, at least if there are not too many changes at once.

@nhoening nhoening added this to the 0.25.0 milestone Feb 9, 2025
@nhoening nhoening added the UI label Feb 9, 2025
@Flix6x Flix6x changed the title Better representation of hanges in JSON structures in audit logs Better representation of changes in JSON structures in audit logs Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants