-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step10_munge_211_data.php
557 lines (431 loc) · 15.5 KB
/
Step10_munge_211_data.php
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
//this allows us to call the run_sql_loop function later on
//it also loads our SQL connection...
require_once('util/run_sql_loop.function.php');
//get the name of the database and the file from the argument list..
if(!isset($argv[3])){
echo "usage Step10_munge_211_data.php {database} {tablename} {file_to_import.csv}\n";
exit();
}else{
$database = $argv[1];
$tablename = $argv[2];
$filename = $argv[3];
}
if(!file_exists($filename)){
echo "We could not find $filename\n";
exit();
}
$filename = realpath($filename); //to be specific
$sql = [];
//lets build a basic need table
$sql["drop the need table"] = "DROP TABLE IF EXISTS $databae.$tablename"."_need";
$sql["create need table"] = "
CREATE TABLE $database.$tablename"."_need (
`id` int(11) NOT NULL,
`need_taxonomy` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`need_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$sql["primary key need table"] = "
ALTER TABLE $database.$tablename"."_need
ADD PRIMARY KEY (`id`);
";
$sql["auto increment need table"] = "
ALTER TABLE $database.$tablename"."_need
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
";
$sql["drop need link table"] = "DROP TABLE IF EXISTS $database.$tablename"."_to_need";
$sql["create need link table"] = "
CREATE TABLE $database.$tablename"."_to_need (
`call_id` int(11) NOT NULL,
`need_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$sql["index need link table"] = "
ALTER TABLE $database.$tablename"."_to_need
ADD PRIMARY KEY (`call_id`,`need_id`);
";
//and a basic agency table
$sql["drop the the agency table"] = "DROP TABLE IF EXISTS $database.$tablename"."_agency";
$sql["create the agency table"] = "
CREATE TABLE $database.$tablename"."_agency (
`id` int(11) NOT NULL,
`agency_identifier` int(11) NOT NULL,
`agency_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$sql["primary key agency table"] = "
ALTER TABLE $database.$tablename"."_agency
ADD PRIMARY KEY (`id`);
";
$sql["auto increment agency table"] = "
ALTER TABLE $database.$tablename"."_agency
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
";
$sql["drop agency link table"] = "DROP TABLE IF EXISTS $database.$tablename"."_to_agency";
$sql["create agency link table"] = "
CREATE TABLE $database.$tablename"."_to_agency (
`call_id` int(11) NOT NULL,
`agency_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$sql["index agency link table"] = "
ALTER TABLE $database.$tablename"."_to_agency
ADD PRIMARY KEY (`call_id`,`agency_id`);
";
//and a basic referral table
$sql["drop the the referral table"] = "DROP TABLE IF EXISTS $database.$tablename"."_referral";
$sql["create the referral table"] = "
CREATE TABLE $database.$tablename"."_referral (
`id` int(11) NOT NULL,
`referral_identifier` int(11) NOT NULL,
`referral_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$sql["primary key referral table"] = "
ALTER TABLE $database.$tablename"."_referral
ADD PRIMARY KEY (`id`);
";
$sql["auto increment referral table"] = "
ALTER TABLE $database.$tablename"."_referral
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
";
$sql["drop referral link table"] = "DROP TABLE IF EXISTS $database.$tablename"."_to_referral";
$sql["create referral link table"] = "
CREATE TABLE $database.$tablename"."_to_referral (
`call_id` int(11) NOT NULL,
`referral_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";
$sql["index referral link table"] = "
ALTER TABLE $database.$tablename"."_to_referral
ADD PRIMARY KEY (`call_id`,`referral_id`);
";
//then the main data table..
$sql["dropping the table"] = "DROP TABLE IF EXISTS $database.$tablename\n";
$sql["creating the table structure"] = "
CREATE TABLE $database.$tablename (
`report_number` int(11) DEFAULT NULL,
`date_of_call_start_text` varchar(25) DEFAULT NULL,
`date_of_call_end_text` varchar(25) DEFAULT NULL,
`length_of_call_minutes` int(11) DEFAULT NULL,
`age` varchar(7) DEFAULT NULL,
`gender` varchar(55) DEFAULT NULL,
`military_status` varchar(55) DEFAULT NULL,
`military_branch_veteran` varchar(55) DEFAULT NULL,
`military_branch_active_duty` varchar(55) DEFAULT NULL,
`preferred_language` varchar(255) DEFAULT NULL,
`other_language` varchar(255) DEFAULT NULL,
`zip_code` varchar(50) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`county` varchar(255) DEFAULT NULL,
`need_name` varchar(1000) DEFAULT NULL,
`need_taxonomy_code` varchar(1000) DEFAULT NULL,
`met_or_reason_unmet` varchar(1000) DEFAULT NULL,
`parent_agency_name` varchar(1000) DEFAULT NULL,
`parent_agency_identifier` varchar(1000) DEFAULT NULL,
`referral_name` varchar(1000) DEFAULT NULL,
`referral_identifier` varchar(1000) DEFAULT NULL,
`benefits_information_call` varchar(1000) DEFAULT NULL,
`benefits_referral_call` varchar(1000) DEFAULT NULL,
`benefits_transfer_call` varchar(1000) DEFAULT NULL,
`first_time_user` varchar(1000) DEFAULT NULL,
`referral_source_first_time_user` varchar(1000) DEFAULT NULL,
`other_referral_source_first_time_user` varchar(1000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
";
//loops every every SQL command one at a time...
$sql["Load data statement to move the CSV data into the table"] = "
LOAD DATA INFILE '$filename'
INTO TABLE $database.$tablename
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 ROWS
";
$sql["create uninque id per row for further data operations"] = "
ALTER TABLE $database.$tablename ADD `id` INT(11) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`);
";
$sql["add time cols"] = "
ALTER TABLE $database.$tablename
ADD `year` INT(11) NOT NULL AFTER `report_number`,
ADD `week_number` INT(11) NOT NULL AFTER `year`,
ADD `week_sunday` VARCHAR(15) NOT NULL AFTER `week_number`,
ADD `call_time_block` VARCHAR(25) NOT NULL AFTER `week_sunday`,
ADD `call_order` INT(11) NOT NULL AFTER `call_time_block`,
ADD `is_weekday` TINYINT(1) NOT NULL AFTER `call_order`,
ADD `age_bucket` VARCHAR(25) NOT NULL AFTER `age`;
";
run_sql_loop($sql,true);
//if we get here, then we have created the database and made space for our new data structures...
//now we need to loop over the data and create our new column information with lots and lots of update statment
//lets start out by emptying sql
$sql = [];
//before we start the loop lets do an hour lookup table for our time block strings..
$time_block_map = [
0 => '0-3',
1 => '0-3',
2 => '0-3',
3 => '0-3',
4 => '4-7',
5 => '4-7',
6 => '4-7',
7 => '4-7',
8 => '8-11',
9 => '8-11',
10 => '8-11',
11 => '8-11',
12 => '12-15',
13 => '12-15',
14 => '12-15',
15 => '12-15',
16 => '16-19',
17 => '16-19',
18 => '16-19',
19 => '16-19',
20 => '20-24',
21 => '20-24',
22 => '20-24',
23 => '20-24',
24 => '20-24',
];
//run the sql..
$select_sql = "SELECT * FROM $database.$tablename";
$result = f_mysql_query($select_sql); //fyi f_mysql_query is just a wrapper for mysqli_query
//prepare for the first looping of records...
//where we are going to put data
$need_data = [];
$referral_data = [];
$agency_data = [];
$day_count = 1;
$last_date_part = 'something';
$row_count = 0;
//this value limits the row import to a few thousand...
// $row_limit = 1000;
//this value keeps the script running for all of the rows..
$row_limit = 999999999999999999999999999999999999999999;
//run the first loop, which will calculate the data and update the rows as we go..
while($row = mysqli_fetch_assoc($result)){
$row_count++;
//var_export($row);
//exit();
extract($row); //saves so much time..
$age = $row['age'];
$id = $row['id'];
//a series of explode statements will allow us to get the specific values of the date and time string...
//the going in format is '1/1/19 12:12'
list($date_part,$time_part) = explode(' ',$date_of_call_start_text);
list($month, $day, $two_digit_year) = explode('/',$date_part);
list($hour,$second) = explode(':',$time_part);
if($last_date_part == $date_part){
$day_count++;
}else{
$day_count = 1;
}
$last_date_part = $date_part;
//now lets calculate the time block..
$time_block = $time_block_map[$hour];
$this_year = "20$two_digit_year";
$row_date_str = "$this_year-$month-$day";
$timestamp = strtotime($row_date_str);
$week_num = idate('W',$timestamp);
$week_data = getStartAndEndDate($week_num,$this_year);
$sunday_date = $week_data['week_start']; //this should be compatible with Google Correlate!!
if(isWeekday($timestamp)){
$is_weekday = 1;
}else{
$is_weekday = 0;
}
if(is_numeric($age)){
$age_above = ceil($age/5) * 5;
$age_below = floor($age/5) * 5;
$age_range = "$age_below-$age_above";
}else{
$age_range = 'unknown';
}
$update_sql = "
UPDATE $database.$tablename SET
year = '$this_year',
week_number = '$week_num',
week_sunday = '$sunday_date',
call_time_block = '$time_block',
is_weekday = '$is_weekday',
call_order = '$day_count',
age_bucket = '$age_range'
WHERE id = '$id'
;
";
f_mysql_query($update_sql);
echo '.';
//yhaaa!! now we have a solid row of data built... but we also need to ensure that we are putting all of the other data in correctly..
//and we are doing to be lazy... looping over the data twice to get this answer...
//on this loop, we will just break out the data!!
//the need field has a name and a taxonomy, in different fields..
//and is seperated by an asterix
$need_name_array = explode('*',$need_name);
$need_taxonomy_array = explode('*',$need_taxonomy_code);
if(count($need_name_array) != count($need_taxonomy_array)){
echo "$need_name and $need_taxonomy_code are broken!!\n";
var_export($need_name_array);
var_export($need_taxonomy_array);
// exit();
}
foreach($need_name_array as $i => $need_name){
$need_name = trim($need_name);
if(isset($need_taxonomy_array[$i])){
$need_taxonomy = trim($need_taxonomy_array[$i]);
}else{
$need_taxonomy = 'unknown';
}
$need_data["$need_name-$need_taxonomy"] = [
'need_name' => $need_name,
'need_taxonomy' => $need_taxonomy
];
}
$agency_name_array = explode(';',$parent_agency_name);
$agency_id_array = explode(';',$parent_agency_identifier);
foreach($agency_id_array as $i => $agency_identifier){
$agency_identifier = trim($agency_identifier);
$agency_name = trim($agency_name_array[$i]);
$agency_data["$agency_name-$agency_identifier"] = [
'agency_name' => $agency_name,
'agency_identifier' => $agency_identifier,
];
}
$referral_name_array = explode(';',$referral_name);
$referral_id_array = explode(';',$referral_identifier);
foreach($referral_id_array as $i => $referral_identifier){
$referral_identifier = trim($referral_identifier);
$referral_name = trim($referral_name_array[$i]);
$referral_data["$referral_name-$referral_identifier"] = [
'referral_name' => $referral_name,
'referral_identifier' => $referral_identifier,
];
}
if($row_count > $row_limit){
break;
}
}
//now we are going to create the database tables of the supporting data sets...
foreach($referral_data as $referral_row){
extract($referral_row);
if(strlen($referral_identifier) == 0){
$referral_identifier = 0;
}
if(strlen($referral_name) > 0){
$referral_name = f_mysql_real_escape_string($referral_name);
$insert_sql = "
INSERT INTO $database.$tablename"."_referral (`id`, `referral_identifier`, `referral_name`) VALUES (NULL, '$referral_identifier', '$referral_name');
";
f_mysql_query($insert_sql);
echo 'r';
}
}
foreach($agency_data as $agency_row){
extract($agency_row);
if(strlen("$agency_identifier") == 0){
$agency_identifier = 0;
}
if(strlen($agency_name) > 0){
$agency_name = f_mysql_real_escape_string($agency_name);
$insert_sql = "
INSERT INTO $database.$tablename"."_agency (`id`, `agency_identifier`, `agency_name`) VALUES (NULL, '$agency_identifier', '$agency_name');
";
f_mysql_query($insert_sql);
echo 'a';
}
}
foreach($need_data as $need_row){
extract($need_row);
if(strlen($need_name) > 0){
$need_name = f_mysql_real_escape_string($need_name);
$insert_sql = "
INSERT INTO $database.$tablename"."_need (`id`, `need_taxonomy`, `need_name`) VALUES (NULL, '$need_taxonomy', '$need_name');
";
f_mysql_query($insert_sql);
echo 'n';
}
}
//now we load the data back for need
$need_query = "SELECT * FROM $database.$tablename"."_need";
$result = f_mysql_query($need_query);
$need_data = [];
while($row = mysqli_fetch_assoc($result)){
$need_data[$row['need_taxonomy']] = $row;
}
//now we load the data back for agency
$agency_query = "SELECT * FROM $database.$tablename"."_agency";
$result = f_mysql_query($agency_query);
$agency_data = [];
while($row = mysqli_fetch_assoc($result)){
$agency_data[$row['agency_identifier']] = $row;
}
//now we load the data back
$referral_query = "SELECT * FROM $database.$tablename"."_referral";
$result = f_mysql_query($referral_query);
$referral_data = [];
while($row = mysqli_fetch_assoc($result)){
$referral_data[$row['referral_identifier']] = $row;
}
///noww... we loop over the main data again...
$select_sql = "SELECT * FROM $database.$tablename";
$result = f_mysql_query($select_sql); //fyi f_mysql_query is just a wrapper for mysqli_query
//the second loop we perform the linking...
$row_count = 0;
while($row = mysqli_fetch_assoc($result)){
$row_count++;
$id = $row['id'];
extract($row); //cannot forget
$need_taxonomy_array = explode('*',$need_taxonomy_code);
$agency_id_array = explode(';',$parent_agency_identifier);
$referral_id_array = explode(';',$referral_identifier);
foreach($need_taxonomy_array as $this_need_taxonomy){
if(isset($need_data[$this_need_taxonomy])){
$need_id = $need_data[$this_need_taxonomy]['id'];
$insert_sql = "INSERT IGNORE INTO $database.$tablename"."_to_need (`call_id`,`need_id`) VALUES ('$id','$need_id')";
f_mysql_query($insert_sql);
}
// echo 'ln';
}
foreach($agency_id_array as $this_agency_identifier){
if(!isset($agency_data[$this_agency_identifier])){
// echo "xa";
}else{
$agency_id = $agency_data[$this_agency_identifier]['id'];
$insert_sql = "INSERT IGNORE INTO $database.$tablename"."_to_agency (`call_id`,`agency_id`) VALUES ('$id','$agency_id')";
f_mysql_query($insert_sql);
// echo 'la';
}
}
foreach($referral_id_array as $this_referral_identifier){
if(isset($referral_data[$this_referral_identifier])){
$referral_id = $referral_data[$this_referral_identifier]['id'];
$insert_sql = "INSERT IGNORE INTO $database.$tablename"."_to_referral (`call_id`,`referral_id`) VALUES ('$id','$referral_id')";
f_mysql_query($insert_sql);
// echo "$this_referral_identifier->$referral_id ";
}
}
echo 'l';
if($row_count > $row_limit){
break;
}
}
$drop_extra_columns_sql = "
ALTER TABLE $database.$tablename
DROP `date_of_call_start_text`,
DROP `date_of_call_end_text`,
DROP `age`,
DROP `city`,
DROP `county`;
";
f_mysql_query($drop_extra_columns_sql);
echo "\nall done.\n";
function isWeekday($timestamp){
return (date('N', $timestamp < 6));
}
function getStartAndEndDate($week, $year) {
$dto = new DateTime();
$ret['week_start'] = $dto->setISODate($year, $week)->format('Y-m-d');
$ret['week_end'] = $dto->modify('+6 days')->format('Y-m-d');
return $ret;
}