<commit_before>neo4j = require "neo4j"
{race} = require 'sinkrow'

module.exports = class Graph
  constructor:(config)->
    @db = new neo4j.GraphDatabase(config.host + ":" + config.port);

  objectify = (incomingObjects, callback)->
    incomingObjects = [].concat(incomingObjects)
    generatedObjects = []
    for incomingObject in incomingObjects
      generatedObject = {}
      for k of incomingObject
        temp = generatedObject
        parts = k.split "."
        key = parts.pop()
        while parts.length
          part = parts.shift()
          temp = temp[part] = temp[part] or {}
        temp[key] = incomingObject[k]
      generatedObjects.push generatedObject
    callback generatedObjects

  fetchAll:(callback)->
    start = new Date().getTime()
    query = [
      'start koding=node:koding(\'id:*\')'
      'where koding.name = "JTutorial"'
      ' or koding.name = "JCodeSnip"'
      ' or koding.name = "JDiscussion"'
      ' or koding.name = "JStatusUpdate"'
      'return *'
      'order by koding.`meta.createdAt` DESC'
      'limit 20'
    ].join('\n');

    params =
      itemId : "515360d23af2fb6b6b000009"

    @db.query query, params, (err, results)=>
#      console.log results
      tempRes = []
      if err then callback err
      else if results.length is 0 then callback null, []
      else
        collectRelations = race (i, res, fin)=>
          id = res.id

          @fecthRelatedItems id, (err, relatedResult)=>
            if err
              callback err
              fin()
            else
              tempRes[i].relationData =  relatedResult
              fin()
        , ->
          console.log new Date().getTime() - start
          callback null, tempRes
        resultData = ( result.koding.data for result in results)
        objectify resultData, (objecteds)->
          for objected in objecteds
            tempRes.push objected
            collectRelations objected

  fecthRelatedItems:(itemId, callback)->
    query = [
      'start koding=node:koding(id={itemId})'
      'match koding-[r]-all'
      'return *'
      'order by koding.`meta.createdAt` DESC'
    ].join('\n');

    params =
      itemId : itemId

    @db.query query, params, (err, results) ->
      if err then throw err
      resultData = []
      for result in results
        type = result.r.type
        data = result.all.data
        data.relationType = type
        resultData.push data

      objectify resultData, (objected)->
        respond = {}
        for obj in objected
          type = obj.relationType
          if not respond[type] then respond[type] = []
          respond[type].push obj
        callback err, respond

  fetchNewInstalledApps:(callback)->
    query = [
      'start koding=node:koding(\'id:*\')'
      'match koding-[r:user]->users'
      'where koding.name="JApp" and r.createdAt > "2012-11-14T23:56:48Z"'
      'return *'
      'limit 40'
    ].join('\n');

    @db.query query, {}, (err, results) ->
        if err then throw err
        resultData = []
        for result in results
          type = result.r.data
          data = result.users.data
          apps = result.koding.data
          data.appName = type
          data.apps = apps
          resultData.push data

        objectify resultData, (objected)->
          callback err, objected

  fetchNewMembers:(callback)->
    query = [
      'start koding=node:koding(\'id:*\')'
      'where koding.name="JAccount" and koding.`meta.createdAt` > "2013-02-14T23:56:48Z"'
      'return *'
      'limit 40'
    ].join('\n');

    @db.query query, {}, (err, results) ->
        if err then throw err
        resultData = []
        for result in results
          data = result.koding.data
          resultData.push data

        objectify resultData, (objected)->
          callback err, objected
<commit_msg>added order by statements for queries<commit_after>neo4j = require "neo4j"
{race} = require 'sinkrow'

module.exports = class Graph
  constructor:(config)->
    @db = new neo4j.GraphDatabase(config.host + ":" + config.port);

  objectify = (incomingObjects, callback)->
    incomingObjects = [].concat(incomingObjects)
    generatedObjects = []
    for incomingObject in incomingObjects
      generatedObject = {}
      for k of incomingObject
        temp = generatedObject
        parts = k.split "."
        key = parts.pop()
        while parts.length
          part = parts.shift()
          temp = temp[part] = temp[part] or {}
        temp[key] = incomingObject[k]
      generatedObjects.push generatedObject
    callback generatedObjects

  fetchAll:(callback)->
    start = new Date().getTime()
    query = [
      'start koding=node:koding(\'id:*\')'
      'where koding.name = "JTutorial"'
      ' or koding.name = "JCodeSnip"'
      ' or koding.name = "JDiscussion"'
      ' or koding.name = "JStatusUpdate"'
      'return *'
      'order by koding.`meta.createdAt` DESC'
      'limit 20'
    ].join('\n');

    params =
      itemId : "515360d23af2fb6b6b000009"

    @db.query query, params, (err, results)=>
#      console.log results
      tempRes = []
      if err then callback err
      else if results.length is 0 then callback null, []
      else
        collectRelations = race (i, res, fin)=>
          id = res.id

          @fecthRelatedItems id, (err, relatedResult)=>
            if err
              callback err
              fin()
            else
              tempRes[i].relationData =  relatedResult
              fin()
        , ->
          console.log new Date().getTime() - start
          callback null, tempRes
        resultData = ( result.koding.data for result in results)
        objectify resultData, (objecteds)->
          for objected in objecteds
            tempRes.push objected
            collectRelations objected

  fecthRelatedItems:(itemId, callback)->
    query = [
      'start koding=node:koding(id={itemId})'
      'match koding-[r]-all'
      'return *'
      'order by koding.`meta.createdAt` DESC'
    ].join('\n');

    params =
      itemId : itemId

    @db.query query, params, (err, results) ->
      if err then throw err
      resultData = []
      for result in results
        type = result.r.type
        data = result.all.data
        data.relationType = type
        resultData.push data

      objectify resultData, (objected)->
        respond = {}
        for obj in objected
          type = obj.relationType
          if not respond[type] then respond[type] = []
          respond[type].push obj
        callback err, respond

  fetchNewInstalledApps:(callback)->
    query = [
      'start koding=node:koding(\'id:*\')'
      'match koding-[r:user]->users'
      'where koding.name="JApp" and r.createdAt > "2012-11-14T23:56:48Z"'
      'return *'
      'order by r.createdAt DESC'
      'limit 40'
    ].join('\n');

    @db.query query, {}, (err, results) ->
        if err then throw err
        resultData = []
        for result in results
          type = result.r.data
          data = result.users.data
          apps = result.koding.data
          data.appName = type
          data.apps = apps
          resultData.push data

        objectify resultData, (objected)->
          callback err, objected

  fetchNewMembers:(callback)->
    query = [
      'start koding=node:koding(\'id:*\')'
      'where koding.name="JAccount" and koding.`meta.createdAt` > "2013-02-14T23:56:48Z"'
      'return *'
      'order by koding.`meta.createdAt` DESC'
      'limit 40'
    ].join('\n');

    @db.query query, {}, (err, results) ->
        if err then throw err
        resultData = []
        for result in results
          data = result.koding.data
          resultData.push data

        objectify resultData, (objected)->
          callback err, objected

{'fraction_non_alphanumeric': 0.11918230907688351, 'fraction_numerical': 0.014142453072769349, 'mean_word_length': 2.0, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, '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': '3285556', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2280, 'n_words': 589}