- added a servlet api/linkstructure.json which generates a link graph information in json - added a javascript link graph renderer hypertree.js using d3 and the new servlet linkstructure.json - embedded the new link graph in the crawler monitor and the host browserpull/1/head
parent
74ab094587
commit
a6bb9be97e
@ -0,0 +1,172 @@
|
||||
// linkstructure.java
|
||||
// ------------
|
||||
// (C) 2014 by Michael Peter Christen; mc@yacy.net
|
||||
// first published 02.04.2014 on http://yacy.net
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import org.apache.solr.common.SolrDocument;
|
||||
|
||||
import net.yacy.cora.document.encoding.ASCII;
|
||||
import net.yacy.cora.document.id.DigestURL;
|
||||
import net.yacy.cora.federate.solr.FailType;
|
||||
import net.yacy.cora.federate.solr.connector.AbstractSolrConnector;
|
||||
import net.yacy.cora.order.Base64Order;
|
||||
import net.yacy.cora.protocol.HeaderFramework;
|
||||
import net.yacy.cora.protocol.RequestHeader;
|
||||
import net.yacy.cora.protocol.ResponseHeader;
|
||||
import net.yacy.kelondro.data.meta.URIMetadataNode;
|
||||
import net.yacy.search.Switchboard;
|
||||
import net.yacy.search.index.Fulltext;
|
||||
import net.yacy.search.schema.CollectionSchema;
|
||||
import net.yacy.server.serverObjects;
|
||||
import net.yacy.server.serverSwitch;
|
||||
import net.yacy.server.servletProperties;
|
||||
|
||||
public class linkstructure {
|
||||
|
||||
public static enum Edgetype {
|
||||
InboundOk, InboundNofollow, Outbound, Dead;
|
||||
}
|
||||
|
||||
public static class Edge {
|
||||
public DigestURL source, target;
|
||||
public Edgetype type;
|
||||
public Edge(DigestURL source, DigestURL target, Edgetype type) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) {
|
||||
final servletProperties prop = new servletProperties();
|
||||
|
||||
final String ext = header.get("EXT", "");
|
||||
//final boolean json = ext.equals("json");
|
||||
final boolean xml = ext.equals("xml");
|
||||
|
||||
final Switchboard sb = (Switchboard) env;
|
||||
Fulltext fulltext = sb.index.fulltext();
|
||||
if (post == null) return prop;
|
||||
String about = post.get("about", null); // may be a URL, a URL hash or a domain hash
|
||||
if (about == null) return prop;
|
||||
boolean authenticated = sb.adminAuthenticated(header) >= 2;
|
||||
int maxtime = Math.min(post.getInt("maxtime", 1000), authenticated ? 60000 : 1000);
|
||||
int maxnodes = Math.min(post.getInt("maxnodes", 100), authenticated ? 1000 : 100);
|
||||
|
||||
DigestURL url = null;
|
||||
String hostname = null;
|
||||
if (about.length() == 12 && Base64Order.enhancedCoder.wellformed(ASCII.getBytes(about))) {
|
||||
byte[] urlhash = ASCII.getBytes(about);
|
||||
url = authenticated ? sb.getURL(urlhash) : null;
|
||||
} else if (url == null && about.length() > 0) {
|
||||
// consider "about" as url or hostname
|
||||
try {
|
||||
url = new DigestURL(about.indexOf("://") >= 0 ? about : "http://" + about); // accept also domains
|
||||
hostname = url.getHost();
|
||||
if (hostname.startsWith("www.")) hostname = hostname.substring(4);
|
||||
} catch (final MalformedURLException e) {
|
||||
}
|
||||
}
|
||||
if (hostname == null) return prop;
|
||||
|
||||
// now collect _all_ documents inside the domain until a timeout appears
|
||||
StringBuilder q = new StringBuilder();
|
||||
q.append(CollectionSchema.host_s.getSolrFieldName()).append(':').append(hostname).append(" OR ").append(CollectionSchema.host_s.getSolrFieldName()).append(':').append("www.").append(hostname);
|
||||
BlockingQueue<SolrDocument> docs = fulltext.getDefaultConnector().concurrentDocumentsByQuery(q.toString(), 0, maxnodes, maxtime, 100, 1,
|
||||
CollectionSchema.id.getSolrFieldName(),
|
||||
CollectionSchema.sku.getSolrFieldName(),
|
||||
CollectionSchema.failreason_s.getSolrFieldName(),
|
||||
CollectionSchema.failtype_s.getSolrFieldName(),
|
||||
CollectionSchema.inboundlinks_protocol_sxt.getSolrFieldName(),
|
||||
CollectionSchema.inboundlinks_urlstub_sxt.getSolrFieldName(),
|
||||
CollectionSchema.outboundlinks_protocol_sxt.getSolrFieldName(),
|
||||
CollectionSchema.outboundlinks_urlstub_sxt.getSolrFieldName()
|
||||
);
|
||||
SolrDocument doc;
|
||||
Map<String, FailType> errorDocs = new HashMap<String, FailType>();
|
||||
Map<String, Edge> edges = new HashMap<String, Edge>();
|
||||
try {
|
||||
while ((doc = docs.take()) != AbstractSolrConnector.POISON_DOCUMENT) {
|
||||
String u = (String) doc.getFieldValue(CollectionSchema.sku.getSolrFieldName());
|
||||
String ids = (String) doc.getFieldValue(CollectionSchema.id.getSolrFieldName());
|
||||
DigestURL from = new DigestURL(u, ASCII.getBytes(ids));
|
||||
String errortype = (String) doc.getFieldValue(CollectionSchema.failtype_s.getSolrFieldName());
|
||||
FailType error = errortype == null ? null : FailType.valueOf(errortype);
|
||||
if (error != null) {
|
||||
errorDocs.put(u, error);
|
||||
} else {
|
||||
Iterator<String> links = URIMetadataNode.getLinks(doc, true); // inbound
|
||||
String link;
|
||||
while (links.hasNext()) {
|
||||
link = links.next();
|
||||
try {
|
||||
DigestURL linkurl = new DigestURL(link, null);
|
||||
String edgehash = ids + ASCII.String(linkurl.hash());
|
||||
edges.put(edgehash, new Edge(from, linkurl, Edgetype.InboundOk));
|
||||
} catch (MalformedURLException e) {}
|
||||
}
|
||||
links = URIMetadataNode.getLinks(doc, false); // outbound
|
||||
while (links.hasNext()) {
|
||||
link = links.next();
|
||||
try {
|
||||
DigestURL linkurl = new DigestURL(link, null);
|
||||
String edgehash = ids + ASCII.String(linkurl.hash());
|
||||
edges.put(edgehash, new Edge(from, linkurl, Edgetype.Outbound));
|
||||
} catch (MalformedURLException e) {}
|
||||
}
|
||||
}
|
||||
if (edges.size() > maxnodes) break;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
} catch (MalformedURLException e) {
|
||||
}
|
||||
// we use the errorDocs to mark all edges with endpoint to error documents
|
||||
for (Map.Entry<String, Edge> edge: edges.entrySet()) {
|
||||
if (errorDocs.containsKey(edge.getValue().target)) edge.getValue().type = Edgetype.Dead;
|
||||
}
|
||||
|
||||
// finally just write out the edge array
|
||||
int c = 0;
|
||||
for (Map.Entry<String, Edge> edge: edges.entrySet()) {
|
||||
prop.putJSON("list_" + c + "_source", edge.getValue().source.getPath());
|
||||
prop.putJSON("list_" + c + "_target", edge.getValue().type.equals(Edgetype.Outbound) ? edge.getValue().target.toNormalform(true) : edge.getValue().target.getPath());
|
||||
prop.putJSON("list_" + c + "_type", edge.getValue().type.name());
|
||||
prop.put("list_" + c + "_eol", 1);
|
||||
c++;
|
||||
}
|
||||
prop.put("list_" + (c-1) + "_eol", 0);
|
||||
prop.put("list", c);
|
||||
|
||||
// Adding CORS Access header for xml output
|
||||
if (xml) {
|
||||
final ResponseHeader outgoingHeader = new ResponseHeader(200);
|
||||
outgoingHeader.put(HeaderFramework.CORS_ALLOW_ORIGIN, "*");
|
||||
prop.setOutgoingHeader(outgoingHeader);
|
||||
}
|
||||
|
||||
// return rewrite properties
|
||||
return prop;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
[
|
||||
#{list}#
|
||||
{"source":"#[source]#", "target":"#[target]#", "type":"#[type]#"}#(eol)#::,#(/eol)#
|
||||
#{/list}#
|
||||
]
|
@ -0,0 +1,37 @@
|
||||
|
||||
#Outbound {
|
||||
fill: lightblue;
|
||||
}
|
||||
#Dead {
|
||||
fill: red;
|
||||
}
|
||||
#InboundOk {
|
||||
fill: green;
|
||||
}
|
||||
.hypertree {
|
||||
border: solid;
|
||||
}
|
||||
.hypertree-link {
|
||||
fill: none;
|
||||
stroke-width: 1.2px;
|
||||
}
|
||||
.hypertree-link.Outbound {
|
||||
stroke: lightblue;
|
||||
}
|
||||
.hypertree-link.Dead {
|
||||
stroke: red;
|
||||
stroke-dasharray: 0,2 1;
|
||||
}
|
||||
.hypertree-link.InboundOk {
|
||||
stroke: green;
|
||||
}
|
||||
circle {
|
||||
fill: lightgreen;
|
||||
stroke: darkgreen;
|
||||
stroke-width: 1.5px;
|
||||
}
|
||||
text {
|
||||
font: 9px sans-serif;
|
||||
pointer-events: none;
|
||||
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,48 @@
|
||||
function linkstructure(hostname, element, width, height, maxtime, maxnodes) {
|
||||
var nodes = {};
|
||||
var links = [];
|
||||
$.getJSON("/api/linkstructure.json?about=" + hostname + "&maxtime=" + maxtime + "&maxnodes=" + maxnodes, function(links) {
|
||||
links.forEach(function(link) {
|
||||
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
|
||||
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
|
||||
});
|
||||
var force = d3.layout.force().nodes(d3.values(nodes)).links(links).size([width, height]).linkDistance(60).charge(-800).on("tick", tick).start();
|
||||
var svg = d3.select(element).append("svg").attr("id", "hypertree").attr("width", width).attr("height", height);
|
||||
svg.append("defs").selectAll("marker")
|
||||
.data(["Dead", "Outbound", "InboundOk"])
|
||||
.enter().append("marker")
|
||||
.attr("id", function(d) { return d; })
|
||||
.attr("viewBox", "0 -5 10 10")
|
||||
.attr("refX", 15)
|
||||
.attr("refY", -1.5)
|
||||
.attr("markerWidth", 6)
|
||||
.attr("markerHeight", 6)
|
||||
.attr("orient", "auto")
|
||||
.append("path")
|
||||
.attr("d", "M0,-5L10,0L0,5");
|
||||
svg.append("text").attr("x", 10).attr("y", 15).text(hostname).attr("style", "font-size:16px").attr("fill", "black");
|
||||
svg.append("text").attr("x", 10).attr("y", 30).text("Site Link Structure Visualization made with YaCy").attr("style", "font-size:9px").attr("fill", "black");
|
||||
svg.append("text").attr("x", 10).attr("y", 40).text(new Date()).attr("style", "font-size:9px").attr("fill", "black");
|
||||
svg.append("text").attr("x", 10).attr("y", height - 20).text("green: links to same domain").attr("style", "font-size:9px").attr("fill", "green");
|
||||
svg.append("text").attr("x", 10).attr("y", height - 10).text("blue: links to other domains").attr("style", "font-size:9px").attr("fill", "lightblue");
|
||||
svg.append("text").attr("x", 10).attr("y", height).text("red: dead links").attr("style", "font-size:9px").attr("fill", "red");
|
||||
var path = svg.append("g").selectAll("path").data(force.links()).enter().append("path").attr("class",
|
||||
function(d) {return "hypertree-link " + d.type; }).attr("marker-end", function(d) { return "url(#" + d.type + ")";});
|
||||
var circle = svg.append("g").selectAll("circle").data(force.nodes()).enter().append("circle").attr("r", 4).call(force.drag);
|
||||
var text = svg.append("g").selectAll("text").data(force.nodes()).enter().append("text").attr("x", 8).attr("y", ".31em").text(
|
||||
function(d) {return d.name; });
|
||||
function tick() {
|
||||
path.attr("d", linkArc);
|
||||
circle.attr("transform", transform);
|
||||
text.attr("transform", transform);
|
||||
}
|
||||
function linkArc(d) {
|
||||
var dx = d.target.x - d.source.x, dy = d.target.y - d.source.y, dr = Math.sqrt(dx * dx + dy * dy);
|
||||
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
|
||||
}
|
||||
function transform(d) {
|
||||
return "translate(" + d.x + "," + d.y + ")";
|
||||
}
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in new issue