- Code hübscher machen [von NNs TODO]

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3471 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
karlchenofhell 18 years ago
parent f04097c3dd
commit e2ac5f62bd

@ -1,13 +1,19 @@
import java.awt.Color;
import java.awt.Image;
import java.util.HashMap;
import java.util.Iterator;
import de.anomic.http.httpHeader;
import de.anomic.plasma.plasmaGrafics;
import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.plasma.plasmaGrafics.CircleThreadPiece;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
import de.anomic.server.serverThread;
public class PeerLoadPicture {
public static Image respond(httpHeader header, serverObjects post, serverSwitch env) {
public static Image respond(httpHeader header, serverObjects post, serverSwitch env) {
int width = 800;
int height = 600;
@ -19,11 +25,59 @@ public class PeerLoadPicture {
showidle = post.get("showidle", "true").equals("true");
}
//too small values lead to an error, too big to huge CPU/memory consumption, resulting in possible DOS.
if (width < 400 ) width = 400;
final CircleThreadPiece idle = new CircleThreadPiece("Idle", new Color(170, 255, 170));
final CircleThreadPiece misc = new CircleThreadPiece("Misc.", new Color(190, 50, 180));
final HashMap /* <String,CircleThreadPiece> */ pieces = new HashMap();
pieces.put(null, idle);
pieces.put(plasmaSwitchboard.CRAWLSTACK, new CircleThreadPiece("Stacking", new Color(115, 200, 210)));
pieces.put(plasmaSwitchboard.INDEXER, new CircleThreadPiece("Parsing/Indexing", new Color(255, 130, 0)));
pieces.put(plasmaSwitchboard.INDEX_DIST, new CircleThreadPiece("DHT-Distribution", new Color(119, 136, 153)));
pieces.put(plasmaSwitchboard.PEER_PING, new CircleThreadPiece("YaCy Core", new Color(255, 230, 160)));
Iterator threads = env.threadNames();
String threadname;
serverThread thread;
long busy_time = 0;
//Iterate over threads
while (threads.hasNext()) {
threadname = (String)threads.next();
thread = env.getThread(threadname);
//count total times
busy_time += thread.getBlockTime();
busy_time += thread.getExecTime();
if (showidle) idle.addExecTime(thread.getSleepTime());
//count threadgroup-specific times
CircleThreadPiece piece = (CircleThreadPiece)pieces.get(threadname);
if (piece == null) {
misc.addExecTime(thread.getBlockTime()+thread.getExecTime());
} else {
piece.addExecTime(thread.getBlockTime()+thread.getExecTime());
}
}
busy_time += idle.getExecTime();
// set respective angles
Iterator it = pieces.values().iterator();
while (it.hasNext()) {
((CircleThreadPiece)it.next()).setAngle(busy_time);
}
// too small values lead to an error, too big to huge CPU/memory consumption,
// resulting in possible DOS.
if (width < 400) width = 400;
if (width > 1920) width = 1920;
if (height < 300) height = 300;
if (height > 1440) height = 1440;
return plasmaGrafics.getPeerLoadPicture(5000, width, height, showidle);
return plasmaGrafics.getPeerLoadPicture(
5000,
width,
height,
(CircleThreadPiece[])pieces.values().toArray(new CircleThreadPiece[pieces.size()]),
misc
);
}
}

@ -47,12 +47,12 @@ package de.anomic.plasma;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import de.anomic.server.serverThread;
import de.anomic.yacy.yacyCore;
import de.anomic.yacy.yacySearch;
import de.anomic.yacy.yacySeed;
@ -84,23 +84,32 @@ public class plasmaGrafics {
private static final Color COL_NORMAL_TEXT = new Color( 0, 0, 0);
private static final Color COL_LOAD_BG = new Color(247, 247, 247);
private static final int CIRCLE_PIECE_COUNT = 6;
private static final String[] CIRCLE_PIECE_NAMES = {
"Idle",
"Stacking",
"Parsing/Indexing",
"DHT-Distribution",
"YaCy Core",
"Misc."
};
private static final Color[] CIRCLE_PIECE_COLORS = {
new Color(170, 255, 170),
new Color(115, 200, 210),
new Color(255, 130, 0),
new Color(119, 136, 153),
new Color(255, 230, 160),
new Color(190, 50, 180)
};
public static class CircleThreadPiece {
private final String pieceName;
private final Color color;
private long execTime = 0;
private int angle = 0;
public CircleThreadPiece(String pieceName, Color color) {
this.pieceName = pieceName;
this.color = color;
}
public int getAngle() { return this.angle; }
public Color getColor() { return this.color; }
public long getExecTime() { return this.execTime; }
public String getPieceName() { return this.pieceName; }
public void addExecTime(long execTime) { this.execTime += execTime; }
public void reset() {
this.execTime = 0;
this.angle = 0;
}
public void setExecTime(long execTime) { this.execTime = execTime; }
public void setAngle(long totalBusyTime) {
this.angle = (int)Math.round(360f * ((float)this.execTime / (float)totalBusyTime));
}
}
private static final int LEGEND_BOX_SIZE = 10;
@ -289,84 +298,50 @@ public class plasmaGrafics {
}
}
public static BufferedImage getPeerLoadPicture(long maxAge, int width, int height, boolean showidle) {
public static BufferedImage getPeerLoadPicture(long maxAge, int width, int height, CircleThreadPiece[] pieces, CircleThreadPiece fillRest) {
if ((peerloadPicture == null) || ((System.currentTimeMillis() - peerloadPictureDate) > maxAge)) {
drawPeerLoadPicture(width, height, showidle);
drawPeerLoadPicture(width, height, pieces, fillRest);
}
return peerloadPicture;
}
private static void drawPeerLoadPicture(int width, int height, boolean showidle) {
private static void drawPeerLoadPicture(int width, int height, CircleThreadPiece[] pieces, CircleThreadPiece fillRest) {
//prepare image
peerloadPicture = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = peerloadPicture.createGraphics();
g.setBackground(COL_LOAD_BG);
g.clearRect(0,0,width,height);
plasmaSwitchboard sb = plasmaSwitchboard.getSwitchboard();
Iterator threads = sb.threadNames();
String threadname;
serverThread thread;
long busy_time = 0;
long[] thread_times = new long[CIRCLE_PIECE_COUNT];
//Iterate over threads
while (threads.hasNext()) {
threadname = (String)threads.next();
thread = sb.getThread(threadname);
//count total times
busy_time += thread.getBlockTime();
busy_time += thread.getExecTime();
if(showidle) thread_times[0] += thread.getSleepTime();
//count threadgroup-specific times
if(threadname.equals(plasmaSwitchboard.CRAWLSTACK)) {
thread_times[1] += thread.getBlockTime()+thread.getExecTime();
} else if(threadname.equals(plasmaSwitchboard.INDEXER)) {
thread_times[2] = thread.getBlockTime()+thread.getExecTime();
} else if(threadname.equals(plasmaSwitchboard.INDEX_DIST)) {
thread_times[3] = thread.getBlockTime()+thread.getExecTime();
} else if(threadname.equals(plasmaSwitchboard.PEER_PING)) {
thread_times[4] = thread.getBlockTime()+thread.getExecTime();
} else {
thread_times[5] += thread.getBlockTime()+thread.getExecTime();
}
}
int[] piece_angles = new int[CIRCLE_PIECE_COUNT];
//calculate angles
for(int i=0;i<CIRCLE_PIECE_COUNT;++i)
piece_angles[i] = (int)Math.round(360f*((float)thread_times[i]/(float)(busy_time+thread_times[0])));
Graphics2D g = peerloadPicture.createGraphics();
g.setBackground(COL_LOAD_BG);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.clearRect(0,0,width,height);
int circ_w = Math.min(width,height)-20; //width of the circle (r*2)
int circ_x = width-circ_w-10; //x-coordinate of circle-left
int circ_y = 10; //y-coordinate of circle-top
int circ_x = width-circ_w-10; //x-coordinate of circle-left
int circ_y = 10; //y-coordinate of circle-top
int curr_arc = 0; //remember current angle
//draw the pieces of the circle
int curr_arc = 0; //remember current angle
for(int i=showidle?0:1;i<CIRCLE_PIECE_COUNT;++i) {
g.setColor(CIRCLE_PIECE_COLORS[i]);
g.fillArc(circ_x, circ_y, circ_w, circ_w, curr_arc, piece_angles[i]);
curr_arc += piece_angles[i];
int i;
for (i=0; i<pieces.length; i++) {
// draw the piece
g.setColor(pieces[i].getColor());
g.fillArc(circ_x, circ_y, circ_w, circ_w, curr_arc, pieces[i].getAngle());
curr_arc += pieces[i].getAngle();
// draw it's legend line
drawLegendLine(g, 5, height - 5 - 15 * i, pieces[i].getPieceName(), pieces[i].getColor());
}
// fill the rest
g.setColor(fillRest.getColor());
//FIXME: better method to avoid gaps on rounding-differences?
g.fillArc(circ_x, circ_y, circ_w, circ_w, curr_arc, 360-curr_arc);
g.fillArc(circ_x, circ_y, circ_w, circ_w, curr_arc, 360 - curr_arc);
drawLegendLine(g, 5, height - 5 - 15 * i, fillRest.getPieceName(), fillRest.getColor());
//draw border around the circle
g.setColor(COL_BORDER);
g.drawArc(circ_x, circ_y, circ_w, circ_w, 0, 360);
for(int i=showidle?0:1;i<CIRCLE_PIECE_COUNT;++i)
drawLegendLine(g, 5, height-15*(i+1), CIRCLE_PIECE_NAMES[i], CIRCLE_PIECE_COLORS[i]);
peerloadPictureDate = System.currentTimeMillis();
}
private static void drawLegendLine(Graphics2D g, int x, int y, String caption, Color item_color) {
g.setColor(item_color);
g.fillRect(x, y-LEGEND_BOX_SIZE, LEGEND_BOX_SIZE, LEGEND_BOX_SIZE);

Loading…
Cancel
Save