<commit_before>define (require) ->
  _ = require 'underscore'
  $ = require 'jquery'
  bacon = require 'baconjs'
  Q = require 'q'
  URI = require 'URIjs'
  moment = require 'moment'
  React = require 'react_abuse'
  dsl = require 'dsl'
  modules = require 'modules'
  function_names = require 'functions'
  http = require 'http'
  docs = require 'graphite_docs'
  parser = require 'graphite_parser'
  builtins = require 'builtins'
  Html = require 'html'
  Documentation = require 'documentation'

  graphite = modules.create 'graphite', ({fn, component_fn, cmd, settings}) ->
    build_function_doc = (ctx, doc) ->
      FunctionDocsComponent {ctx, docs: docs.function_docs[doc.function_name]}

    build_parameter_doc = (ctx, doc) ->
      ParameterDocsComponent {ctx, docs: docs.parameter_docs[doc.parameter_name]}

    _.each docs.function_docs, (d, n) ->
      Documentation.register_documentation ['graphite_functions', n], function_name: n, summary: d.signature, complete: build_function_doc

    _.each docs.parameter_docs, (d, n) ->
      Documentation.register_documentation ['graphite_parameters', n], parameter_name: n, summary: 'A Graphite parameter', complete: build_parameter_doc

    Documentation.register_documentation 'graphite_functions', index: true
    Documentation.register_documentation 'graphite_parameters', index: true

    args_to_params = (context, args) ->
      graphite.args_to_params {args, default_options: context.options()}

    default_target_command = 'img'

    fn 'q', 'Escapes a Graphite metric query', (targets...) ->
      for t in targets
        unless _.isString t
          throw new TypeError "#{t} is not a string"
      @value new dsl.type.q targets.map(String)...

    FunctionDocsComponent = React.createClass
      render: ->
        React.DOM.div {}, [
          React.DOM.div dangerouslySetInnerHTML: __html: @props.docs.docs
          _.map @props.docs.examples, (example) =>
            builtins.ExampleComponent ctx: @props.ctx, value: "#{default_target_command} #{JSON.stringify example}", run: false
        ]

    ParameterDocsComponent = React.createClass
      render: -> React.DOM.div()
      componentDidMount: (node) ->
        ctx = @props.ctx
        # TODO
        $docs = $(node).append @props.docs
        $docs.find('a').on 'click', (e) ->
          e.preventDefault()
          href = $(this).attr 'href'
          if href[0] is '#'
            ctx.run "docs '#{decodeURI href[1..]}'"

    cmd 'docs', 'Shows the documentation for a Graphite function or parameter', (name) ->
      if name?
        name = name.to_js_string() if name.to_js_string?
        name = name._lead_context_fn?.name if name._lead_op?
        function_docs = docs.function_docs[name]
        if function_docs?
          @help "graphite_functions.#{name}"
        name = docs.parameter_doc_ids[name] ? name
        parameter_docs = docs.parameter_docs[name]
        if parameter_docs?
          @help "graphite_parameters.#{name}"
        unless function_docs? or parameter_docs?
          @text 'Documentation not found'
      else
        @add_component React.DOM.h3 {}, 'Functions'
        @help 'graphite_functions'

        @add_component React.DOM.h3 {}, 'Parameters'
        @help 'graphite_parameters'

    fn 'params', 'Generates the parameters for a Graphite render call', (args...) ->
      result = args_to_params @, args
      @value result

    component_fn 'url', 'Generates a URL for a Graphite image', (args...) ->
      params = args_to_params @, args
      url = graphite.render_url params
      React.DOM.pre {}, React.DOM.a {href: url, target: 'blank'}, url

    fn 'img', 'Renders a Graphite graph image', (args...) ->
      params = args_to_params @, args
      url = graphite.render_url params
      @async ->
        # TODO move this to react once it supports onload and onerror
        # https://github.com/facebook/react/pull/774
        $img = $ "<img src='#{url}'/>"
        @div $img
        deferred = Q.defer()
        $img.on 'load', deferred.resolve
        $img.on 'error', deferred.reject

        promise = deferred.promise
        promise.fail (args...) =>
          @error 'Failed to load image'
        promise

    TimeSeriesTable = React.createClass
      render: ->
        React.DOM.table {}, _.map @props.datapoints, ([value, timestamp]) ->
          time = moment(timestamp * 1000)
          React.DOM.tr {}, [
            React.DOM.th {}, time.format 'MMMM Do YYYY, h:mm:ss a'
            React.DOM.td {className: 'cm-number number'}, value?.toFixed(3) or '(none)'
          ]
<commit_msg>#140: document some graphite functions<commit_after>define (require) ->
  _ = require 'underscore'
  $ = require 'jquery'
  bacon = require 'baconjs'
  Q = require 'q'
  URI = require 'URIjs'
  moment = require 'moment'
  React = require 'react_abuse'
  dsl = require 'dsl'
  modules = require 'modules'
  function_names = require 'functions'
  http = require 'http'
  docs = require 'graphite_docs'
  parser = require 'graphite_parser'
  builtins = require 'builtins'
  Html = require 'html'
  Documentation = require 'documentation'

  graphite = modules.create 'graphite', ({fn, component_fn, cmd, settings, doc}) ->
    build_function_doc = (ctx, doc) ->
      FunctionDocsComponent {ctx, docs: docs.function_docs[doc.function_name]}

    build_parameter_doc = (ctx, doc) ->
      ParameterDocsComponent {ctx, docs: docs.parameter_docs[doc.parameter_name]}

    _.each docs.function_docs, (d, n) ->
      Documentation.register_documentation ['graphite_functions', n], function_name: n, summary: d.signature, complete: build_function_doc

    _.each docs.parameter_docs, (d, n) ->
      Documentation.register_documentation ['graphite_parameters', n], parameter_name: n, summary: 'A Graphite parameter', complete: build_parameter_doc

    Documentation.register_documentation 'graphite_functions', index: true
    Documentation.register_documentation 'graphite_parameters', index: true

    args_to_params = (context, args) ->
      graphite.args_to_params {args, default_options: context.options()}

    default_target_command = 'img'

    doc 'q',
      'Escapes a metric query'
      """
      Use `q` to reference a metric name in the DSL.

      The Graphite API uses unquoted strings to specify metric names and patterns.
      The string argument to `q` will be passed directly to the API.

      For example, `sumSeries(q('twitter.*.tweetcount'))` will be sent as `sumSeries(twitter.*.tweetcount)`.
      """

    fn 'q', (targets...) ->
      for t in targets
        unless _.isString t
          throw new TypeError "#{t} is not a string"
      @value new dsl.type.q targets.map(String)...

    FunctionDocsComponent = React.createClass
      render: ->
        React.DOM.div {}, [
          React.DOM.div dangerouslySetInnerHTML: __html: @props.docs.docs
          _.map @props.docs.examples, (example) =>
            builtins.ExampleComponent ctx: @props.ctx, value: "#{default_target_command} #{JSON.stringify example}", run: false
        ]

    ParameterDocsComponent = React.createClass
      render: -> React.DOM.div()
      componentDidMount: (node) ->
        ctx = @props.ctx
        # TODO
        $docs = $(node).append @props.docs
        $docs.find('a').on 'click', (e) ->
          e.preventDefault()
          href = $(this).attr 'href'
          if href[0] is '#'
            ctx.run "docs '#{decodeURI href[1..]}'"

    cmd 'docs', 'Shows the documentation for a Graphite function or parameter', (name) ->
      if name?
        name = name.to_js_string() if name.to_js_string?
        name = name._lead_context_fn?.name if name._lead_op?
        function_docs = docs.function_docs[name]
        if function_docs?
          @help "graphite_functions.#{name}"
        name = docs.parameter_doc_ids[name] ? name
        parameter_docs = docs.parameter_docs[name]
        if parameter_docs?
          @help "graphite_parameters.#{name}"
        unless function_docs? or parameter_docs?
          @text 'Documentation not found'
      else
        @add_component React.DOM.h3 {}, 'Functions'
        @help 'graphite_functions'

        @add_component React.DOM.h3 {}, 'Parameters'
        @help 'graphite_parameters'

    doc 'params',
      'Generates the parameters for a render API call'
      '''
      `params` interprets its arguments in the same way as [`get_data`](help:graphite.get_data),
      but simply returns the arguments that would be passed to the API.

      For example:
      ```
      options areaMode: 'stacked'
      object params sumSeries(q('twitter.*.tweetcount')), width: 1024, height: 768
      ```
      '''

    fn 'params', (args...) ->
      result = args_to_params @, args
      @value result

    component_fn 'url', 'Generates a URL for a graph image', (args...) ->
      params = args_to_params @, args
      url = graphite.render_url params
      React.DOM.pre {}, React.DOM.a {href: url, target: 'blank'}, url

    fn 'img', 'Renders a graph image', (args...) ->
      params = args_to_params @, args
      url = graphite.render_url params
      @async ->
        # TODO move this to react once it supports onload and onerror
        # https://github.com/facebook/react/pull/774
        $img = $ "<img src='#{url}'/>"
        @div $img
        deferred = Q.defer()
        $img.on 'load', deferred.resolve
        $img.on 'error', deferred.reject

        promise = deferred.promise
        promise.fail (args...) =>
          @error 'Failed to load image'
        promise

    TimeSeriesTable = React.createClass
      render: ->
        React.DOM.table {}, _.map @props.datapoints, ([value, timestamp]) ->
          time = moment(timestamp * 1000)
          React.DOM.tr {}, [
            React.DOM.th {}, time.format 'MMMM Do YYYY, h:mm:ss a'
            React.DOM.td {className: 'cm-number number'}, value?.toFixed(3) or '(none)'
          ]

{'fraction_non_alphanumeric': 0.12397696271597454, 'fraction_numerical': 0.003435384459937355, 'mean_word_length': 2.6864059590316574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '468603', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2918, 'n_words': 799}