scheduler.html 28 KB

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