Skip to content

Commit a6b2758

Browse files
committed
Added Log
1 parent cce4811 commit a6b2758

File tree

1 file changed

+104
-14
lines changed

1 file changed

+104
-14
lines changed

insert.php

+104-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2+
23
$time1 = microtime(true);
34

45
require_once('dbconn.php');
56
$bulk = new MongoDB\Driver\BulkWrite();
67

7-
// Depending on file, pick the right mapping
88
switch ($_GET['file']) {
99
case "product_onhand.txt":
1010
$mapping = 'mapping_onhand';
@@ -14,16 +14,20 @@
1414
$mapping = 'mapping_general';
1515
break;
1616

17+
case "product_avgcost.txt":
18+
$mapping = 'product_avgcost';
19+
break;
20+
1721
case "product_sales.txt":
18-
$mapping = 'mapping_sales';
22+
$mapping = 'product_sales';
1923
break;
24+
2025
}
2126

22-
// this must be called after the right mapping is selected
23-
insert_into($_GET['file']);
27+
$log = insert_into($_GET['file']);
28+
2429

2530

26-
// Mapping for ...
2731
function mapping_general($line){
2832
$map =
2933
[
@@ -32,14 +36,13 @@ function mapping_general($line){
3236
'category' => $line[3],
3337
'sales_all' => floatval($line[4]),
3438
'price' => floatval($line[5]),
35-
'product_name' => my_encrypt($line[6], '#############'),
36-
'keyword' => my_encrypt($line[7], '#############'),
39+
'product_name' => my_encrypt($line[6], '############'),
40+
'keyword' => my_encrypt($line[7], '############'),
3741
'avgcost' => floatval($line[8])
3842
];
3943
return $map;
4044
}
4145

42-
// Mapping for ...
4346
function mapping_onhand($line){
4447
$map =
4548
[
@@ -58,20 +61,69 @@ function mapping_onhand($line){
5861
'onhand.BR19' => floatval($line[13]),
5962
'onhand.BR21' => floatval($line[14]),
6063
'onhand.BR22' => floatval($line[15]),
61-
'onhand.BRPT' => floatval($line[16])
64+
'onhand.BRPT' => floatval($line[16]),
65+
'onhand.BR60' => floatval($line[17])
66+
];
67+
return $map;
68+
}
69+
70+
function product_avgcost($line){
71+
$map =
72+
[
73+
'avg_cost.BR01' => floatval($line[1]),
74+
'avg_cost.BR02' => floatval($line[2]),
75+
'avg_cost.BR03' => floatval($line[3]),
76+
'avg_cost.BR04' => floatval($line[4]),
77+
'avg_cost.BR05' => floatval($line[5]),
78+
'avg_cost.BR07' => floatval($line[6]),
79+
'avg_cost.BR08' => floatval($line[7]),
80+
'avg_cost.BR12' => floatval($line[8]),
81+
'avg_cost.BR13' => floatval($line[9]),
82+
'avg_cost.BR14' => floatval($line[10]),
83+
'avg_cost.BR16' => floatval($line[11]),
84+
'avg_cost.BR17' => floatval($line[12]),
85+
'avg_cost.BR19' => floatval($line[13]),
86+
'avg_cost.BR21' => floatval($line[14]),
87+
'avg_cost.BR22' => floatval($line[15]),
88+
'avg_cost.BRPT' => floatval($line[16]),
89+
'avg_cost.BR60' => floatval($line[17])
90+
];
91+
return $map;
92+
}
93+
94+
function product_sales($line){
95+
$map =
96+
[
97+
'sales.BR01' => floatval($line[1]),
98+
'sales.BR02' => floatval($line[2]),
99+
'sales.BR03' => floatval($line[3]),
100+
'sales.BR04' => floatval($line[4]),
101+
'sales.BR05' => floatval($line[5]),
102+
'sales.BR07' => floatval($line[6]),
103+
'sales.BR08' => floatval($line[7]),
104+
'sales.BR12' => floatval($line[8]),
105+
'sales.BR13' => floatval($line[9]),
106+
'sales.BR14' => floatval($line[10]),
107+
'sales.BR16' => floatval($line[11]),
108+
'sales.BR17' => floatval($line[12]),
109+
'sales.BR19' => floatval($line[13]),
110+
'sales.BR21' => floatval($line[14]),
111+
'sales.BR22' => floatval($line[15]),
112+
'sales.BRPT' => floatval($line[16]),
113+
'sales.BR60' => floatval($line[17])
62114
];
63115
return $map;
64116
}
65117

66118

67-
// Inserting file into databae
68-
function insert_into($file) {
119+
120+
function insert_into($file = "key_user") {
69121
global $manager, $bulk, $mapping;
70122

71123
$file = "../../../home/ftpscript/inbox/".$file;
72124
if (file_exists($file)) {
73125

74-
// Go through each line of the file, one by one.
126+
// go through each line of the file
75127
$handle = @fopen($file, "r");
76128
if ($handle) {
77129
while (($line = fgets($handle, 4096)) !== false) {
@@ -85,21 +137,59 @@ function insert_into($file) {
85137
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
86138
$result = $manager->executeBulkWrite('onlinestore.products3', $bulk, $writeConcern);
87139

88-
// display results on one line
140+
// display results
89141
printf("PHP matched %d, ", $result->getMatchedCount());
90142
printf("inserted %d, ", $result->getInsertedCount());
91143
printf("updated %d, ", $result->getModifiedCount());
92144
printf("upserted %d.\n", $result->getUpsertedCount());
145+
146+
// this is "output" from this function
147+
$output = (object) [
148+
'matched' => $result->getMatchedCount(),
149+
'inserted' => $result->getInsertedCount(),
150+
'modified' => $result->getModifiedCount(),
151+
'upserted' => $result->getUpsertedCount()
152+
];
153+
154+
//printf("Deleted %d document(s)\n", $result->getDeletedCount());
155+
//foreach ($result->getUpsertedIds() as $index => $id) {
156+
// printf('upsertedId[%d]: ', $index);
157+
// var_dump($id);
158+
//}
93159

94160
// delete the file
95161
if (!unlink($file)) echo ("Error deleting $file <br>\n");
96162

97163
// if file does not exist, say so
98164
} else {
99165
echo "The file \"".$file."\" does not exist<br>\n";
100-
}
166+
}
167+
return $output;
101168
}
102169

170+
171+
// creating document to insert into log collection
172+
$doc = [
173+
'date' => new MongoDB\BSON\UTCDateTime,
174+
'execution_time' => round( ( (microtime(true) - $time1)),3 ),
175+
'file_name' => $_GET['file'],
176+
'matched' => $log->matched,
177+
'inserted' => $log->inserted,
178+
'updated' => $log->modified,
179+
'upserted' => $log->upserted,
180+
'ip' => $_SERVER['REMOTE_ADDR'],
181+
'browser' => $_SERVER['HTTP_USER_AGENT'],
182+
'page' => $_SERVER['REQUEST_URI']
183+
];
184+
185+
// inserting document into log collection
186+
$bulk = new MongoDB\Driver\BulkWrite();
187+
$bulk->insert($doc);
188+
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
189+
$result = $manager->executeBulkWrite('onlinestore.insert_log', $bulk, $writeConcern);
190+
191+
103192
// show how much time it took to process
104193
echo 'PHP script execution time: ' .round( ( (microtime(true) - $time1)),3 ). ' milliseconds';
194+
105195
?>

0 commit comments

Comments
 (0)