scheduler.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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>Add an agi script (.agi / .js) from one of your directories by selecting below</li>
  137. </ul>
  138. </div>
  139. <form class="ui form" onsubmit="handleTaskSubmition(event);">
  140. <div class="field">
  141. <label>Task Name (Max: 32 chars) <span class="compulsory">*</span></label>
  142. <input type="text" id="tasknanme" name="tasknanme" placeholder="New Task" maxlength="16" autocomplete="off">
  143. </div>
  144. <div class="field">
  145. <label>Description</label>
  146. <input type="text" id="desc" name="desc" placeholder="Description goes here" autocomplete="off">
  147. </div>
  148. <div class="field">
  149. <label>Script Path <span class="compulsory">*</span></label>
  150. <div class="ui icon fluid input">
  151. <input id="scriptpath" type="text" placeholder="Virtual path to the script file" autocomplete="off">
  152. <i onclick="openFileSelector();" class="inverted circular search link icon"></i>
  153. </div>
  154. <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>
  155. </div>
  156. <div class="field">
  157. <label>Execution Interval <span class="compulsory">*</span></label>
  158. <div>
  159. <div class="ui input" style="width: 200px;">
  160. <input id="intervalvalue" type="number" placeholder="1" value="1">
  161. </div>
  162. <div class="ui selection dropdown">
  163. <input type="hidden" id="intervalunit" name="intervalunit" value="60">
  164. <i class="dropdown icon"></i>
  165. <div class="default text">Unit</div>
  166. <div class="menu">
  167. <div class="item" data-value="60">Minutes</div>
  168. <div class="item" data-value="3600">Hours</div>
  169. <div class="item" data-value="86400">Days</div>
  170. <div class="item" data-value="2628333">Months (approximate)</div>
  171. </div>
  172. </div>
  173. <br>
  174. <small>Execute the script above ever x unit. Month (Unit) is approximated as 2628333 seconds</small>
  175. </div>
  176. </div>
  177. <div class="field">
  178. <label>Execution Base <span class="compulsory">*</span></label>
  179. <div class="ui selection dropdown">
  180. <input type="hidden" id="intervalbase" name="intervalbase" value="now">
  181. <i class="dropdown icon"></i>
  182. <div class="default text">Base Timeframe</div>
  183. <div class="menu">
  184. <div class="item" data-value="now">Now</div>
  185. <div class="item" data-value="hour">Start of the Hour</div>
  186. <div class="item" data-value="day">Start of the Day</div>
  187. <div class="item" data-value="month">Start of the Month</div>
  188. <div class="item" data-value="year">Start of the Year</div>
  189. <div class="item" data-value="mon">Monday Mid-night</div>
  190. <div class="item" data-value="tue">Tuesday Mid-night</div>
  191. <div class="item" data-value="wed">Wednesday Mid-night</div>
  192. <div class="item" data-value="thu">Thursday Mid-night</div>
  193. <div class="item" data-value="fri">Friday Mid-night</div>
  194. <div class="item" data-value="sat">Saturday Mid-night</div>
  195. <div class="item" data-value="sun">Sunday Mid-night</div>
  196. </div>
  197. </div>
  198. <small>The base day helps you to offset the interval for weekly / monthly based schedules.</small>
  199. </div>
  200. <button class="ui button" type="submit">Submit</button>
  201. <br> <br>
  202. <small>Fields with <span class="compulsory">*</span> are compulsory</small>
  203. </form>
  204. <br><br>
  205. </div>
  206. <!-- Remove Scheduule view-->
  207. <div id="removeShedule" class="view hidden" >
  208. <table class="ui celled striped table">
  209. <thead>
  210. <tr>
  211. <th class="ui red message" colspan="5">
  212. <i class="remove icon"></i> Remove Scheduled Tasks<br>
  213. <small>WARNING! All operations are not reversible.</small>
  214. </th>
  215. </tr>
  216. <tr>
  217. <th>
  218. Task Name (Creator)
  219. </th>
  220. <th>
  221. Script Location
  222. </th>
  223. <th>
  224. Description
  225. </th>
  226. <th>
  227. Interval
  228. </th>
  229. <th>
  230. Action
  231. </th>
  232. </tr>
  233. </thead>
  234. <tbody id="removeScheduleList">
  235. </tbody>
  236. </table>
  237. </div>
  238. <!-- Scheduler Log view-->
  239. <div id="listLog" class="view hidden" >
  240. <p>Select a log file to preview</p>
  241. <select id="logIndex" class="ui search fluid dropdown" onchange="loadLogFile(this.value);">
  242. <option value="">Date (DD-MM-YYYY)</option>
  243. <option value="AL">Alabama</option>
  244. </select>
  245. <div class="ui segment form">
  246. <div class="field">
  247. <textarea id="logPreview" rows="10"></textarea>
  248. </div>
  249. </div>
  250. </div>
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. <script>
  256. $(".ui.dropdown").dropdown();
  257. initScheduleList();
  258. function initScheduleList(){
  259. $.ajax({
  260. url: "../../system/arsm/aecron/list",
  261. data: {},
  262. success: function(data){
  263. $("#scheduleList").html("");
  264. if (data.length == 0){
  265. $("#scheduleList").append(`<tr>
  266. <td colspan="4">
  267. No registered schedule.
  268. </td>
  269. </tr>`);
  270. }else{
  271. data.forEach(task => {
  272. var filter = $("#filter").val().trim();
  273. if (filter != ""){
  274. //Apply filter to the results
  275. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  276. //Matching
  277. }else{
  278. //Not matching
  279. return
  280. }
  281. }
  282. var scriptName = task.ScriptVpath.split("/").pop();
  283. $("#scheduleList").append(`<tr>
  284. <td class="collapsing">
  285. <i class="code file outline icon"></i> ${scriptName}
  286. </td>
  287. <td>${task.ScriptVpath}</td>
  288. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  289. <td class="right aligned collapsing">${moment.unix(task.BaseTime).format('LLL')}</td>
  290. </tr>`);
  291. });
  292. }
  293. }
  294. });
  295. }
  296. function initScheduleFullList(){
  297. $.ajax({
  298. url: "../../system/arsm/aecron/list?listall=true",
  299. data: {},
  300. success: function(data){
  301. $("#scheduleListAllContent").html("");
  302. if (data.length == 0){
  303. $("#scheduleListAllContent").append(`<tr>
  304. <td colspan="4">
  305. No registered schedule.
  306. </td>
  307. </tr>`);
  308. }else{
  309. data.forEach(task => {
  310. var filter = $("#filter").val().trim();
  311. if (filter != ""){
  312. //Apply filter to the results
  313. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  314. //Matching
  315. }else{
  316. //Not matching
  317. return
  318. }
  319. }
  320. var scriptLocation = task.ScriptVpath;
  321. $("#scheduleListAllContent").append(`<tr>
  322. <td class="collapsing">
  323. <i class="user icon"></i> ${task.Name} (${task.Creator})
  324. </td>
  325. <td>${scriptLocation}</td>
  326. <td>${task.Description}</td>
  327. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  328. <td class="right aligned collapsing">${moment.unix(task.BaseTime).format('LLL')}</td>
  329. </tr>`);
  330. });
  331. }
  332. }
  333. });
  334. }
  335. function parseSecondsToHumanReadableFormat(seconds){
  336. seconds = Number(seconds);
  337. var d = Math.floor(seconds / (3600*24));
  338. var h = Math.floor(seconds % (3600*24) / 3600);
  339. var m = Math.floor(seconds % 3600 / 60);
  340. var s = Math.floor(seconds % 60);
  341. var dDisplay = d > 0 ? d + (d == 1 ? " day " : " days ") : "";
  342. var hDisplay = h > 0 ? h + (h == 1 ? " hour " : " hours ") : "";
  343. var mDisplay = m > 0 ? m + (m == 1 ? " minute " : " minutes ") : "";
  344. var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
  345. return dDisplay + hDisplay + mDisplay + sDisplay;
  346. }
  347. $(".functBtn").on("click", function(data){
  348. $(".functBtn.active").removeClass("active");
  349. $(this).addClass("active");
  350. });
  351. handleWindowResize();
  352. $(window).on("resize", function(){
  353. handleWindowResize();
  354. });
  355. function handleWindowResize(){
  356. $("#main").css({
  357. height: window.innerHeight + "px"
  358. });
  359. }
  360. function removeTask(object){
  361. var taskName = $(object).attr("name");
  362. if (confirm("Removing " + taskName + ". Confirm?")){
  363. $.ajax({
  364. url: "../../system/arsm/aecron/remove",
  365. data: {name: taskName},
  366. success: function(data){
  367. console.log(data);
  368. showRemoveSchedule();
  369. }
  370. })
  371. }
  372. }
  373. function newTaskUI(btn){
  374. $(".view").hide();
  375. $(".functBtn.active").removeClass("active");
  376. $("#nsb").addClass("active");
  377. $("#newTask").show();
  378. }
  379. function scheduleListAll(btn){
  380. $(".view").hide();
  381. $("#scheudleListAll").show();
  382. $(".functBtn.active").removeClass("active");
  383. $("#asb").addClass("active");
  384. initScheduleFullList();
  385. }
  386. function showLogInfo(btn){
  387. $(".view").hide();
  388. $("#listLog").show();
  389. $(".functBtn.active").removeClass("active");
  390. $("#log").addClass("active");
  391. initLogIndex();
  392. }
  393. function scheduleList(btn){
  394. $(".view").hide();
  395. $(".functBtn.active").removeClass("active");
  396. $("#stb").addClass("active");
  397. $("#scheduleView").show();
  398. initScheduleList();
  399. }
  400. function showRemoveSchedule(btn){
  401. $(".view").hide();
  402. $(".functBtn.active").removeClass("active");
  403. $("#rsb").addClass("active");
  404. $("#removeShedule").show();
  405. initRemoveScheduleList();
  406. }
  407. function initLogIndex(){
  408. $("#logIndex").html("");
  409. $.get("../../system/arsm/aecron/listlog", function(data){
  410. if (data.error !== undefined){
  411. alert(data.error);
  412. }else{
  413. $("#logIndex").append(`<option value="">Date (DD-MM-YYYY)</option>`);
  414. data.forEach(logFile => {
  415. var filename = logFile.split("/").pop();
  416. $("#logIndex").append(`<option value="${filename}">${filename}</option>`);
  417. });
  418. }
  419. })
  420. }
  421. function initRemoveScheduleList(){
  422. $.ajax({
  423. url: "../../system/arsm/aecron/list?listall=true",
  424. data: {},
  425. success: function(data){
  426. $("#removeScheduleList").html("");
  427. if (data.length == 0){
  428. $("#removeScheduleList").append(`<tr>
  429. <td colspan="4">
  430. No registered schedule.
  431. </td>
  432. </tr>`);
  433. }else{
  434. data.forEach(task => {
  435. var filter = $("#filter").val().trim();
  436. if (filter != ""){
  437. //Apply filter to the results
  438. if (task.Creator.includes(filter) || task.Name.includes(filter) || task.Description.includes(filter)){
  439. //Matching
  440. }else{
  441. //Not matching
  442. return
  443. }
  444. }
  445. var scriptLocation = task.ScriptVpath;
  446. var removebutton = `<button class="ui negative mini button" name="${task.Name}" onclick="removeTask(this);">Remove</button>`;
  447. $("#removeScheduleList").append(`<tr>
  448. <td class="collapsing">
  449. <i class="user icon"></i> ${task.Name} (${task.Creator})
  450. </td>
  451. <td>${scriptLocation}</td>
  452. <td>${task.Description}</td>
  453. <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
  454. <td class="right aligned collapsing">
  455. ${removebutton}
  456. </td>
  457. </tr>`);
  458. });
  459. }
  460. }
  461. });
  462. }
  463. //Handle task creation
  464. function handleTaskSubmition(e){
  465. e.preventDefault();
  466. var taskName = $("#tasknanme").val();
  467. var desc = $("#desc").val();
  468. var scriptPath = $("#scriptpath").val();
  469. var interval = parseFloat($("#intervalvalue").val()) * parseFloat($("#intervalunit").val());
  470. var baseunix = Date.now();
  471. var baseTimeframe = $("#intervalbase").val();
  472. switch (baseTimeframe){
  473. case "now":
  474. //Round off to the nearest minute
  475. baseunix = moment().startOf('minute').unix()
  476. break;
  477. case "hour":
  478. baseunix = moment().startOf('hour').unix()
  479. break;
  480. case "day":
  481. baseunix = moment().startOf('day').unix()
  482. break;
  483. case "month":
  484. baseunix = moment().startOf('month').unix()
  485. break;
  486. case "year":
  487. baseunix = moment().startOf('year').unix()
  488. break;
  489. case "mon":
  490. baseunix = moment().startOf('week').add(1,'days').unix();
  491. break;
  492. case "tue":
  493. baseunix = moment().startOf('week').add(2,'days').unix();
  494. break;
  495. case "wed":
  496. baseunix = moment().startOf('week').add(3,'days').unix();
  497. break;
  498. case "thu":
  499. baseunix = moment().startOf('week').add(4,'days').unix();
  500. break;
  501. case "fri":
  502. baseunix = moment().startOf('week').add(5,'days').unix();
  503. break;
  504. case "sat":
  505. baseunix = moment().startOf('week').add(6,'days').unix();
  506. break;
  507. case "sun":
  508. baseunix = moment().startOf('week').unix();
  509. break;
  510. }
  511. //Create ajax request
  512. $.ajax({
  513. url: "../../system/arsm/aecron/add",
  514. method: "POST",
  515. data: {
  516. "name":taskName,
  517. "interval": interval,
  518. "base": baseunix,
  519. "desc": desc,
  520. "path": scriptPath
  521. },
  522. success: function(data){
  523. if (data.error !== undefined){
  524. alert(data.error);
  525. }else{
  526. //Clear all inputs
  527. $("#tasknanme").val("");
  528. $("#desc").val("");
  529. $("#scriptpath").val("");
  530. //Update the list and show it
  531. scheduleList();
  532. }
  533. }
  534. })
  535. }
  536. function applyFilter(){
  537. initScheduleFullList();
  538. initScheduleList();
  539. initRemoveScheduleList();
  540. }
  541. function loadLogFile(filename){
  542. $.get("../../system/arsm/aecron/listlog?filename=" + filename, function(data){
  543. $("#logPreview").text(data);
  544. })
  545. }
  546. function handleFilterEnterKeydown(event){
  547. if (event.which == 13){
  548. event.preventDefault();
  549. applyFilter();
  550. }
  551. }
  552. function openFileSelector(){
  553. ao_module_openFileSelector(scriptSelected, "user:/", "file", false);
  554. }
  555. function scriptSelected(filedata){
  556. for (var i=0; i < filedata.length; i++){
  557. var filename = filedata[i].filename;
  558. var filepath = filedata[i].filepath;
  559. $("#scriptpath").val(filepath);
  560. //See if the filepath end with valid extension
  561. var ext = filepath.split(".").pop();
  562. if (ext == "js" || ext == "agi" || ext == "sh" || ext == "bat"){
  563. $("#extensionWarning").hide();
  564. }else{
  565. $("#extensionWarning").show();
  566. }
  567. }
  568. }
  569. </script>
  570. </body>
  571. </html>