-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinputTUV2DB.m
277 lines (235 loc) · 11 KB
/
inputTUV2DB.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
%% inputTUV2DB.m
% This application lists the input tuv files pushed by the HFR data providers
% and insert into the HFR database the information needed for the conversion of
% the Codar total data files into the European standard data
% model.
% Author: Lorenzo Corgnati
% Date: July 3, 2018
% E-mail: lorenzo.corgnati@sp.ismar.cnr.it
%%
warning('off', 'all');
iTDB_err = 0;
disp(['[' datestr(now) '] - - ' 'inputTUV2DB started.']);
startDateNum = datenum(startDate);
%%
%% Connect to database
try
conn = database(sqlConfig.database,sqlConfig.user,sqlConfig.password,'Vendor','MySQL','Server',sqlConfig.host);
disp(['[' datestr(now) '] - - ' 'Connection to database successfully established.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
%%
%% Query the database for retrieving the networks managed by the HFR provider username
% Set and exectute the query
try
HFRPusername_selectquery = ['SELECT network_id FROM account_tb WHERE username = ' '''' HFRPusername ''''];
HFRPusername_curs = exec(conn,HFRPusername_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to account_tb table for retrieving the networks managed by the HFR provider username successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Fetch data
try
HFRPusername_curs = fetch(HFRPusername_curs);
HFRPusername_data = HFRPusername_curs.Data;
disp(['[' datestr(now) '] - - ' 'Data of the networks managed by the HFR provider username successfully fetched from account_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Close cursor
try
close(HFRPusername_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to account_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
%%
%% Retrieve networks ID managed by the HFR provider username
try
HFRPnetworks = regexp(HFRPusername_data{1}, '[ ,;]+', 'split');
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
%%
%% Query the database for retrieving data from managed networks
% Set and exectute the query
try
network_selectquery = 'SELECT * FROM network_tb WHERE (network_id = ''';
for HFRPntw_idx=1:length(HFRPnetworks)-1
network_selectquery = [network_selectquery HFRPnetworks{HFRPntw_idx} ''' OR network_id = ' ''''];
end
network_selectquery = [network_selectquery HFRPnetworks{length(HFRPnetworks)} ''') AND EU_HFR_processing_flag=0'];
network_curs = exec(conn,network_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to network_tb table for retrieving data of the managed networks successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Fetch data
try
network_curs = fetch(network_curs);
network_data = network_curs.Data;
disp(['[' datestr(now) '] - - ' 'Data of the managed networks successfully fetched from network_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Retrieve column names
try
network_columnNames = columnnames(network_curs,true);
disp(['[' datestr(now) '] - - ' 'Column names from network_tb table successfully retrieved.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Retrieve the number of networks
try
numNetworks = rows(network_curs);
disp(['[' datestr(now) '] - - ' 'Number of managed networks successfully retrieved from network_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Close cursor
try
close(network_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to network_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
%%
%% Scan the networks, list the related total files and insert information into the database
try
% Find the index of the network_id field
network_idIndexC = strfind(network_columnNames, 'network_id');
network_idIndex = find(not(cellfun('isempty', network_idIndexC)));
% Find the index of the input file path field
inputPathIndexC = strfind(network_columnNames, 'total_input_folder_path');
inputPathIndex = find(not(cellfun('isempty', inputPathIndexC)));
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Scan the networks
try
for network_idx=1:numNetworks
iTDB_err = 0;
if(~isempty(network_data{network_idx,inputPathIndex}))
% Trim heading and trailing whitespaces from folder path
network_data{network_idx,inputPathIndex} = strtrim(network_data{network_idx,inputPathIndex});
% List the input tuv files
try
tuvFiles = rdir([network_data{network_idx,inputPathIndex} filesep '**' filesep '*.tuv'],'datenum>floor(now-8)');
disp(['[' datestr(now) '] - - ' 'Total files from ' network_data{network_idx,network_idIndex} ' network successfully listed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Insert information about the tuv file into the database (if not yet present)
for tuv_idx=1:length(tuvFiles)
iTDB_err = 0;
% Retrieve the filename
[pathstr,name,ext]=fileparts(tuvFiles(tuv_idx).name);
noFullPathName=[name ext];
% Check if the current tuv file is already present on the database
try
dbTotals_selectquery = ['SELECT * FROM total_input_tb WHERE datetime>' '''' startDate ''' AND network_id = ' '''' network_data{network_idx,network_idIndex} ''' AND filename = ' '''' noFullPathName ''' ORDER BY timestamp'];
dbTotals_curs = exec(conn,dbTotals_selectquery);
disp(['[' datestr(now) '] - - ' 'Query to total_input_tb table for checking if ' noFullPathName ' total file is already present in the database successfully executed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Fetch data
try
dbTotals_curs = fetch(dbTotals_curs);
disp(['[' datestr(now) '] - - ' 'Data about the presence of ' noFullPathName ' total file in the database successfully fetched from total_input_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
if(rows(dbTotals_curs) == 0)
% Retrieve information about the tuv file
try
% Load the total file as structure
totStruct = loadRDLFile(tuvFiles(tuv_idx).name,'false','warning');
% Read the file header
totHeader = totStruct.OtherMetadata.Header;
% Retrieve information from header
for header_idx=1:length(totHeader)
splitLine = regexp(totHeader{header_idx}, ' ', 'split');
% Retrieve TimeStamp
if(strcmp(splitLine{1}, '%TimeStamp:'))
TimeStamp = strrep(totHeader{header_idx}(length('%TimeStamp:')+2:length(totHeader{header_idx})), '"', '');
break;
end
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Evaluate datetime from, Time Stamp
try
[t2d_err,DateTime] = timestamp2datetime(TimeStamp);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Retrieve information about the tuv file
try
tuvFileInfo = dir(tuvFiles(tuv_idx).name);
tuvFilesize = tuvFileInfo.bytes/1024;
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
% Write tuv info in total_input_tb table
try
% Define a cell array containing the column names to be added
addColnames = {'filename' 'filepath' 'network_id' 'timestamp' 'datetime' 'reception_date' 'filesize' 'extension' 'NRT_processed_flag'};
% Define a cell array that contains the data for insertion
addData = {noFullPathName,pathstr,network_data{network_idx,network_idIndex},TimeStamp,DateTime,(datestr(now,'yyyy-mm-dd HH:MM:SS')),tuvFilesize,ext,0};
% Append the product data into the total_input_tb table on the database.
tablename = 'total_input_tb';
datainsert(conn,tablename,addColnames,addData);
disp(['[' datestr(now) '] - - ' noFullPathName ' total file information successfully inserted into total_input_tb table.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
end
% Close cursor to total_input_tb table
try
close(dbTotals_curs);
disp(['[' datestr(now) '] - - ' 'Cursor to total_input_tb table successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
end
end
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
%%
%% Close connection
try
close(conn);
disp(['[' datestr(now) '] - - ' 'Connection to database successfully closed.']);
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
iTDB_err = 1;
end
%%
if(iTDB_err==0)
disp(['[' datestr(now) '] - - ' 'inputTUV2DB successfully executed.']);
end