scheduler.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="mobile-web-app-capable" content="yes">
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  6. <meta charset="UTF-8">
  7. <title>System Task Scheduler</title>
  8. <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
  9. <script src="../../script/jquery.min.js"></script>
  10. <script src="../../script/semantic/semantic.min.js"></script>
  11. <script src="../../script/ao_module.js"></script>
  12. <script src="js/moment.min.js"></script>
  13. <style>
  14. body{
  15. margin-top: 12px;
  16. padding-right: 15px;
  17. }
  18. .hidden{
  19. display:none;
  20. }
  21. .compulsory{
  22. color: red;
  23. font-weight: bold;
  24. }
  25. #main{
  26. overflow-y:auto;
  27. }
  28. #logPreview{
  29. width: 100%;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div>
  35. <div class="ui stackable grid">
  36. <div class="four wide column" >
  37. <div class="ui vertical fluid menu">
  38. <div class="item">
  39. <img class="ui image" src="img/banner.png">
  40. </div>
  41. <a id="stb" class="active teal item functBtn" onclick="scheduleList(this);">
  42. User's Scheduled Tasks
  43. </a>
  44. <a id="asb" class="teal item functBtn" onclick="scheduleListAll(this);">
  45. All Scheduled Tasks
  46. </a>
  47. <a id="nsb" class="item functBtn" onclick="newTaskUI(this);">
  48. <i class="add icon"></i> New Scheduled Task
  49. </a>
  50. <a id="rsb" class="item functBtn" onclick="showRemoveSchedule(this);">
  51. <i class="remove icon"></i> Remove Scheduled Task
  52. </a>
  53. <div class="item">
  54. <div class="ui transparent icon input">
  55. <input type="text" id="filter" placeholder="Filter" onkeydown="handleFilterEnterKeydown(event);">
  56. <i class="search icon"></i>
  57. </div>
  58. </div>
  59. <a class="item" onclick="applyFilter();">
  60. <i class="filter icon"></i> Apply Filter
  61. </a>
  62. <a id="log" class="teal item functBtn" onclick="showLogInfo(this);">
  63. Show Logs
  64. </a>
  65. </div>
  66. </div>
  67. <div id="main" class="twelve wide column">
  68. <div class="ui container">
  69. <br>
  70. <!-- Scheduled Task view-->
  71. <div id="scheduleView" class="view">
  72. <table class="ui celled striped table">
  73. <thead>
  74. <tr>
  75. <th colspan="4">
  76. Scheduled Tasks
  77. </th>
  78. </tr>
  79. <tr>
  80. <th>
  81. Script
  82. </th>
  83. <th>
  84. Location
  85. </th>
  86. <th>
  87. Interval
  88. </th>
  89. <th>
  90. Base Time
  91. </th>
  92. </tr>
  93. </thead>
  94. <tbody id="scheduleList">
  95. </tbody>
  96. </table>
  97. </div>
  98. <!-- All Scheudle List-->
  99. <div id="scheudleListAll" class="view" style="display:none;">
  100. <table class="ui celled striped table">
  101. <thead>
  102. <tr>
  103. <th colspan="5">
  104. System Wide Scheduled Tasks
  105. </th>
  106. </tr>
  107. <tr>
  108. <th>
  109. Task Name (Creator)
  110. </th>
  111. <th>
  112. Script Location
  113. </th>
  114. <th>
  115. Description
  116. </th>
  117. <th>
  118. Interval
  119. </th>
  120. <th>
  121. Base Time
  122. </th>
  123. </tr>
  124. </thead>
  125. <tbody id="scheduleListAllContent">
  126. </tbody>
  127. </table>
  128. </div>
  129. <!-- New Scheduule view-->
  130. <div id="newTask" class="view hidden" >
  131. <div class="ui message">
  132. <div class="header">
  133. New Scheduled Task
  134. </div>
  135. <ul class="list">
  136. <li>You can add an agi script (.agi / .js) from one of your directories or</li>
  137. <li>A shell script (.bat or .sh) if you have admin privileges.</li>
  138. </ul>
  139. </div>
  140. <form class="ui form" onsubmit="handleTaskSubmition(event);">
  141. <div class="field">
  142. <label>Task Name (Max: 32 chars) <span class="compulsory">*</span></label>
  143. <input type="text" id="tasknanme" name="tasknanme" placeholder="New Task" maxlength="16" autocomplete="off">
  144. </div>
  145. <div class="field">
  146. <label>Description</label>
  147. <input type="text" id="desc" name="desc" placeholder="Description goes here" autocomplete="off">
  148. </div>
  149. <div class="field">
  150. <label>Script Path <span class="compulsory">*</span></label>
  151. <div class="ui icon fluid input">
  152. <input id="scriptpath" type="text" placeholder="Virtual path to the script file" autocomplete="off">
  153. <i onclick="openFileSelector();" class="inverted circular search link icon"></i>
  154. </div>
  155. <div id="extensionWarning" style="display:none;" class="ui yellow small message">This extension is not supported by the scheduler. Please continue with your own risk.</div>
  156. </div>
  157. <div class="field">
  158. <label>Execution Interval <span class="compulsory">*</span></label>
  159. <div>
  160. <div class="ui input" style="width: 200px;">
  161. <input id="intervalvalue" type="number" placeholder="1" value="1">
  162. </div>
  163. <div class="ui selection dropdown">
  164. <input type="hidden" id="intervalunit" name="intervalunit" value="60">
  165. <i class="dropdown icon"></i>
  166. <div class="default text">Unit</div>
  167. <div class="menu">
  168. <div class="item" data-value="60">Minutes</div>
  169. <div class="item" data-value="3600">Hours</div>
  170. <div class="item" data-value="86400">Days</div>
  171. <div class="item" data-value="2628333">Months (approximate)</div>
  172. </div>
  173. </div>
  174. <br>
  175. <small>Execute the script above ever x unit. Month (Unit) is approximated as 2628333 seconds</small>
  176. </div>
  177. </div>
  178. <div class="field">
  179. <label>Execution Base <span class="compulsory">*</span></label>
  180. <div class="ui selection dropdown">
  181. <input type="hidden" id="intervalbase" name="intervalbase" value="now">
  182. <i class="dropdown icon"></i>
  183. <div class="default text">Base Timeframe</div>
  184. <div class="menu">
  185. <div class="item" data-value="now">Now</div>
  186. <div class="item" data-value="hour">Start of the Hour</div>
  187. <div class="item" data-value="day">Start of the Day</div>
  188. <div class="item" data-value="month">Start of the Month</div>
  189. <div class="item" data-value="year">Start of the Year</div>
  190. <div class="item" data-value="mon">Monday Mid-night</div>
  191. <div class="item" data-value="tue">Tuesday Mid-night</div>
  192. <div class="item" data-value="wed">Wednesday Mid-night</div>
  193. <div class="item" data-value="thu">Thursday Mid-night</div>
  194. <div class="item" data-value="fri">Friday Mid-night</div>
  195. <div class="item" data-value="sat">Saturday Mid-night</div>
  196. <div class="item" data-value="sun">Sunday Mid-night</div>
  197. </div>
  198. </div>
  199. <small>The base day helps you to offset the interval for weekly / monthly based schedules.</small>
  200. </div>
  201. <button class="ui button" type="submit">Submit</button>
  202. <br> <br>
  203. <small>Fields with <span class="compulsory">*</span> are compulsory</small>
  204. </form>
  205. <div class="ui info message">
  206. <i class="close icon" onclick='$(this).parent().fadeOut();'></i>
  207. <div class="header">
  208. Tips for running shell scripts
  209. </div>
  210. <ul class="list">
  211. <li>The execution root of all bash script and batch script are based on the arozos root.</li>
  212. <li>Remember always use absolute path inside your scripts.</li>
  213. </ul>
  214. </div>
  215. <br><br>
  216. </div>
  217. <!-- Remove Scheduule view-->
  218. <div id="removeShedule" class="view hidden" >
  219. <table class="ui celled striped table">
  220. <thead>
  221. <tr>
  222. <th class="ui red message" colspan="5">
  223. <i class="remove icon"></i> Remove Scheduled Tasks<br>
  224. <small>WARNING! All operations are not reversible.</small>
  225. </th>
  226. </tr>
  227. <tr>
  228. <th>
  229. Task Name (Creator)
  230. </th>
  231. <th>
  232. Script Location
  233. </th>
  234. <th>
  235. Description
  236. </th>
  237. <th>
  238. Interval
  239. </th>
  240. <th>
  241. Action
  242. </th>
  243. </tr>
  244. </thead>
  245. <tbody id="removeScheduleList">
  246. </tbody>
  247. </table>
  248. </div>
  249. <!-- Scheduler Log view-->
  250. <div id="listLog" class="view hidden" >
  251. <p>Select a log file to preview</p>
  252. <select id="logIndex" class="ui search fluid dropdown" onchange="loadLogFile(this.value);">
  253. <option value="">Date (DD-MM-YYYY)</option>
  254. <option value="AL">Alabama</option>
  255. </select>
  256. <div class="ui segment form">
  257. <div class="field">
  258. <textarea id="logPreview" rows="10"></textarea>
  259. </div>
  260. </div>
  261. </div>
  262. </div>
  263. </div>
  264. </div>
  265. </div>
  266. <script>
  267. $(".ui.dropdown").dropdown();
  268. initScheduleList();
  269. function initScheduleList(){
  270. $.ajax({
  271. url: "../../system/arsm/aecron/list",
  272. data: {},
  273. success: function(data){
  274. $("#scheduleList").html("");
  275. if (data.length == 0){
  276. $("#scheduleList").append(`<tr>
  277. <td colspan="4">
  278. No registered schedule.
  279. </td>
  280. </tr>`);
  281. }else{
  282. data.forEach(task => {
  283. var filter = $("#filter").val().trim();
  284. if (filter != ""){
  285. //Apply filter to the results
  286. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  287. //Matching
  288. }else{
  289. //Not matching
  290. return
  291. }
  292. }
  293. var scriptName = task.ScriptVpath.split("/").pop();
  294. $("#scheduleList").append(`<tr>
  295. <td class="collapsing">
  296. <i class="code file outline icon"></i> ${scriptName}
  297. </td>
  298. <td>${task.ScriptVpath}</td>
  299. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  300. <td class="right aligned collapsing">${moment.unix(task.BaseTime).format('LLL')}</td>
  301. </tr>`);
  302. });
  303. }
  304. }
  305. });
  306. }
  307. function initScheduleFullList(){
  308. $.ajax({
  309. url: "../../system/arsm/aecron/list?listall=true",
  310. data: {},
  311. success: function(data){
  312. $("#scheduleListAllContent").html("");
  313. if (data.length == 0){
  314. $("#scheduleListAllContent").append(`<tr>
  315. <td colspan="4">
  316. No registered schedule.
  317. </td>
  318. </tr>`);
  319. }else{
  320. data.forEach(task => {
  321. var filter = $("#filter").val().trim();
  322. if (filter != ""){
  323. //Apply filter to the results
  324. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  325. //Matching
  326. }else{
  327. //Not matching
  328. return
  329. }
  330. }
  331. var scriptLocation = task.ScriptVpath;
  332. $("#scheduleListAllContent").append(`<tr>
  333. <td class="collapsing">
  334. <i class="user icon"></i> ${task.Name} (${task.Creator})
  335. </td>
  336. <td>${scriptLocation}</td>
  337. <td>${task.Description}</td>
  338. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  339. <td class="right aligned collapsing">${moment.unix(task.BaseTime).format('LLL')}</td>
  340. </tr>`);
  341. });
  342. }
  343. }
  344. });
  345. }
  346. function parseSecondsToHumanReadableFormat(seconds){
  347. seconds = Number(seconds);
  348. var d = Math.floor(seconds / (3600*24));
  349. var h = Math.floor(seconds % (3600*24) / 3600);
  350. var m = Math.floor(seconds % 3600 / 60);
  351. var s = Math.floor(seconds % 60);
  352. var dDisplay = d > 0 ? d + (d == 1 ? " day " : " days ") : "";
  353. var hDisplay = h > 0 ? h + (h == 1 ? " hour " : " hours ") : "";
  354. var mDisplay = m > 0 ? m + (m == 1 ? " minute " : " minutes ") : "";
  355. var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
  356. return dDisplay + hDisplay + mDisplay + sDisplay;
  357. }
  358. $(".functBtn").on("click", function(data){
  359. $(".functBtn.active").removeClass("active");
  360. $(this).addClass("active");
  361. });
  362. handleWindowResize();
  363. $(window).on("resize", function(){
  364. handleWindowResize();
  365. });
  366. function handleWindowResize(){
  367. $("#main").css({
  368. height: window.innerHeight + "px"
  369. });
  370. }
  371. function removeTask(object){
  372. var taskName = $(object).attr("name");
  373. if (confirm("Removing " + taskName + ". Confirm?")){
  374. $.ajax({
  375. url: "../../system/arsm/aecron/remove",
  376. data: {name: taskName},
  377. success: function(data){
  378. console.log(data);
  379. showRemoveSchedule();
  380. }
  381. })
  382. }
  383. }
  384. function newTaskUI(btn){
  385. $(".view").hide();
  386. $(".functBtn.active").removeClass("active");
  387. $("#nsb").addClass("active");
  388. $("#newTask").show();
  389. }
  390. function scheduleListAll(btn){
  391. $(".view").hide();
  392. $("#scheudleListAll").show();
  393. $(".functBtn.active").removeClass("active");
  394. $("#asb").addClass("active");
  395. initScheduleFullList();
  396. }
  397. function showLogInfo(btn){
  398. $(".view").hide();
  399. $("#listLog").show();
  400. $(".functBtn.active").removeClass("active");
  401. $("#log").addClass("active");
  402. initLogIndex();
  403. }
  404. function scheduleList(btn){
  405. $(".view").hide();
  406. $(".functBtn.active").removeClass("active");
  407. $("#stb").addClass("active");
  408. $("#scheduleView").show();
  409. initScheduleList();
  410. }
  411. function showRemoveSchedule(btn){
  412. $(".view").hide();
  413. $(".functBtn.active").removeClass("active");
  414. $("#rsb").addClass("active");
  415. $("#removeShedule").show();
  416. initRemoveScheduleList();
  417. }
  418. function initLogIndex(){
  419. $("#logIndex").html("");
  420. $.get("../../system/arsm/aecron/listlog", function(data){
  421. if (data.error !== undefined){
  422. alert(data.error);
  423. }else{
  424. $("#logIndex").append(`<option value="">Date (DD-MM-YYYY)</option>`);
  425. data.forEach(logFile => {
  426. var filename = logFile.split("/").pop();
  427. $("#logIndex").append(`<option value="${filename}">${filename}</option>`);
  428. });
  429. }
  430. })
  431. }
  432. function initRemoveScheduleList(){
  433. $.ajax({
  434. url: "../../system/arsm/aecron/list?listall=true",
  435. data: {},
  436. success: function(data){
  437. $("#removeScheduleList").html("");
  438. if (data.length == 0){
  439. $("#removeScheduleList").append(`<tr>
  440. <td colspan="4">
  441. No registered schedule.
  442. </td>
  443. </tr>`);
  444. }else{
  445. data.forEach(task => {
  446. var filter = $("#filter").val().trim();
  447. if (filter != ""){
  448. //Apply filter to the results
  449. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  450. //Matching
  451. }else{
  452. //Not matching
  453. return
  454. }
  455. }
  456. var scriptLocation = task.ScriptVpath;
  457. var removebutton = `<button class="ui negative mini button" name="${task.Name}" onclick="removeTask(this);">Remove</button>`;
  458. $("#removeScheduleList").append(`<tr>
  459. <td class="collapsing">
  460. <i class="user icon"></i> ${task.Name} (${task.Creator})
  461. </td>
  462. <td>${scriptLocation}</td>
  463. <td>${task.Description}</td>
  464. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  465. <td class="right aligned collapsing">
  466. ${removebutton}
  467. </td>
  468. </tr>`);
  469. });
  470. }
  471. }
  472. });
  473. }
  474. //Handle task creation
  475. function handleTaskSubmition(e){
  476. e.preventDefault();
  477. var taskName = $("#tasknanme").val();
  478. var desc = $("#desc").val();
  479. var scriptPath = $("#scriptpath").val();
  480. var interval = parseFloat($("#intervalvalue").val()) * parseFloat($("#intervalunit").val());
  481. var baseunix = Date.now();
  482. var baseTimeframe = $("#intervalbase").val();
  483. switch (baseTimeframe){
  484. case "now":
  485. //Round off to the nearest minute
  486. baseunix = moment().startOf('minute').unix()
  487. break;
  488. case "hour":
  489. baseunix = moment().startOf('hour').unix()
  490. break;
  491. case "day":
  492. baseunix = moment().startOf('day').unix()
  493. break;
  494. case "month":
  495. baseunix = moment().startOf('month').unix()
  496. break;
  497. case "year":
  498. baseunix = moment().startOf('year').unix()
  499. break;
  500. case "mon":
  501. baseunix = moment().startOf('week').add(1,'days').unix();
  502. break;
  503. case "tue":
  504. baseunix = moment().startOf('week').add(2,'days').unix();
  505. break;
  506. case "wed":
  507. baseunix = moment().startOf('week').add(3,'days').unix();
  508. break;
  509. case "thu":
  510. baseunix = moment().startOf('week').add(4,'days').unix();
  511. break;
  512. case "fri":
  513. baseunix = moment().startOf('week').add(5,'days').unix();
  514. break;
  515. case "sat":
  516. baseunix = moment().startOf('week').add(6,'days').unix();
  517. break;
  518. case "sun":
  519. baseunix = moment().startOf('week').unix();
  520. break;
  521. }
  522. //Create ajax request
  523. $.ajax({
  524. url: "../../system/arsm/aecron/add",
  525. method: "POST",
  526. data: {
  527. "name":taskName,
  528. "interval": interval,
  529. "base": baseunix,
  530. "desc": desc,
  531. "path": scriptPath
  532. },
  533. success: function(data){
  534. if (data.error !== undefined){
  535. alert(data.error);
  536. }else{
  537. //Clear all inputs
  538. $("#tasknanme").val("");
  539. $("#desc").val("");
  540. $("#scriptpath").val("");
  541. //Update the list and show it
  542. scheduleList();
  543. }
  544. }
  545. })
  546. }
  547. function applyFilter(){
  548. initScheduleFullList();
  549. initScheduleList();
  550. initRemoveScheduleList();
  551. }
  552. function loadLogFile(filename){
  553. $.get("../../system/arsm/aecron/listlog?filename=" + filename, function(data){
  554. $("#logPreview").text(data);
  555. })
  556. }
  557. function handleFilterEnterKeydown(event){
  558. if (event.which == 13){
  559. event.preventDefault();
  560. applyFilter();
  561. }
  562. }
  563. function openFileSelector(){
  564. ao_module_openFileSelector(scriptSelected, "user:/", "file", false);
  565. }
  566. function scriptSelected(filedata){
  567. for (var i=0; i < filedata.length; i++){
  568. var filename = filedata[i].filename;
  569. var filepath = filedata[i].filepath;
  570. $("#scriptpath").val(filepath);
  571. //See if the filepath end with valid extension
  572. var ext = filepath.split(".").pop();
  573. if (ext == "js" || ext == "agi" || ext == "sh" || ext == "bat"){
  574. $("#extensionWarning").hide();
  575. }else{
  576. $("#extensionWarning").show();
  577. }
  578. }
  579. }
  580. </script>
  581. </body>
  582. </html>