data_generator.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. require_once('../application/utils.inc.php');
  3. require_once('../application/itopwebpage.class.inc.php');
  4. //require_once('../application/menunode.class.inc.php');
  5. require_once('../application/applicationcontext.class.inc.php');
  6. require_once('../business/data.samples.inc.php');
  7. require_once('../core/data.generator.class.inc.php');
  8. require_once('../application/startup.inc.php');
  9. // Display the menu on the left
  10. $oContext = new UserContext();
  11. $oAppContext = new ApplicationContext();
  12. $iActiveNodeId = utils::ReadParam('menu', -1);
  13. $currentOrganization = utils::ReadParam('org_id', 1);
  14. $operation = utils::ReadParam('operation', '');
  15. if (!isset($_SERVER['PHP_AUTH_USER']))
  16. {
  17. header('WWW-Authenticate: Basic realm="iTop"');
  18. header('HTTP/1.0 401 Unauthorized');
  19. echo 'Sorry, this page requires authentication. (No user)';
  20. echo "<pre>\n";
  21. echo "DEBUG: \$_SERVER\n";
  22. print_r($_SERVER);
  23. echo "</pre>\n";
  24. exit;
  25. }
  26. if (!UserRights::Login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
  27. {
  28. header('WWW-Authenticate: Basic realm="iTop"');
  29. header('HTTP/1.0 401 Unauthorized');
  30. echo 'Authentication failed !';
  31. exit;
  32. }
  33. $oPage = new iTopWebPage("iTop Data Generator", $currentOrganization);
  34. $oPage->no_cache();
  35. // From now on the context is limited to the the selected organization ??
  36. // Retrieve the root node for the menu
  37. $oSearchFilter = $oContext->NewFilter("menuNode");
  38. $oSearchFilter->AddCondition('parent_id', 0, '=');
  39. // There may be more criteria added later to have a specific menu based on the user's profile
  40. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  41. while ($oRootMenuNode = $oSet->Fetch())
  42. {
  43. $oRootMenuNode->DisplayMenu($oPage, $oContext, $iActiveNodeId, null, $oAppContext->GetAsHash());
  44. }
  45. /**
  46. * The (ordered) list of classes for which to generate objects
  47. *
  48. * Each class in this list must implement a non-static Generate(cmdbDataGenerator $oGenerator) method
  49. */
  50. //$aClassesToGenerate = array('bizOrganization' /* This one is special and must go first */, 'bizService', 'bizContact', 'bizPC', 'bizNetworkDevice');
  51. $aClassesToGenerate = array('bizOrganization' /* This one is special and must go first */, 'bizLocation', 'bizPC', 'bizNetworkDevice', 'bizPerson', 'bizIncidentTicket', 'bizInfraGroup', 'bizInfraInfra');
  52. /////////////////////////////////////////////////////////////////////////////////////
  53. // Actual actions of the page
  54. /////////////////////////////////////////////////////////////////////////////////////
  55. /**
  56. * Populate an organization with objects of each class listed in the (global) $aClassesToGenerate array
  57. *
  58. * @param web_page $oPage The object used for the HTML output
  59. * @param cmdbGenerator $oGenerator The object used for the generation of the objects
  60. * @param string $sSize An enum specifying (roughly) how many objects of each class to create: one of 'small', 'medium', 'big', 'huge' or 'max'
  61. */
  62. function PopulateOrganization(CMDBChange $oMyChange, web_page $oPage, cmdbDataGenerator $oGenerator, $sSize = 'small')
  63. {
  64. global $aClassesToGenerate;
  65. for ($i=1 /* skip the first one (i=0) which is the org itself */; $i<count($aClassesToGenerate); $i++)
  66. {
  67. switch($sSize)
  68. {
  69. case 'max':
  70. $nbObjects = 50000;
  71. break;
  72. case 'huge':
  73. $nbObjects = rand(1000,50000);
  74. break;
  75. case 'big':
  76. $nbObjects = rand(30,500);
  77. break;
  78. case 'medium':
  79. $nbObjects = rand(5,50);
  80. break;
  81. case 'small':
  82. default:
  83. $nbObjects = rand(2,20);
  84. }
  85. $sClass = $aClassesToGenerate[$i];
  86. for($j=0; $j<$nbObjects; $j++)
  87. {
  88. $oObject = MetaModel::NewObject($sClass);
  89. if (method_exists($oObject, 'Generate'))
  90. {
  91. $oObject->Generate($oGenerator);
  92. // By rom
  93. // $oObject->DBInsert();
  94. $oObject->DBInsertTracked($oMyChange);
  95. //$oObject->DisplayDetails($oPage);
  96. }
  97. }
  98. $oPage->p("$nbObjects $sClass objects created.");
  99. }
  100. }
  101. /**
  102. * Delete an organization and all the instances of 'Object' belonging to this organization
  103. *
  104. * @param web_page $oPage The object used for the HTML output
  105. * @param string $sOrganizationCode The code (pkey) of the organization to delete
  106. */
  107. function DeleteOrganization($oMyChange, $oPage, $sOrganizationCode)
  108. {
  109. $oOrg = MetaModel::GetObject('bizOrganization', $sOrganizationCode);
  110. if ($oOrg == null)
  111. {
  112. $oPage->p("<strong>Organization '$sOrganizationCode' already deleted!!</strong>");
  113. }
  114. else
  115. {
  116. // Delete all the objects linked to this organization
  117. $oFilter = new CMDBSearchFilter('logRealObject');
  118. $oFilter->AddCondition('organization', $sOrganizationCode, '=');
  119. $oSet = new CMDBObjectSet($oFilter);
  120. $countDeleted = $oSet->Count();
  121. MetaModel::BulkDeleteTracked($oMyChange, $oFilter); // Should do a one by one delete to let the objects do their own cleanup !
  122. $oPage->p("<strong>$countDeleted object(s) deleted!!</strong>");
  123. $oOrg->DBDeleteTracked($oMyChange);
  124. $oPage->p("<strong>Ok, organization '$sOrganizationCode' deleted!</strong>");
  125. }
  126. }
  127. /////////////////////////////////////////////////////////////////////////////////////////////////
  128. //
  129. // M a i n P r o g r a m
  130. //
  131. /////////////////////////////////////////////////////////////////////////////////////////////////
  132. $operation = utils::ReadParam('operation', '');
  133. $oPage->add("<div style=\"border:1px solid #ffffff; margin:0.5em;\">\n");
  134. $oPage->add("<div style=\"padding:0.25em;text-align:center\">\n");
  135. switch($operation)
  136. {
  137. case 'specify_generate':
  138. // Display a form to specify what to generate
  139. $oPage->p("<strong>iTop Data Generator</strong>\n");
  140. $oPage->add("<form method=\"post\"\">\n");
  141. $oPage->add("Number of organizations to generate: \n");
  142. $oPage->add("<input name=\"org_count\" size=\"3\" value=\"\"> (max ".count($aCompanies).")&nbsp;\n");
  143. $oPage->add("Size of the organizations\n");
  144. $oPage->add("<select name=\"org_size\"\">\n");
  145. $oPage->add("<option value=\"small\">Small (1 - 20 contacts)</option>\n");
  146. $oPage->add("<option value=\"medium\">Medium (5 - 50 contacts)</option>\n");
  147. $oPage->add("<option value=\"big\">Big (30 - 500 contacts)</option>\n");
  148. $oPage->add("<option value=\"huge\">Huge (1000 - 50000 contacts)</option>\n");
  149. $oPage->add("<option value=\"max\">Max (50000 contacts)</option>\n");
  150. $oPage->add("</select>\n");
  151. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"generate\">\n");
  152. $oPage->add("<input type=\"submit\" value=\" Generate ! \">\n");
  153. $oPage->add("</form>\n");
  154. break;
  155. case 'generate':
  156. // perform the actual generation
  157. $iOrgCount = utils::ReadParam('org_count', 0);
  158. $sOrgSize = utils::ReadParam('org_size', 'small');
  159. // By rom
  160. $oMyChange = MetaModel::NewObject("CMDBChange");
  161. $oMyChange->Set("date", time());
  162. $oMyChange->Set("userinfo", "Made by data generator ($iOrgCount orgs of size '$sOrgSize')");
  163. $oMyChange->DBInsert();
  164. while($iOrgCount > 0)
  165. {
  166. set_time_limit(5*60); // let it run for 5 minutes for each organization
  167. $oGenerator = new cmdbDataGenerator();
  168. // Create the new organization
  169. $oOrg = MetaModel::NewObject('bizOrganization');
  170. $oOrg->Generate($oGenerator);
  171. // By rom
  172. // $oOrg->DBInsert();
  173. $oOrg->DBInsertTracked($oMyChange);
  174. $oGenerator->SetOrganizationId($oOrg->GetKey());
  175. $oPage->p("Organization '".$oOrg->GetKey()."' created\n");
  176. $oOrg->DisplayDetails($oPage);
  177. $oPage->add("<hr />\n");
  178. PopulateOrganization($oMyChange, $oPage, $oGenerator, $sOrgSize);
  179. $oPage->add("<hr />\n");
  180. unset($oGenerator);
  181. $iOrgCount--;
  182. }
  183. break;
  184. case 'specify_update':
  185. // Specify which organization to update
  186. $oPage->add("<form method=\"post\"\">\n");
  187. $oPage->add("Select the organization to update: \n");
  188. $oPage->add("<select name=\"org\"\">\n");
  189. $oSearchFilter = new CMDBSearchFilter("bizOrganization");
  190. $oSet = new CMDBObjectSet($oSearchFilter); // All organizations
  191. while($oOrg = $oSet->Fetch())
  192. {
  193. $oPage->add("<option value=\"".$oOrg->GetKey()."\">".$oOrg->Get('name')."</option>\n");
  194. }
  195. $oPage->add("</select>\n");
  196. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"update\">\n");
  197. $oPage->p("");
  198. $oPage->add("Quantity of of objects to add in each class: \n");
  199. $oPage->add("<select name=\"org_size\"\">\n");
  200. $oPage->add("<option value=\"small\">A few (1 - 20 objects)</option>\n");
  201. $oPage->add("<option value=\"medium\">Some (5 - 50 objects)</option>\n");
  202. $oPage->add("<option value=\"big\">Many (30 - 500 objects)</option>\n");
  203. $oPage->add("<option value=\"huge\">Too many (1000 - 50000 objects)</option>\n");
  204. $oPage->add("<option value=\"max\">Max (50000 objects)</option>\n");
  205. $oPage->add("</select>\n");
  206. $oPage->p("");
  207. $oPage->add("<input type=\"button\" value=\" << Back \" onClick=\"javascript:window.history.back()\">&nbsp;&nbsp;\n");
  208. $oPage->add("<input type=\"submit\" value=\" Update \">\n");
  209. $oPage->add("</form>\n");
  210. break;
  211. case 'update':
  212. // perform the actual update
  213. set_time_limit(5*60); // let it run for 5 minutes
  214. $sOrganizationCode = utils::ReadParam('org', '');
  215. $sOrgSize = utils::ReadParam('org_size', 'small');
  216. if ($sOrganizationCode == '')
  217. {
  218. $oPage->p("<strong>Error: please specify an organization (org).</strong>");
  219. }
  220. else
  221. {
  222. // By rom
  223. $oMyChange = MetaModel::NewObject("CMDBChange");
  224. $oMyChange->Set("date", time());
  225. $oMyChange->Set("userinfo", "Made by data generator (update org '$sOrganizationCode', size '$sOrgSize')");
  226. $oMyChange->DBInsert();
  227. $oPage->p("<strong>Organization '$sOrganizationCode' updated.</strong>");
  228. $oGenerator = new cmdbDataGenerator($sOrganizationCode);
  229. PopulateOrganization($oMyChange, $oPage, $oGenerator, $sOrgSize);
  230. }
  231. break;
  232. case 'specify_delete':
  233. // Select an organization to be deleted
  234. $oPage->add("<form method=\"post\"\">\n");
  235. $oPage->add("Select the organization to delete: \n");
  236. $oPage->add("<select name=\"org\"\">\n");
  237. $oSearchFilter = new CMDBSearchFilter("bizOrganization");
  238. $oSet = new CMDBObjectSet($oSearchFilter); // All organizations
  239. while($oOrg = $oSet->Fetch())
  240. {
  241. $oPage->add("<option value=\"".$oOrg->GetKey()."\">".$oOrg->Get('name')."</option>\n");
  242. }
  243. $oPage->add("</select>\n");
  244. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"confirm_delete\">\n");
  245. $oPage->p("");
  246. $oPage->add("<input type=\"button\" value=\" << Back \" onClick=\"javascript:window.history.back()\">&nbsp;&nbsp;\n");
  247. $oPage->add("<input type=\"submit\" value=\" Delete! \">\n");
  248. $oPage->add("</form>\n");
  249. break;
  250. case 'confirm_delete':
  251. // confirm the dangerous action
  252. $sOrganizationCode = ReadParam('org', '');
  253. $oPage->p("<strong>iTop Data Generator</strong>\n");
  254. $oPage->p("<strong>Warning: you are about to delete the organization '$sOrganizationCode' and all its related objects</strong>\n");
  255. $oPage->add("<form method=\"post\"\">\n");
  256. $oPage->add("<input type=\"hidden\" name=\"org\" value=\"$sOrganizationCode\">\n");
  257. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"delete\">\n");
  258. $oPage->add("<input type=\"button\" value=\" << Back \" onClick=\"javascript:window.history.back()\">&nbsp;&nbsp;\n");
  259. $oPage->add("<input type=\"submit\" value=\" Delete them ! \">\n");
  260. $oPage->add("</form>\n");
  261. break;
  262. case 'delete':
  263. // perform the actual deletion
  264. $sOrganizationCode = ReadParam('org', '');
  265. if ($sOrganizationCode == '')
  266. {
  267. $oPage->p("<strong>Error: please specify an organization (org).</strong>");
  268. }
  269. else
  270. {
  271. // By rom
  272. $oMyChange = MetaModel::NewObject("CMDBChange");
  273. $oMyChange->Set("date", time());
  274. $oMyChange->Set("userinfo", "Made by data generator (delete org '$sOrganizationCode'");
  275. $oMyChange->DBInsert();
  276. $oPage->p("<strong>Deleting '$sOrganizationCode'</strong>\n");
  277. DeleteOrganization($oMyChange, $oPage, $sOrganizationCode);
  278. }
  279. break;
  280. // display the menu of actions
  281. case 'menu':
  282. default:
  283. $oPage->p("<strong>Data Generator Menu</strong>");
  284. $oPage->p("<a href=\"?operation=specify_generate\">Generate one or more organizations</a>");
  285. $oPage->p("<a href=\"?operation=specify_update\">Add more objects into an organization</a>");
  286. $oPage->p("<a href=\"?operation=specify_delete\">Delete an organization</a>");
  287. }
  288. $oPage->add("</div>\n");
  289. $oPage->add("</div>\n");
  290. $oPage->p("<a href=\"?operation=menu\">Return to the data generator menu</a>");
  291. $oPage->output();
  292. ?>