Repository: rucio/rucio

Issue:
The transmogrifier updates the subscription history table everytime it runs
### Description

The subscription_history table is filled with entries without any updates to the subscription itself. 

The `transmogrifier` adds an entry to the subscription_history table on every run. 
https://github.com/rucio/rucio/blob/ea4d2c7e2702b85a86a8668b2d852971f42fceb4/lib/rucio/daemons/transmogrifier/transmogrifier.py#L694-L699

I believe the intention was just to update the `last_processed` value for the subscription. 
This also updated the `updated_at` value of subscriptions which eventually always match the `last_processed` value.



### Steps to reproduce

Set `subscriptions.keep_history` to `True` in the rucio configuration table and observe the `subscription_history` table.

`subscription_history` table after setting `keep_history` to `True`.
![image](https://user-images.githubusercontent.com/69708588/222174429-40a6b9e4-4ec1-4214-afc7-83d08bb3f1ce.png)


### Rucio Version

_No response_

### Additional Information



Hi @cserf, 
Would you be able to find some time to help with this? This will help me get a sense of the code and would help with the implementation of the retroactive flag.


Discussion:
Good catch. The history must not be updated in case only `last_processed` is changed. Just need to change the test on this line : https://github.com/rucio/rucio/blob/ea4d2c7e2702b85a86a8668b2d852971f42fceb4/lib/rucio/core/subscription.py#L177  . Do you want to do it or should I do it ?
I can try to do it. 
Hi @cserf 
Another thing, shouldn't the update_subscription call be only for subscriptions that run? i.e under this `if statement`. At the moment, subscriptions that are not evaluated get updated too. 
 https://github.com/rucio/rucio/blob/ea4d2c7e2702b85a86a8668b2d852971f42fceb4/lib/rucio/daemons/transmogrifier/transmogrifier.py#L512

Also, another column indicating the date for `subscription_update` will be helpful for us.

Resolving patch:
diff --git a/lib/rucio/core/subscription.py b/lib/rucio/core/subscription.py
--- a/lib/rucio/core/subscription.py
+++ b/lib/rucio/core/subscription.py
@@ -173,8 +173,17 @@ def update_subscription(name: str,
     SubscriptionHistory = models.SubscriptionHistory
     try:
         subscription = session.query(models.Subscription).filter_by(account=account, name=name).one()
+
+        # To avoid update in the subscription history table whenever last processed field is changed
+        current_subscription_state = subscription.to_dict()
+        new_subscription_state = values.copy()
+
+        for key in ["updated_at", "last_processed"]:
+            new_subscription_state.pop(key, "None")
+            current_subscription_state.pop(key, "None")
+
         subscription.update(values)
-        if keep_history:
+        if keep_history and current_subscription_state != new_subscription_state:
             subscription_history = SubscriptionHistory(id=subscription.id,
                                                        name=subscription.name,
                                                        filter=subscription.filter,
@@ -264,7 +273,9 @@ def list_subscription_rule_states(name=None, account=None, *, session: "Session"
     subscription = aliased(models.Subscription)
     rule = aliased(models.ReplicationRule)
     # count needs a label to allow conversion to dict (label name can be changed)
-    query = session.query(subscription.account, subscription.name, rule.state, func.count().label('count')).join(rule, subscription.id == rule.subscription_id)
+    query = session.query(
+        subscription.account, subscription.name, rule.state, func.count().label('count')).join(
+        rule, subscription.id == rule.subscription_id)
 
     try:
         if name:


Test patch: