To solve these tasks, we will use Python and the Pandas library to process the data in the SQL_data.xlsx file. Below are the answers to the questions along with the explanations.
Question 1: Tickets awaiting processing (10 points)
We need to find the number of tickets that are currently awaiting processing by an agent. According to the problem statement, we need to consider the latest status of each ticket. If the latest status is not 1, the ticket requires processing. Additionally, if at the same timestamp, there are multiple logs with status 1 and other statuses for the same ticket, the logs with status 1 are not considered.
import pandas as pd
# Load the data
df = pd.read_excel('SQL_data.xlsx', parse_dates=['time'])
# Exclude logs with status 1 at the same timestamp for the same ticket
df_latest = df.sort_values(by=['question_id', 'time']).drop_duplicates(subset=['question_id'], keep='last')
df_latest = df_latest[(df_latest['status'] != 1)]
# Count the number of tickets awaiting processing
answer1 = df_latest['question_id'].nunique()
Answer:
1, [number obtained from the calculation above]
Question 2: Deleted messages (5 points)
We need to calculate how many messages were deleted by employees and how many by users, then choose the group that deletes messages more frequently.
Assuming that deletions are marked with event_type equal to 'message_deleted' and we can distinguish users and agents by the user_type field.
# Filter deleted messages
deleted_messages = df[df['event_type'] == 'message_deleted']
# Count deletions by user_type
deletions_by_user_type = deleted_messages['user_type'].value_counts()
# Determine who deletes messages more often
most_frequent_deleter = deletions_by_user_type.idxmax()
number_of_deletions = deletions_by_user_type.max()
Answer:
2, [number_of_deletions]
Question 3: Repeat questions after closure due to 'trolling' (5 points)
We need to find the number of tickets where after an event event_type = 'troll_closed', the user asked a new question (reply).
# Find tickets with 'troll_closed' event
troll_closed_tickets = df[df['event_type'] == 'troll_closed']['question_id'].unique()
count = 0
for qid in troll_closed_tickets:
ticket_events = df[df['question_id'] == qid].sort_values(by='time')
troll_closed_time = ticket_events[ticket_events['event_type'] == 'troll_closed']['time'].max()
# Check if there's a 'reply' after 'troll_closed'
if any((ticket_events['event_type'] == 'reply') & (ticket_events['time'] > troll_closed_time)):
count += 1
answer3 = count
Answer:
3, [answer3]
Question 4: Analysis of ticket tags (10 points)
We need to find the most popular tag among the last tags in each ticket.
# Filter events where event_subtype starts with 'tag_'
tag_events = df[df['event_subtype'].str.startswith('tag_')]
# Get the last tag event for each ticket
last_tag_events = tag_events.sort_values(by=['question_id', 'time']).drop_duplicates(subset=['question_id'], keep='last')
# Find the most popular tag
most_popular_tag = last_tag_events['event_subtype'].value_counts().idxmax()
Answer:
4, [most_popular_tag]
Question 5: Tag most often set by agents (5 points)
We need to find the tag that agents set most often in tickets, considering only events where event_type = 'tag_set_manually'.
# Filter events where event_type is 'tag_set_manually'
agent_tag_events = df[df['event_type'] == 'tag_set_manually']
# Find the most frequent tag
most_frequent_agent_tag = agent_tag_events['event_subtype'].value_counts().idxmax()
Answer:
5, [most_frequent_agent_tag]
Question 6: Change of category with status 0 (5 points)
We need to find the question_id where there is an event event_type = 'set_category' and the ticket status was 0 at that time.
# Find events where event_type is 'set_category' and status is 0
set_category_events = df[(df['event_type'] == 'set_category') & (df['status'] == 0)]
# Get the question_id(s)
question_id_with_status_0 = set_category_events['question_id'].unique()
Answer:
6, [question_id_with_status_0]
Question 7: Calculation of UWT (15 points)
We need to calculate the average User Waiting Time (UWT).
# Exclude deleted messages
deleted_reply_ids = df[df['event_type'] == 'message_deleted']['reply_id']
df_clean = df[~df['reply_id'].isin(deleted_reply_ids)]
# Initialize an empty list to store UWTs
uwt_list = []
for qid in df_clean['question_id'].unique():
ticket_events = df_clean[df_clean['question_id'] == qid].sort_values(by='time')
i = 0
while i < len(ticket_events):
user_question = None
agent_reply = None
# Find user question
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'reply' and row['user_type'] == 'client':
user_question = row
i += 1
break
i += 1
# Skip additional user questions
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'reply' and row['user_type'] == 'client':
i += 1
else:
break
# Find agent reply
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'agent_reply':
agent_reply = row
i += 1
break
i += 1
# Skip additional agent replies
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'agent_reply':
i += 1
else:
break
# Calculate UWT
if user_question is not None and agent_reply is not None:
uwt = (agent_reply['time'] - user_question['time']).total_seconds()
uwt_list.append(uwt)
Compute the average UWT:
average_uwt = sum(uwt_list) / len(uwt_list)
Answer:
7, [average_uwt rounded to 2 decimal places]
Question 8: Calculation of SL (15 points)
Using the UWTs calculated in Question 7, we can compute SL.
# Number of chains with UWT <= 15 minutes (900 seconds)
chains_within_15_min = sum(1 for uwt in uwt_list if uwt <= 900)
# Total number of chains
total_chains = len(uwt_list)
# Calculate SL
sl_percentage = (chains_within_15_min / total_chains) * 100
Answer:
8, [sl_percentage rounded to 2 decimal places]
Question 9: Determining employee response speed (10 points)
We need to calculate the average response time for each user_type and determine which group is the fastest.
# Initialize a dictionary to store total response times and counts for each user_type
response_times_by_user_type = {}
for qid in df_clean['question_id'].unique():
ticket_events = df_clean[df_clean['question_id'] == qid].sort_values(by='time')
i = 0
while i < len(ticket_events):
user_question = None
agent_reply = None
agent_user_type = None
# Find user question
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'reply' and row['user_type'] == 'client':
user_question = row
i += 1
break
i += 1
# Skip additional user questions
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'reply' and row['user_type'] == 'client':
i += 1
else:
break
# Find agent reply
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'agent_reply':
agent_reply = row
agent_user_type = row['user_type']
i += 1
break
i += 1
# Skip additional agent replies
while i < len(ticket_events):
row = ticket_events.iloc[i]
if row['event_type'] == 'agent_reply':
i += 1
else:
break
# Calculate response time
if user_question is not None and agent_reply is not None:
response_time = (agent_reply['time'] - user_question['time']).total_seconds()
if agent_user_type not in response_times_by_user_type:
response_times_by_user_type[agent_user_type] = {'total_time': 0, 'count': 0}
response_times_by_user_type[agent_user_type]['total_time'] += response_time
response_times_by_user_type[agent_user_type]['count'] += 1
# Calculate average response times
average_response_times = {user_type: data['total_time'] / data['count'] for user_type, data in response_times_by_user_type.items()}
# Find the user_type with the fastest response time
fastest_user_type = min(average_response_times, key=average_response_times.get)
Answer:
9, [fastest_user_type]
Question 10: Time until ticket closure (10 points)
Calculate the average time from the last action to the ticket being closed by timeout.
from datetime import timedelta
closure_times = []
for qid in df['question_id'].unique():
ticket_events = df[df['question_id'] == qid].sort_values(by='time')
closed_by_time_events = ticket_events[ticket_events['event_type'] == 'closed_by_time']
if not closed_by_time_events.empty:
last_closed_by_time = closed_by_time_events['time'].max()
events_before_closure = ticket_events[ticket_events['time'] < last_closed_by_time]
if not events_before_closure.empty:
last_action_time = events_before_closure['time'].max()
time_diff = last_closed_by_time - last_action_time
closure_times.append(time_diff.total_seconds() / (24 * 3600)) # Convert seconds to days
average_closure_time = sum(closure_times) / len(closure_times)
Answer:
10, [average_closure_time rounded to 2 decimal places]
Question 11: Time between agent assignment and reset (10 points)
Calculate the average time from agent assignment to reset.
assignment_times = []
for qid in df['question_id'].unique():
ticket_events = df[df['question_id'] == qid].sort_values(by='time')
i = 0
while i < len(ticket_events):
# Find 'set_agent' event(s)
while i < len(ticket_events) and ticket_events.iloc[i]['event_type'] != 'set_agent':
i += 1
if i >= len(ticket_events):
break
# Handle multiple 'set_agent' events
while i + 1 < len(ticket_events) and ticket_events.iloc[i + 1]['event_type'] == 'set_agent':
i += 1
set_agent_time = ticket_events.iloc[i]['time']
i += 1
# Find 'drop_agent' event(s)
while i < len(ticket_events) and ticket_events.iloc[i]['event_type'] != 'drop_agent':
i += 1
if i >= len(ticket_events):
break
# Exclude resets initiated by users
if ticket_events.iloc[i]['initiated_by'] == 'user': # Assuming there's an 'initiated_by' field
i += 1
continue
drop_agent_time = ticket_events.iloc[i]['time']
# Handle multiple 'drop_agent' events
while i + 1 < len(ticket_events) and ticket_events.iloc[i + 1]['event_type'] == 'drop_agent':
i += 1
# Calculate time difference
time_diff = (drop_agent_time - set_agent_time).total_seconds()
assignment_times.append(time_diff)
i += 1
average_assignment_time = sum(assignment_times) / len(assignment_times)
Answer:
11, [average_assignment_time rounded to 2 decimal places]
Note: Replace [...] with the actual numbers obtained from running the code on the dataset.