Count specific key/value pairs in dynamic json object - json
I have a json object which will contain key-value pairs for clients for example, the clients will vary on a day to day basis, depending on which ones have made a certain transaction on said day of the month. My data will therefore only contain the transactions made by clients. Here is an example of my data object for the first day of the month
$scope.dailybreakdown = [
{'day': '1', 'client':'client1', 'received': '3', 'approved':'0'},
{'day': '1', 'client':'client2', 'received': '8', 'approved':'1'},
{'day': '1', 'client':'client3', 'received': '4', 'approved':'0'},
{'day': '1', 'client':'client4', 'received': '4', 'approved':'0'},
{'day': '2', 'client':'client1', 'received': '3', 'approved':'1'},
{'day': '2', 'client':'client4', 'received': '12', 'approved':'5'},
{'day': '3', 'client':'client1', 'received': '10', 'approved':'2'},
{'day': '3', 'client':'client3', 'received': '5', 'approved':'1'},
{'day': '3', 'client':'client8', 'received': '1', 'approved':'0'},
{'day': '3', 'client':'client9', 'received': '2', 'approved':'2'},
]
The data for the second day may contain more or less clients and they could be different, since clients who have not made any transactions will not be present in the object whatsoever. This object would obviously contain data for every day of the month and not just for the first three day as in the sample i added above.
I am using this data to display it graphically in a table and on flot charts on my view. This is all well and good, but where I am looking for some assistance is to how to sort this data and count it.
For instance my table needs to display the total number of transacations for each day. This means I need to count all the Day 1 kay/value pairs and create totals for each field. (total received, approved & recalculate a percentage)
Since this json object could be gigantic, depending on the number of clients thats transacted each day, should I create a new json array in my controller to hold this daily data or is there a way I can filter through it and count/recalculate each day as needed?
My table row looks as follows: (which would also need to change to accomodate the data filtering done in the controller for the client totals)
<tr ng-repeat="daily in dailystats" ng-click="setSelected($index);" ng-class="{'selected':$index == selectedRow}">
<td>{{daily.day}}</td>
<td>{{daily.received}}</td>
<td>{{daily.approved}}</td>
<td>{{daily.percentage}}</td>
</tr>
Any help would be greatly appreciated since I have not been working long with json data objects and I have not been successful in finding any resources or examples to help me with this task.
To anyone willing to help, you have my gratitude in advance.
Below is a way of going through each row in your json table and using a for loop to find all the days equal to 1, 2, 3...etc then finding the "received" total and adding them all together and pushing that total number to a new array then using ng-repeat displaying that total in a table. I hope this helps you along the right track...
Index.html
<table>
<thead>
<th>Day</th>
<th>Recieved</th>
<th>Approved</th>
<th>Percentage</th>
</thead>
<tbody>
<tr ng-repeat="daily in dailybreakdown" ng-click="setSelected($index);" ng-class="{'selected':$index == selectedRow}">
<td>{{daily.day}}</td>
<td>{{daily.received}}</td>
<td>{{daily.approved}}</td>
<td>{{daily.percentage}}</td>
</tr>
</tbody>
</table>
<table>
<thead>
<th>Day</th>
<th>Recieved</th>
</thead>
<tbody>
<tr ng-repeat="day in days" ng-click="setSelected($index);" ng-class="{'selected':$index == selectedRow}">
<td>Day {{$index + 1}}</td>
<td>{{day}}</td>
</tr>
</tbody>
</table>
app.js
$scope.days = [];
$scope.dailybreakdown = [
{'day': '1', 'client':'client1', 'received': '3', 'approved':'0'},
{'day': '1', 'client':'client2', 'received': '8', 'approved':'1'},
{'day': '1', 'client':'client3', 'received': '4', 'approved':'0'},
{'day': '1', 'client':'client4', 'received': '4', 'approved':'0'},
{'day': '2', 'client':'client1', 'received': '3', 'approved':'1'},
{'day': '2', 'client':'client4', 'received': '12', 'approved':'5'},
{'day': '3', 'client':'client1', 'received': '10', 'approved':'2'},
{'day': '3', 'client':'client3', 'received': '5', 'approved':'1'},
{'day': '3', 'client':'client8', 'received': '1', 'approved':'0'},
{'day': '3', 'client':'client9', 'received': '2', 'approved':'2'},
]
for(var x = 1; x <= 31; x++){
var count = 0;
angular.forEach($scope.dailybreakdown, function(value, key) {
if(value.day == x){
count += parseInt(value.received,10);
}
});
$scope.days.push(count);
console.log($scope.days);
}
http://plnkr.co/edit/TK9QPwkcDWSJ1NjbH95a?p=preview
Related
Flask render select based on a list of tuples
I have a data that looks like this that I send from flask render_template a list of tuples called product_list [(22, '1', 'false'), (94, '2', 'true'), (95, '3', 'false'), (100, '4', 'false'), (101, '5', 'false'), (102, '6', 'false'), (103, '7', 'false'), (104, '8', 'false'), (105, '9', 'false'), (106, '10', 'false'), (120, '11', 'false'), (121, '12', 'false')] Essentially first item in tuple is "product_id", second item is "product_version", third item is "is_product_active" I am trying to render these tuples in a select tag on my html. The list is ordered in a way such that first item in the list is suppose to be selected in the dropdown so I tried this <select size="2" data-width="fit" style="line-height:2px"> <option value="{{product_list[0][0]}}" selected >Version: {{product_list[0][1]}}</option> {% for product in product_list|slice:"1:" %} <option value="{{product[0]}}">Version {{product[1]}}</option> {% endfor %} </select> This doesn't work, flask jinja throws error saying jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'string' Mostly coming from product_list|slice:"1:" I essentially set the first item of the array as selected, then I am attempting to skip the first item in my array and simply add other items in the dropdown. What is the cleaner way of doing this such that it also helps in readability of the html code.
You can use bracket notation. {% for product in product_list[1:] %}
List of tuples with strings not all arguments converted during string formatting
I am trying to insert hundreds of rows into a MySQL db at once. There are two types of records, unanswered calls and answered calls. I am putting all records into a list of tuples, and each record is it's own tuple, so that I can use the executemany function. I am getting a TypeError: not all arguments converted during string formatting, and I don't understand why. answered = [] unanswered = [] insertQuery = """ INSERT INTO cdr (recno, relcause, starttime, answertime, endtime, releasecausetext, releasecausecode, 1streleasedialog, origtrunk, callingnumber, orighost, callednumber, desthost, origcallid, origremotepayloadip, origremotepayloadport, origlocalpayloadip, origlocalpayloadport, termtrunk, termsourcenumber, termsourcehost, termdestnumber, termdesthostname, termcallid, termremotepayloadip, termremotepayloadport, termlocalpayloadip, termlocalpayloadport, duration, postdialdelay, ringtime, durationms, routetableused, origtidalias, termtidalias, termpddms, reasoncause, mappedcausecode, mappedreasoncause, reasoncausetext, origmos, termmos) VALUES ('%s'); """ for y in cdrList: #Check to make sure record does not exist sqlQuery = "select * from cdr where recno = %d and origcallid = %s;" % (int(y[0]), y[13]) if cursor.execute(sqlQuery): print("Record exists") else: if y[7]=='NA': unanswered.append((y[0], y[5],extractSqlDate(y[6]), 'null', extractSqlDate(y[8]), y[10], y[11], y[12], y[13], y[15], y[16], y[17], y[18], y[19], y[20], y[21], y[22], y[23], y[32], y[34], y[35], y[36], y[37], y[38], y[39], y[40], y[41], y[42], y[53], y[54], y[55], y[56], y[60], y[66], y[67], y[71], y[78], y[79], y[80], y[81], y[85], y[88])) else: answered.append((y[0], y[5],extractSqlDate(y[6]), extractSqlDate(y[7]), extractSqlDate(y[8]), y[10], y[11], y[12], y[13], y[15], y[16], y[17], y[18], y[19], y[20], y[21], y[22], y[23], y[32], y[34], y[35], y[36], y[37], y[38], y[39], y[40], y[41], y[42], y[53], y[54], y[55], y[56], y[60], y[66], y[67], y[71], y[78], y[79], y[80], y[81], y[85], y[88])) try: print(answered) cursor.executemany(insertQuery, answered) cursor.executemany(insertQuery, unanswered) db.commit() print("Record inserted successfully") except MySQLdb.Error as e: print(e) I have confirmed that each element in each tuple in the list is a string: Successfully connected to database /PATH/20190610/20190610-0015-1750147245-1750147250.cdr [('1750147245', '0001', '2019-06-10 00:10:50', '2019-06-10 00:10:59', '2019-06-10 00:11:13', 'Normal BYE', ' 200', 'O', '001102', '+tn', 'ip', '+tn', 'ip', '273418599_83875291#ip', 'ip', '20530', 'ip', '11944', '000020', '+tn', 'ip', 'tn', 'ip', '4121333-0-2851866068#ip', 'ip', '16840', 'ip', '11946', '13', '1', '8', '13450', '50', 'C - Peerless C6933_04 Origin', 'P - Thirdlane 6', '1150', '', '200', '', '', '0', '0')]
I found the problem. The tuple was returning strings, so the insert query was trying to insert values like this: ''value''. I removed the ' around the %s, and, based on #jonrsharpe's comment, added %s for each other value, and it worked.
PDO insert into MySQL Database
I'm trying to insert a parsed table into a mysql database. But the table within the database stays empty. None of the values are inserted. I already checked this PDO with INSERT INTO through prepared statements and tried to apply the answers to my case. But I'm still getting trouble. What am I missing? This is my code so far: // Simple Dom require_once('simple_html_dom.php'); require_once 'config.php'; $host=$config['DB_HOST']; $dbname=$config['DB_DATABASE']; // Get Connection to HTML and DATABASE $html = file_get_html('url'); $pdo = new PDO("mysql:host=$host;dbname=$dbname",$config['DB_USERNAME'],$config['DB_PASSWORD']); // Find TABLE INSIDE HTML $table = $html->find('table', 1); // find CELLS of each ROW, starting at 2nd row foreach($table ->find('tr') as $rowNumber => $row) { if ( $rowNumber < 1 ) continue; $team = "Timberwolves"; $season = "9"; // Get all columns of the table $pos = $row->find('td', 0); $player = $row->find('td', 1)->plaintext; $age = $row->find('td', 2); $twoga = $row->find('td', 3); $twopct = $row->find('td', 4); $fta = $row->find('td', 5); $ftpct = $row->find('td', 6); $threega = $row->find('td', 7); $threepct = $row->find('td', 8); $orb = $row->find('td', 9); $drb = $row->find('td', 10); $ast = $row->find('td', 11); $stl = $row->find('td', 12); $tov = $row->find('td', 13); $blk = $row->find('td', 14); $oo = $row->find('td', 15); $do = $row->find('td', 16); $po = $row->find('td', 17); $to = $row->find('td', 18); $od = $row->find('td', 19); $dd = $row->find('td', 20); $pd = $row->find('td', 21); $td = $row->find('td', 22); // Echo some of the found values to test the code echo "'$season', '$team', '$pos', '$player', '$age', '$twoga', '$twopct','$fta','$ftpct','$threega', '$threepct', '$orb','$drb','$ast','$stl','$tov','$blk','$oo','$do','$po','$to','$od','$dd','$pd','$td')<br>"; // INSERT for each row $statement = $pdo->prepare("INSERT INTO `teams` (season,team,pos,player,age,2ga,2g%,fta,ft%,3ga,3g%,orb,drb,ast,stl,tov,blk,oo,do,po,to,od,dd,pd,td) VALUES (:season,:team,:pos,:player,:age,:twoga,:twopct,:fta,:ftpct,:threega,:threepct,:orb,:drb,:ast,:stl,:tov,:blk,:oo,:do,:po,:to,:od,:dd,:pd,:td)"); $statement->execute(array(":season"=>$season,":team"=>$team,":pos"=>$pos,":player"=>$player,":age"=>$age,":twoga"=>$twoga,":twopct"=>$twopct,":fta"=>$fta,":ftpct"=>$ftpct, ":threega"=>$threega,":threepct"=>$threepct,":orb"=>$orb,":drb"=>$drb,":ast"=>$ast,":stl"=>$stl,":tov"=>$tov,":blk"=>$blk,":oo"=>$oo,":do"=>$do,":po"=>$po,":to"=>$to,":od"=>$od,":dd"=>$dd,":pd"=>$pd,":td"=>$td)); // END OF LOOP } The table-parsing works fine in my eyes. I'll get this as output: '9', 'Timberwolves', 'PG', 'Mike Conley ', '29', '47', '50','51','86','61', '41', '8','28','61','56','58','8','8','8','6','7','6','6','2','7') '9', 'Timberwolves', 'PG', 'Steve Blake ', '35', '15', '46','6','80','47', '34', '5','23','60','31','52','4','7','6','1','4','4','4','1','5') '9', 'Timberwolves', 'SG', 'Isaiah Thomas ', '27', '59', '53','80','91','83', '38', '10','19','56','37','49','4','7','9','8','9','5','4','2','6') '9', 'Timberwolves', 'SF', 'Rondae Hollis-Jefferson ', '22', '48', '46','45','75','12', '22', '34','62','27','64','60','25','3','5','7','3','8','6','7','6') '9', 'Timberwolves', 'SF', 'Tony Snell ', '25', '14', '55','7','81','50', '41', '6','30','13','32','86','6','9','2','3','1','5','4','2','6') '9', 'Timberwolves', 'PF', 'Joel Embiid ', '22', '77', '50','99','78','41', '37', '49','72','27','48','6','98','3','7','9','6','9','3','9','4') '9', 'Timberwolves', 'PF', 'Draymond Green ', '26', '28', '49','27','71','35', '31', '25','63','69','87','53','43','5','7','4','4','9','7','7','8') '9', 'Timberwolves', ' C', 'Marc Gasol ', '32', '64', '48','41','84','35', '39', '15','49','43','37','59','40','7','6','5','4','6','4','5','5') '9', 'Timberwolves', ' C', 'Marreese Speights ', '29', '35', '52','31','88','65', '37', '40','63','15','23','70','29','7','4','7','1','5','1','6','2') '9', 'Timberwolves', 'SF', 'Dante Cunningham ', '29', '19', '58','5','59','36', '39', '21','41','7','33','90','17','8','2','4','1','5','4','4','5') '9', 'Timberwolves', ' C', 'Boban Marjanovic ', '28', '69', '55','53','81','0', '0', '84','74','8','24','82','35','4','5','9','2','9','4','9','4') '9', 'Timberwolves', 'SF', 'Rasual Butler ', '36', '19', '62','10','69','32', '31', '3','32','15','36','91','46','7','3','4','1','8','6','5','6') This is how the DB-table looks like:
How do I iterate a table in Jade assigning each <td> with a unique id?
I am trying to create a table containing 9 rows with 9 columns. I want to create this by iterating through it with the help of Jade. I'm quite new to Jade so I am probably way off here, but this is my code now; var test1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8'] var test2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8'] for (var i = 0; i < test1.length; i++) { tr for (var o = 0; i < test2.length; o++) { td(id='square-'+test1[i]+test2[o]) } } This (obviously?) gives me a syntax error. How do I go about to iterate a table with 9 rows and 9 cols and give them id=square00, square01, square02 etc..?
Something like this should do the trick: - var rows = ['0', '1', '2', '3', '4', '5', '6', '7', '8']; - var columns = ['0', '1', '2', '3', '4', '5', '6', '7', '8']; table tbody each row in rows tr each column in columns td(id="square-" + row + "-" + column) #{row} - #{column} Note that the javascript is prefaced by - , with the exception of the lines that are doing the iterating. That's because each is a reserved keyword that Jade (now Pug) recognizes as meaning you want to iterate. Good luck!
Updating the database from 1.5.1.3 to 1.5.3.1 OpenCart
I'm trying to pass data from my table OpenCart version 1.5.1.3 to version 1.5.3.1. I'll be using a new theme and only a few of the modules used in the previous version, but do not want to lose the data records of customers, sales and products. Bearing this in mind I guess the easiest way would be estrair the table to a SQL script (I used phpmyadmin), but there are some differences between the versions for the database schema and data so I am not able to pass data from a table another. As an example I'll use the table "address" 1.5.1.3 version that has the following structure when exported by phpmyadmin: INSERT INTO `address` (`address_id`, `customer_id`, `firstname`, `lastname`, `company`, `address_1`, `address_2`, `city`, `postcode`, `country_id`, `zone_id`) VALUES (6, 6, 'Fulano', 'Silva', '', 'My Street, 455', 'Neighborhood 1', 'City 1', 'd9c 5t7', 30, 464), (2, 2, 'Cicrano', 'Souza', '', 'My Avenue, 921', 'Neighborhood 2', 'City 2', 'd9c 5t7', 30, 464), (4, 4, 'Beltrano', 'Cabrito', '', 'My Street 2, 191', 'Neighborhood 3', 'City 3', 'd9c 3t7', 30, 464); And the database version 1.5.3.1 follows this model: INSERT INTO `address` (`address_id`, `customer_id`, `firstname`, `lastname`, `apelido`, `company`, `company_id`, `tax_id`, `address_1`, `numero`, `address_2`, `complemento`, `city`, `postcode`, `country_id`, `zone_id`) VALUES (6, 6, 'Fulano', 'Silva', '', '', '', '', 'My Street', '455', 'Neighborhood 1', '', 'City 1', 'd9c 5t7', 30, 464), (2, 2, 'Beltrano', 'Cabrito', '', '', '', '', '', 'My Street 2', '191', 'Neighborhood 3', '', 'City 3', 'd9c 3t7', 30, 464); This is repeated in several tables, now how do I get only the data you want from v1.5.1.3 to v1.5.3.1 using SQL or other simpler if any?
This is why the upgrade script is there. You can find out how to upgrade here