2

i'm writing a backbone.js 'app', and would like to map the json output of graphite/carbon directly to some backbone models/collections.

in general, the json output is something like this:

[
  {
    'target': 'some.string.1',
    'datapoints': [ [ val1, timestamp1 ], [ val2, timestamp2 ]... ]
  },
  {
    'target': 'some.string.2',
    'datapoints': [ [ val1, timestamp1 ], [ val2, timestamp2 ]... ]
  },
  ...
]

i have defined a simple model and collection such that:

class Measurement extends Backbone.Model
  defaults:
    id: undefined
    val: undefined

class Measurements extends Backbone.Collection
  model: Measurement
    initialize: (model, options) ->
      if options.metric
        @metric = options.metric
    url: -> 
      '/?target=' + @metric
    parse: (data,xhr) ->
      if _.size(data) == 1
        return _(data[0]['datapoints']).map( (d) ->
          m = {}
          m['id'] = new Date(0)
          m['id'].setUTCSeconds( d[1] )
          m['val'] = d[0]
          m
        )
      undefined

as you can see, i overload the id to be the timestamp of each Measurement and all measurements for a specific 'metric' is stored under a Collection called Measurements.

i've also hardcoded it so it really only works for one Measurements Collection (ie the 'target' in the json).

my question concerns how best/elegant/flexible to implement the gathering of multiple Measurements (the collection) in a single call. ie graphite supports the use of wildcards for its 'targets' so that an ajax request to /?target=some.string.* would bring back all matching targets and datapoints (like in the json example). i would then present this to a View where i would render say cumulative data or plot all of the Measurements against time.

i was thinking of using another Collection (lets call it a Set) that would contain many Measurements. I would like to be able to do something like Set.fetch() to get all the matching Measurements from the server and have the Set create many Measurements Collections from the single ajax request.

does anyone have any suggestions of how to implement this? or even a better way of representing this model/collection layer?

Machavity
  • 30,841
  • 27
  • 92
  • 100
yee379
  • 6,498
  • 10
  • 56
  • 101

1 Answers1

1

whilst not directly answering your question - have you looked at graphene? - it's a javascript dashboard for graphite and it's using backbone.js. You might see how it was implemented there and borrow/steal some ideas.

As a side note - from my experience building a different beast (giraffe, sorry for the plug, but it's important to give context), there are some difficulties matching wildcard targets with graphite. The main reason is that you don't know how many targets are returned. This can be tricky when, for example, you want one of those targets to have a red colour on the graph, or want to add a target like an annotator (to draw as infinite to mark a time-based event). Just something to bear in mind when working with wildcards and multi-target requests with graphite.

gingerlime
  • 5,206
  • 4
  • 37
  • 66