eDNC: Use machineid instead of hostname for proper FK relationship

- ednclogs table now uses machineid to link to machines table
- Removed redundant hostname storage (derive from machines table)
- Updated LogDNCEvent to look up and insert machineid
- Updated GetDNCStats to join machines table for hostname
- Updated displaypc.asp queries to use machineid directly
- sql/ednc_tables.sql is now a migration script for existing production

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
cproudlock
2025-12-12 09:33:27 -05:00
parent 4441dde2e8
commit a1a10a51d2
3 changed files with 92 additions and 132 deletions

117
api.asp
View File

@@ -2553,15 +2553,13 @@ Sub LogDNCEvent()
On Error Resume Next
' Get parameters
Dim hostname, filename, eventAction, bytesRemoved, version, message, watchFolder, fileFilter
Dim hostname, filename, eventAction, bytesRemoved, version, message
hostname = Trim(Request.Form("hostname") & "")
filename = Trim(Request.Form("filename") & "")
eventAction = Trim(Request.Form("eventType") & "")
bytesRemoved = Request.Form("bytesRemoved")
version = Trim(Request.Form("version") & "")
message = Trim(Request.Form("message") & "")
watchFolder = Trim(Request.Form("watchFolder") & "")
fileFilter = Trim(Request.Form("fileFilter") & "")
' Validate required fields
If hostname = "" Or eventAction = "" Then
@@ -2569,23 +2567,36 @@ Sub LogDNCEvent()
Exit Sub
End If
' Sanitize inputs
Dim safeHostname, safeFilename, safeAction, safeVersion, safeMessage, safeWatchFolder, safeFileFilter
' Default bytesRemoved to 0 if not numeric
If Not IsNumeric(bytesRemoved) Or bytesRemoved = "" Then bytesRemoved = 0
' Get machineid from hostname (required for logging)
Dim safeHostname, machineid, rsLookup
safeHostname = Replace(hostname, "'", "''")
Set rsLookup = objConn.Execute("SELECT machineid FROM machines WHERE UPPER(hostname) = UPPER('" & safeHostname & "') AND pctypeid IS NOT NULL LIMIT 1")
If rsLookup.EOF Then
rsLookup.Close
Set rsLookup = Nothing
SendError "Unknown hostname: " & hostname
Exit Sub
End If
machineid = CLng(rsLookup("machineid"))
rsLookup.Close
Set rsLookup = Nothing
' Sanitize remaining inputs
Dim safeFilename, safeAction, safeVersion, safeMessage
safeFilename = Replace(filename, "'", "''")
safeAction = Replace(eventAction, "'", "''")
safeVersion = Replace(version, "'", "''")
safeMessage = Replace(message, "'", "''")
safeWatchFolder = Replace(watchFolder, "'", "''")
safeFileFilter = Replace(fileFilter, "'", "''")
' Default bytesRemoved to 0 if not numeric
If Not IsNumeric(bytesRemoved) Or bytesRemoved = "" Then bytesRemoved = 0
' Insert log entry
' Insert log entry using machineid
Dim insertSQL
insertSQL = "INSERT INTO ednc_logs (hostname, filename, action, bytes_removed, version, message) " & _
"VALUES ('" & safeHostname & "', '" & safeFilename & "', '" & safeAction & "', " & _
insertSQL = "INSERT INTO ednclogs (machineid, filename, action, bytes_removed, version, message) " & _
"VALUES (" & machineid & ", '" & safeFilename & "', '" & safeAction & "', " & _
CLng(bytesRemoved) & ", '" & safeVersion & "', '" & safeMessage & "')"
objConn.Execute insertSQL
@@ -2594,52 +2605,18 @@ Sub LogDNCEvent()
Exit Sub
End If
' Update or insert installation record
Dim checkSQL, rsCheck
checkSQL = "SELECT installid, total_cleaned, total_failed FROM ednc_installations WHERE hostname = '" & safeHostname & "'"
Set rsCheck = objConn.Execute(checkSQL)
' Track in installedapps (appid 79 = eDNC Special Character Fix)
Dim edncAppId, rsApp
edncAppId = 79
If rsCheck.EOF Then
' New installation - INSERT
Dim installSQL
installSQL = "INSERT INTO ednc_installations (hostname, version, watch_folder, file_filter, total_cleaned, total_failed) " & _
"VALUES ('" & safeHostname & "', '" & safeVersion & "', '" & safeWatchFolder & "', '" & safeFileFilter & "', "
If eventAction = "cleaned" Then
installSQL = installSQL & "1, 0)"
ElseIf eventAction = "failed" Or eventAction = "error" Then
installSQL = installSQL & "0, 1)"
Else
installSQL = installSQL & "0, 0)"
End If
objConn.Execute installSQL
Else
' Existing installation - UPDATE
Dim totalCleaned, totalFailed, updateSQL
totalCleaned = CLng(rsCheck("total_cleaned"))
totalFailed = CLng(rsCheck("total_failed"))
If eventAction = "cleaned" Then
totalCleaned = totalCleaned + 1
ElseIf eventAction = "failed" Or eventAction = "error" Then
totalFailed = totalFailed + 1
End If
updateSQL = "UPDATE ednc_installations SET " & _
"version = '" & safeVersion & "', " & _
"last_seen = NOW(), " & _
"total_cleaned = " & totalCleaned & ", " & _
"total_failed = " & totalFailed
If safeWatchFolder <> "" Then
updateSQL = updateSQL & ", watch_folder = '" & safeWatchFolder & "'"
End If
If safeFileFilter <> "" Then
updateSQL = updateSQL & ", file_filter = '" & safeFileFilter & "'"
End If
updateSQL = updateSQL & " WHERE hostname = '" & safeHostname & "'"
objConn.Execute updateSQL
' Check if already in installedapps
Set rsApp = objConn.Execute("SELECT installedappid FROM installedapps WHERE machineid = " & machineid & " AND appid = " & edncAppId)
If rsApp.EOF Then
' Insert new record
objConn.Execute "INSERT INTO installedapps (appid, machineid, isactive) VALUES (" & edncAppId & ", " & machineid & ", 1)"
End If
rsCheck.Close
Set rsCheck = Nothing
rsApp.Close
Set rsApp = Nothing
' Send success response
Response.Write "{""success"":true,""message"":""Event logged""}"
@@ -2648,12 +2625,19 @@ End Sub
Sub GetDNCStats()
On Error Resume Next
' Get installations with recent activity
' Get stats derived from ednclogs, joined to machines for hostname
Dim sql, rs
sql = "SELECT i.hostname, i.version, i.watch_folder, i.file_filter, " & _
"i.first_seen, i.last_seen, i.is_active, i.total_cleaned, i.total_failed, " & _
"(SELECT COUNT(*) FROM ednc_logs l WHERE l.hostname = i.hostname AND l.created > DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS events_24h " & _
"FROM ednc_installations i ORDER BY i.last_seen DESC"
sql = "SELECT m.hostname, " & _
"(SELECT version FROM ednclogs WHERE machineid = l.machineid ORDER BY created DESC LIMIT 1) AS version, " & _
"MIN(l.created) AS first_seen, " & _
"MAX(l.created) AS last_seen, " & _
"SUM(CASE WHEN l.action = 'cleaned' THEN 1 ELSE 0 END) AS total_cleaned, " & _
"SUM(CASE WHEN l.action = 'failed' THEN 1 ELSE 0 END) AS total_failed, " & _
"(SELECT COUNT(*) FROM ednclogs WHERE machineid = l.machineid AND created > DATE_SUB(NOW(), INTERVAL 24 HOUR)) AS events_24h " & _
"FROM ednclogs l " & _
"INNER JOIN machines m ON l.machineid = m.machineid " & _
"GROUP BY l.machineid, m.hostname " & _
"ORDER BY last_seen DESC"
Set rs = objConn.Execute(sql)
@@ -2674,14 +2658,11 @@ Sub GetDNCStats()
json = json & "{" & _
"""hostname"":""" & (rs("hostname") & "") & """," & _
"""version"":""" & (rs("version") & "") & """," & _
"""watchFolder"":""" & Replace(rs("watch_folder") & "", "\", "\\") & """," & _
"""fileFilter"":""" & (rs("file_filter") & "") & """," & _
"""firstSeen"":""" & (rs("first_seen") & "") & """," & _
"""lastSeen"":""" & (rs("last_seen") & "") & """," & _
"""isActive"":" & rs("is_active") & "," & _
"""totalCleaned"":" & rs("total_cleaned") & "," & _
"""totalFailed"":" & rs("total_failed") & "," & _
"""events24h"":" & rs("events_24h") & _
"""totalCleaned"":" & (rs("total_cleaned") + 0) & "," & _
"""totalFailed"":" & (rs("total_failed") + 0) & "," & _
"""events24h"":" & (rs("events_24h") + 0) & _
"}"
rs.MoveNext
Loop