1
1
<?php
2
+
2
3
$ time1 = microtime (true );
3
4
4
5
require_once ('dbconn.php ' );
5
6
$ bulk = new MongoDB \Driver \BulkWrite ();
6
7
7
- // Depending on file, pick the right mapping
8
8
switch ($ _GET ['file ' ]) {
9
9
case "product_onhand.txt " :
10
10
$ mapping = 'mapping_onhand ' ;
14
14
$ mapping = 'mapping_general ' ;
15
15
break ;
16
16
17
+ case "product_avgcost.txt " :
18
+ $ mapping = 'product_avgcost ' ;
19
+ break ;
20
+
17
21
case "product_sales.txt " :
18
- $ mapping = 'mapping_sales ' ;
22
+ $ mapping = 'product_sales ' ;
19
23
break ;
24
+
20
25
}
21
26
22
- // this must be called after the right mapping is selected
23
- insert_into ( $ _GET [ ' file ' ]);
27
+ $ log = insert_into ( $ _GET [ ' file ' ]);
28
+
24
29
25
30
26
- // Mapping for ...
27
31
function mapping_general ($ line ){
28
32
$ map =
29
33
[
@@ -32,14 +36,13 @@ function mapping_general($line){
32
36
'category ' => $ line [3 ],
33
37
'sales_all ' => floatval ($ line [4 ]),
34
38
'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 ], '############ ' ),
37
41
'avgcost ' => floatval ($ line [8 ])
38
42
];
39
43
return $ map ;
40
44
}
41
45
42
- // Mapping for ...
43
46
function mapping_onhand ($ line ){
44
47
$ map =
45
48
[
@@ -58,20 +61,69 @@ function mapping_onhand($line){
58
61
'onhand.BR19 ' => floatval ($ line [13 ]),
59
62
'onhand.BR21 ' => floatval ($ line [14 ]),
60
63
'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 ])
62
114
];
63
115
return $ map ;
64
116
}
65
117
66
118
67
- // Inserting file into databae
68
- function insert_into ($ file ) {
119
+
120
+ function insert_into ($ file = " key_user " ) {
69
121
global $ manager , $ bulk , $ mapping ;
70
122
71
123
$ file = "../../../home/ftpscript/inbox/ " .$ file ;
72
124
if (file_exists ($ file )) {
73
125
74
- // Go through each line of the file, one by one.
126
+ // go through each line of the file
75
127
$ handle = @fopen ($ file , "r " );
76
128
if ($ handle ) {
77
129
while (($ line = fgets ($ handle , 4096 )) !== false ) {
@@ -85,21 +137,59 @@ function insert_into($file) {
85
137
$ writeConcern = new MongoDB \Driver \WriteConcern (MongoDB \Driver \WriteConcern::MAJORITY , 100 );
86
138
$ result = $ manager ->executeBulkWrite ('onlinestore.products3 ' , $ bulk , $ writeConcern );
87
139
88
- // display results on one line
140
+ // display results
89
141
printf ("PHP matched %d, " , $ result ->getMatchedCount ());
90
142
printf ("inserted %d, " , $ result ->getInsertedCount ());
91
143
printf ("updated %d, " , $ result ->getModifiedCount ());
92
144
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
+ //}
93
159
94
160
// delete the file
95
161
if (!unlink ($ file )) echo ("Error deleting $ file <br> \n" );
96
162
97
163
// if file does not exist, say so
98
164
} else {
99
165
echo "The file \"" .$ file ."\" does not exist<br> \n" ;
100
- }
166
+ }
167
+ return $ output ;
101
168
}
102
169
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
+
103
192
// show how much time it took to process
104
193
echo 'PHP script execution time: ' .round ( ( (microtime (true ) - $ time1 )),3 ). ' milliseconds ' ;
194
+
105
195
?>
0 commit comments