*) better formatting for network QPM

*) refactoring
pull/1/head
Marc Nause 13 years ago
parent 77f8e9fb9b
commit a691023d04

@ -33,6 +33,7 @@ import javax.imageio.ImageIO;
import net.yacy.cora.protocol.RequestHeader; import net.yacy.cora.protocol.RequestHeader;
import net.yacy.peers.Seed; import net.yacy.peers.Seed;
import net.yacy.peers.graphics.BannerData;
import net.yacy.peers.graphics.NetworkGraph; import net.yacy.peers.graphics.NetworkGraph;
import net.yacy.search.Switchboard; import net.yacy.search.Switchboard;
import net.yacy.search.SwitchboardConstants; import net.yacy.search.SwitchboardConstants;
@ -40,23 +41,22 @@ import net.yacy.visualization.RasterPlotter;
import de.anomic.server.serverObjects; import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch; import de.anomic.server.serverSwitch;
/** draw a banner with information about the peer */ /** Draw a banner with information about the peer. */
public class Banner public class Banner {
{
public static RasterPlotter respond( public static RasterPlotter respond(
final RequestHeader header, final RequestHeader header,
final serverObjects post, final serverObjects post,
final serverSwitch env) throws IOException { final serverSwitch env) throws IOException {
final Switchboard sb = (Switchboard) env; final Switchboard sb = (Switchboard) env;
final String IMAGE = "htroot/env/grafics/yacy.png"; final String pathToImage = "htroot/env/grafics/yacy.png";
int width = 468; int width = 468;
int height = 60; int height = 60;
String bgcolor = "e7effc"; String bgcolor = "e7effc";
String textcolor = "000000"; String textcolor = "000000";
String bordercolor = "5090d0"; String bordercolor = "5090d0";
if ( post != null ) { if (post != null) {
bgcolor = post.get("bgcolor", bgcolor); bgcolor = post.get("bgcolor", bgcolor);
textcolor = post.get("textcolor", textcolor); textcolor = post.get("textcolor", textcolor);
bordercolor = post.get("bordercolor", bordercolor); bordercolor = post.get("bordercolor", bordercolor);
@ -70,8 +70,12 @@ public class Banner
int myppm = 0; int myppm = 0;
double myqph = 0; double myqph = 0;
String type = ""; String type = "";
final String network = env.getConfig(SwitchboardConstants.NETWORK_NAME, "unspecified").toUpperCase(); final String network =
final int peers = sb.peers.sizeConnected() + 1; // the '+ 1': the own peer is not included in sizeConnected() env.getConfig(
SwitchboardConstants.NETWORK_NAME,
"unspecified").toUpperCase();
// the '+ 1': the own peer is not included in sizeConnected()
final int peers = sb.peers.sizeConnected() + 1;
long nlinks = sb.peers.countActiveURL(); long nlinks = sb.peers.countActiveURL();
long nwords = sb.peers.countActiveRWI(); long nwords = sb.peers.countActiveRWI();
final double nqpm = sb.peers.countActiveQPM(); final double nqpm = sb.peers.countActiveQPM();
@ -79,26 +83,26 @@ public class Banner
double nqph = 0; double nqph = 0;
final Seed seed = sb.peers.mySeed(); final Seed seed = sb.peers.mySeed();
if ( seed != null ) { if (seed != null) {
name = seed.get(Seed.NAME, "-").toUpperCase(); name = seed.get(Seed.NAME, "-").toUpperCase();
links = seed.getLinkCount(); links = seed.getLinkCount();
words = seed.getWordCount(); words = seed.getWordCount();
myppm = seed.getPPM(); myppm = seed.getPPM();
myqph = 60d * seed.getQPM(); myqph = 60d * seed.getQPM();
if ( sb.peers.mySeed().isVirgin() ) { if (sb.peers.mySeed().isVirgin()) {
type = "VIRGIN"; type = "VIRGIN";
nqph = Math.round(6000d * nqpm) / 100d; nqph = Math.round(6000d * nqpm) / 100d;
} else if ( sb.peers.mySeed().isJunior() ) { } else if (sb.peers.mySeed().isJunior()) {
type = "JUNIOR"; type = "JUNIOR";
nqph = Math.round(6000d * nqpm) / 100d; nqph = Math.round(6000d * nqpm) / 100d;
} else if ( sb.peers.mySeed().isSenior() ) { } else if (sb.peers.mySeed().isSenior()) {
type = "SENIOR"; type = "SENIOR";
nlinks = nlinks + links; nlinks = nlinks + links;
nwords = nwords + words; nwords = nwords + words;
nqph = Math.round(6000d * nqpm + 100d * myqph) / 100d; nqph = Math.round(6000d * nqpm + 100d * myqph) / 100d;
nppm = nppm + myppm; nppm = nppm + myppm;
} else if ( sb.peers.mySeed().isPrincipal() ) { } else if (sb.peers.mySeed().isPrincipal()) {
type = "PRINCIPAL"; type = "PRINCIPAL";
nlinks = nlinks + links; nlinks = nlinks + links;
nwords = nwords + words; nwords = nwords + words;
@ -107,48 +111,23 @@ public class Banner
} }
} }
if ( !NetworkGraph.logoIsLoaded() ) { final BannerData data =
ImageIO.setUseCache(false); // do not write a cache to disc; keep in RAM new BannerData(
final BufferedImage logo = ImageIO.read(new File(IMAGE)); width, height, bgcolor, textcolor, bordercolor, name, links,
return NetworkGraph.getBannerPicture( words, type, myppm, network, peers, nlinks, nwords,
nqph, nppm);
if (!net.yacy.peers.graphics.Banner.logoIsLoaded()) {
// do not write a cache to disc; keep in RAM
ImageIO.setUseCache(false);
final BufferedImage logo = ImageIO.read(new File(pathToImage));
return net.yacy.peers.graphics.Banner.getBannerPicture(
data,
1000, 1000,
width,
height,
bgcolor,
textcolor,
bordercolor,
name,
links,
words,
type,
myppm,
network,
peers,
nlinks,
nwords,
nqph,
nppm,
logo); logo);
} }
return NetworkGraph.getBannerPicture( return net.yacy.peers.graphics.Banner.getBannerPicture(data, 1000);
1000,
width,
height,
bgcolor,
textcolor,
bordercolor,
name,
links,
words,
type,
myppm,
network,
peers,
nlinks,
nwords,
nqph,
nppm);
} }
} }

@ -0,0 +1,178 @@
package net.yacy.peers.graphics;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import net.yacy.visualization.PrintTool;
import net.yacy.visualization.RasterPlotter;
/**
* Creates banner which displays dtat about peer and network.
*/
public final class Banner {
/** Private constructor to avoid instantiation of utility class. */
private Banner() { }
/** Always use dot as decimal separator since
* banner text is always in English.
*/
private static final DecimalFormatSymbols DFS =
new DecimalFormatSymbols();
static {
DFS.setDecimalSeparator('.');
}
private static RasterPlotter bannerPicture = null;
private static BufferedImage logo = null;
private static long bannerPictureDate = 0;
/**
* Creates new banner image if max age has been reached,
* else returns cached version.
* @param data data to display
* @param maxAge age in ms since 01.01.1970
* @return banner image
*/
public static RasterPlotter getBannerPicture(
final BannerData data, final long maxAge) {
if ((bannerPicture == null)
|| ((System.currentTimeMillis() - bannerPictureDate) > maxAge)) {
drawBannerPicture(data, logo);
}
return bannerPicture;
}
/**
* Creates new banner image if max age has been reached,
* else returns cached version.
* @param data data to display
* @param maxAge age in ms since 01.01.1970
* @param newLogo logo to display
* @return banner image
*/
public static RasterPlotter getBannerPicture(
final BannerData data,
final long maxAge,
final BufferedImage newLogo) {
if ((bannerPicture == null)
|| ((System.currentTimeMillis() - bannerPictureDate) > maxAge)) {
drawBannerPicture(data, newLogo);
}
return bannerPicture;
}
private static void drawBannerPicture(
final BannerData data, final BufferedImage newLogo) {
final int exprlength = 19;
logo = newLogo;
bannerPicture =
new RasterPlotter(
data.getWidth(),
data.getHeight(),
RasterPlotter.DrawMode.MODE_REPLACE,
data.getBgcolor());
final int height = data.getHeight();
final int width = data.getWidth();
// draw description
bannerPicture.setColor(data.getTextcolor());
PrintTool.print(bannerPicture, 100, 12, 0, "PEER: " + addTrailingBlanks(data.getName(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 22, 0, "LINKS: " + addBlanksAndDots(data.getLinks(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 32, 0, "WORDS: " + addBlanksAndDots(data.getWords(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 42, 0, "TYPE: " + addTrailingBlanks(data.getType(), exprlength), -1);
PrintTool.print(bannerPicture, 100, 52, 0, "SPEED: " + addTrailingBlanks(data.getPpm() + " PAGES/MINUTE", exprlength), -1);
PrintTool.print(bannerPicture, 285, 12, 0, "NETWORK: " + addTrailingBlanks(data.getNetwork() + " [" + data.getPeers() + "]", exprlength), -1);
PrintTool.print(bannerPicture, 285, 22, 0, "LINKS: " + addBlanksAndDots(data.getNlinks(), exprlength), -1);
PrintTool.print(bannerPicture, 285, 32, 0, "WORDS: " + addBlanksAndDots(data.getNwords(), exprlength), -1);
PrintTool.print(bannerPicture, 285, 42, 0, "QUERIES: " + addTrailingBlanks(formatQpm(data.getNqph()) + " QUERIES/HOUR", exprlength), -1);
PrintTool.print(bannerPicture, 285, 52, 0, "SPEED: " + addTrailingBlanks(data.getNppm() + " PAGES/MINUTE", exprlength), -1);
if (logo != null) {
final int x = (100 / 2 - logo.getWidth() / 2);
final int y = (height / 2 - logo.getHeight() / 2);
bannerPicture.insertBitmap(logo, x, y, 0, 0, RasterPlotter.FilterMode.FILTER_ANTIALIASING);
}
final String bordercolor = data.getBordercolor();
if (bordercolor != null && !bordercolor.isEmpty()) {
bannerPicture.setColor(bordercolor);
bannerPicture.line(0, 0, 0, height - 1, 100);
bannerPicture.line(0, 0, width - 1, 0, 100);
bannerPicture.line(width - 1, 0, width - 1, height - 1, 100);
bannerPicture.line(0, height - 1, width - 1, height - 1, 100);
}
// set timestamp
bannerPictureDate = System.currentTimeMillis();
}
private static String addBlanksAndDots(final long input, final int length) {
return addBlanksAndDots(Long.toString(input), length);
}
private static String addBlanksAndDots(String input, final int length) {
input = addDots(input);
input = addTrailingBlanks(input,length);
return input;
}
private static String addDots(String word) {
String tmp = "";
int len = word.length();
if (len > 3) {
while (len > 3) {
if (tmp.equals("")) {
tmp = word.substring(len - 3,len);
} else {
tmp = word.substring(len - 3,len) + "." + tmp;
}
word = word.substring(0,len - 3);
len = word.length();
}
word = word + "." + tmp;
}
return word;
}
private static String addTrailingBlanks(String word, int length) {
if (length > word.length()) {
String blanks = "";
length = length - word.length();
int i = 0;
while (i++ < length) {
blanks += " ";
}
word = blanks + word;
}
return word;
}
private static String formatQpm(final double qpm) {
final String ret;
if (qpm < 10) {
ret = new DecimalFormat("0.00", DFS).format(qpm);
} else {
ret = Long.toString(Math.round(qpm));
}
return ret;
}
/**
* Tells if a logo has been set.
* @return true if logo has been set, else false
*/
public static boolean logoIsLoaded() {
if (logo == null) {
return false;
}
return true;
}
}

@ -0,0 +1,122 @@
package net.yacy.peers.graphics;
public class BannerData {
private final int width;
private final int height;
private final String bgcolor;
private final String textcolor;
private final String bordercolor;
private final String name;
private final long links;
private final long words;
private final String type;
private final int ppm;
private final String network;
private final int peers;
private final long nlinks;
private final long nwords;
private final double nqph;
private final long nppm;
public BannerData(
final int width,
final int height,
final String bgcolor,
final String textcolor,
final String bordercolor,
final String name,
final long links,
final long words,
final String type,
final int ppm,
final String network,
final int peers,
final long nlinks,
final long nwords,
final double nqph,
final long nppm
) {
this.width = width;
this.height = height;
this.bgcolor = bgcolor;
this.textcolor = textcolor;
this.bordercolor = bordercolor;
this.name = name;
this.links = links;
this.words = words;
this.type = type;
this.ppm = ppm;
this.network = network;
this.peers = peers;
this.nlinks = nlinks;
this.nwords = nwords;
this.nqph = nqph;
this.nppm = nppm;
}
public final int getWidth() {
return width;
}
public final int getHeight() {
return height;
}
public final String getBgcolor() {
return bgcolor;
}
public final String getTextcolor() {
return textcolor;
}
public final String getBordercolor() {
return bordercolor;
}
public final String getName() {
return name;
}
public final long getLinks() {
return links;
}
public final long getWords() {
return words;
}
public final String getType() {
return type;
}
public final int getPpm() {
return ppm;
}
public final String getNetwork() {
return network;
}
public final int getPeers() {
return peers;
}
public final long getNlinks() {
return nlinks;
}
public final long getNwords() {
return nwords;
}
public final double getNqph() {
return nqph;
}
public final long getNppm() {
return nppm;
}
}

@ -80,6 +80,9 @@ public class NetworkGraph {
private static final String COL_NORMAL_TEXT = "000000"; private static final String COL_NORMAL_TEXT = "000000";
private static final String COL_LOAD_BG = "F7F7F7"; private static final String COL_LOAD_BG = "F7F7F7";
/** Private constructor to avoid instantiation of utility class. */
private NetworkGraph() { }
public static class CircleThreadPiece { public static class CircleThreadPiece {
private final String pieceName; private final String pieceName;
private final Color color; private final Color color;
@ -113,10 +116,6 @@ public class NetworkGraph {
private static BufferedImage peerloadPicture = null; private static BufferedImage peerloadPicture = null;
private static long peerloadPictureDate = 0; private static long peerloadPictureDate = 0;
private static RasterPlotter bannerPicture = null;
private static BufferedImage logo = null;
private static long bannerPictureDate = 0;
public static RasterPlotter getSearchEventPicture(final SeedDB seedDB, final String eventID, final int coronaangle, final int cyc) { public static RasterPlotter getSearchEventPicture(final SeedDB seedDB, final String eventID, final int coronaangle, final int cyc) {
final SearchEvent event = SearchEventCache.getEvent(eventID); final SearchEvent event = SearchEventCache.getEvent(eventID);
if (event == null) return null; if (event == null) return null;
@ -459,104 +458,4 @@ public class NetworkGraph {
g.drawChars(caption.toCharArray(), 0, caption.length(), x+LEGEND_BOX_SIZE+5,y); g.drawChars(caption.toCharArray(), 0, caption.length(), x+LEGEND_BOX_SIZE+5,y);
} }
public static RasterPlotter getBannerPicture(final long maxAge, final int width, final int height, final String bgcolor, final String textcolor, final String bordercolor, final String name, final long links, final long words, final String type, final int ppm, final String network, final int peers, final long nlinks, final long nwords, final double nqph, final long nppm) {
if ((bannerPicture == null) || ((System.currentTimeMillis() - bannerPictureDate) > maxAge)) {
drawBannerPicture(width, height, bgcolor, textcolor, bordercolor, name, links, words, type, ppm, network, peers, nlinks, nwords, nqph, nppm, logo);
}
return bannerPicture;
}
public static RasterPlotter getBannerPicture(final long maxAge, final int width, final int height, final String bgcolor, final String textcolor, final String bordercolor, final String name, final long links, final long words, final String type, final int ppm, final String network, final int peers, final long nlinks, final long nwords, final double nqph, final long nppm, final BufferedImage newLogo) {
if ((bannerPicture == null) || ((System.currentTimeMillis() - bannerPictureDate) > maxAge)) {
drawBannerPicture(width, height, bgcolor, textcolor, bordercolor, name, links, words, type, ppm, network, peers, nlinks, nwords, nqph, nppm, newLogo);
}
return bannerPicture;
}
private static void drawBannerPicture(final int width, final int height, final String bgcolor, final String textcolor, final String bordercolor, final String name, final long links, final long words, final String type, final int ppm, final String network, final int peers, final long nlinks, final long nwords, final double nqph, final long nppm, final BufferedImage newLogo) {
final int exprlength = 19;
logo = newLogo;
bannerPicture = new RasterPlotter(width, height, RasterPlotter.DrawMode.MODE_REPLACE, bgcolor);
// draw description
bannerPicture.setColor(textcolor);
PrintTool.print(bannerPicture, 100, 12, 0, "PEER: " + addTrailingBlanks(name, exprlength), -1);
PrintTool.print(bannerPicture, 100, 22, 0, "LINKS: " + addBlanksAndDots(links, exprlength), -1);
PrintTool.print(bannerPicture, 100, 32, 0, "WORDS: " + addBlanksAndDots(words, exprlength), -1);
PrintTool.print(bannerPicture, 100, 42, 0, "TYPE: " + addTrailingBlanks(type, exprlength), -1);
PrintTool.print(bannerPicture, 100, 52, 0, "SPEED: " + addTrailingBlanks(ppm + " PAGES/MINUTE", exprlength), -1);
PrintTool.print(bannerPicture, 285, 12, 0, "NETWORK: " + addTrailingBlanks(network + " [" + peers + "]", exprlength), -1);
PrintTool.print(bannerPicture, 285, 22, 0, "LINKS: " + addBlanksAndDots(nlinks, exprlength), -1);
PrintTool.print(bannerPicture, 285, 32, 0, "WORDS: " + addBlanksAndDots(nwords, exprlength), -1);
PrintTool.print(bannerPicture, 285, 42, 0, "QUERIES: " + addTrailingBlanks(nqph + " QUERIES/HOUR", exprlength), -1);
PrintTool.print(bannerPicture, 285, 52, 0, "SPEED: " + addTrailingBlanks(nppm + " PAGES/MINUTE", exprlength), -1);
if (logo != null) {
final int x = (100/2 - logo.getWidth()/2);
final int y = (height/2 - logo.getHeight()/2);
bannerPicture.insertBitmap(logo, x, y, 0, 0, RasterPlotter.FilterMode.FILTER_ANTIALIASING);
}
if (!bordercolor.equals("")) {
bannerPicture.setColor(bordercolor);
bannerPicture.line(0, 0, 0, height-1, 100);
bannerPicture.line(0, 0, width-1, 0, 100);
bannerPicture.line(width-1, 0, width-1, height-1, 100);
bannerPicture.line(0, height-1, width-1, height-1, 100);
}
// set timestamp
bannerPictureDate = System.currentTimeMillis();
}
public static boolean logoIsLoaded() {
if (logo == null) {
return false;
}
return true;
}
private static String addBlanksAndDots(final long input, final int length) {
return addBlanksAndDots(Long.toString(input), length);
}
private static String addBlanksAndDots(String input, final int length) {
input = addDots(input);
input = addTrailingBlanks(input,length);
return input;
}
private static String addDots(String word) {
String tmp = "";
int len = word.length();
if (len > 3) {
while(len > 3) {
if(tmp.equals("")) {
tmp = word.substring(len-3,len);
} else {
tmp = word.substring(len-3,len) + "." + tmp;
}
word = word.substring(0,len-3);
len = word.length();
}
word = word + "." + tmp;
}
return word;
}
private static String addTrailingBlanks(String word, int length) {
if (length > word.length()) {
String blanks = "";
length = length - word.length();
int i = 0;
while(i++ < length) {
blanks += " ";
}
word = blanks + word;
}
return word;
}
} }
Loading…
Cancel
Save