handleGetMonitors handles GET requests for monitor data
(w http.ResponseWriter, r *http.Request)
| 1556 | |
| 1557 | // handleGetMonitors handles GET requests for monitor data |
| 1558 | func handleGetMonitors(w http.ResponseWriter, r *http.Request) { |
| 1559 | if r.Method != "GET" { |
| 1560 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 1561 | return |
| 1562 | } |
| 1563 | |
| 1564 | monitors, err := getCronitorApi().GetMonitors() |
| 1565 | if err != nil { |
| 1566 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 1567 | return |
| 1568 | } |
| 1569 | |
| 1570 | // Sync monitor names with crontab job names |
| 1571 | go func() { |
| 1572 | // Get all crontabs for name syncing |
| 1573 | crontabs, err := lib.GetAllCrontabs(parseUsers()) |
| 1574 | if err != nil { |
| 1575 | log(fmt.Sprintf("Warning: Failed to get crontabs for name syncing: %v", err)) |
| 1576 | return |
| 1577 | } |
| 1578 | |
| 1579 | var crontabsToSave []*lib.Crontab |
| 1580 | |
| 1581 | // Process each crontab to sync monitor names |
| 1582 | for _, crontab := range crontabs { |
| 1583 | crontabModified := false |
| 1584 | |
| 1585 | for i := range crontab.Lines { |
| 1586 | line := crontab.Lines[i] |
| 1587 | if !line.IsJob || line.Ignored { |
| 1588 | continue |
| 1589 | } |
| 1590 | |
| 1591 | // Check if this job has a monitor and if the name needs updating |
| 1592 | jobKey := line.Key(crontab.CanonicalName()) |
| 1593 | if len(line.Code) > 0 { |
| 1594 | for _, monitor := range monitors { |
| 1595 | // Match by code or key |
| 1596 | if (monitor.Attributes.Code == line.Code) || monitor.Key == jobKey { |
| 1597 | // If monitor name differs from crontab line name, update the crontab |
| 1598 | if monitor.Name != "" && monitor.Name != line.Name { |
| 1599 | line.Name = monitor.Name |
| 1600 | crontabModified = true |
| 1601 | } |
| 1602 | break |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | // If this crontab was modified, mark it for saving |
| 1609 | if crontabModified { |
| 1610 | crontabsToSave = append(crontabsToSave, crontab) |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | // Save any modified crontabs |
| 1615 | for _, crontab := range crontabsToSave { |
nothing calls this directly
no test coverage detected