OpenSpending csv updates
OpenSpending csv updates

{ {
"dataset":{ "dataset": {
"name":"australian_federal_government_contract_spending", "languages": [
"label":"Australian Federal Government Contract Spending", "en"
"description":"Spending by Australian Federal Government agencies on goods and services from 2007 onwards.", ],
"currency":"AUD", "currency": "AUD",
"unique_keys":[ "name": "australian_federal_government_contract_spending",
"id" "territories": [],
], "default_time": null,
"temporal_granularity":"year" "description": "Spending by Australian Federal Government agencies on goods and services from 2007 onwards.",
}, "schema_version": "2011-12-07",
"mapping":{ "label": "Australian Federal Government Contract Spending"
"category": {  
"fields": [ {  
"column": "category",  
"datatype": "string",  
"name": "label"  
} ],  
   
"type": "classifier",  
"description": "",  
"taxonomy": "unspsc",  
"label": "Contract Goods/Services Category"  
}, },
"from":{ "mapping": {
"fields":[ "category": {
{ "description": "Contract Goods/Services Category",
"column":"agencyABN", "datatype": "string",
"datatype":"string", "label": "category",
"name":"id" "column": "cat1",
}, "type": "attribute",
{ "dimension": "category"
"column":"agencyName", },
"datatype":"string", "sourceurl": {
"name":"label" "description": "Source Document URL",
} "datatype": "string",
], "label": "sourceurl",
"type":"entity", "column": "sourceurl",
"description":"", "type": "attribute",
"label":"Government Agency Australian Business Number" "dimension": "sourceurl"
}, },
"description":{ "from": {
"column":"description", "attributes": {
"datatype":"string", "name": {
"type":"value", "column": "agencyABN",
"description":"Contract as described by agency", "datatype": "id",
"label":"Contract Description" "type": "id",
}, "description": null
"to":{ },
"fields":[ "label": {
{ "column": "agencyName",
"column":"supplierName", "datatype": "string",
"datatype":"string", "type": "attribute",
"name":"label" "description": null
}, }
{ },
"column":"supplierID", "label": "Government Agency",
"datatype":"string", "type": "compound",
"name":"id" "dimension": "from",
} "description": "Government Agency"
], },
"type":"entity", "description": {
"description":"Government Supplier", "description": null,
"label":"Supplier Name" "datatype": "string",
}, "label": "description",
"currency":{ "column": "description",
"default_value":"AUD", "type": "attribute",
"description":"All entries in AUD, foreign transactions are converted at the time of their payment", "dimension": "description"
"column":"", },
"label":"", "publishdate": {
"datatype":"currency", "description": null,
"type":"value" "format": null,
}, "datatype": "date",
"amount":{ "label": "publishDate",
"column":"value", "column": "publishDate",
"datatype":"float", "type": "date",
"type":"value", "dimension": "publishdate"
"description":"The total value of the contract including all variations/amendments/extensions", },
"label":"Contract Value" "to": {
}, "attributes": {
"time":{ "name": {
"column":"contractStart", "column": "supplierid",
"datatype":"date", "datatype": "id",
"type":"value", "type": "id"
"description":"Start of the contract period (goods or services being received)", },
"label":"Contract Start Date" "label": {
}, "column": "supplierName",
"id":{ "datatype": "string",
"column": "CNID", "type": "attribute"
"datatype": "string", }
"name": "label", },
"type": "value", "label": "Supplier Name",
"description": "", "type": "compound",
"label": "Contract Notice ID" "dimension": "to",
} "description": "Government Supplier"
}, },
  "amount": {
"views": [ "description": "The total value of the contract including all variations/amendments/extensions",
  "datatype": "float",
  "label": "value",
  "column": "value",
  "type": "measure",
  "dimension": "amount"
  },
  "cnid": {
  "description": "Contract Notice ID",
  "datatype": "string",
  "label": "CNID",
  "column": "CNID",
  "key": true,
  "type": "attribute",
  "dimension": "cnid"
  },
  "contractend": {
  "description": "End of the contract period (goods or services being received)",
  "format": null,
  "datatype": "date",
  "label": "contractEnd",
  "column": "contractEnd",
  "type": "date",
  "dimension": "contractend"
  },
  "time": {
  "description": "Start of the contract period (goods or services being received)",
  "format": null,
  "datatype": "date",
  "label": "contractStart",
  "column": "contractStart",
  "type": "date",
  "dimension": "time"
  }
  },
  "views": [
{ {
"name": "default", "name": "default",
"entity": "dataset", "label": "Breakdown by Category",
"label": "Default breakdown by Category", "cuts": {},
"dimension": "dataset", "drilldown": "category",
"breakdown": "category", "entity": "dataset",
"filters" : {"name": "australian_federal_government_contract_spending" } "dimension": "dataset"
}, },
{ {
"name":"default", "name": "by_supplier",
"entity": "classifier", "label": "Breakdown by Supplier",
"label":"Breakdown of Spending Areas by Supplier Company", "cuts": {},
"dimension": "category", "drilldown": "to",
"breakdown": "to", "entity": "dataset",
"filters": {"taxonomy": "unspsc"} "dimension": "dataset"
}, },
{ {
"name":"default", "name": "by_agency",
"entity": "classifier", "label": "Breakdown by Agency",
"label":"Breakdown of Spending Areas by Government Agency", "cuts": {},
"dimension": "category", "drilldown": "from",
"breakdown": "from", "entity": "dataset",
"filters": {"taxonomy": "unspsc"} "dimension": "dataset"
} },
] {
  "name": "default",
  "label": "Agency spending breakdown by supplier",
  "cuts": {},
  "drilldown": "to",
  "entity": "dimension",
  "dimension": "from"
  },
  {
  "name": "default",
  "label": "Supplier contracts breakdown by procuring agency",
  "cuts": {},
  "drilldown": "from",
  "entity": "dimension",
  "dimension": "to"
  }
  ]
} }
   
   
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
<title>Minimal BubbleTree Demo</title> <title>Minimal BubbleTree Demo</title>
<script type="text/javascript" src="lib/bubbletree/lib/jquery-1.5.2.min.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/jquery.history.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/jquery.history.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/raphael.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/raphael.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/vis4.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/vis4.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/Tween.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/Tween.js"></script>
<script type="text/javascript" src="lib/bubbletree/build/bubbletree.js"></script> <script type="text/javascript" src="lib/bubbletree/build/bubbletree.js"></script>
<link rel="stylesheet" type="text/css" href="lib/bubbletree/build/bubbletree.css" /> <link rel="stylesheet" type="text/css" href="lib/bubbletree/build/bubbletree.css" />
<script type="text/javascript" src="lib/bubbletree/styles/cofog.js"></script> <script type="text/javascript" src="lib/bubbletree/styles/cofog.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
<?php <?php
include_once ("lib/common.inc.php"); include_once ("lib/common.inc.php");
   
include("lib/Color.php"); include("lib/Color.php");
$color = new Lux_Color(); $color = new Lux_Color();
   
   
$unspscresult= $conn->prepare('select * from "UNSPSCcategories";'); $unspscresult= $conn->prepare('select * from "UNSPSCcategories";');
$unspscresult->execute(); $unspscresult->execute();
foreach ($unspscresult->fetchAll() as $row) { foreach ($unspscresult->fetchAll() as $row) {
$unspsc[$row['UNSPSC']] = $row['Title']; $unspsc[$row['UNSPSC']] = $row['Title'];
   
} }
$total = 0; $total = 0;
   
$catsresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 2 ) as cat $catsresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 3 ) as cat
FROM contractnotice WHERE "categoryUNSPSC" IS NOT NULL group by "categoryUNSPSC";;'); FROM contractnotice WHERE "categoryUNSPSC" IS NOT NULL group by "categoryUNSPSC";;');
$catsresult->execute(); $catsresult->execute();
foreach ($catsresult->fetchAll() as $row) { foreach ($catsresult->fetchAll() as $row) {
$cats[] = $row['cat']; $cats[] = $row['cat'];
} }
$cattwosresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 3 ) as cat $cattwosresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 5 ) as cat
FROM contractnotice FROM contractnotice
WHERE "categoryUNSPSC" IS NOT NULL WHERE "categoryUNSPSC" IS NOT NULL
GROUP BY "categoryUNSPSC" order by cat ;'); GROUP BY "categoryUNSPSC" order by cat ;');
$cattwosresult->execute(); $cattwosresult->execute();
foreach ($cattwosresult->fetchAll() as $tworow) { foreach ($cattwosresult->fetchAll() as $tworow) {
$cattwos[substr($tworow['cat'],0,1)][] = $tworow['cat']; $cattwos[substr($tworow['cat'],0,2)][] = $tworow['cat'];
} }
   
$catthreesresult = $conn->prepare('SELECT substr( "categoryUNSPSC"::text, 0, 4 ) as cat , SUM( "value" ) as value $catthreesresult = $conn->prepare('SELECT substr( "categoryUNSPSC"::text, 0, 7 ) as cat , SUM( "value" ) as value
FROM contractnotice FROM contractnotice
WHERE "childCN" is null and "categoryUNSPSC" IS NOT NULL WHERE "childCN" is null and "categoryUNSPSC" IS NOT NULL
GROUP BY cat order by cat ;'); GROUP BY cat order by cat ;');
$catthreesresult->execute(); $catthreesresult->execute();
foreach ($catthreesresult->fetchAll() as $threerow) { foreach ($catthreesresult->fetchAll() as $threerow) {
$catthrees[substr($threerow['cat'],0,2)][] = $threerow; $catthrees[substr($threerow['cat'],0,4)][] = $threerow;
} }
$nodes = Array(); $nodes = Array();
foreach ($cats as $catNum) { foreach ($cats as $catNum) {
$catColor = $color->hsv2hex(Array($catNum/10, .7, abs(($catNum*(1/10))-.5) + .5)); $catColor = $color->hsv2hex(Array($catNum/10, .7, abs(($catNum*(1/10))-.5) + .5));
$catName = $unspsc[$catNum . "0000000"] . $catNum; $catName = substr($unspsc[$catNum . "000000"],0,18) . $catNum;
$subnodes = Array(); $subnodes = Array();
$catValue = 0; $catValue = 0;
foreach ($cattwos[$catNum] as $twoCatNum) { foreach ($cattwos[$catNum] as $twoCatNum) {
$subcatName = $unspsc[$twoCatNum. "000000"] .$twoCatNum; $subcatName = $unspsc[$twoCatNum. "0000"] .$twoCatNum;
$subsubnodes = Array(); $subsubnodes = Array();
$subCatValue = 0; $subCatValue = 1;
$subCatColor = $color->hsv2hex(Array($catNum/10, rand(1,10)/10, abs(($catNum*(1/10))-.5) + .5)); $subCatColor = $color->hsv2hex(Array($catNum/10, rand(1,10)/10, abs(($catNum*(1/10))-.5) + .5));
foreach ($catthrees[$twoCatNum] as $threerow) { foreach ($catthrees[$twoCatNum] as $threerow) {
$subsubcatName = $unspsc[$threerow['cat'] . "00000"] . $threerow['cat']; $subsubcatName = $unspsc[$threerow['cat'] . "00"] . $threerow['cat'];
$subsubnodes[] = Array( $subsubnodes[] = Array(
"label" => $subsubcatName, "label" => $subsubcatName,
"amount" => $threerow['value'], "amount" => $threerow['value'],
//"color" => "#".$subCatColor "color" => "#".$subCatColor
); );
$subCatValue += $threerow['value']; $subCatValue += $threerow['value'];
} }
$subnodes[] = Array( $subnodes[] = Array(
"label" => $subcatName, "label" => $subcatName,
"amount" => $subCatValue, "amount" => $subCatValue,
"color" => "#".$subCatColor, "color" => "#".$subCatColor,
"children" => $subsubnodes "children" => $subsubnodes
); );
$catValue += $subCatValue; $catValue += $subCatValue;
} }
$nodes[] = Array( $nodes[] = Array(
"label" => $catName, "label" => $catName,
"amount" => $catValue, "amount" => $catValue,
"color" => "#".$catColor, "color" => "#".$catColor,
"children" => $subnodes "children" => $subnodes
); );
$total += $catValue; $total += $catValue;
} }
$data = Array( $data = Array(
"label" => "Australian Federal Government Contract Spending", "label" => "Australian Federal Government Contract Spending",
"amount" => $total, "amount" => $total,
"color" => "#000000", "color" => "#000000",
"children" => $nodes "children" => $nodes
); );
echo "var data =eval('('+'" . json_encode($data) . "'+')');"; echo "var data =eval('('+'" . json_encode($data) . "'+')');";
?> ?>
   
new BubbleTree({ new BubbleTree({
data: data, data: data,
container: '.bubbletree' container: '.bubbletree'
}); });
}); });
</script> </script>
</head> </head>
<body> <body>
<div class="bubbletree-wrapper"> <div class="bubbletree-wrapper">
<div class="bubbletree"></div> <div class="bubbletree"></div>
</div> </div>
</body> </body>
</html> </html>
   
</
<?php <?php
   
include_once("./lib/common.inc.php"); include_once("./lib/common.inc.php");
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
// source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951 // source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951
   
$unspsc = Array(); $unspsc = Array();
$unspscresult = $conn->prepare('select * from "UNSPSCcategories" where "UNSPSC"::text like \'%00000\';'); $unspscresult = $conn->prepare('select * from "UNSPSCcategories" where "UNSPSC"::text like \'%00000\';');
$unspscresult->execute(); $unspscresult->execute();
foreach ($unspscresult->fetchAll() as $row) { foreach ($unspscresult->fetchAll() as $row) {
$unspsc[$row['UNSPSC']] = $row['Title']; $unspsc[$row['UNSPSC']] = $row['Title'];
} }
   
$query = $conn->prepare(' $query = $conn->prepare('
SELECT "CNID",contractnotice."agencyName",agency.abn as "agencyABN", SELECT "CNID",contractnotice."agencyName",agency.abn as "agencyABN",
EXTRACT(EPOCH FROM "publishDate") as "publishDate", EXTRACT(EPOCH FROM "publishDate") as "publishDate",
EXTRACT(EPOCH FROM "contractStart") as "contractStart", EXTRACT(EPOCH FROM "contractStart") as "contractStart",
EXTRACT(EPOCH FROM "contractEnd") as "contractEnd", EXTRACT(EPOCH FROM "contractEnd") as "contractEnd",
value,description,"procurementMethod",category,"categoryUNSPSC", value,description,category,
(substr( "categoryUNSPSC"::text, 0, 2 ) || \'0000000\'::text) as "cat1", "supplierName",(case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID,