% process_syslog_CRITICAL.m
% Lats modified Sep 14, 2014
% Ben Raanan

% This script filters syslog critical faults by component and extracts
% time-stamp for each log

% clear 

% workd = '~/Documents/MATLAB/MBARI/mat/syslog/';
% yr=2010:2014;
% vh={'Tethys','Daphne','Makai'};

global yr vh workd 

compHeader={'Component','Mission','Log','Year','Month','Day','HH','MM','SS','Datenum','Msg','Path'};

for q=1:length(yr);
    
    load([workd 'syslog_' num2str(yr(q)) '.mat']);
    
    for v=1:length(fieldnames(syslog))
        
        % remove 'fault' struct if already exists
        if any(strcmp(fieldnames(syslog.(vh{v})),'fault'))
            
            syslog.(vh{v}) = rmfield(syslog.(vh{v}),'fault');
        
        end
        
        % get missions
        clear ms
        ms = fieldnames(syslog.(vh{v}));
        
        clear comp
        comp = cell(1,12);
        for c = 1:numel(ms)
            
            % get logs
            clear log
            log = fieldnames(syslog.(vh{v}).(ms{c}));
            
            for j=1:numel(log)
                
                clear X
                chek = fieldnames(syslog.(vh{v}).(ms{c}).(log{j}));
                if any(strcmp(chek,'CRITICAL'))
                    X = syslog.(vh{v}).(ms{c}).(log{j}).CRITICAL;
                else
                    X = [];
                    warning([vh{v} '/' ms{c} '/' log{j} ': NO CRITICAL'])
                end
                
                if ~isempty(X)
                    com = cell(length(X),12);
                    in = size(comp,1)+1;
                    ind = length(X)+in-1;
                    
                    for k = 1:size(X,1);
                        
                        % reduce to line
                        dataS = X{k,:};
                        
                        % categorize by component and extract message
                        cInd1 = strfind(dataS,'[')+1;
                        cInd2 = strfind(dataS,'](CRITICAL)')-1;
                        cInd  = strfind(dataS,'component: ')+11; % comp name case CBIT
                        wInd  = strfind(dataS,'WATCHDOG');       % flag WATCHDOG reset
                        eInd  = strfind(dataS,'Environmental');
                        mInd = strfind(dataS,'):')+3;
                        
                        % case CBIT/WATCHDOG
                        if strcmp(dataS(cInd1:cInd2),'CBIT')
                            if ~isempty(cInd)
                                com{k,1} = dataS(cInd:end);
                            elseif ~isempty(wInd)
                                com{k,1} = 'WatchDog';
                            elseif ~isempty(eInd)
                                com{k,1} = 'Environmental';
                            else
                                com{k,1} = dataS(cInd1:cInd2);
                            end
                            com{k,11} = ['[CBIT]: ' dataS(mInd:end)];
                        else
                            com{k,1} = dataS(cInd1:cInd2);
                            com{k,11} = dataS(mInd:end);
                        end; clear cInd cInd1 cInd2 wInd
                        
                        % extract mission, log and path
                        com{k,2}  = ms{c};
                        com{k,3}  = log{j};
                        com{k,12} = syslog.(vh{v}).(ms{c}).(log{j}).path;
                        
                        
                        % extract date/time
                        indT = strfind(dataS,'T'); % flag 'T'
                        indZ = strfind(dataS,'Z'); % flag 'Z'
                        
                        % extract date/time
                        year = str2double(dataS(1:4));
                        month = str2double(dataS(6:7));
                        day = str2double(dataS(9:10));
                        hh = str2double(dataS(12:13));
                        mm = str2double(dataS(15:16));
                        ss = str2double(dataS(18:indZ-1));
                        clear indT indZ
                        
                        % log time data
                        com(k,4) = num2cell(year);
                        com(k,5) = num2cell(month);
                        com(k,6) = num2cell(day);
                        com(k,7) = num2cell(hh);
                        com(k,8) = num2cell(mm);
                        com(k,9) = num2cell(ss);
                        com(k,10)= num2cell(datenum(year,month,day,hh,mm,ss));
                    end; clear dataS year month day hh mm ss k;
                    
                    % log data
                    comp(in:ind,:) = com;
                    clear com in ind;
                end;
            end; clear j;
        end
        
        comp(1,:)=[];
        % kickout 'SBIT'
        comp(strcmp(comp(:,1),'SBIT'),:)=[];
        
        % sort chronologically
        [~,si] = sort(cell2mat(comp(:,10)));
        syslog.(vh{v}).fault.data.comp = comp(si,:);
        %         clearvars -except workd yr vh q v syslog filePath mission logName compHeader
    end
    
    % save
    %
    save(['~/Documents/MATLAB/MBARI/mat/syslog/syslog_' num2str(yr(q)) '_comp.mat'],...
        'filePath','mission','logName','syslog','compHeader')
    %}
    
end