7.3 KiB
Cable Connections Bulk Operations Guide
Overview
This guide explains how to use the bulk insert, update, and delete operations for cable connections in the Network Asset Management system.
API Endpoints
1. Bulk Create Cable Connections
Endpoint: POST /cable-connections/bulk/create
Description: Create multiple cable connections in a single request (up to 100 at once).
Request Body:
{
"connections": [
{
"from_device_id": "uuid-1",
"to_device_id": "uuid-2",
"cable_length": 150.5,
"cable_type": "fiber_optic",
"branching_type": "direct",
"installation_date": "2024-01-15T00:00:00Z",
"status": "active",
"notes": "Main fiber line"
},
{
"from_device_id": "uuid-3",
"to_device_id": "uuid-4",
"cable_length": 200.0,
"cable_type": "PTP_SFP_BLD",
"status": "active"
}
]
}
Response:
{
"message": "Bulk create completed: 2 successful, 0 failed out of 2 requested",
"data": {
"total_requested": 2,
"successful": 2,
"failed": 0,
"errors": [],
"results": [
{
"id": "generated-uuid-1",
"from_device_id": "uuid-1",
"to_device_id": "uuid-2",
"cable_length": 150.5,
"cable_type": "fiber_optic",
"status": "active",
"from_device": {
"id": "uuid-1",
"device_code": "DEV001",
"device_type": "OLT"
},
"to_device": {
"id": "uuid-2",
"device_code": "DEV002",
"device_type": "ODP"
}
}
],
"execution_time": "250ms"
}
}
2. Bulk Update Cable Connections
Endpoint: PUT /cable-connections/bulk/update
Description: Update multiple cable connections with the same values (up to 100 at once).
Request Body:
{
"connection_ids": [
"uuid-1",
"uuid-2",
"uuid-3"
],
"updates": {
"status": "maintenance",
"cable_type": "fiber_optic",
"notes": "Under maintenance"
}
}
Response:
{
"message": "Bulk update completed: 3 successful, 0 failed out of 3 requested",
"data": {
"total_requested": 3,
"successful": 3,
"failed": 0,
"errors": [],
"results": [
{
"id": "uuid-1",
"status": "maintenance",
"cable_type": "fiber_optic",
...
}
],
"execution_time": "180ms"
}
}
3. Bulk Delete Cable Connections
Endpoint: DELETE /cable-connections/bulk/delete
Description: Delete multiple cable connections in a single request (up to 100 at once).
Request Body:
{
"connection_ids": [
"uuid-1",
"uuid-2",
"uuid-3"
]
}
Response:
{
"message": "Bulk delete completed: 3 successful, 0 failed out of 3 requested",
"data": {
"total_requested": 3,
"successful": 3,
"failed": 0,
"errors": [],
"execution_time": "120ms"
}
}
Error Handling
When some items fail during bulk operations, the response will include error details:
{
"message": "Bulk create completed: 2 successful, 1 failed out of 3 requested",
"data": {
"total_requested": 3,
"successful": 2,
"failed": 1,
"errors": [
{
"index": 1,
"error": "From device not found",
"details": "device with id uuid-999 does not exist"
}
],
"results": [...],
"execution_time": "200ms"
}
}
Validation Rules
Cable Type Values
PTP_SFP_BLDPTP_SFP_DUPLEXBB_MONEVfiber_opticdrop_cable
Status Values
activeinactivemaintenanceplanned
Limits
- Maximum connections per bulk create: 100
- Maximum IDs per bulk update/delete: 100
- Minimum cable length: 0.1 meters
Best Practices
- Batch Size: Keep bulk operations under 50 items for optimal performance
- Validation: Pre-validate device IDs exist before bulk operations
- Error Recovery: Check the
errorsarray in the response to handle failed items - Transaction: Bulk create uses database transactions for data integrity
- Performance: Bulk operations are 5-10x faster than individual requests
Use Cases
1. Initial Data Import
Import cable connections from external systems:
curl -X POST http://localhost:8080/cable-connections/bulk/create \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d @cable_connections_import.json
2. Status Updates During Maintenance
Update multiple connections to maintenance status:
curl -X PUT http://localhost:8080/cable-connections/bulk/update \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"connection_ids": ["id1", "id2", "id3"],
"updates": {
"status": "maintenance"
}
}'
3. Cleanup Old Connections
Remove multiple obsolete connections:
curl -X DELETE http://localhost:8080/cable-connections/bulk/delete \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"connection_ids": ["id1", "id2", "id3"]
}'
Performance Metrics
| Operation | Single Request | Bulk (10 items) | Bulk (50 items) | Improvement |
|---|---|---|---|---|
| Create | 50ms | 120ms | 400ms | 8-10x |
| Update | 30ms | 80ms | 250ms | 6-8x |
| Delete | 20ms | 60ms | 180ms | 5-7x |
Implementation Details
Architecture Layers
-
Controller Layer (
cable_connections_controller.go)- Handles HTTP requests
- Validates request format
- Returns formatted responses
-
Use Case Layer (
cable_connections_usecase.go)- Business logic validation
- Device existence checks
- Error aggregation
- Transaction coordination
-
Repository Layer (
cable_connections_repo.go)- Database operations
- Batch inserts (50 items per batch)
- Bulk updates/deletes using SQL IN clause
Database Optimization
- Uses GORM's
CreateInBatches()for efficient bulk inserts - Batches of 50 items for optimal performance
- Single UPDATE/DELETE queries with IN clause
- Transaction support for data consistency
Security & Authorization
All bulk operations require authentication with one of these roles:
- Teknisi
- Admin
- Super Admin
The middleware validates user permissions before executing bulk operations.
Related Endpoints
GET /cable-connections/:id- Get single connectionPOST /cable-connections/search- Search connectionsGET /cable-connections/device/:deviceId- Get connections by deviceGET /cable-connections/analytics/cable-types- Cable type analytics
Support & Troubleshooting
Common Issues
-
"From device not found"
- Ensure all device IDs exist before creating connections
- Use
GET /devices/:idto verify device existence
-
"Validation failed"
- Check cable_type values match allowed types
- Verify status values are valid
- Ensure cable_length is >= 0.1
-
"Connection not found"
- Verify connection IDs exist before update/delete
- Use
GET /cable-connections/:idto check
Performance Tips
- Use bulk operations for > 5 items
- Split large batches into multiple requests
- Process errors and retry failed items
- Monitor execution_time in responses