PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.2.0/www/server/grids/aws_ec2_pricehistory_list.php

http://scalr.googlecode.com/
PHP | 141 lines | 104 code | 28 blank | 9 comment | 18 complexity | dfb1b05483f3833cc10031d462ffba49 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. $response = array();
  3. // AJAX_REQUEST;
  4. $context = 6;
  5. try
  6. {
  7. $enable_json = true;
  8. include("../../src/prepend.inc.php");
  9. $req_show_all = true;
  10. if (isset($req_show_all))
  11. {
  12. if ($req_show_all == 'true')
  13. $_SESSION['sg_show_all'] = true;
  14. else
  15. $_SESSION['sg_show_all'] = false;
  16. }
  17. $Client = Client::Load($_SESSION['uid']);
  18. $AmazonEC2Client = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($_SESSION['aws_region']));
  19. $AmazonEC2Client->SetAuthKeys($Client->AWSPrivateKey, $Client->AWSCertificate);
  20. if($req_query)
  21. {
  22. //---------------------------- filter for SpotPriceHistory:
  23. $filter = array();
  24. $filter = explode(" ", $req_query);
  25. $instanceTypes = explode(" ","m1.small m1.large m1.xlarge c1.medium c1.xlarge m2.2xlarge m2.4xlarge");
  26. $i = 0;
  27. $instanceTypeSet = array();
  28. $requestDesription = array();
  29. foreach($filter as $item) // search instance type conjunctions
  30. {
  31. for($i = 0; $i<count($instanceTypes);$i++)
  32. {
  33. // serach by instance type
  34. if(strcasecmp($instanceTypes[$i],$item) == 0)
  35. {
  36. $instanceTypeSet[] = $instanceTypes[$i];
  37. continue;
  38. }
  39. }
  40. // serach by description
  41. $win = stristr($item,"windows");
  42. $linux = stristr($item,"linux");
  43. $unix = stristr($item,"unix");
  44. if($win)
  45. {
  46. $requestDesription[] = "Windows";
  47. continue;
  48. }
  49. elseif(($linux) || ($unix))
  50. {
  51. $requestDesription[] = "Linux/UNIX";
  52. continue;
  53. }
  54. }
  55. //---------------------------- end filter ------------------------
  56. $describeSpotPriceType = new DescribeSpotPriceHistoryType(null,$instanceTypeSet,$requestDesription,null);
  57. $aws_response = $AmazonEC2Client->DescribeSpotPriceHistory($describeSpotPriceType);
  58. }
  59. else
  60. $aws_response = $AmazonEC2Client->DescribeSpotPriceHistory(); // show all prices
  61. // Rows
  62. $rows = (array)$aws_response->spotPriceHistorySet;
  63. if ($rows["item"] instanceof stdClass)
  64. $rows["item"] = array($rows["item"]); // convert along subnet record to array
  65. $rowz = array();
  66. foreach ($rows['item'] as $row)
  67. $rowz[]=(array)$row;
  68. if ($req_sort == 'price' )
  69. {
  70. $nrowz = array();
  71. foreach ($rowz as $row)
  72. $nrowz[(string)$row['spotPrice']] = $row;
  73. ksort($nrowz);
  74. if ($req_dir == 'ASC')
  75. $rowz = array_reverse($nrowz);
  76. else
  77. $rowz = $nrowz;
  78. }
  79. if ($req_sort == 'timestamp')
  80. {
  81. $nrowz = array();
  82. foreach ($rowz as $row)
  83. $nrowz[(string)$row['timestamp']] = $row;
  84. ksort($nrowz);
  85. if ($req_dir == 'ASC')
  86. $rowz = array_reverse($nrowz);
  87. else
  88. $rowz = $nrowz;
  89. }
  90. // diplay list limits
  91. $start = $req_start ? (int) $req_start : 0;
  92. $limit = $req_limit ? (int) $req_limit : 20;
  93. $response['total'] = count($rowz);
  94. $rowz = (count($rowz) > $limit) ? array_slice($rowz, $start, $limit) : $rowz;
  95. // descending sorting of requested result
  96. $response["data"] = array();
  97. // Rows. Create final rows array for script
  98. foreach ($rowz as $row)
  99. {
  100. $response["data"][] = array(
  101. "type" => (string)$row['instanceType'], // have to call only like "id" for correct script work in template
  102. "description" => (string)$row['productDescription'],
  103. "price" => (string)$row['spotPrice'],
  104. "timestamp" => (string)$row['timestamp']
  105. );
  106. }
  107. }
  108. catch(Exception $e)
  109. {
  110. $response = array("error" => $e->getMessage(), "data" => array());
  111. }
  112. print json_encode($response);
  113. ?>