# Copyright (c) HashiCorp, Inc.# SPDX-License-Identifier: MPL-2.0"""Test connector for Grove."""importtimefromdatetimeimportdatetime,timedeltafromgrove.connectorsimportBaseConnectorfromgrove.constantsimportCHRONOLOGICALfromgrove.exceptionsimportNotFoundExceptionDATESTAMP_FORMAT="%Y-%m-%dT%H:%M:%SZ"
[docs]classConnector(BaseConnector):NAME="local_heartbeat"POINTER_PATH="timestamp"LOG_ORDER=CHRONOLOGICAL@propertydefcount(self):"""Fetches the count of heartbeat messages to emit from the configuration. :return: The number of heartbeat messages to emit. """try:returnint(self.configuration.count)except(AttributeError,ValueError):return5@propertydefinterval(self):"""Fetches the interval to emit a heartbeat message from the configuration. :return: The heartbeat interval, in seconds. """try:returnint(self.configuration.interval)except(AttributeError,ValueError):return1
[docs]defcollect(self):"""Generates test log entries at the configured interval."""try:_=self.pointerexceptNotFoundException:self.pointer=(datetime.utcnow()-timedelta(days=7)).isoformat()for_inrange(self.count):# Writes this batch of entries to the output and updates the pointer.self.save([{"type":"heartbeat","timestamp":datetime.utcnow().strftime(DATESTAMP_FORMAT),}])time.sleep(self.interval)