scheduler.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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.ScriptFile.split("/").pop();
  294. $("#scheduleList").append(`<tr>
  295. <td class="collapsing">
  296. <i class="code file outline icon"></i> ${scriptName}
  297. </td>
  298. <td>${task.ScriptFile}</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. $("#scheduleListAllContent").append(`<tr>
  332. <td class="collapsing">
  333. <i class="user icon"></i> ${task.Name} (${task.Creator})
  334. </td>
  335. <td>${task.ScriptFile}</td>
  336. <td>${task.Description}</td>
  337. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  338. <td class="right aligned collapsing">${moment.unix(task.BaseTime).format('LLL')}</td>
  339. </tr>`);
  340. });
  341. }
  342. }
  343. });
  344. }
  345. function parseSecondsToHumanReadableFormat(seconds){
  346. seconds = Number(seconds);
  347. var d = Math.floor(seconds / (3600*24));
  348. var h = Math.floor(seconds % (3600*24) / 3600);
  349. var m = Math.floor(seconds % 3600 / 60);
  350. var s = Math.floor(seconds % 60);
  351. var dDisplay = d > 0 ? d + (d == 1 ? " day " : " days ") : "";
  352. var hDisplay = h > 0 ? h + (h == 1 ? " hour " : " hours ") : "";
  353. var mDisplay = m > 0 ? m + (m == 1 ? " minute " : " minutes ") : "";
  354. var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
  355. return dDisplay + hDisplay + mDisplay + sDisplay;
  356. }
  357. $(".functBtn").on("click", function(data){
  358. $(".functBtn.active").removeClass("active");
  359. $(this).addClass("active");
  360. });
  361. handleWindowResize();
  362. $(window).on("resize", function(){
  363. handleWindowResize();
  364. });
  365. function handleWindowResize(){
  366. $("#main").css({
  367. height: window.innerHeight + "px"
  368. });
  369. }
  370. function removeTask(object){
  371. var taskName = $(object).attr("name");
  372. if (confirm("Removing " + taskName + ". Confirm?")){
  373. $.ajax({
  374. url: "../../system/arsm/aecron/remove",
  375. data: {name: taskName},
  376. success: function(data){
  377. console.log(data);
  378. showRemoveSchedule();
  379. }
  380. })
  381. }
  382. }
  383. function newTaskUI(btn){
  384. $(".view").hide();
  385. $(".functBtn.active").removeClass("active");
  386. $("#nsb").addClass("active");
  387. $("#newTask").show();
  388. }
  389. function scheduleListAll(btn){
  390. $(".view").hide();
  391. $("#scheudleListAll").show();
  392. $(".functBtn.active").removeClass("active");
  393. $("#asb").addClass("active");
  394. initScheduleFullList();
  395. }
  396. function showLogInfo(btn){
  397. $(".view").hide();
  398. $("#listLog").show();
  399. $(".functBtn.active").removeClass("active");
  400. $("#log").addClass("active");
  401. initLogIndex();
  402. }
  403. function scheduleList(btn){
  404. $(".view").hide();
  405. $(".functBtn.active").removeClass("active");
  406. $("#stb").addClass("active");
  407. $("#scheduleView").show();
  408. initScheduleList();
  409. }
  410. function showRemoveSchedule(btn){
  411. $(".view").hide();
  412. $(".functBtn.active").removeClass("active");
  413. $("#rsb").addClass("active");
  414. $("#removeShedule").show();
  415. initRemoveScheduleList();
  416. }
  417. function initLogIndex(){
  418. $("#logIndex").html("");
  419. $.get("../../system/arsm/aecron/listlog", function(data){
  420. if (data.error !== undefined){
  421. alert(data.error);
  422. }else{
  423. $("#logIndex").append(`<option value="">Date (DD-MM-YYYY)</option>`);
  424. data.forEach(logFile => {
  425. var filename = logFile.split("/").pop();
  426. $("#logIndex").append(`<option value="${filename}">${filename}</option>`);
  427. });
  428. }
  429. })
  430. }
  431. function initRemoveScheduleList(){
  432. $.ajax({
  433. url: "../../system/arsm/aecron/list?listall=true",
  434. data: {},
  435. success: function(data){
  436. $("#removeScheduleList").html("");
  437. if (data.length == 0){
  438. $("#removeScheduleList").append(`<tr>
  439. <td colspan="4">
  440. No registered schedule.
  441. </td>
  442. </tr>`);
  443. }else{
  444. data.forEach(task => {
  445. var filter = $("#filter").val().trim();
  446. if (filter != ""){
  447. //Apply filter to the results
  448. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  449. //Matching
  450. }else{
  451. //Not matching
  452. return
  453. }
  454. }
  455. $("#removeScheduleList").append(`<tr>
  456. <td class="collapsing">
  457. <i class="user icon"></i> ${task.Name} (${task.Creator})
  458. </td>
  459. <td>${task.ScriptFile}</td>
  460. <td>${task.Description}</td>
  461. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  462. <td class="right aligned collapsing">
  463. <button class="ui negative mini button" name="${task.Name}" onclick="removeTask(this);">Remove</button>
  464. </td>
  465. </tr>`);
  466. });
  467. }
  468. }
  469. });
  470. }
  471. //Handle task creation
  472. function handleTaskSubmition(e){
  473. e.preventDefault();
  474. var taskName = $("#tasknanme").val();
  475. var desc = $("#desc").val();
  476. var scriptPath = $("#scriptpath").val();
  477. var interval = parseFloat($("#intervalvalue").val()) * parseFloat($("#intervalunit").val());
  478. var baseunix = Date.now();
  479. var baseTimeframe = $("#intervalbase").val();
  480. switch (baseTimeframe){
  481. case "now":
  482. //Round off to the nearest minute
  483. baseunix = moment().startOf('minute').unix()
  484. break;
  485. case "hour":
  486. baseunix = moment().startOf('hour').unix()
  487. break;
  488. case "day":
  489. baseunix = moment().startOf('day').unix()
  490. break;
  491. case "month":
  492. baseunix = moment().startOf('month').unix()
  493. break;
  494. case "year":
  495. baseunix = moment().startOf('year').unix()
  496. break;
  497. case "mon":
  498. baseunix = moment().startOf('week').add(1,'days').unix();
  499. break;
  500. case "tue":
  501. baseunix = moment().startOf('week').add(2,'days').unix();
  502. break;
  503. case "wed":
  504. baseunix = moment().startOf('week').add(3,'days').unix();
  505. break;
  506. case "thu":
  507. baseunix = moment().startOf('week').add(4,'days').unix();
  508. break;
  509. case "fri":
  510. baseunix = moment().startOf('week').add(5,'days').unix();
  511. break;
  512. case "sat":
  513. baseunix = moment().startOf('week').add(6,'days').unix();
  514. break;
  515. case "sun":
  516. baseunix = moment().startOf('week').unix();
  517. break;
  518. }
  519. //Create ajax request
  520. $.ajax({
  521. url: "../../system/arsm/aecron/add",
  522. method: "POST",
  523. data: {
  524. "name":taskName,
  525. "interval": interval,
  526. "base": baseunix,
  527. "desc": desc,
  528. "path": scriptPath
  529. },
  530. success: function(data){
  531. if (data.error !== undefined){
  532. alert(data.error);
  533. }else{
  534. //Clear all inputs
  535. $("#tasknanme").val("");
  536. $("#desc").val("");
  537. $("#scriptpath").val("");
  538. //Update the list and show it
  539. scheduleList();
  540. }
  541. }
  542. })
  543. }
  544. function applyFilter(){
  545. initScheduleFullList();
  546. initScheduleList();
  547. initRemoveScheduleList();
  548. }
  549. function loadLogFile(filename){
  550. $.get("../../system/arsm/aecron/listlog?filename=" + filename, function(data){
  551. $("#logPreview").text(data);
  552. })
  553. }
  554. function handleFilterEnterKeydown(event){
  555. if (event.which == 13){
  556. event.preventDefault();
  557. applyFilter();
  558. }
  559. }
  560. function openFileSelector(){
  561. ao_module_openFileSelector(scriptSelected, "user:/", "file", false);
  562. }
  563. function scriptSelected(filedata){
  564. for (var i=0; i < filedata.length; i++){
  565. var filename = filedata[i].filename;
  566. var filepath = filedata[i].filepath;
  567. $("#scriptpath").val(filepath);
  568. //See if the filepath end with valid extension
  569. var ext = filepath.split(".").pop();
  570. if (ext == "js" || ext == "agi" || ext == "sh" || ext == "bat"){
  571. $("#extensionWarning").hide();
  572. }else{
  573. $("#extensionWarning").show();
  574. }
  575. }
  576. }
  577. </script>
  578. </body>
  579. </html>