How to get per-partition state for a projection?

I’ve been following Rob Ashton’s projection series (http://docs.geteventstore.com/#rob-ashton’s-projections-series), and I can get pretty far. I’ve been able to write events to the “ponies” stream, and create basic projections from that stream. For example, the following projection (PonyJumpCount) works just fine:

fromStream(“ponies”)
.when({

$init: function() { return { count: 0 } },

“PonyJumped”: function(state, event) { state.count += 1 }
})

Suppose I have two events in the “ponies” stream:

{ EventType: “PonyJumped”, Data: { Pony: “Pinkie Pie”, Distance: 13 } }
{ EventType: “PonyJumped”, Data: { Pony: “Rainbow Dash”, Distance: 13 } }

I can retrieve the state of the projection by a query to “/projection/PonyJumpCount/state”. The result, as you might imagine, is {“count”: 2}. Great.

Ashton goes on to demonstrate that projections can be used to partition streams:

fromStream(‘ponies’)
.whenAny(function(state, ev) { linkTo(‘pony-’ + ev.data.Pony, ev) })

I save this projection as “continuous”, with the “emit-enabled” flag turned on under the name “PonyPartition”. It processes each event, and creates the appropriate streams. I can query the streams “pony-Rainbow%20Dash” and “pony-Pinkie%20Pie” as expected.

But what I can’t get to work are projections that use the “fromCategory” function.

fromCategory(‘pony’)

.foreachStream()

.when({

“$init”: function(state, ev) { return { count: 0 } },

“PonyJumped”: function(state, ev) { state.count++ }

})

I name this projection “JumpingPonies”. The web interface reports that the projection has processed all events, and lists the number of cached partitions as 2. I’m looking to get the state of the projection for each of the streams of the “pony” category. In Ashton’s blog post (http://codeofrob.com/entries/creating-a-projection-per-stream-in-the-eventstore.html), he writes that a query to “/projection/JumpingPonies/state?partition=Rainbow%20Dash” should get me what I’m looking for. When I perform that query, though, I get a 200 OK response, with no content (Content-Length: 0). I would expect instead to see something like {“count”: 1}.

I’ve also seen (on other posts to this mailing list), that projections should emit “state” events to a stream like $projection-JumpingPonies-Rainbow%20Dash-state, but I can’t find any evidence of those streams being created either.

I’m running EventStore 3.0.3 from within a Docker container. The container uses the pre-packaged Linux binary. I’m running event store with the “–run-projections=all” flag, and I’m running all system projections ($by_category, $by_event_type, $stream_by_category, $streams).

"I've also seen (on other posts to this mailing list), that
projections should emit "state" events to a stream like
$projection-JumpingPonies-Rainbow%20Dash-state, but I can't find any
evidence of those streams being created either."

This is for a projection with a single state e.g. fromAll().when({})

For projections with multiple states they are stored differently.

This is however an internal thing and you really should not be depending on it.

Cheers,

Greg

Ok, so even if I can’t read states from streams in this situation, would you expect to be able to read the states from the /projection/ProjectionName/state endpoint with the partition parameter?

I’m actually hitting the same exact issue as Chris here - I’ve been able to get basic projections working just fine, but when trying to get the partitioned states via the following code, it seems to be processing everything from individual streams, and the projection itself exists, but state just doesn’t seem to exist at all. My playerstreams are playerstream-1, 2, and 3, but if I go to partitions 1, 2, or 3, nothing’s coming back at all. Is there something basic I’m doing incorrectly here?

fromCategory(“playerstream”)
.foreachStream()
.when({
$init: function() {
return {adds:0}
},
“addItem”: function(state, event) {
state.adds++
}

})

Aha! Sam Kanai helped me find the answer to this - it’s something that’s no longer correct in Rob’s blog post! What you want to do, in order to get your partitioned state properly, is use the full name of the stream instead of just the part after the dash, that is:

projection/playerdata/state?partition=playerstream-1 instead of projection/playerdata/state?partition=1

Or, in your case:

/projection/JumpingPonies/state?partition=pony-Rainbow%20Dash

I hope this helps anyone else out who was having this issue!

You’re a godsend, Ben. Thanks!