private fun updateServerStatus() { try { val page = StringBuffer() page.append("<?php\n") page.append("session_start();\n") page.append("\$username = 'Guest';\n") page.append("if (array_key_exists('username', $_SESSION)) { \$username = $_SESSION['username']; }\n") page.append("?>\n") page.append("<html>\n<head>\n <title>R Server</title>\n") page.append(" <script>\n") page.append(" function reload() { \n") page.append(" var isIE = /*@cc_on!@*/false || !!document.documentMode;\n") page.append(" if (!isIE) location.reload();\n") page.append(" }\n") page.append( """ ${" setTimeout(reload, $pageRefreshTime"}) """.trimIndent() ) page.append(" </script>\n") page.append(" <link rel=\"stylesheet\" type=\"text/css\" href=\"rserver.css\">\n") page.append("</head>\n<body>\n") page.append("<div id=\"MainDiv\">\n\n") page.append("<div id=\"HeaderDiv\">\n") page.append(" <div id=\"NavHeaderDiv\"></div>\n") page.append(" <div id=\"NavLogo\"></div>\n") page.append(" <a id=\"newHeaderNavigation-logo\" href=\"/RServer\" title=\"R Server\"></a>\n") page.append(" <ul id=\"NavBar\">\n") page.append(" <li><a href=\"https://github.com/bgweber/RServer\">EA RServer</a></li>\n") page.append(" <li><a href=\"NewTask.php\">Submit New Task</a>\n") page.append(" <li><a href=\"Reports.php\">Reports</a></li>\n") page.append(" <li><a href=\"logs\">Server Logs</a></li>\n") page.append(" <li><a href=\"Rout\">Script Results</a></li>\n") page.append(" <li><a href=\"reports/RServer/RServerTasks.html\">Task Report</a></li>\n") page.append(" </ul>\n\n") page.append("</div>\n\n") page.append(" <div id=\"SubHeaderDiv\">\n") page.append(" <h2 id=\"RHeader\">R Server: </h2>\n") page.append( """ <?php if ((time() - ${System.currentTimeMillis() / 1000}) < 10) { echo "<h2 id=\"ServerOnline\">Online</h2>"; } else { echo "<h2 id=\"ServerOffline\">Offline</h2>"; } ?> """.trimIndent() ) page.append(" </div>\n\n") page.append(" <div id=\"ContentDiv\">\n") page.append(" <script>\n") page.append(" var isIE = /*@cc_on!@*/false || !!document.documentMode;\n") page.append(" if (isIE) document.write('<p><h2>Note: Auto-reload is disabled for Internet Explorer. Please use Chrome, Firefox, or Opera.<h2>'); \n") page.append(" </script>\n") page.append("<h3 id=\"StatsHeader\">Server Statistics</h3>\n") page.append("<table id=\"StatsTable\">\n") page.append(" <thead>\n") page.append(" <th>Property</th>\n <th>Value</th>\n") page.append(" </thead>\n") if (fullHostName != null) { page.append( """ ${" <tr><td>Server Name</td><td>$fullHostName"}</td></tr> """.trimIndent() ) } page.append( """ ${" <tr><td>Server Updated</td><td>" + Date(System.currentTimeMillis()).toString()}</td></tr> """.trimIndent() ) val runtime = Runtime.getRuntime() val memory = ((runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024).toString() + " / " + runtime.totalMemory() / 1024 / 1024 + " MB" try { var totalMem: Double = sigar.getMem().getTotal() / 1024 / 1024 / 1024.0 var usedMem: Double = (sigar.getMem().getTotal() - sigar.getMem().getFree()) / 1024 / 1024 / 1024.0 cpuLoad = 1 - sigar.getCpuPerc().getIdle() totalMem = (totalMem * 10).toInt() / 10.0 usedMem = (usedMem * 10).toInt() / 10.0 page.append(" <tr><td>Server CPU Usage</td><td>" + (cpuLoad * 1000) as Int / 10.0 + "%</td></tr>\n") page.append(" <tr><td>Server Memory</td><td>$usedMem / $totalMem GB</td></tr>\n") } catch (e: Exception) { log("ERROR: unable to update system stats: " + e.message) } page.append(" <tr><td>Application Memory</td><td>$memory</td></tr>\n") page.append( """ <tr><td>Thread Count</td><td>${Thread.activeCount()}</td></tr> """ ) page.append( """ <tr><td>Git Updated</td><td>${if (lastPerforceUpdate > 0) Date(lastPerforceUpdate).toString() else "No history"}</td></tr> """ ) page.append( """ ${" <tr><td>Server Launched</td><td>" + Date(bootTime).toString()}</td></tr> """.trimIndent() ) page.append( """ ${" <tr><td>Server Version</td><td>$ServerDate"}</td></tr> """.trimIndent() ) page.append("</table>\n\n") page.append("<h3 id=\"LogHeader\">R Server Reports</h3>\n") page.append("<ul id=\"LogList\">\n") page.append(" <li><a href=\"reports/RServer/RServerReport.html\">R Server Activity</a></li>\n") page.append(" <li><a href=\"reports/RServer/RServerTasks.html\">Task Report</a></li>\n") page.append("</ul>\n") page.append("<h3 id=\"RunningHeader\">Running Tasks</h3>\n") page.append("<table id=\"RunningTable\">\n") page.append(" <thead>\n") page.append( """ <th>Task Name</th> <th>Log File</th> <th>Start Time</th> <th>Duration</th> <th>Runtime Parameters</th> <th>Owner</th> <th>End Process</th> """ ) page.append(" </thead>\n") synchronized(this@RServer) { for (task in runningTasks.values()) { val startTime: Long = task.getStartTime() val duration = (System.currentTimeMillis() - startTime) / 1000 page.append(" <tr>\n") page.append(" <td>" + (if (task.getShinyApp()) "<a href=\"" + task.getShinyUrl() + "\">" else "") + task.getTaskName() + (if (task.getShinyApp()) "</a>" else "") + "</td>") if (task.getrScript().toLowerCase().contains(".r") || task.getIsPython()) { val link = "<a href=\"Rout.php?task=" + task.getTaskName() + "&log=" + (if (task.getrScript() .contains(".r") ) task.getrScript() + ".Rout" else task.getrScript().split("\\.") .get(0) + ".Rout") + "\">" + task.getrScript() + "</a>" page.append("\n <td>$link</td>") } else { page.append("\n <td> </td>") } page.append( """ <td>${Date(startTime).toString()}</td>""" ) page.append( """ <td>${if (task.getShinyApp()) "" else (duration / 60).toString() + " m " + duration % 60 + " s"}</td>""" ) page.append( """ <td>${task.getParameters()}</td>""" ) page.append( """ <td>${task.getOwner()}</td>""" ) if (PerforceTask.equalsIgnoreCase(task.getTaskName())) { page.append("\n <td> </td>") } else { page.append( """${ """ <td><form onsubmit="return confirm('Are you sure?')" action="KillTask.php" method="post"> <input type="hidden" name="name" value="${task.getTaskName()}""" }" /> <button >Terminate</button> </form></td>""" ) } page.append("\n </tr>\n") } } page.append("</table>\n\n") page.append("<h3 id=\"LogHeader\">Recent Log Entries</h3>\n") synchronized(this@RServer) { page.append("<ul id=\"LogList\">\n") for (log in logEntries) { if (log.contains("ERROR:")) { page.append(" <li class=\"FontLogError\">$log</li>\n") } else { page.append(" <li>$log</li>\n") } } page.append("</ul>\n\n") } page.append("<h3 id=\"SchedulesHeader\">Scheduled Tasks</h3>\n") page.append("<table id=\"ScheduleTable\">\n") page.append(" <thead>\n") page.append( """ <th>Task Name</th> <th>R Script</th> <th>Runtime Parameters</th> <th>Frequency</th> <th>Next Run Time</th> <th>Owner</th> <th>Run Now</th> """ ) page.append(" </thead>\n") page.append("<ul id=\"LogList\">\n") if (schedulePath.split("//").length > 1) { page.append( """ ${" <li>Schedule File: //" + schedulePath.split("//").get(1)}</li> """.trimIndent() ) } else { page.append( """ ${" <li>Schedule File: $schedulePath"}</li> """.trimIndent() ) } page.append("</ul>\n") for (schedule in regularSchedules) { page.append(" <tr>\n") page.append( """${ " <td>" + schedule.getTaskName() .replace("_", " ") + "</td>\n " + "<td>" + schedule.getrScript().replace( "_", " " ) + "</td>\n " + "<td>" + schedule.getParameters() + "</td>\n " + "<td>" + (if (schedule.getFrequency() === Schedule.Frequency.Now) "Startup" else schedule.getFrequency()) + "</td>\n <td>" + (if (schedule.getFrequency() === Schedule.Frequency.Now || schedule.getFrequency() === Schedule.Frequency.Never) " " else Date( schedule.getNextRunTime() ).toString()) + "</td>\n <td>" + schedule.getOwner() + "</td>\n " + "<td><form onsubmit=\"return confirm('Are you sure?')\"" + " action=\"SubmitTask.php\" method=\"post\">" + "\n <input type=\"hidden\" name=\"name\" value=\"" + schedule.getTaskName() + "\" />" + "\n <input type=\"hidden\" name=\"path\" value=\"" + schedule.getPerforcePath() + "\" />" + "\n <input type=\"hidden\" name=\"rscript\" value=\"" + schedule.getrScript() + "\" />" + "\n <input type=\"hidden\" name=\"email\" value=\"" + schedule.getOwner() + "\" />" + "\n <input type=\"hidden\" name=\"sendemail\" value=\"" + schedule.getEmailOnSuccess() + "\" />" + "\n <input type=\"hidden\" name=\"params\" value=\"" + schedule.getParameters() + "\" />" + "\n <input type=\"hidden\" name=\"shiny\" value=\"" + schedule.getShinyApp() }" /> <button>Start</button> </form></td> """ ) page.append(" </tr>\n") } page.append("</table>\n\n") page.append("<h3 id=\"CompletedHeader\">Completed Tasks</h3>\n") page.append("<table id=\"CompletedTable\">\n") page.append(" <thead>\n") page.append( """ <th>Task Name</th> <th>Completion Time</th> <th>Duration</th> <th>Runtime Parameters</th> <th>Outcome</th><th>.Rout log</th> <th>Owner</th> """ ) page.append(" </thead>\n") synchronized(this@RServer) { for (task in completed) { val duration: Long = (task.getEndTime() - task.getStartTime()) / 1000 page.append(" <tr>\n") page.append( """${" <td>" + task.getTaskName().replace("_", " ")}</td> <td>""" ) page.append( """${" " + Date(task.getEndTime()).toString()}</td> <td>""" ) page.append( """ ${(duration / 60).toString() + " m " + duration % 60 + " s "}</td> <td>""" ) page.append( """${" " + task.getParameters()}</td> <td>""" ) page.append( " " + if (task.getOutcome() .equals("Success") ) "<font class=\"FontTaskSuccess\">" else "<font class=\"FontTaskFailure\">" ) page.append( """${" " + task.getOutcome()}</font></td> <td>""" ) if (task.getRout() != null) { page.append( "<a href=\"Rout.php?path=" + task.getRout() + "&log=" + task.getrScript() + ".Rout\">" + task.getrScript() .replace("_", " ") + "</a></td>\n" ) } else { page.append(" </td>\n") } page.append( """ ${" " + "<td>" + task.getOwner()}</td> """.trimIndent() ) page.append(" </tr>\n") } } page.append("</table>\n\n") page.append("</div>\n\n") page.append("<div id=\"FooterDiv\">\n") page.append(" <a href=\"https://github.com/bgweber/RServer\">RServer on GitHub</a>\n") page.append("</div>\n\n") page.append("</div>\n</body>\n</html>\n") val writer = BufferedWriter(FileWriter(indexPagePath)) writer.write(page.toString()) writer.close() } catch (e: Exception) { log("ERROR: failure updating server status: " + e.message) e.printStackTrace() } }
Generates the index.php file for the server dashboard .