Skip to content

Commit

Permalink
Now with basic api for greasemonkey script
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham Tackley committed Nov 25, 2011
1 parent 36745fc commit a1a3c20
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/scala/bootstrap/liftweb/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.DefaultServlet
import org.eclipse.jetty.webapp.WebAppContext
import scala.PartialFunction
import net.liftweb.common.Box
import dashboard.Api


class Boot {
Expand All @@ -17,6 +20,10 @@ class Boot {

LiftRules.addToPackages("dashboard")

LiftRules.statelessDispatchTable.append {
case Req("api" :: "counts" :: Nil, _, GetRequest) => Api.counts _
}

Backend.start()

LiftRules.unloadHooks.append(Backend.stop _)
Expand Down
22 changes: 22 additions & 0 deletions src/main/scala/dashboard/Api.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dashboard

import comet.TopHitsServer
import net.liftweb.common.Full

import net.liftweb.json._
import net.liftweb.http.{S, JsonResponse}
import net.liftweb.http.js.JE

object Api {
implicit val formats = Serialization.formats(NoTypeHints)

private case class ApiCount(path: String, hitsPerSec: Double)

def counts = {
val cb = S.param("callback").getOrElse("fuck")
val response = TopHitsServer.topStuff.everything.hits
.map{ hit => "http://www.guardian.co.uk" + hit.url -> "%.1f".format(hit.hitsPerSec) }.toMap

Full(JsonResponse(JE.Call(cb, Extraction.decompose(response))))
}
}
2 changes: 1 addition & 1 deletion src/main/scala/dashboard/lib/Calculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Calculator {
val clicksPerPath = clickStream.userClicks.groupBy(_.path).map {
case (k, v) => (k, v, v.size)
}.toList
val topTen = clicksPerPath.sortBy(_._3).reverse.take(50)
val topTen = clicksPerPath.sortBy(_._3).reverse

topTen map {
case (url, hits, hitCount) =>
Expand Down
19 changes: 19 additions & 0 deletions src/main/webapp/gm1.user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ==UserScript==
// @include http://www.guardian.co.uk/*
// @name Show Guardian Link Hits
// @namespace http://www.guardian.co.uk/usage
// @description Show's Live Usage information on all guardian links
// ==/UserScript==

var jQuery = unsafeWindow.jQuery;

jQuery.getJSON("http://gnmfasteragain.int.gnl:8081/api/counts?callback=?", function(result) {
jQuery('.greasy-hits').remove()
jQuery('a.link-text').each(function(i, node) {
console.log(node);
if (result[node.href] != undefined) {
jQuery(node).before("<span class=greasy-hits style='position: absolute; z-index: 9000; background-color: #62CFFC;font-size: 10pt;border-radius: 3px;color: white;padding: 1px 3px 2px; text-indent: 0'>" + result[node.href] +"</span>");
}
});
});

0 comments on commit a1a3c20

Please sign in to comment.