Skip to content

Commit 3cfb807

Browse files
authored
Merge pull request #12321 from Azure/revert-12231-FixActivityListJsonInteger
Revert "Update __init__.py to fix TypeError: 'int' object is not subscriptable"
2 parents 0330cc1 + a86007f commit 3cfb807

File tree

2 files changed

+28
-70
lines changed

2 files changed

+28
-70
lines changed
Binary file not shown.

Solutions/GoogleWorkspaceReports/Data Connectors/GWorkspaceReportsAPISentinelConn/GWorkspaceReports-TimeTrigger/__init__.py

Lines changed: 28 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -120,81 +120,39 @@ def convertToDatetime(date_time,format):
120120
return datetime_str
121121

122122
def GetDates(logType):
123-
end_time_dt_obj = GetEndTime(logType) # Renamed for clarity, this is a datetime object
123+
end_time = GetEndTime(logType)
124124
state = StateManager(connection_string=connection_string)
125-
past_time_str_from_state = state.get() # This is a string from the state file, or None
126-
127-
# activity_json_str will hold the JSON string representing the dictionary of activity timestamps
128-
activity_json_str = ""
129-
130-
# Default start time if no valid state or specific activity timestamp is found
131-
# Use end_time_dt_obj for this calculation, as it's the relevant end time for the current GetDates call
132-
default_initial_timestamp_str = (end_time_dt_obj - timedelta(minutes=5)).strftime("%Y-%m-%dT%H:%M:%S.%fZ")[:-4] + 'Z'
133-
134-
if past_time_str_from_state and len(past_time_str_from_state) > 0:
135-
if is_json(past_time_str_from_state):
136-
try:
137-
decoded_past_time = json.loads(past_time_str_from_state)
138-
if isinstance(decoded_past_time, dict):
139-
# The state is a valid JSON dictionary, use it.
140-
# Ensure all known activities have an entry, initialize if not.
141-
for activity_key in activities:
142-
if activity_key not in decoded_past_time:
143-
decoded_past_time[activity_key] = default_initial_timestamp_str
144-
activity_json_str = json.dumps(decoded_past_time)
145-
else:
146-
# The state was valid JSON, but not a dictionary (e.g., "123" or "\"a string\"").
147-
# This is an invalid state format; reinitialize all activities.
148-
temp_dict = {}
149-
for activity_key_init in activities:
150-
temp_dict[activity_key_init] = default_initial_timestamp_str
151-
activity_json_str = json.dumps(temp_dict)
152-
except json.JSONDecodeError as e:
153-
# is_json passed, but json.loads failed. This should be rare.
154-
logging.error(f"Failed to decode JSON from state (is_json was true): {past_time_str_from_state}. Error: {e}. Re-initializing.")
155-
temp_dict = {}
156-
for activity_key_init in activities:
157-
temp_dict[activity_key_init] = default_initial_timestamp_str
158-
activity_json_str = json.dumps(temp_dict)
125+
past_time = state.get()
126+
activity_list = {}
127+
if past_time is not None and len(past_time) > 0:
128+
if is_json(past_time):
129+
activity_list = past_time
130+
# Check if state file has non-date format. If yes, then set start_time to past_time
159131
else:
160-
# past_time_str_from_state is not JSON, try legacy format conversion (single timestamp for all activities)
161-
temp_dict = {}
162132
try:
163-
newtime = datetime.strptime(past_time_str_from_state[:-1] + '.000Z', "%Y-%m-%dT%H:%M:%S.%fZ")
164-
newtime_str = newtime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")[:-4] + 'Z'
165-
for activity_key_init in activities:
166-
temp_dict[activity_key_init] = newtime_str
167-
activity_json_str = json.dumps(temp_dict)
133+
newtime = datetime.strptime(past_time[:-1] + '.000Z', "%Y-%m-%dT%H:%M:%S.%fZ")
134+
newtime = newtime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
135+
newtime = newtime[:-4] + 'Z'
136+
for activity in activities:
137+
activity_list[activity] = newtime
138+
activity_list = json.dumps(activity_list)
168139
except Exception as err:
169-
logging.error("Error while converting legacy state. Its neither a json nor a valid date format: {}. Error: {}".format(past_time_str_from_state, err))
170-
logging.info("Setting start time to get events for last 5 minutes for all activities.")
171-
for activity_key_init in activities:
172-
temp_dict[activity_key_init] = default_initial_timestamp_str
173-
activity_json_str = json.dumps(temp_dict)
140+
logging.info("Error while converting state. Its neither a json nor a valid date format {}".format(err))
141+
logging.info("Setting start time to get events for last 5 minutes.")
142+
past_time = (end_time - timedelta(minutes=5)).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
143+
for activity in activities:
144+
activity_list[activity] = past_time[:-4] + 'Z'
145+
activity_list = json.dumps(activity_list)
174146
else:
175-
# No past_time state found or it's empty. Initialize all activities.
176-
logging.info("No valid last time point in state, initializing all activities to fetch events for last 5 minutes.")
177-
temp_dict = {}
178-
for activity_key_init in activities:
179-
temp_dict[activity_key_init] = default_initial_timestamp_str
180-
activity_json_str = json.dumps(temp_dict)
181-
182-
# Convert the end_time_dt_obj (datetime object) to the required string format for returning
183-
final_end_time_str = end_time_dt_obj.strftime("%Y-%m-%dT%H:%M:%S.%fZ")[:-4] + 'Z'
184-
185-
# activity_json_str now reliably holds a JSON string of a dictionary
186-
loaded_activity_dict = json.loads(activity_json_str)
187-
188-
if isBlank(logType):
189-
# Return the entire dictionary of activity timestamps
190-
return loaded_activity_dict
191-
else:
192-
# Return the specific start time for the logType and the calculated end_time string
193-
start_time_for_logtype = loaded_activity_dict.get(logType)
194-
if start_time_for_logtype is None:
195-
# This should ideally not happen if all activities are initialized correctly
196-
start_time_for_logtype = default_initial_timestamp_str
197-
return start_time_for_logtype, final_end_time_str
147+
logging.info("There is no last time point, trying to get events for last 5 minutes.")
148+
past_time = (end_time - timedelta(minutes=5)).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
149+
for activity in activities:
150+
activity_list[activity] = past_time[:-4] + 'Z'
151+
activity_list = json.dumps(activity_list)
152+
end_time = end_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
153+
end_time = end_time[:-4] + 'Z'
154+
155+
return json.loads(activity_list) if (isBlank(logType)) else (json.loads(activity_list)[logType],end_time)
198156

199157
def check_if_script_runs_too_long(script_start_time):
200158
now = int(time.time())

0 commit comments

Comments
 (0)