Skip to content

Commit

Permalink
Merge pull request #11553 from amaltaro/fix-11546
Browse files Browse the repository at this point in the history
Couple pileup activatedOn/deactivatedOn to its active state change
  • Loading branch information
amaltaro authored Apr 17, 2023
2 parents ce2c996 + 4ff83a2 commit 04de648
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/python/WMCore/MicroService/MSPileup/MSPileupData.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ def stripKeys(docs, skeys=None):
return docs


def getNewTimestamp(doc):
"""
Given a pileup doc - or a subset of it - return a dictionary
with a couple timestamp attributes that need to be updated.
:param doc: a python dictionary representing the pileup information
:return: a python dictionary to update the pileup object
"""
subDoc = {'lastUpdateTime': gmtimeSeconds()}
if "active" in doc and doc['active'] is True:
subDoc['activatedOn'] = gmtimeSeconds()
elif "active" in doc and doc['active'] is False:
subDoc['deactivatedOn'] = gmtimeSeconds()
return subDoc


class MSPileupData():
"""
MSPileupData provides logic behind data used and stored by MSPileup module
Expand Down Expand Up @@ -164,7 +179,8 @@ def updatePileup(self, doc, rseList=None, validate=True):
self.logger.error(err)
return [err.error()]

doc['lastUpdateTime'] = gmtimeSeconds()
# mandatory timestamp updates
doc.update(getNewTimestamp(doc))
# we do not need to create MSPileupObj and validate it since our doc comes directly from DB
try:
self.dbColl.update_one(spec, {"$set": doc})
Expand Down
27 changes: 25 additions & 2 deletions test/python/WMCore_t/MicroService_t/MSPileup_t/MSPileupData_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import unittest

# WMCore modules
from WMCore.MicroService.MSPileup.MSPileupData import MSPileupData, stripKeys
from WMCore.MicroService.MSPileup.MSPileupData import MSPileupData, stripKeys, getNewTimestamp
from WMCore.MicroService.MSPileup.MSPileupError import MSPILEUP_SCHEMA_ERROR
from Utils.Timers import encodeTimestamp, decodeTimestamp
from Utils.Timers import encodeTimestamp, decodeTimestamp, gmtimeSeconds


class MSPileupTest(unittest.TestCase):
Expand Down Expand Up @@ -150,6 +150,29 @@ def testMSPileupQuery(self):
res = self.mgr.getPileup(spec)
self.assertEqual(len(res), 0)

def testGetNewTimestamp(self):
"""Test the getNewTimestamp function"""
timeNow = gmtimeSeconds()
resp = getNewTimestamp({})
self.assertEqual(len(resp), 1)
self.assertTrue(resp['lastUpdateTime'] >= timeNow)

resp = getNewTimestamp({'lastUpdateTime': 1})
self.assertEqual(len(resp), 1)
self.assertTrue(resp['lastUpdateTime'] >= timeNow)

resp = getNewTimestamp({'active': True})
self.assertEqual(len(resp), 2)
self.assertTrue(resp['lastUpdateTime'] >= timeNow)
self.assertTrue(resp['activatedOn'] >= timeNow)
self.assertFalse('deactivatedOn' in resp)

resp = getNewTimestamp({'active': False})
self.assertEqual(len(resp), 2)
self.assertTrue(resp['lastUpdateTime'] >= timeNow)
self.assertTrue(resp['deactivatedOn'] >= timeNow)
self.assertFalse('activatedOn' in resp)


if __name__ == '__main__':
unittest.main()

0 comments on commit 04de648

Please sign in to comment.