Hel-lo!
There is a square matrix of 10x10, which contains the cost of travel from city A to city B.
Find a route that provides travel from town to town for a minimal cost (to implement the task to HTML + javascript) ".
Can you please tell how to write it on JavaScript? I do not want the animation as like in FindPath.js. Table (10x10) and output the result in other DOM-element are enough.
If someone faced with this, help me, please. I found plugins, but no one allows to input my own costs.
Ok. I tried to do it (http://experiments.hertzen.com/pathfinding/index.html, the 2nd variant). I introduced my own costs:
/*
* #author Niklas von Hertzen <niklas at hertzen.com>
*/
PathFinding = function(){
this.nodes = [];
this.nodeCounter = 0;
return this;
};
PathFinding.prototype.addNode = function(x, y){
var node = new PathFinding.Node(x, y);
node.id = this.nodeCounter++;
this.nodes.push(node);
return node;
};
PathFinding.Vertex = function(dest, cost) {
this.cost = cost || 1;
this.dest = dest;
};
PathFinding.prototype.Solver = function(from, to, solver){
if (solver === undefined){
solver = "astar";
}
switch(solver.toLowerCase()){
case "astar":
return this.AStarSolver(from, to);
break;
default:
alert("Unknown solver "+solver);
}
};
PathFinding.prototype.AStarSolver = function(from, to, startCount){
var closedset = []; // The set of nodes already evaluated.
var openset = []; // The set of tentative nodes to be evaluated, initially containing the start node
var came_from = []; // The map of navigated nodes.
var goal = to;
openset.push(from);
var route = [];
var reconstruct_path = function(fromNode){
if (fromNode !== from){
route.push(fromNode);
reconstruct_path(came_from[fromNode.id]);
}else{
route.push(from);
}
return route;
};
var heurestic_cost_estimate = function(from, to){
var xDist = Math.abs(from.x - to.x);
var yDist = Math.abs(from.y - to.y);
return Math.sqrt(Math.pow(xDist,2) + Math.pow(yDist,2));
};
var start = from.id, g_score = [], h_score = [], f_score = [];
g_score[start] = +startCount;
h_score[start] = heurestic_cost_estimate(from, to);
f_score[start] = h_score[start];
while (openset.length > 0){
var x = null; // current node
for (var i = 0, openset_len = openset.length; i < openset_len; i++){
if (x === null || f_score[openset[i].id] < f_score[x.id]){
x = openset[i];
}
}
// we have reached the goal
if (x === goal) {
route.push(goal);
reconstruct_path(came_from[x.id]);
return route.reverse();
}
// delete openset[0];
openset.splice(openset.indexOf(x),1);
closedset.push(x);
for(var y = 0, neighbors = x.vertices, neighbor_len = neighbors.length; y < neighbor_len; y++){
var tentative_is_better = false;
var yd = neighbors[y].dest;
if (closedset.indexOf(yd) > -1){
continue;
}
var tentative_g_score = g_score[x.id] + neighbors[y].cost;
if (openset.indexOf(yd) === -1){
openset.push(yd);
tentative_is_better = true;
} else if (tentative_g_score < g_score[yd.id]){
tentative_is_better = true;
}
if (tentative_is_better){
came_from[yd.id] = x;
g_score[yd.id] = tentative_g_score;
h_score[yd.id] = heurestic_cost_estimate(yd, to);
f_score[yd.id] = g_score[yd.id] + h_score[yd.id];
}
}
}
return false;
};
PathFinding.Node = function(x, y){
this.x = x;
this.y = y;
this.vertices = [];
return this;
};
PathFinding.Node.prototype.addVertex = function(dest, cost, oneWay){
var vertex = new PathFinding.Vertex(dest, cost);
this.vertices.push(vertex);
if (oneWay === undefined || oneWay === false){
var vertex2 = new PathFinding.Vertex(this, cost);
dest.vertices.push(vertex2);
}
return vertex;
};
td{
width:10px;
height:10px;
border:1px solid black;
cursor:pointer;
}
.current{
border-color:red;
}
<!DOCTYPE html>
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
//output table with my own costs
function addTable (table){
var PathFinder = new PathFinding();
table = $('#table2');
tableRowsLen = $('#table2 tr').length;
tableColLen = $('#table2 tr')[0].cells.length;
var nodes = [];
for (var r = 0; r < tableRowsLen; r++){
nodes[r] = [];
for (var c = 0; c < tableColLen; c++){
// add walls
if ((c==3 && r == 3) || (c==4 && r == 3) || (c==5 && r == 3) || (c==6 && r == 3) || (c==6 && r == 4) || (c==6 && r == 5)){
$('#table2').children('tbody').children('tr').eq(r).children('td').eq(c).css('background','black');
}else{
// add nodes
nodes[r][c] = PathFinder.addNode(c, r);
// add verticies between nodes
if (nodes[r][c-1] !== undefined){
nodes[r][c].addVertex(nodes[r][c-1], $('#table2').children('tbody').children('tr').eq(r).children('td').eq(c-1).html());
}
if (nodes[r-1] !== undefined && nodes[r-1][c] !== undefined){
nodes[r][c].addVertex(nodes[r-1][c], $('#table2').children('tbody').children('tr').eq(r-1).children('td').eq(c).html());
}
}
}
}
table.delegate("td","click",function(){
var current = table.find('.current').removeClass('current'); //from this place path starts
$(this).addClass('current');
var route = PathFinder.AStarSolver(nodes[current.parent().index()][current.index()],nodes[$(this).parent().index()][$(this).index()], current.html()); //value of cur. elem. Need for considering the cost
(function(i, route){
var $a, $b = 0;
function timer(){
if (i < route.length){
window.setTimeout(function(){
$a = table.find('tr').eq(route[i].y).find("td").eq([route[i].x]).css('background','green').html();
$a = +$a; //just for total sum of costs
$b += $a;
(function(i,route){
window.setTimeout(function(){
table.find('tr').eq(route[i].y).find("td").eq([route[i].x]).css('background','transparent');
},1000);
})(i, route);
i++;
timer();
},40);
}
}
timer();
})(0, route);
});
table.find("tr").eq(8).find("td").eq(1).addClass('current');
table.find("tr").eq(6).find("td").eq(8).trigger('click');
}
addTable($('#table2'));
});
</script>
</head>
<body>
<h2>Without diagonal movement</h2>
<table id="table2">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td class="" style="background: transparent;">4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td style="background: transparent;">8</td>
<td style="background: transparent;">9</td>
<td style="background: transparent;">54</td>
<td class="" style="background: transparent;">7</td>
<td>4</td>
<td>40</td>
<td>9</td>
</tr>
<tr>
<td>8</td>
<td>9</td>
<td style="background: transparent;">10</td>
<td style="background: transparent;">9</td>
<td style="background: transparent;">3</td>
<td class="" style="background: transparent;">5</td>
<td style="background: transparent;">9</td>
<td style="background: transparent;">15</td>
<td>54</td>
<td>54</td>
</tr>
<tr>
<td>89</td>
<td>1</td>
<td style="background: transparent;">5</td>
<td>9</td>
<td>2</td>
<td>78</td>
<td>25</td>
<td style="background: transparent;">45</td>
<td style="background: transparent;">4</td>
<td>8</td>
</tr>
<tr>
<td>8</td>
<td style="background: transparent;">9</td>
<td style="background: transparent;">9</td>
<td>8</td>
<td>7</td>
<td>7</td>
<td>9</td>
<td style="background: transparent;">6</td>
<td class="" style="background: transparent;">7</td>
<td>9</td>
</tr>
<tr>
<td>8</td>
<td class="" style="background: transparent;">4</td>
<td style="background: transparent;">1</td>
<td style="background: transparent;">9</td>
<td class="" style="background: transparent;">3</td>
<td style="background: transparent;">5</td>
<td>7</td>
<td style="background: transparent;">3</td>
<td style="background: transparent;">4</td>
<td>10</td>
</tr>
<tr>
<td>4</td>
<td style="background: transparent;">15</td>
<td class="" style="background: transparent;">9</td>
<td style="background: transparent;">8</td>
<td style="background: transparent;">7</td>
<td style="background: transparent;">6</td>
<td style="background: transparent;" class="">5</td>
<td style="background: transparent;">7</td>
<td class="" style="background: transparent;">3</td>
<td>9</td>
</tr>
<tr>
<td style="background: transparent;">7</td>
<td style="background: transparent;">1</td>
<td>6</td>
<td>1</td>
<td>7</td>
<td>9</td>
<td style="background: transparent;">8</td>
<td style="background: transparent;">9</td>
<td>4</td>
<td>3</td>
</tr>
<tr>
<td class="current" style="background: transparent;">8</td>
<td class="" style="background: transparent;">3</td>
<td style="background: transparent;">2</td>
<td style="background: transparent;">9</td>
<td style="background: transparent;">1</td>
<td style="background: transparent;">4</td>
<td style="background: transparent;">5</td>
<td>8</td>
<td>9</td>
<td>7</td>
</tr>
<tr>
<td>4</td>
<td>9</td>
<td>6</td>
<td>3</td>
<td>5</td>
<td>4</td>
<td>7</td>
<td>9</td>
<td>6</td>
<td>3</td>
</tr>
</tbody>
</table>
</body></html>
But behavior of the code is not that I expected. Please, help. Where is mistake can be?
Link to playground here
or here: http: //jsbin.com/ninolufiqa/1/edit?html,css,js,output
I don't know much about JavaScript, but the problem you are describing sounds like Dijkstra's algorithm.
Hope that helps.
Related
I am trying to set color when my $key value is changed. I mean I am generating a table when my $key is changing. Now I want to set a different bg-color when the $key is changing and a new table created. Now the <th colspan="5">{{$key}}</th> is changing and creating table for each $key I need to set color that $key is changing.
#foreach($lists as $key => $ser)
<table class="table table-striped table-bordered table-hover" id="sample_3">
<thead>
<tr>
<th colspan="5">{{$key}}</th>
</tr>
<tr>
<th> Destination</th>
<th> Services</th>
<th> Status</th>
<th> Time</th>
</tr>
</thead>
<tbody>
#foreach($ser as $s)
<tr>
<td style="background: rgb(176,224,230);"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
You can use a variable to store the value of $key in each loop. Then you can check to see if the $key is same as the previous one and change the color accordingly.
#php($prevvalue='')
#foreach($ser as $s)
<tr>
<td style="background: {{$s->key == $prevvalue ? 'oldcolor','newcolor'}}"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
#php($prevvalue = $s->key)
#endforeach
You can first add class from blade to group elements having similar value :
#php($prev = null)
#php($counter = 1)
#foreach($ser as $s)
#php
$counter = $s->key == $prev || is_null($prev) ? $counter : $counter + 1;
#endphp
<tr>
<td class="td_color_{{ $counter }}"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
#php($prev = $s->key)
#endforeach
This will give you following for the first column in each <tr> :
<td class="td_color_1">...</td>
<td class="td_color_1">...</td>
<td class="td_color_2">...</td>
<td class="td_color_2">...</td>
<td class="td_color_3">...</td>
<td class="td_color_4">...</td>
<td class="td_color_5">...</td>
Then in your css you have colors for these :
<style type="text/css">
td.td_color_1 { background: red; }
td.td_color_2 { background: blue; }
td.td_color_3 { background: green; }
td.td_color_4 { background: yellow; }
td.td_color_5 { background: pink; }
</style>
i found the showFooter Property. but i do not get how to customize the Content for that.
i only need to fill three of total 8 in ...
so table Looks basically like this:
<div class="table-responsive">
<table class="table table-bordered" id="tblKontoauszug">
<thead>
<tr>
<th data-sortable="#sortable" data-sorter="dateSorter">Buchungsdat.</th>
<th data-sortable="#sortable">Belegnr.</th>
<th data-sortable="#sortable">BA</th>
<th data-sortable="#sortable" data-sorter="betragSorter">Betrag</th>
<th data-sortable="#sortable">Buchungstext</th>
<th data-sortable="#sortable">Gegenkontoart</th>
<th data-sortable="#sortable">Gegenkonto</th>
<th data-sortable="#sortable">Bezeichnung</th>
</tr>
<tr class="info text-bold">
<td>
#if ( Model.Zeilen.Count() > 0 )
{
<span>#Model.Zeilen.Min(b => b.Buchungsdatum).ToShortDateString()</span>
}
</td>
<td colspan="2">Anfangsbestand</td>
<td class="text-right">#Model.Anfangsbestand.ToString("N")</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tfoot>
<tr class="info text-bold">
<td>
#if ( Model.Zeilen.Count() > 0 )
{
<span>#Model.Zeilen.Max( b => b.Buchungsdatum ).ToShortDateString()</span>
}
</td>
<td colspan="2">Endbestand</td>
<td class="text-right #( Model.Endbestand < 0 ? "negative" : "")">#Model.Endbestand.ToString( "N" )</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
<tbody>
#foreach ( var zeile in Model.Zeilen )
{
<tr>
<td>#zeile.Buchungsdatum.ToShortDateString()</td>
<td>#zeile.Belegnummer</td>
<td title="#zeile.BelegartText">#zeile.Belegart</td>
<td class="text-right #( zeile.Betrag < 0 ? "negative" : "")">#zeile.Betrag.ToString("N")</td>
<td>#zeile.Buchungstext</td>
<td>#zeile.Gegenkontoart</td>
<td>#zeile.Gegenkonto</td>
<td>#zeile.Bezeichnung</td>
</tr>
}
</tbody>
</table>
</div>
You can customize content in the footer by adding the data-footer-formatter attribute to the column you want to add the footer to and setting that attribute equal to a defined javascript function. Here's an example:
HTML:
<table data-toggle="table" data-show-footer="true" data-footer-style="footerStyle" data-show-columns="true">
<thead>
<tr>
<th data-sortable="true" data-footer-formatter="totalText">Name</th>
<th data-sortable="true" data-footer-formatter="carsSum"># Cars</th>
</tr>
</thead>
<tbody>
<tr>
<td>Johnny</td>
<td>2</td>
</tr>
<tr>
<td>Zack</td>
<td>3</td>
</tr>
<tr>
<td>Timmy</td>
<td>2</td>
</tr>
</tbody>
</table>
Javascript:
function totalText(data) {
return 'TOTAL';
}
function carsSum(data) {
return '7';
}
function footerStyle(value, row, index) {
return {
css: { "font-weight": "bold" }
};
}
Notice I'm not using <tfoot> at all here. That is because <tfoot> won't work with the show columns dropdown list but this will (if I were to use the data-show-columns="true" attribute on the <table> element as I did in the example above). You can also style the footer using the data-footer-style attribute. In my example, I'm making all text in the footer bold.
You can see the code in action here: https://jsfiddle.net/3mou92px/9/
I am working in mvc3 razor view. I create pdf report from mvc3 razor view below is my controller code.
public ActionResult PReqDocPrint()
{
List<PReqPrintModel> Rec = new List<PReqPrintModel> { };
Rec = PReqPrint("", "", "");
return this.ViewPdf("Purchase Requisition", "PReqDocPrint", Rec);
}
I get data from database through stored procedure and return to controller
and my view code is given below.
#model IEnumerable<CBS.Models.PReqPrintModel>
#{
Layout = "~/Views/Shared/_LayoutForPrints.cshtml";
}
<table border="1" >
<tr>
<td>
<h2 style="text-align:center; font-family:Times New Roman;">Purchase Requisition</h2>
</td>
</tr>
</table>
<div style="color:Black;border-width:0px 1px 1px 0px;font-size:8px; font-weight:normal; page-break-before:always;">
#{
var OddColor = "#aaaaff";
var evenColor = "#EEFFFF";
var Odd = OddColor;
var eve = evenColor;
}
<table style="font-family:Times New Roman; font-size:small">
#foreach (var doc in Model.GroupBy(d => d.PReqNo))
{
foreach (var pre in doc)
{
<tr style="border:1;">
<td>
Purchase Requisition: #Html.Encode(pre.PReqNo)
</td>
<td>
Purchase Date: #String.Format("{0:dd/MM/yyyy}", #Html.Encode(pre.PReqDate.Date))
</td>
</tr>
<tr style="border:1;">
<td>
Store Location: #Html.Encode(pre.StName)
</td>
<td>
Department: #Html.Encode(pre.DeptName)
</td>
</tr>
<tr style="border:1;">
<td>
Remarks: #Html.Encode(pre.RefRemarks)
</td>
<td></td>
</tr>
}
<tr >
<td colspan="2">
<table border="1" style="color:Black;border-width:0px 1px 1px 0px;font-size:8px;font-family:Helvetica;font-weight:normal;">
<tr style="background-color:#7070a0; font-weight:bold; color:Black; border:1;" >
<td align="left">
Code
</td>
<td align="left">
Description
</td>
<td align="left">
Unit
</td>
<td align="left">
Part No
</td>
<td align="left">
Lot No
</td>
<td align="left">
Item Remarks
</td>
<td align="right">
Quantity
</td>
<td align="right">
Amount
</td>
</tr>
#foreach (var item in doc)
{
<tr bgcolor="#Odd">
<td align="left">
#Html.Encode(item.ItemCode)
</td>
<td align="left">
#Html.Encode(item.ItemDesc)
</td>
<td align="left">
#Html.Encode(item.Units)
</td>
<td align="left">
#Html.Encode(item.PartNo)
</td>
<td align="left">
#Html.Encode(item.LotNo)
</td>
<td align="left">
#Html.Encode(item.Remarks)
</td>
<td align="right">
#Html.Encode(item.QtyIN)
</td>
<td align="right">
#Html.Encode(item.Qty)
</td>
</tr>
}
if (Odd == OddColor)
{
Odd = evenColor;
}
else
{
Odd = OddColor;
}
}
</table>
</td>
</tr>
}
</table>
</div>
<div class="break"></div>
Below is my PDF result image which return from view
Here I want to start new PDF page When second Purchase Requisition no 2014030001 start. Any one please help me.
i already try inline CSS for page break or table page-break-before:always; and also try a separate div tag with page-break-before:always; but nothing obtain my required result.
Also it not apply separate style sheet formats on PDF view.
i have table with 2 columns and in 2nd column I have nested table. I want to split tr in two horizontally block... in first i put all the tds and in 2nd I put confirm and cancel buttons
<table id="ElementTable" class="customTable_01">
<thead>
<tr>
<th class="SC_a1">
#Html.DisplayName("Element")
</th>
<th class="SC_a2">
Marking-Scheme
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model._Element)
{
<tr>
<td class="SC_a1"> #Html.DisplayFor(modelitem => item.ElementTitle) </td>
<td class="SC_a2">
<input type="button" value="Add" name="button" data-id="#item.ElementID" class="k-button k-button-icontext k-grid-Add ComponentScheme_Add" />
#*Nasted Table....*#
<table class="NastedElementTable">
<thead>
<tr>
<th class="N_th">Mark Scheme Title</th>
<th class="N_th">Available-Mark</th>
<th class="N_th">Pass-Mark</th>
<th class="N_th">Merit-Mark</th>
<th class="N_th">Distinction-Mark</th>
</tr>
</thead>
<tbody>
<tr>
<td class="N_td">#Html.TextBox("Input_Title_Element", null, new { id = #item.ElementID + "_DM", #class = "ElementDistinctionMark k-grid-input k-textbox_3" }) </td>
<td class="N_td">#Html.TextBox("Input_AvailableMark_Element", null, new { id = #item.ElementID + "_AM", #class = "ElementAvailableMarks k-grid-input k-textbox_4" })</td>
<td class="N_td">#Html.TextBox("Input_PassMark_Element", null, new { id = #item.ElementID + "_PM", #class = "ElementPassMark k-grid-input k-textbox_4" })</td>
<td class="N_td">#Html.TextBox("Input_MeritMark_Element", null, new { id = #item.ElementID + "_MM", #class = "ElementMeritMark k-grid-input k-textbox_4" })</td>
<td rowspan="2" class="N_td">#Html.TextBox("Input_DistinctionMark_Element", null, new { id = #item.ElementID + "_DM", #class = "ElementDistinctionMark k-grid-input k-textbox_4" })</td>
</tr>
</tbody>
</table>
</td>
</tr>
}
</tbody>
</table>
i have found solution, notice in my table in thead there is 1 tr so trick is add two tr in tbody
<thead>
<tr>
<td></td>
....
</tr>
</thead>
<tbody>
<tr>
<td><td/>
......
</tr>
<tr>
<td>
<input type="button" value="Cancel">
<td/>
<td>
<input type="button" value="Confirm">
</td>
</tr>
is there a built-in property in any of the ASP.net classes that determines what the uptime of the webserver, where the webapplication is running on, is?
These are some of the possibilities that popped into my mind:
global.asax and the Application_OnStart Event. Get the timestamp and to determine the uptime, I just substract it from DateTime.Now
Write a windows service (automatically startup) which's only purpose is to store the value of the service start somewhere
But neither of these two offer a "built-in" solution. Is there something I might oversee in the .net framework?
To get system uptime, use the following code:
TimeSpan uptime;
using (var uptimeCounter = new PerformanceCounter("System", "System Up Time")) {
uptimeCounter.NextValue();
uptime = TimeSpan.FromSeconds(uptimeCounter.NextValue());
}
EDIT: Note that this can't be used by partially trusted code.
You could use TimeSpan.FromMilliseconds(Environment.TickCount), but it'll wrap after two weeks.
I wrote a server status page in ASP.Net which shows server uptime and more.
Here is the entire page:
<%# Page Title="Server Stats" Language="C#" MasterPageFile="~/Admin.Master" AutoEventWireup="true"
CodeFile="Stats.aspx.cs" Inherits="Stats" %>
<%# Import Namespace="System.Diagnostics" %>
<%# Import Namespace="Microsoft.VisualBasic.Devices" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
body {
background-color: #9DC0E4;
}
table.Details {
width: 550px;
margin-left: -275px;
left: 50%;
position: absolute;
}
table.Details tbody.Group {
border-bottom: solid black 2px;
margin-bottom: 15px;
}
table.Details th.Group {
font-size: x-large;
border-bottom: dashed 1px navy;
}
table.Details th.Name {
text-align: left;
}
table.Details td.Value {
text-align: right;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<%
var computer = new ComputerInfo();
using (var iis = Process.GetCurrentProcess())
using (var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total"))
using (var uptime = new PerformanceCounter("System", "System Up Time")) {
cpu.NextValue();
uptime.NextValue();
%>
<table class="Details">
<tbody class="Group">
<tr>
<th class="Group" colspan="2">Environment</th>
</tr>
<tr>
<th class="Name">Local server time</th>
<td class="Value">
<%= DateTime.Now.ToString("F")%></td>
</tr>
<tr>
<th class="Name">OS</th>
<td class="Value">
<%= computer.OSFullName%><br />
<%= Environment.OSVersion.ToString()%></td>
</tr>
<tr>
<th class="Name">Machine name</th>
<td class="Value">
<%= Environment.MachineName%></td>
</tr>
<tr>
<th class="Name">User name</th>
<td class="Value">
<%= Environment.UserName%></td>
</tr>
<tr>
<th class="Name">Windows domain</th>
<td class="Value">
<%= Environment.UserDomainName%></td>
</tr>
</tbody>
<tbody class="Group">
<tr>
<th class="Group" colspan="2">IIS</th>
</tr>
<tr>
<th class="Name">IIS Uptime</th>
<td class="Value">
<%= (DateTime.Now- iis.StartTime).ToApproximateString()%></td>
</tr>
<tr>
<th class="Name">Priority</th>
<td class="Value">
<%= iis.PriorityClass%></td>
</tr>
<tr>
<th class="Name">Physical Memory Used</th>
<td class="Value">
<%= ToSizeString(iis.WorkingSet64)%></td>
</tr>
<tr>
<th class="Name">Virtual Memory Used</th>
<td class="Value">
<%= ToSizeString(iis.VirtualMemorySize64)%></td>
</tr>
</tbody>
<tbody class="Group">
<tr>
<th class="Group" colspan="2">Hardware</th>
</tr>
<tr>
<th class="Name">Processors</th>
<td class="Value">
<%= Environment.ProcessorCount.ToString()%></td>
</tr>
<tr>
<th class="Name">Physical memory</th>
<td class="Value">
<%= ToSizeString(computer.TotalPhysicalMemory)%></td>
</tr>
<tr>
<th class="Name">Virtual memory</th>
<td class="Value">
<%= ToSizeString(computer.TotalVirtualMemory)%></td>
</tr>
</tbody>
<tbody class="Group">
<tr>
<th class="Group" colspan="2">Performance</th>
</tr>
<tr>
<th class="Name">Uptime</th>
<td class="Value">
<%= TimeSpan.FromSeconds(uptime.NextValue()).ToApproximateString()%>
</td>
</tr>
<tr>
<th class="Name">CPU Usage</th>
<td class="Value">
<%= (cpu.NextValue()/100).ToString("p")%>
</td>
</tr>
<tr>
<th class="Name">Physical memory free</th>
<td class="Value">
<%= ToSizeString(computer.AvailablePhysicalMemory)%></td>
</tr>
<tr>
<th class="Name">Virtual memory free</th>
<td class="Value">
<%= ToSizeString(computer.AvailableVirtualMemory)%></td>
</tr>
</tbody>
</table>
<%} %>
</asp:Content>
ToSizeString is defined in the .cs file:
protected static string ToSizeString(double bytes) {
var culture = CultureInfo.CurrentUICulture;
const string format = "#,0.0";
if (bytes < 1024)
return bytes.ToString("#,0", culture);
bytes /= 1024;
if (bytes < 1024)
return bytes.ToString(format, culture) + " KB";
bytes /= 1024;
if (bytes < 1024)
return bytes.ToString(format, culture) + " MB";
bytes /= 1024;
if (bytes < 1024)
return bytes.ToString(format, culture) + " GB";
bytes /= 1024;
return bytes.ToString(format, culture) + " TB";
}
ToApproximateString is an extension method defined elsewhere:
public static string ToApproximateString(this TimeSpan time) {
if (time.TotalDays > 14)
return ((int)(time.TotalDays / 7)).ToString("#,0.0") + " weeks";
if (14 - time.TotalDays < .75)
return "two weeks";
if (time.TotalDays > 1)
return time.TotalDays.ToString("#,0.0") + " days";
else if (time.TotalHours > 1)
return time.TotalHours.ToString("#,0.0") + " hours";
else if (time.TotalMinutes > 1)
return time.TotalMinutes.ToString("#,0.0") + " minutes";
else
return time.TotalSeconds.ToString("#,0.0") + " seconds";
}
Thanks, that was the kind of answer I was looking for =)
I'll stick to
TimeSpan _diff = DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime);
this.litRuntime.Text = string.Format("{0} days and {1:00}:{2:00}:{3:00}", (int)_diff.TotalDays, _diff.Hours, _diff.Minutes, _diff.Seconds);
for the moment.