|
| 1 | +function plantLogPath = seedPlantLog(rawDir, cfg) |
| 2 | +%SEEDPLANTLOG Generate a synthetic plant log CSV for the industrial plant demo. |
| 3 | +% plantLogPath = seedPlantLog(rawDir, cfg) writes ~30 deterministic |
| 4 | +% operator-log entries to data/raw/plant_log.csv with timestamps spread |
| 5 | +% across the last 7 days plus 3 entries in the recent past (now-30s, |
| 6 | +% now-15s, now+0s) so the live-tail demo immediately has fresh-looking |
| 7 | +% entries to display. |
| 8 | +% |
| 9 | +% The CSV columns are: |
| 10 | +% Timestamp,Message,Unit,Shift,Operator |
| 11 | +% |
| 12 | +% - Timestamp uses 'yyyy-MM-dd HH:mm:ss' (PlantLogReader auto-detect format). |
| 13 | +% - Message is the free-text operator note. |
| 14 | +% - Unit values are drawn from cfg.Subsystems ({'FeedLine','Reactor','Cooling'}) |
| 15 | +% plus 'ALL' for plant-wide entries. |
| 16 | +% - Shift is 'A' | 'B' | 'C'. |
| 17 | +% - Operator is a small name pool. |
| 18 | +% |
| 19 | +% The function reseeds RNG to 1015 at entry and restores the previous RNG |
| 20 | +% state at exit (matching seedHistory.m's idiom). Determinism + state |
| 21 | +% restore lets repeated run_demo() calls produce byte-identical CSVs |
| 22 | +% (modulo the now-relative anchor timestamp). |
| 23 | +% |
| 24 | +% Inputs: |
| 25 | +% rawDir - char, absolute path to demo/industrial_plant/data/raw (must exist). |
| 26 | +% cfg - struct returned by plantConfig() -- uses cfg.Subsystems. |
| 27 | +% |
| 28 | +% Output: |
| 29 | +% plantLogPath - char, absolute path to the generated CSV. |
| 30 | +% |
| 31 | +% See also: seedHistory, plantConfig, run_demo, PlantLogReader. |
| 32 | + |
| 33 | + % --- Input validation ----------------------------------------------- |
| 34 | + if ~ischar(rawDir) && ~(isstring(rawDir) && isscalar(rawDir)) |
| 35 | + error('IndustrialPlant:invalidRawDir', ... |
| 36 | + 'rawDir must be a char or scalar string.'); |
| 37 | + end |
| 38 | + rawDir = char(rawDir); |
| 39 | + if ~exist(rawDir, 'dir') |
| 40 | + error('IndustrialPlant:rawDirMissing', ... |
| 41 | + 'rawDir does not exist: %s', rawDir); |
| 42 | + end |
| 43 | + if ~isstruct(cfg) |
| 44 | + error('IndustrialPlant:invalidCfg', ... |
| 45 | + 'cfg must be a struct (plantConfig() output).'); |
| 46 | + end |
| 47 | + |
| 48 | + % --- Seed RNG, restore on exit (matches seedHistory.m idiom) -------- |
| 49 | + prevRng = rng(1015, 'twister'); |
| 50 | + cleanup = onCleanup(@() rng(prevRng)); |
| 51 | + |
| 52 | + % --- Build entry pool ------------------------------------------------ |
| 53 | + % Each entry: offsetSeconds (relative to now()) + message + unit + shift + operator. |
| 54 | + % First 30 entries spread across the last 7 days at shift-start times |
| 55 | + % (06:00, 14:00, 22:00) and an early-morning maintenance window (02:30); |
| 56 | + % final 3 entries land in the recent past so live-tail picks them up. |
| 57 | + entries = buildEntries_(cfg); |
| 58 | + |
| 59 | + % --- Write CSV via fprintf (cross-runtime, MATLAB + Octave 7+) ------ |
| 60 | + % writetable's 'Size'+'VariableTypes' form is MATLAB-only on some |
| 61 | + % Octave builds; fprintf is the safe lowest-common-denominator. |
| 62 | + plantLogPath = fullfile(rawDir, 'plant_log.csv'); |
| 63 | + nowRef = now(); |
| 64 | + |
| 65 | + % Sort entries by offsetSeconds ASC so timestamps land chronologically |
| 66 | + % in the CSV (PlantLogStore dedup tolerates out-of-order but ordered |
| 67 | + % is the canonical state we want the live-tail tail to read). |
| 68 | + [~, order] = sort([entries.offsetSeconds]); |
| 69 | + entries = entries(order); |
| 70 | + |
| 71 | + fid = fopen(plantLogPath, 'w'); |
| 72 | + if fid == -1 |
| 73 | + error('IndustrialPlant:writeFailed', ... |
| 74 | + 'Could not open %s for writing.', plantLogPath); |
| 75 | + end |
| 76 | + closer = onCleanup(@() fclose(fid)); |
| 77 | + fprintf(fid, 'Timestamp,Message,Unit,Shift,Operator\n'); |
| 78 | + for k = 1:numel(entries) |
| 79 | + e = entries(k); |
| 80 | + ts = datestr(nowRef + e.offsetSeconds/86400, 'yyyy-mm-dd HH:MM:SS'); %#ok<DATST> |
| 81 | + % Quote message field (may contain commas/colons); other fields |
| 82 | + % are short alphanum so unquoted is fine. |
| 83 | + fprintf(fid, '%s,"%s",%s,%s,%s\n', ts, e.message, e.unit, e.shift, e.operator); |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +function entries = buildEntries_(cfg) |
| 88 | + %BUILDENTRIES_ Construct the 33-entry plant-log pool. |
| 89 | + % First 30 entries: shift-pattern times spread over 7 days. Final 3 |
| 90 | + % entries: near-now (-30s, -15s, 0s) so the live-tail demo has fresh |
| 91 | + % content as soon as the dashboard renders. |
| 92 | + % |
| 93 | + % Unit values use the 4-element set [{'ALL'}, cfg.Subsystems(:)'] |
| 94 | + % directly so the demo's subsystem nomenclature is the single source |
| 95 | + % of truth (changing cfg.Subsystems propagates to seedPlantLog). |
| 96 | + |
| 97 | + units = [{'ALL'}, cfg.Subsystems(:)']; %#ok<NASGU> referenced via literals below |
| 98 | + |
| 99 | + % Shift-start anchor times within a day (HH * 3600 + MM * 60 + SS): |
| 100 | + % 06:00 -> 21600s |
| 101 | + % 14:00 -> 50400s |
| 102 | + % 22:00 -> 79200s |
| 103 | + % 02:30 -> 9000s (overnight maintenance) |
| 104 | + shiftA = 21600; |
| 105 | + shiftB = 50400; |
| 106 | + shiftC = 79200; |
| 107 | + maint = 9000; |
| 108 | + |
| 109 | + % Helper to compute an offsetSeconds: secondsIntoDay - daysAgo * 86400. |
| 110 | + % Day 0 is "today"; negative offsetSeconds = past. |
| 111 | + secOf = @(daysAgo, secondsIntoDay) -(daysAgo * 86400) + (secondsIntoDay - 86400); |
| 112 | + % Explanation: relative to now (= 86400s offset within today), an event |
| 113 | + % at secondsIntoDay of (today - daysAgo) sits at: |
| 114 | + % (secondsIntoDay) + (-daysAgo - 0) * 86400 - 86400 |
| 115 | + % which simplifies above. The result is strictly <= 0 for any |
| 116 | + % daysAgo >= 0 and secondsIntoDay <= 86400. |
| 117 | + |
| 118 | + % Build the 30-entry historical pool (shift-pattern times across days 0..6). |
| 119 | + % Mix shift-starts with overnight maintenance entries for variety. |
| 120 | + rows = { ... |
| 121 | + % daysAgo secondsIntoDay message unit shift operator |
| 122 | + 6, shiftA, 'Operator Mehta starting morning shift, all systems nominal', 'ALL', 'A', 'Mehta'; ... |
| 123 | + 6, shiftB, 'Routine maintenance: cooling pump filter changed', 'Cooling', 'B', 'Yamamoto'; ... |
| 124 | + 6, shiftC, 'Reactor heated to 160C setpoint', 'Reactor', 'A', 'Patel'; ... |
| 125 | + 5, maint, 'Feedline pressure alarm cleared', 'FeedLine', 'C', 'Davis'; ... |
| 126 | + 5, shiftA, 'Batch B-2381 started', 'Reactor', 'B', 'Patel'; ... |
| 127 | + 5, shiftB, 'Batch B-2381 complete, 1843 L yield', 'Reactor', 'B', 'Patel'; ... |
| 128 | + 5, shiftC, 'Shift handover: Davis -> Patel, no anomalies reported', 'ALL', 'A', 'Patel'; ... |
| 129 | + 4, maint, 'Heat exchanger fouling suspected, cleaning scheduled', 'Cooling', 'A', 'Yamamoto'; ... |
| 130 | + 4, shiftB, 'Reactor pressure spike at 14:32 acknowledged by operator Chen', 'Reactor', 'B', 'Chen'; ... |
| 131 | + 4, shiftC, 'Feedline valve V-117 replaced with conditioning unit', 'FeedLine', 'C', 'Davis'; ... |
| 132 | + 3, shiftA, 'Cooling loop flow rate adjusted to 95 L/min', 'Cooling', 'A', 'Yamamoto'; ... |
| 133 | + 3, shiftA + 1800, 'Pre-shift safety briefing complete', 'ALL', 'A', 'Mehta'; ... |
| 134 | + 3, shiftB, 'Reactor mode transition: heating -> running', 'Reactor', 'B', 'Chen'; ... |
| 135 | + 3, shiftC, 'Inlet temperature sensor calibration verified', 'Cooling', 'A', 'Yamamoto'; ... |
| 136 | + 2, shiftA, 'Feedline pressure transient observed during startup', 'FeedLine', 'A', 'Patel'; ... |
| 137 | + 2, shiftB, 'Emergency stop test (drill) completed successfully', 'ALL', 'B', 'Mehta'; ... |
| 138 | + 2, shiftC, 'Reactor RPM trending nominal, no action required', 'Reactor', 'C', 'Davis'; ... |
| 139 | + 2, maint, 'Cooling tower fan cycled per maintenance schedule', 'Cooling', 'C', 'Yamamoto'; ... |
| 140 | + 1, shiftA, 'Batch B-2382 started', 'Reactor', 'A', 'Patel'; ... |
| 141 | + 1, shiftA + 600, 'Feedline strainer inspection: clean', 'FeedLine', 'A', 'Davis'; ... |
| 142 | + 1, shiftB, 'Reactor temperature setpoint changed to 165C per recipe revision','Reactor', 'B', 'Chen'; ... |
| 143 | + 1, shiftC, 'Night shift quiet period, monitoring only', 'ALL', 'C', 'Davis'; ... |
| 144 | + 0, maint, 'Cooling water pH within spec (7.4)', 'Cooling', 'A', 'Yamamoto'; ... |
| 145 | + 0, shiftA, 'Feedline flow stable at 122 L/min', 'FeedLine', 'B', 'Patel'; ... |
| 146 | + 0, shiftA + 1200, 'Reactor agitator vibration spike investigated, within tolerance','Reactor', 'A', 'Chen'; ... |
| 147 | + 0, shiftA + 2400, 'Batch B-2382 complete, 1798 L yield', 'Reactor', 'B', 'Patel'; ... |
| 148 | + 0, shiftA + 3000, 'Shift handover: Patel -> Mehta, batch B-2383 queued', 'ALL', 'A', 'Mehta'; ... |
| 149 | + 0, shiftA + 3600, 'Cooling out-temp briefly exceeded 50C, alarm cleared after 12s', 'Cooling', 'B', 'Yamamoto'; ... |
| 150 | + 0, shiftA + 4200, 'Feedline valve V-118 actuator stroke time verified', 'FeedLine', 'A', 'Davis'; ... |
| 151 | + 0, shiftA + 4800, 'Reactor pressure trending up -- operator confirms expected', 'Reactor', 'B', 'Chen' ... |
| 152 | + }; |
| 153 | + |
| 154 | + nHist = size(rows, 1); |
| 155 | + entries = repmat(struct( ... |
| 156 | + 'offsetSeconds', 0, ... |
| 157 | + 'message', '', ... |
| 158 | + 'unit', '', ... |
| 159 | + 'shift', '', ... |
| 160 | + 'operator', ''), 1, nHist + 3); |
| 161 | + |
| 162 | + for k = 1:nHist |
| 163 | + daysAgo = rows{k, 1}; |
| 164 | + secondsIntoDay = rows{k, 2}; |
| 165 | + entries(k).offsetSeconds = secOf(daysAgo, secondsIntoDay); |
| 166 | + entries(k).message = rows{k, 3}; |
| 167 | + entries(k).unit = rows{k, 4}; |
| 168 | + entries(k).shift = rows{k, 5}; |
| 169 | + entries(k).operator = rows{k, 6}; |
| 170 | + end |
| 171 | + |
| 172 | + % --- 3 entries near now() so the live-tail demo shows fresh content -- |
| 173 | + entries(nHist + 1).offsetSeconds = -30; |
| 174 | + entries(nHist + 1).message = 'Live-tail entry: 30s ago -- routine check, all green'; |
| 175 | + entries(nHist + 1).unit = 'ALL'; |
| 176 | + entries(nHist + 1).shift = 'A'; |
| 177 | + entries(nHist + 1).operator = 'Mehta'; |
| 178 | + |
| 179 | + entries(nHist + 2).offsetSeconds = -15; |
| 180 | + entries(nHist + 2).message = 'Live-tail entry: 15s ago -- feedline pressure 5.1 bar nominal'; |
| 181 | + entries(nHist + 2).unit = 'FeedLine'; |
| 182 | + entries(nHist + 2).shift = 'A'; |
| 183 | + entries(nHist + 2).operator = 'Davis'; |
| 184 | + |
| 185 | + entries(nHist + 3).offsetSeconds = 0; |
| 186 | + entries(nHist + 3).message = 'Live-tail entry: now -- beginning fresh observation window'; |
| 187 | + entries(nHist + 3).unit = 'ALL'; |
| 188 | + entries(nHist + 3).shift = 'A'; |
| 189 | + entries(nHist + 3).operator = 'Mehta'; |
| 190 | +end |
0 commit comments