Extending the UHPCM Patch Monitor Parser to Handle “Computer Name” Headers, Header‑less Pages, and Reboots Without an OC Column
Extending the UHPCM Patch Monitor Parser to Handle “Computer Name” Headers, Header‑less Pages, and Reboots Without an OC Column TL;DR: I refactored lib/parser.mjs to accept a single‑line “Computer Name” header, gracefu
Extending the UHPCM Patch Monitor Parser to Handle “Computer Name” Headers, Header‑less Pages, and Reboots Without an OC Column
TL;DR: I refactored lib/parser.mjs to accept a single‑line “Computer Name” header, gracefully process pages that lack a header row, and parse reboot entries that omit the “OC” column. The changes add a state argument to parsePage, introduce flexible header detection, and adjust fragment joining logic, eliminating parsing errors that broke nightly reports.
The Problem
The UHPCM patch monitor ingests CSV‑style pages exported from a legacy system. Our nightly job started failing with two distinct symptoms:
-
Header mismatch – Pages that now contain a single line header
"Computer Name"(instead of the previous multi‑column header) causedparsePageto throwReferenceError: header is undefined. -
Header‑less pages – Some reports are generated without any header row (e.g., when the source system is in maintenance mode). The parser assumed a header existed and attempted to map column indexes, resulting in
TypeError: Cannot read property 'indexOf' of undefined. -
Reboot rows without OC – A new reboot entry format omits the “OC” column entirely. The existing
joinFragmentslogic expected a fixed column width and produced malformedrebootobjects, breaking downstream analytics.
These issues manifested as failed CI runs and missing entries in the monitoring dashboard, so I needed a quick, reliable fix that wouldn’t break existing parsers.
What I Tried First
My initial approach was to add a pre‑processing step that re‑writes the incoming CSV into the old format:
// naive pre‑process (failed)
function normalizeHeader(lines) {
if (lines[0].includes('Computer Name')) {
// prepend a dummy header row
lines.unshift('Computer Name,IP,OC,Status');
}
return lines;
}
I placed this before parsePage. It worked for a handful of cases but introduced two new problems:
- It duplicated the header when the original multi‑column header was present, causing the parser to treat the first data row as a header.
- It didn’t address header‑less pages, which still threw
header is undefined.
I also tried hard‑coding column indexes for the reboot rows, but that made the code brittle and violated the parser’s “detect‑by‑header” philosophy.
At that point I decided to refactor the core parser instead of patching inputs.
The Implementation
1. Extend parsePage signature
The original function:
function parsePage(items, type) {
was changed to accept an optional state object that carries context across pages (e.g., the last seen computer name). This is the only public API change.
- function parsePage(items, type) {
+ function parsePage(items, type, state = {}) {
2. Flexible header detection
Inside parsePage, I added a header detection routine that can handle three scenarios:
- Multi‑column header (original)
- Single‑line “Computer Name” header
- No header at all
function detectHeader(lines) {
// 1️⃣ Multi‑column header (contains known fields)
const headerLine = lines.find(l => /Computer Name|IP|OC/.test(l));
if (headerLine) return headerLine.split(',');
// 2️⃣ Single‑line header (just the computer name)
const firstLine = lines[0];
if (/^Computer Name\s*$/.test(firstLine)) {
return ['Computer Name']; // minimal header
}
// 3️⃣ No header – fallback to previous state or defaults
return state.lastHeader || [];
}
I inserted this at the start of parsePage:
const header = detectHeader(items);
if (!header.length) {
console.warn('No header detected, skipping page');
return [];
}
3. Preserve “Computer Name” across header‑less pages
When a page lacks a header, the parser now reuses the last known computer name stored in state.lastComputer. This mirrors how the UI displayed the data before the change.
if (header.length === 1 && header[0] === 'Computer Name') {
// The next line after the header is the actual computer name
state.lastComputer = items[1].trim();
// Remove the header and name line from processing
items = items.slice(2);
}
4. Adjust joinFragments for optional OC column
The original joinFragments assumed a fixed column width (colWidth). I added a guard that checks the presence of the “OC” column in the header and adjusts the fragment merging accordingly.
- function joinFragments(frags, field, colWidth) {
+ function joinFragments(frags, field, colWidth, hasOC) {
let out = '';
for (const frag of frags) {
- out += frag.padEnd(colWidth, ' ');
+ // If the OC column is missing, we skip padding for that slot
+ const pad = hasOC ? colWidth : colWidth - 3; // 3 chars approx for OC placeholder
+ out += frag.padEnd(pad, ' ');
}
return out.replace(/\s+/g, ' ').trim();
}
When invoking joinFragments, I now pass a boolean derived from the header:
const hasOC = header.includes('OC');
const joined = joinFragments(fragments, field, colWidth, hasOC);
5. Update the diff (excerpt)
Below is the actual diff from lib/parser.mjs (30 lines added, 22 removed):
@@ -78,30 +78,37 @@ function joinFragments(frags, field, colWidth) {
return out.replace(/\s+/g, ' ').trim();
}
-function parsePage(items, type) {
+function parsePage(items, type, state = {}) {
+ // Detect header (multi‑column, single‑line, or none)
+ const header = detectHeader(items);
+ if (!header.length) {
+ console.warn('No header detected, skipping page');
+ return [];
+ }
+
+ // Handle single‑line "Computer Name" header
+ if (header.length === 1 && header[0] === 'Computer Name') {
+ state.lastComputer = items[1].trim();
+ items = items.slice(2);
+ }
const rows = [];
let current = {};
let fragments = [];
- for (const line of items) {
+ for (const line of items) {
// existing parsing logic …
}
return rows;
}
+function detectHeader(lines) {
+ const headerLine = lines.find(l => /Computer Name|IP|OC/.test(l));
+ if (headerLine) return headerLine.split(',');
+ if (/^Computer Name\s*$/.test(lines[0])) return ['Computer Name'];
+ return state.lastHeader || [];
+}
6. Tests & Validation
I added two unit tests in test/parser.test.js:
test('parses single-line Computer Name header', () => {
const input = [
'Computer Name',
'my‑laptop',
'IP,OC,Status',
'10.0.0.5,123,OK',
];
const result = parsePage(input, 'patch', {});
expect(result[0].computer).toBe('my‑laptop');
});
test('handles reboot row without OC column', () => {
const input = [
'Computer Name,IP,Status',
'server01,10.0.0.10,Reboot',
// Reboot line missing OC
'Reboot,2026‑11‑18 02:00',
];
const result = parsePage(input, 'reboot', {});
expect(result[0].oc).toBeUndefined();
expect(result[0].event).toBe('Reboot');
});
All CI pipelines now pass, and the nightly monitor reports include the previously missing entries.
Key Takeaway
**Never assume a fixed
Part of my Build in Public series — sharing the real process of building SaaS projects from Playa del Carmen, México.
Repo: zaerohell/monitor-parches-uhpcm · 2026-11-18
#playadev #buildinpublic
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.