in experimenter/experimenter/experiments/models.py [0:0]
def get_changelogs_by_date(self):
# Inline import to prevent circular import
from experimenter.experiments.changelog_utils import get_formatted_change_object
changes_by_date = defaultdict(list)
date_option = "%I:%M %p %Z"
date_option = "%I:%M %p %Z"
changelogs = list(
self.changes.order_by("-changed_on").prefetch_related("changed_by")
)
for index, changelog in enumerate(changelogs[:-1]):
current_data = changelog.experiment_data
previous_data = changelogs[index + 1].experiment_data
local_timestamp = timezone.localtime(changelog.changed_on)
timestamp = local_timestamp.strftime(date_option)
local_timestamp = timezone.localtime(changelog.changed_on)
timestamp = local_timestamp.strftime(date_option)
diff_fields = {
field: {
"old_value": previous_data.get(field),
"new_value": current_data.get(field),
}
for field in current_data
if (
field != "_updated_date_time"
and field != "published_dto"
and field != "status_next"
and current_data[field] != previous_data.get(field)
)
}
for field, field_diff in diff_fields.items():
change = get_formatted_change_object(
field, field_diff, changelog, timestamp
)
changes_by_date[changelog.changed_on.date()].append(change)
if changelogs:
creation_log = changelogs[-1]
first_local_timestamp = timezone.localtime(creation_log.changed_on)
first_timestamp = first_local_timestamp.strftime(date_option)
if self.parent:
message = (
f"{creation_log.changed_by} "
f"cloned this experiment from {self.parent.name}"
)
else:
message = f"{creation_log.changed_by} created this experiment"
change = {
"event": ChangeEventType.CREATION.name,
"event_message": message,
"changed_by": creation_log.changed_by,
"timestamp": first_timestamp,
}
changes_by_date[creation_log.changed_on.date()].append(change)
transformed_changelogs = [
{"date": date, "changes": changes}
for date, changes in changes_by_date.items()
]
return transformed_changelogs