1
1
<?php
2
- $ time1 = microtime (true );
2
+ ob_implicit_flush (true );
3
+ ob_end_flush ();
4
+
5
+ echo ('PHP: ' );
6
+
7
+ $ time1 = '' ;
8
+ $ time2 = microtime (true );
3
9
ini_set ('max_execution_time ' , 300 ); //300 seconds = 5 minutes
4
10
5
11
require_once ('dbconn.php ' );
6
- $ bulk = new MongoDB \ Driver \ BulkWrite ();
12
+
7
13
8
14
$ mapping = 'mass_upload ' ;
9
15
10
16
// this must be called after the $mapping variable is declared
11
- get_the_job_done ();
17
+ //$output = get_the_job_done();
18
+ //insert_log($output);
12
19
20
+ $ output = populate_array ();
21
+ insert_log ($ output );
13
22
14
23
// Mapping for ...
15
24
function mass_upload ($ doc ){
@@ -30,11 +39,15 @@ function mass_upload($doc){
30
39
'onhand_value.BR19 ' => (double )(number_format ($ doc ->avg_cost ->BR19 * $ doc ->onhand ->BR19 ,2 ,'. ' ,'' )),
31
40
'onhand_value.BR21 ' => (double )(number_format ($ doc ->avg_cost ->BR21 * $ doc ->onhand ->BR21 ,2 ,'. ' ,'' )),
32
41
'onhand_value.BR22 ' => (double )(number_format ($ doc ->avg_cost ->BR22 * $ doc ->onhand ->BR22 ,2 ,'. ' ,'' )),
33
- 'onhand_value.BRPT ' => (double )(number_format ($ doc ->avg_cost ->BRPT * $ doc ->onhand ->BRPT ,2 ,'. ' ,'' ))
42
+ 'onhand_value.BRPT ' => (double )(number_format ($ doc ->avg_cost ->BRPT * $ doc ->onhand ->BRPT ,2 ,'. ' ,'' )),
43
+ 'onhand_value.BR60 ' => (double )(number_format ($ doc ->avg_cost ->BR60 * $ doc ->onhand ->BR60 ,2 ,'. ' ,'' ))
34
44
];
35
45
return $ map ;
36
46
}
47
+
37
48
/*
49
+ To run this query in MongoDB Shell, use this command:
50
+
38
51
db.products3.find({pline: {$ne:"NONSTOCK-"}, pline: {$exists:1}, onhand: {$exists:1}, avg_cost: {$exists:1} } ).forEach(function(doc) {
39
52
db.products3.update({ _id: doc._id }, { $set: {
40
53
"onhand_value.BR01": parseFloat((doc.onhand.BR01 * doc.avg_cost.BR01).toFixed(2)),
@@ -52,55 +65,191 @@ function mass_upload($doc){
52
65
"onhand_value.BR19": parseFloat((doc.onhand.BR19 * doc.avg_cost.BR19).toFixed(2)),
53
66
"onhand_value.BR21": parseFloat((doc.onhand.BR21 * doc.avg_cost.BR21).toFixed(2)),
54
67
"onhand_value.BR22": parseFloat((doc.onhand.BR22 * doc.avg_cost.BR22).toFixed(2)),
55
- "onhand_value.BRPT": parseFloat((doc.onhand.BRPT * doc.avg_cost.BRPT).toFixed(2))
56
-
68
+ "onhand_value.BRPT": parseFloat((doc.onhand.BRPT * doc.avg_cost.BRPT).toFixed(2)),
69
+ "onhand_value.BR60": parseFloat((doc.onhand.BR60 * doc.avg_cost.BR60).toFixed(2))
57
70
} } )
58
71
})
59
72
*/
60
73
61
74
function get_the_job_done (){
62
- global $ manager , $ bulk , $ mapping ;
75
+ global $ manager , $ mapping , $ time1 ;
76
+ $ time1 = microtime (true );
77
+ $ bulk = new MongoDB \Driver \BulkWrite ();
63
78
$ query = new MongoDB \Driver \Query (
64
- [
65
- //'pline ' => ['$ne ' => 'NONSTOCK'],
79
+ [ // query (empty: select all)
80
+ //'sales_all ' => ['$gt ' => 100], // ADDED FOR TESTINTG
66
81
'onhand ' => ['$exists ' => true ],
67
82
'avg_cost ' => ['$exists ' => true ]
68
- ], // query (empty: select all)
69
- [
83
+ ],
84
+ [ // options
70
85
'projection ' => ['avg_cost ' => 1 , 'onhand ' => 1 ]
71
86
//'limit' => 10
72
87
//'sort' => [ 'onhand.BR01' => -1], 'skip' => 0, 'limit' => 1
73
- ] // options
88
+ ]
74
89
);
75
90
76
91
// Execute query and obtain cursor:
77
92
$ cursor = $ manager ->executeQuery ('onlinestore.products3 ' , $ query );
78
-
79
- // Display info in a table
80
- //echo '<table class="fixed_headers seven_columns">';
81
- //echo "<thead><tr><th>Product ID & Buyline</th><th>Product Description</th><th>$ / Qty: </th><th>onhand</th><th>avg cost</th></tr></thead>\n";
93
+ $ j = 0 ; $ k = 0 ;
82
94
foreach ($ cursor as $ doc ) {
83
-
84
- //echo '<tr><td><a href="product/'.$doc->_id .'/">'.$doc->_id . '</a> ('.$doc->pline. ')</td>'.
85
- //'<td>--</td><td>$' . ''.
86
- //"</td><td>".$doc->onhand->BR01."</td><td>". $doc->avg_cost->BR01 . "</td></tr>\n";
87
- //echo $doc->_id . "<br />\n";
88
-
89
95
$ bulk ->update (['_id ' => intval ($ doc ->_id )], ['$set ' => $ mapping ($ doc )], ['multi ' => false , 'upsert ' => true ]);
96
+ $ j ++;
97
+ if ($ j >10000 ){
98
+ $ k ++;
99
+ echo ', ' .$ k ;
100
+ $ j =0 ;
101
+ }
90
102
}
91
- //echo "</table>";
92
103
93
104
$ writeConcern = new MongoDB \Driver \WriteConcern (MongoDB \Driver \WriteConcern::MAJORITY , 100 );
94
105
$ result = $ manager ->executeBulkWrite ('onlinestore.products3 ' , $ bulk , $ writeConcern );
95
106
96
107
// display results on one line
97
- printf ("PHP matched %d, " , $ result ->getMatchedCount ());
108
+ printf ("\nmatched %d, " , $ result ->getMatchedCount ());
109
+ printf ("inserted %d, " , $ result ->getInsertedCount ());
110
+ printf ("updated %d, " , $ result ->getModifiedCount ());
111
+ printf ("upserted %d. \n" , $ result ->getUpsertedCount ());
112
+
113
+ $ output = (object ) [
114
+ 'matched ' => $ result ->getMatchedCount (),
115
+ 'inserted ' => $ result ->getInsertedCount (),
116
+ 'modified ' => $ result ->getModifiedCount (),
117
+ 'upserted ' => $ result ->getUpsertedCount ()
118
+ ];
119
+
120
+ return $ output ;
121
+ }
122
+
123
+ // insert_log updates log collection
124
+ function insert_log ($ log ){
125
+ global $ manager , $ time1 ;
126
+ $ bulk = new MongoDB \Driver \BulkWrite ();
127
+
128
+ // creating document to insert into log collection
129
+ $ _GET ['file ' ] = 'Mass Load ' ;
130
+ $ doc = [
131
+ 'date ' => new MongoDB \BSON \UTCDateTime ,
132
+ 'execution_time ' => round ( ( (microtime (true ) - $ time1 )),3 ),
133
+ 'file_name ' => $ _GET ['file ' ],
134
+ 'matched ' => $ log ->matched ,
135
+ 'inserted ' => $ log ->inserted ,
136
+ 'updated ' => $ log ->modified ,
137
+ 'upserted ' => $ log ->upserted ,
138
+ 'ip ' => $ _SERVER ['REMOTE_ADDR ' ],
139
+ 'browser ' => $ _SERVER ['HTTP_USER_AGENT ' ],
140
+ 'page ' => $ _SERVER ['REQUEST_URI ' ]
141
+ ];
142
+
143
+ // inserting document into log collection
144
+ $ bulk = new MongoDB \Driver \BulkWrite ();
145
+ $ bulk ->insert ($ doc );
146
+ $ writeConcern = new MongoDB \Driver \WriteConcern (MongoDB \Driver \WriteConcern::MAJORITY , 100 );
147
+ $ result = $ manager ->executeBulkWrite ('onlinestore.insert_log ' , $ bulk , $ writeConcern );
148
+ }
149
+
150
+
151
+
152
+ // Mapping for populate_array()
153
+ function elements ($ line ){
154
+ $ map = [
155
+ "branch " => ['$each ' => $ line ]
156
+ ];
157
+ return $ map ;
158
+ }
159
+ /* To run this query in MongoDB Shell, use this command:
160
+ It works with testing mechanisms.
161
+ function isNumber(n) {
162
+ return !isNaN(parseFloat(n)) && isFinite(n);
163
+ }
164
+ db.people.find( { "data" : { $type : 3 } } ).forEach( function (x) {
165
+ var out =[];
166
+ for( var i in x.data ) {
167
+ if (x.data.hasOwnProperty(i)){
168
+ if (isNumber(i)){
169
+ out[i] = x.data[i];
170
+ }else{
171
+ out.push(x.data[i]);
172
+ }
173
+ }
174
+ }
175
+ x.data = out
176
+ db.people.save(x);
177
+ });
178
+ */
179
+
180
+ // This function get data from each product as object and saves as an array.
181
+ function populate_array (){
182
+ global $ manager , $ mapping , $ time1 ;
183
+ $ time1 = microtime (true );
184
+ $ bulk = new MongoDB \Driver \BulkWrite ();
185
+ $ query = new MongoDB \Driver \Query (
186
+ [
187
+ //'_id' => 1,
188
+ 'sales ' => ['$exists ' => 1 ]
189
+ ],
190
+ [ // options
191
+ 'projection ' => ['sales ' => 1 ]
192
+ //'limit' => 10
193
+ //'sort' => [ 'onhand.BR01' => -1], 'skip' => 0, 'limit' => 1
194
+ ]
195
+ );
196
+
197
+ // Execute query and obtain cursor:
198
+ $ cursor = $ manager ->executeQuery ('onlinestore.products3 ' , $ query );
199
+ $ j = 0 ; $ k = 0 ;
200
+ echo ' Array: ' ;
201
+ foreach ($ cursor as $ doc ) {
202
+ $ arr = toArray ($ doc ->sales );
203
+ //$arr = $arr['sales'];
204
+ //print_r($arr);
205
+ $ bulk ->update (['_id ' => intval ($ doc ->_id )], ['$addToSet ' => elements ($ arr )], ['multi ' => false , 'upsert ' => false ]);
206
+ //echo 'out: ' . $doc->_id ."<br>\n";
207
+ $ j ++;
208
+ if ($ j >10000 ){
209
+ $ k ++;
210
+ echo ', ' .$ k ;
211
+ $ j =0 ;
212
+ }
213
+ }
214
+
215
+ $ writeConcern = new MongoDB \Driver \WriteConcern (MongoDB \Driver \WriteConcern::MAJORITY , 100 );
216
+ $ result = $ manager ->executeBulkWrite ('onlinestore.products3 ' , $ bulk , $ writeConcern );
217
+
218
+ // display results on one line
219
+ printf ("\nmatched %d, " , $ result ->getMatchedCount ());
98
220
printf ("inserted %d, " , $ result ->getInsertedCount ());
99
221
printf ("updated %d, " , $ result ->getModifiedCount ());
100
222
printf ("upserted %d. \n" , $ result ->getUpsertedCount ());
223
+
224
+ $ output = (object ) [
225
+ 'matched ' => $ result ->getMatchedCount (),
226
+ 'inserted ' => $ result ->getInsertedCount (),
227
+ 'modified ' => $ result ->getModifiedCount (),
228
+ 'upserted ' => $ result ->getUpsertedCount ()
229
+ ];
230
+
231
+ return $ output ;
232
+ }
233
+
234
+ // toArray is special helper function to convert object to an array.
235
+ function toArray ($ obj ){
236
+ if (is_object ($ obj )) $ obj = (array )$ obj ;
237
+ if (is_array ($ obj )) {
238
+ $ new = array ();
239
+ $ i = 0 ;
240
+ foreach ($ obj as $ key => $ val ) {
241
+ $ subArray ['id ' ] = $ key ;
242
+ $ subArray ['sales ' ] = toArray ($ val );
243
+ $ new [$ i ] = $ subArray ;
244
+ $ i ++;
245
+ }
246
+ } else {
247
+ $ new = $ obj ;
248
+ }
249
+ return $ new ;
101
250
}
102
251
103
252
104
253
// show how much time it took to process
105
- echo 'PHP script execution time: ' .round ( ( (microtime (true ) - $ time1 )),3 ). ' milliseconds ' ;
254
+ echo 'PHP time: ' .round ( ( (microtime (true ) - $ time2 )),3 ). ' seconds ' ;
106
255
?>
0 commit comments