Skip to content

Commit

Permalink
Early exit condition was reversed + added debug logs and use f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Antoine Hinse committed Nov 1, 2024
1 parent d00305c commit 90f2935
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
20 changes: 13 additions & 7 deletions packages/flare/python/cron_job_ingest_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ def main() -> None:

# To avoid cron jobs from doing the same work at the same time, exit new cron jobs if a cron job is already doing work
last_fetched_timestamp = get_last_fetched(app)
if last_fetched_timestamp and last_fetched_timestamp < (
if last_fetched_timestamp and last_fetched_timestamp > (
datetime.now() - CRON_JOB_THRESHOLD_SINCE_LAST_FETCH
):
logger.debug(
"Fetched events less than {} minutes ago, exiting".format(
(CRON_JOB_THRESHOLD_SINCE_LAST_FETCH.seconds) / 60
)
f"Fetched events less than {int(CRON_JOB_THRESHOLD_SINCE_LAST_FETCH.seconds / 60)} minutes ago, exiting"
)
return

Expand All @@ -73,6 +71,10 @@ def main() -> None:

next = get_next(app=app, tenant_id=tenant_id)
start_date = get_start_date(app=app)
logger.debug(
f"Fetching Tenant:{tenant_id}, Next:{next}, Start Date:{start_date}"
)
events_retrieved_count = 0
for response in flare_api.retrieve_feed(next=next, start_date=start_date):
save_last_fetched(app=app)

Expand All @@ -90,13 +92,17 @@ def main() -> None:
if event_feed["items"]:
for item in event_feed["items"]:
print(json.dumps(item))

events_retrieved_count += len(event_feed["items"])
except Exception as e:
logger.error("Exception={}".format(e))
logger.error(f"Exception={e}")

logger.debug(f"Retrieved {events_retrieved_count} events")


def get_next(app: client.Application, tenant_id: int) -> Optional[str]:
return get_collection_value(
app=app, key="{}{}".format(CollectionKeys.NEXT_TOKEN.value, tenant_id)
app=app, key=f"{CollectionKeys.NEXT_TOKEN.value}{tenant_id}"
)


Expand Down Expand Up @@ -162,7 +168,7 @@ def save_next(app: client.Application, tenant_id: int, next: Optional[str]) -> N

save_collection_value(
app=app,
key="{}{}".format(CollectionKeys.NEXT_TOKEN.value, tenant_id),
key=f"{CollectionKeys.NEXT_TOKEN.value}{tenant_id}",
value=next,
)

Expand Down
5 changes: 2 additions & 3 deletions packages/flare/python/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ def retrieve_feed(
"from": next if next else None,
}
if not next:
params["time"] = "{}@".format(
start_date if start_date else date.today().isoformat()
)
from_date = start_date if start_date else date.today().isoformat()
params["time"] = f"{from_date}@"
return self.flare_client.scroll(
method="GET",
url=url,
Expand Down
20 changes: 9 additions & 11 deletions packages/flare/python/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ def __init__(self, *, class_name: str) -> None:
log_filepath = ""
if SPLUNK_HOME:
log_filepath = os.path.join(
SPLUNK_HOME, "var", "log", "splunk", "{}.log".format(APP_NAME)
SPLUNK_HOME, "var", "log", "splunk", f"{APP_NAME}.log"
)
APPLICATION_FOLDER = os.path.join(SPLUNK_HOME, "etc", "apps", APP_NAME)
is_local_build = os.path.islink(APPLICATION_FOLDER)
else:
log_filepath = os.path.join(
tempfile.gettempdir(), "{}.log".format(APP_NAME)
)
log_filepath = os.path.join(tempfile.gettempdir(), f"{APP_NAME}.log")

self.tag_name = os.path.splitext(os.path.basename(class_name))[0]
self._logger = logging.getLogger("flare-{}".format(self.tag_name))
self._logger = logging.getLogger(f"flare-{self.tag_name}")

if is_local_build:
# If the application is a symlink, it's been installed locally
Expand All @@ -39,19 +37,19 @@ def __init__(self, *, class_name: str) -> None:
self._logger.addHandler(handler)

def debug(self, msg: Any) -> None:
self._logger.debug(msg="{}: {}".format(self.tag_name, msg))
self._logger.debug(msg=f"{self.tag_name}: {msg}")

def info(self, msg: Any) -> None:
self._logger.info(msg="{}: {}".format(self.tag_name, msg))
self._logger.info(msg=f"{self.tag_name}: {msg}")

def warning(self, msg: Any) -> None:
self._logger.warning(msg="{}: {}".format(self.tag_name, msg))
self._logger.warning(msg=f"{self.tag_name}: {msg}")

def error(self, msg: Any) -> None:
self._logger.error(msg="{}: {}".format(self.tag_name, msg))
self._logger.error(msg=f"{self.tag_name}: {msg}")

def exception(self, msg: Any) -> None:
self._logger.exception(msg="{}: {}".format(self.tag_name, msg))
self._logger.exception(msg=f"{self.tag_name}: {msg}")

def critical(self, msg: Any) -> None:
self._logger.critical(msg="{}: {}".format(self.tag_name, msg))
self._logger.critical(msg=f"{self.tag_name}: {msg}")

0 comments on commit 90f2935

Please sign in to comment.