Skip to content

Commit 2b55289

Browse files
committed
Added workshop section README
1 parent cf217c4 commit 2b55289

File tree

3 files changed

+93
-27
lines changed

3 files changed

+93
-27
lines changed

04-refactoring/IT/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# AI-Assisted Code Refactoring Workshop: Server Monitoring System
2+
3+
## Workshop Overview
4+
This workshop demonstrates how AI coding assistants can help improve error handling and code maintainability in IT systems. Using a server monitoring system as our example, we'll explore common code quality issues and learn how AI can assist in identifying and fixing them.
5+
6+
## Learning Objectives
7+
- Understand how AI can assist in identifying code quality issues
8+
- Learn to translate theoretical best practices into practical improvements
9+
- Experience real-world error handling scenarios
10+
- Bridge the gap between development and IT operations
11+
12+
## The Scenario
13+
We're working with a server monitoring system that represents a common situation in IT:
14+
- A robust third-party tool or API (`mock_ping.py`) that we can't modify
15+
- Our own integration code (`server_availability.py`) that needs improvement
16+
- Real-world challenges like unclear error messages, poor logging, and lost context
17+
18+
### Understanding mock_ping.py
19+
In our workshop, `mock_ping.py` represents third-party APIs or tools that IT teams commonly work with, such as:
20+
- Cloud provider APIs (AWS, Azure, GCP)
21+
- Monitoring tool APIs (Nagios, Zabbix, SolarWinds)
22+
- Network infrastructure APIs (Cisco, F5, etc.)
23+
24+
This file serves as our "API documentation" for the AI, similar to how you might provide AWS documentation when asking AI to help with AWS integrations.
25+
26+
## Project Structure
27+
```
28+
IT
29+
├── server_availability_hints.py # Initial version of the server availability checker with comments on potential improvements
30+
├── server_availability.py # Unrefactored implementation of the server availability checker
31+
├── server_availability_refactored.py # Refactored version of the server availability checker with enhanced error handling and maintainability
32+
├── mock_ping.py # Mock implementation of a ping function which represents a third-party API/tool
33+
├── test-for-intial-code-notebook.ipynb # Notebook testing the unrefactored server availability checker
34+
├── test-for-refactored-code-notebook.ipynb # Notebook validating the improvements in the refactored server availability checker
35+
└── README.md # This file
36+
```
37+
38+
## Workshop Flow
39+
1. **Review Current State**
40+
- Examine the current `server_availability.py`
41+
- Identify common issues in error handling and logging
42+
- Run test scenarios via `test-for-intial-code-notebook.ipynb` to see problematic outputs
43+
44+
2. **Understanding the Context**
45+
- Learn how `mock_ping.py` represents real-world APIs
46+
- See how error information gets lost in our integration
47+
- Identify where improvements are needed
48+
49+
3. **AI-Assisted Refactoring**
50+
- Use AI to help identify improvements
51+
- Implement better error handling
52+
- Add proper logging and resource management
53+
- Maintain better error context
54+
55+
4. **Validation**
56+
- Run the same test scenarios via `test-for-refactored-code-notebook.ipynb`
57+
- Compare output quality
58+
- Understand the improvements
59+
60+
61+
62+
## Workshop Materials
63+
- `mock_ping.py`: Simulates a third-party API with rich error types
64+
- `server_availability.py`: Starting point with common issues
65+
- `server-test-notebook.ipynb`: Test scenarios for before/after comparison
66+
67+
## Expected Outcomes
68+
After this workshop, participants will:
69+
- Better understand how to work with third-party APIs
70+
- Know how to use AI to improve error handling
71+
- Be able to identify and fix common code quality issues
72+
- Understand how to maintain error context
73+
- Have practical experience with code refactoring

04-refactoring/IT/server_availability_hints.py

+20-27
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,23 @@
33
from io import StringIO
44

55
def check_server_availability(servers):
6-
# No input validation or type hints
7-
8-
try:
9-
output = StringIO()
10-
11-
# Write header with timestamp
12-
output.write(f"Time: {datetime.datetime.now()}\n")
13-
14-
for s in servers: # Poor variable naming
15-
try:
16-
# Ignores all the rich information from mock_ping
17-
# and reduces it to just True/False
18-
result = mock_ping(s)
19-
if result == True: # Non-pythonic comparison
20-
f.write(s + " is up\n")
21-
else:
22-
# Ambiguous error messaging
23-
f.write(s + " is down\n")
24-
except: # Bare except clause
25-
# Generic error message without context
26-
f.write("Error checking server\n")
27-
28-
f.close() # Manual file closing
29-
except: # Another bare except
30-
# Unhelpful error message
31-
print("Error writing to log file") # Inconsistent error reporting (print vs write)
32-
return # Silent return on error
6+
output = StringIO()
7+
8+
# Write header with timestamp
9+
output.write(f"Time: {datetime.datetime.now()}\n")
10+
11+
# Check each server
12+
for server in servers: # Poor variable naming
13+
try:
14+
# Ignores all the rich information from mock_ping
15+
# and reduces it to just True/False
16+
result = mock_ping(server)
17+
status = "UP" if result else "DOWN"
18+
output.write(f"{server} is {status}\n")
19+
except: # Bare except clause
20+
output.write(f"Error checking server {server}\n") # Generic error message without context
21+
22+
# Print and return the complete output
23+
result = output.getvalue()
24+
print(result)
25+
return

0 commit comments

Comments
 (0)