Permalink
Browse files

stuffs graph now works... still now to find how to add links!

  • Loading branch information...
1 parent 080faa2 commit e5aa26ccae8f23782985714d60cc2f135e044299 @andypetrella committed Apr 16, 2012
@@ -1,9 +1,9 @@
class Stuffs extends Spine.Module
- init: (params) ->
+ init: (params) =>
@controller = new Controller(params)
- destroy: () ->
+ destroy: () =>
@controller.destroy()
class Form extends Spine.Controller
@@ -16,7 +16,7 @@ class Stuffs extends Spine.Module
@rendered = true
@
- create: (e)->
+ create: (e)=>
e.preventDefault()
stuff = Stuff.fromForm(e.target)
stuff.save()
@@ -37,8 +37,9 @@ class Stuffs extends Spine.Module
@selectorEl = params.selectorEl
- @nodes = []
- @links = []
+ @data =
+ stuffs: []
+ relations: []
@width = 960
@height = 500
@@ -68,13 +69,11 @@ class Stuffs extends Spine.Module
@restart()
-
-
newAdd:(stuff) =>
stuff.x = Math.random()*(@width-50)+25
stuff.y = Math.random()*(@height-50)+25
- @nodes.push(stuff)
- @links.push({source:@nodes[0], target:stuff})
+ @data.stuffs.push(stuff)
+ @data.relations.push({source:@data.stuffs[0], target:stuff})
@restart()
@@ -89,66 +88,60 @@ class Stuffs extends Spine.Module
.attr("width", @width)
.attr("height", @height)
- @force = d3.layout.force()
- #.charge(-120)
- #.linkDistance(30)
- .size([@width, @height])
- .nodes(@nodes)
- .links(@links)
-
- @force.on("tick", () ->
- @svg.selectAll("line.link")
- .attr("x1", (d) -> d.source.x)
- .attr("y1", (d) -> d.source.y)
- .attr("x2", (d) -> d.target.x)
- .attr("y2", (d) -> d.target.y)
+ window.brole2 = {}
+ window.brole2.svg = @svg
- @svg.selectAll("circle.node")
- .attr("cx", (d) -> d.x)
- .attr("cy", (d) -> d.y)
- )
+ @force = d3.layout.force()
+ #.charge(-120)
+ #.linkDistance(30)
+ .size([@width, @height])
@
+ @
fetched: () =>
- @nodes = Stuff.all().map((i) =>
- i.x = Math.random()*(@width-50)+25
- i.y = Math.random()*(@height-50)+25
- i
- )
- @links = @nodes.map((n) => {source:@nodes[0], target:n})
+ @data.stuffs = Stuff.all()
+ @data.relations = @data.stuffs.map((n) => {source:@data.stuffs[0], target:n})
@restart()
restart: () =>
- @svg.selectAll("line.link")
- .data(@links)
+ window.brole2.links = @svg.selectAll("line.link")
+ .data(@data.relations)
.enter().append("svg:line")
.attr("class", "link")
- .attr("x1", (d) -> d.source.x )
- .attr("y1", (d) -> d.source.y )
- .attr("x2", (d) -> d.target.x )
- .attr("y2", (d) -> d.target.y )
- @svg.selectAll("circle.node")
- .data(@nodes)
+ window.brole2.nodes = @svg.selectAll("circle.node")
+ .data(@data.stuffs)
.enter().append("svg:circle")
.attr("class", "node")
- .attr("cx", (d) -> d.x )
- .attr("cy", (d) -> d.y )
.attr("r", 5)
.call(@force.drag)
+ @force.nodes(@data.stuffs)
+ .links(@data.relations)
+ .on("tick", () ->
+ window.brole2.nodes
+ .attr("cx", (d) -> d.x = Math.max(5, Math.min(960 - 5, d.x)))
+ .attr("cy", (d) -> d.y = Math.max(5, Math.min(500 - 5, d.y)))
+
+ window.brole2.links
+ .attr("x1", (d) -> d.source.x)
+ .attr("y1", (d) -> d.source.y)
+ .attr("x2", (d) -> d.target.x)
+ .attr("y2", (d) -> d.target.y)
+ )
+
@force.start()
+ @
showForm: () =>
if @addForm.rendered
@addForm.el.show()
else
@append(@addForm.render())
-
destroy:() =>
@release()
@@ -49,155 +49,152 @@ object Application extends Controller {
Ok(views.html.index("Your new application is ready."))
}
- def all(implicit neo: Neo4JEndPoint): Promise[Seq[Stuff]] = for (
- r <- neo.root;
- ref <- r.referenceNode;
- c <- r.cypher(JsObject(Seq(
- "query" -> JsString("start init=node({reference}) match init-[:STUFF]->(n) return n"),
- "params" -> JsObject(Seq(
- "reference" -> JsNumber(ref.asInstanceOf[Node].id) // just for test: ".asIn..." directly
- ))
- )))
- ) yield (c match {
- case cr: CypherResult => cr.result map { l =>
- Stuff.fromNode(Node(l.find(_._1 == "n").get._2.asInstanceOf[JsObject]))
+ def allStuffs = Action {
+ implicit request =>
+ Async {
+ Stuff.all map {
+ list => Ok(toJson(list))
+ }
}
- case _ => Nil
- })
-
- def allStuffs = Action { implicit request =>
- Async {
- all map { list => Ok(toJson(list)) }
- }
}
-
+
def createStuff = Action {
implicit request =>
Async {
stuffForm.bindFromRequest.fold(
formWithErrors => Promise.pure(BadRequest("Missing Information to create Stuff")),
- stuff => for (
- r <- neo.root;
- ref <- r.referenceNode;
- s <- r.createNode(Some(Stuff.toNode(stuff)));
- l <- ref.asInstanceOf[Node].createRelationship(
- Relation(
- JsObject(
- Seq(
- "type" -> JsString("STUFF"),
- "end" -> JsString(s.asInstanceOf[Node].self),
- "data" -> JsObject(Seq())
- )))) //quick cast
- ) yield s match {
- case node: Node => {
- val st: Stuff = Stuff.fromNode(node)
- stuffsActor ! NewStuff(st)
- stuffsActor ! IncStuffs(1, st.creation)
- Ok(toJson(st))
- }
- case _ => InternalServerError("Cannot create stuff...")
+ stuff => Stuff.create(stuff) map {
+ st => {
+ stuffsActor ! NewStuff(st)
+ stuffsActor ! IncStuffs(1, st.creation)
+ Ok(toJson(st))
}
+ }
)
}
}
def getStuff(id: Int) = Action {
Async {
- for (
- r <- neo.root;
- n <- r.getNode(id)
- ) yield n match {
- case node: Node => Ok(toJson(Stuff.fromNode(node)))
- case _ => NotFound("Stuff not found")
+ Stuff.get(id) map {
+ _ match {
+ case Some(stuff) => Ok(toJson(stuff))
+ case None => NotFound("Stuff not found")
+ }
}
}
}
- val toEventSource = Enumeratee.map[(Int, Long)] {
- msg =>
- "data: " + stringify(JsObject(Seq("n"->JsNumber(msg._1), "d" ->JsNumber(msg._2)))) + """
-"""
- } //two \n are needed => spec
+
+ def stuffAdd = Action {
+ println("start add stream")
+
+ val uuid: String = BigInt(1000, scala.util.Random).toString(36)
+
+ SimpleResult(
+ header = ResponseHeader(OK, Map(
+ CONTENT_LENGTH -> "-1",
+ CONTENT_TYPE -> "text/event-stream",
+ CONNECTION -> "keep-alive",
+ CACHE_CONTROL -> "no-cache"
+ )), body = addEventStream(uuid) &> toAddEventSource)
+ }
+
+ def stuffsCount = Action {
+ println("start count stream")
+
+ val uuid: String = BigInt(1000, scala.util.Random).toString(36)
+
+ SimpleResult(
+ header = ResponseHeader(OK, Map(
+ CONTENT_LENGTH -> "-1",
+ CONTENT_TYPE -> "text/event-stream",
+ CONNECTION -> "keep-alive",
+ CACHE_CONTROL -> "no-cache"
+ )), body = countEventStream(uuid) &> toEventSource)
+ }
val stuffsActor = Akka.system.actorOf(Props[StuffsActor], name = "stuffsActor")
val masterActor = Akka.system.actorOf(Props[MasterActor], name = "masterStuffsActor")
+ case class NewStuff(s: Stuff) {}
+ case class IncStuffs(n: Int = 0, date: Long) {}
+
class StuffsActor extends Actor {
protected def receive = {
- case ns@NewStuff(s:Stuff) => masterActor ! ns
- case i@IncStuffs(n:Int, d:Long) => masterActor ! i
+ case ns@NewStuff(s: Stuff) => masterActor ! ns
+ case i@IncStuffs(n: Int, d: Long) => masterActor ! i
}
}
+
class MasterActor extends Actor {
-
+
protected def receive = {
- case uuid:String => {
+ case uuid: String => {
Cache.set("uuids", Cache.getOrElse[Seq[String]]("uuids")(Nil).filter(_ != uuid))
- Cache.set(uuid+"."+"count", None)
+ Cache.set(uuid + "." + "count", None)
}
- case (uuid:String, t:String, cs:Pushee[_]) => {
+ case (uuid: String, t: String, cs: Pushee[_]) => {
Cache.set("uuids", uuid +: Cache.getOrElse[Seq[String]]("uuids")(Nil))
- Cache.set(uuid+"."+t, Some(cs))
+ Cache.set(uuid + "." + t, Some(cs))
+ }
+ case NewStuff(s: Stuff) => Cache.getOrElse[Seq[String]]("uuids")(Nil) foreach {
+ e => Cache.getAs[Option[Pushee[Stuff]]](e + "." + "add") map {
+ op => op map {
+ _.push(s)
+ }
+ }
+ }
+ case IncStuffs(n: Int, l: Long) => Cache.getOrElse[Seq[String]]("uuids")(Nil) foreach {
+ e => Cache.getAs[Option[Pushee[(Int, Long)]]](e + "." + "count") map {
+ op => op map {
+ _.push((n, l))
+ }
+ }
}
- case NewStuff(s:Stuff) => Cache.getOrElse[Seq[String]]("uuids")(Nil) foreach { e => Cache.getAs[Option[Pushee[Stuff]]](e+"."+"add") map {op => op map {_.push(s)}}}
- case IncStuffs(n:Int, l:Long) => Cache.getOrElse[Seq[String]]("uuids")(Nil) foreach { e => Cache.getAs[Option[Pushee[(Int, Long)]]](e+"."+"count") map {op => op map {_.push((n,l))}}}
}
}
- def eventStream(uuid:String) = {
+ def countEventStream(uuid: String) = {
println("entering the event stream")
Enumerator.pushee[(Int, Long)](
- { (pushee: Pushee[(Int, Long)]) => masterActor ! (uuid, "count", pushee) },
- { println("completed"); masterActor ! uuid }
+ {
+ (pushee: Pushee[(Int, Long)]) => masterActor !(uuid, "count", pushee)
+ }, {
+ println("completed");
+ masterActor ! uuid
+ }
)
}
- case class NewStuff(s:Stuff){}
- case class IncStuffs(n:Int = 0, date:Long){}
-
- def stuffsCount = Action {
- println("start count stream")
-
- val uuid: String = BigInt(1000, scala.util.Random).toString(36)
+ val toEventSource = Enumeratee.map[(Int, Long)] {
+ msg =>
+ "data: " + stringify(JsObject(Seq("n" -> JsNumber(msg._1), "d" -> JsNumber(msg._2)))) + """
- SimpleResult(
- header = ResponseHeader(OK, Map(
- CONTENT_LENGTH -> "-1",
- CONTENT_TYPE -> "text/event-stream",
- CONNECTION -> "keep-alive",
- CACHE_CONTROL -> "no-cache"
- )), body = eventStream(uuid) &> toEventSource)
+"""
}
val toAddEventSource = Enumeratee.map[Stuff] {
msg =>
- "data: " + stringify(toJson(msg)) + """
+ "data: " + stringify(toJson(msg)) + """
"""
}
- def addEventStream(uuid:String) = {
+
+ def addEventStream(uuid: String) = {
println("entering the add event stream")
Enumerator.pushee[Stuff](
- { (pushee: Pushee[Stuff]) => masterActor ! (uuid, "add", pushee) },
- { println("completed"); masterActor ! uuid }
+ {
+ (pushee: Pushee[Stuff]) => masterActor !(uuid, "add", pushee)
+ }, {
+ println("completed");
+ masterActor ! uuid
+ }
)
}
- def stuffAdd = Action {
- println("start add stream")
-
- val uuid: String = BigInt(1000, scala.util.Random).toString(36)
-
- SimpleResult(
- header = ResponseHeader(OK, Map(
- CONTENT_LENGTH -> "-1",
- CONTENT_TYPE -> "text/event-stream",
- CONNECTION -> "keep-alive",
- CACHE_CONTROL -> "no-cache"
- )), body = addEventStream(uuid) &> toAddEventSource)
- }
}
Oops, something went wrong.

0 comments on commit e5aa26c

Please sign in to comment.