-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.php
195 lines (163 loc) · 6 KB
/
insert.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
<?php
$time1 = microtime(true);
require_once('dbconn.php');
$bulk = new MongoDB\Driver\BulkWrite();
switch ($_GET['file']) {
case "product_onhand.txt":
$mapping = 'mapping_onhand';
break;
case "product_general.txt":
$mapping = 'mapping_general';
break;
case "product_avgcost.txt":
$mapping = 'product_avgcost';
break;
case "product_sales.txt":
$mapping = 'product_sales';
break;
}
$log = insert_into($_GET['file']);
function mapping_general($line){
$map =
[
'pline' => $line[1],
'bline' => $line[2],
'category' => $line[3],
'sales_all' => floatval($line[4]),
'price' => floatval($line[5]),
'product_name' => my_encrypt($line[6], '############'),
'keyword' => my_encrypt($line[7], '############'),
'avgcost' => floatval($line[8])
];
return $map;
}
function mapping_onhand($line){
$map =
[
'onhand.BR01' => floatval($line[1]),
'onhand.BR02' => floatval($line[2]),
'onhand.BR03' => floatval($line[3]),
'onhand.BR04' => floatval($line[4]),
'onhand.BR05' => floatval($line[5]),
'onhand.BR07' => floatval($line[6]),
'onhand.BR08' => floatval($line[7]),
'onhand.BR12' => floatval($line[8]),
'onhand.BR13' => floatval($line[9]),
'onhand.BR14' => floatval($line[10]),
'onhand.BR16' => floatval($line[11]),
'onhand.BR17' => floatval($line[12]),
'onhand.BR19' => floatval($line[13]),
'onhand.BR21' => floatval($line[14]),
'onhand.BR22' => floatval($line[15]),
'onhand.BRPT' => floatval($line[16]),
'onhand.BR60' => floatval($line[17])
];
return $map;
}
function product_avgcost($line){
$map =
[
'avg_cost.BR01' => floatval($line[1]),
'avg_cost.BR02' => floatval($line[2]),
'avg_cost.BR03' => floatval($line[3]),
'avg_cost.BR04' => floatval($line[4]),
'avg_cost.BR05' => floatval($line[5]),
'avg_cost.BR07' => floatval($line[6]),
'avg_cost.BR08' => floatval($line[7]),
'avg_cost.BR12' => floatval($line[8]),
'avg_cost.BR13' => floatval($line[9]),
'avg_cost.BR14' => floatval($line[10]),
'avg_cost.BR16' => floatval($line[11]),
'avg_cost.BR17' => floatval($line[12]),
'avg_cost.BR19' => floatval($line[13]),
'avg_cost.BR21' => floatval($line[14]),
'avg_cost.BR22' => floatval($line[15]),
'avg_cost.BRPT' => floatval($line[16]),
'avg_cost.BR60' => floatval($line[17])
];
return $map;
}
function product_sales($line){
$map =
[
'sales.BR01' => floatval($line[1]),
'sales.BR02' => floatval($line[2]),
'sales.BR03' => floatval($line[3]),
'sales.BR04' => floatval($line[4]),
'sales.BR05' => floatval($line[5]),
'sales.BR07' => floatval($line[6]),
'sales.BR08' => floatval($line[7]),
'sales.BR12' => floatval($line[8]),
'sales.BR13' => floatval($line[9]),
'sales.BR14' => floatval($line[10]),
'sales.BR16' => floatval($line[11]),
'sales.BR17' => floatval($line[12]),
'sales.BR19' => floatval($line[13]),
'sales.BR21' => floatval($line[14]),
'sales.BR22' => floatval($line[15]),
'sales.BRPT' => floatval($line[16]),
'sales.BR60' => floatval($line[17])
];
return $map;
}
function insert_into($file = "key_user") {
global $manager, $bulk, $mapping;
$file = "../../../home/ftpscript/inbox/".$file;
if (file_exists($file)) {
// go through each line of the file
$handle = @fopen($file, "r");
if ($handle) {
while (($line = fgets($handle, 4096)) !== false) {
$line = explode("\t", $line);
$bulk->update(['_id' => intval($line[0])], ['$set' => $mapping($line)], ['multi' => false, 'upsert' => true]);
}
fclose($handle);
}
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$result = $manager->executeBulkWrite('onlinestore.products3', $bulk, $writeConcern);
// display results
printf("PHP matched %d, ", $result->getMatchedCount());
printf("inserted %d, ", $result->getInsertedCount());
printf("updated %d, ", $result->getModifiedCount());
printf("upserted %d.\n", $result->getUpsertedCount());
// this is "output" from this function
$output = (object) [
'matched' => $result->getMatchedCount(),
'inserted' => $result->getInsertedCount(),
'modified' => $result->getModifiedCount(),
'upserted' => $result->getUpsertedCount()
];
//printf("Deleted %d document(s)\n", $result->getDeletedCount());
//foreach ($result->getUpsertedIds() as $index => $id) {
// printf('upsertedId[%d]: ', $index);
// var_dump($id);
//}
// delete the file
if (!unlink($file)) echo ("Error deleting $file <br>\n");
// if file does not exist, say so
} else {
echo "The file \"".$file."\" does not exist<br>\n";
}
return $output;
}
// creating document to insert into log collection
$doc = [
'date' => new MongoDB\BSON\UTCDateTime,
'php_seconds' => round( ( (microtime(true) - $time1)),3 ),
'file' => $_GET['file'],
'matched' => $log->matched,
'inserted' => $log->inserted,
'updated' => $log->modified,
'upserted' => $log->upserted,
'ip' => $_SERVER['REMOTE_ADDR'],
'browser' => $_SERVER['HTTP_USER_AGENT'],
'page' => $_SERVER['REQUEST_URI']
];
// inserting document into log collection
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert($doc);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
$result = $manager->executeBulkWrite('onlinestore.insert_log', $bulk, $writeConcern);
// show how much time it took to process
echo 'PHP script execution time: ' .round( ( (microtime(true) - $time1)),3 ). ' milliseconds';
?>