<commit_before>config = require './config'
common = require './common'
util = require 'util'
http = require 'http'
mongo = require 'mongodb'
tr069 = require './tr-069'
tasks = require './tasks'
Memcached = require 'memcached'
memcached = new Memcached(config.MEMCACHED_SOCKET)
sanitize = require('./sanitize').sanitize


# Create MongoDB connections
dbserver = new mongo.Server(config.MONGODB_SOCKET, 0, {auto_reconnect: true})
tasksCollection = null
devicesCollection = null
db = new mongo.Db(config.DATABASE_NAME, dbserver, {native_parser:true})
db.open( (err, db) ->
  db.collection('tasks', (err, collection) ->
    tasksCollection = collection
    tasksCollection.ensureIndex({device: 1, timestamp: 1})
  )

  db.collection('devices', (err, collection) ->
    devicesCollection = collection
  )
)


updateDevice = (deviceId, actions, callback) ->
  if not actions?
    callback() if callback?
    return

  updates = {}
  if actions.inform
    updates['_last_inform'] = new Date(Date.now())

  if actions.parameterValues?
    for p in actions.parameterValues
      v = sanitize(p[0], p[1])
      path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}."
      if v is undefined
        updates["#{path}_value"] = p[1]
      else
        updates["#{path}_value"] = v
        updates["#{path}_orig"] = p[1]

  if actions.parameterNames?
    for p in actions.parameterNames
      if common.endsWith(p[0], '.')
        updates["#{p[0]}_writable"] = p[1]
        updates["#{p[0]}_object"] = true
      else
        updates["#{p[0]}._writable"] = p[1]

  if Object.keys(updates).length > 0
    devicesCollection.update({'_id' : deviceId}, {'$set' : updates}, {upsert: true, safe: true}, (err) ->
      callback(err) if callback?
    )
  else
    callback() if callback?<commit_msg>Added _timestamp for parameters<commit_after>config = require './config'
common = require './common'
util = require 'util'
http = require 'http'
mongo = require 'mongodb'
tr069 = require './tr-069'
tasks = require './tasks'
Memcached = require 'memcached'
memcached = new Memcached(config.MEMCACHED_SOCKET)
sanitize = require('./sanitize').sanitize


# Create MongoDB connections
dbserver = new mongo.Server(config.MONGODB_SOCKET, 0, {auto_reconnect: true})
tasksCollection = null
devicesCollection = null
db = new mongo.Db(config.DATABASE_NAME, dbserver, {native_parser:true})
db.open( (err, db) ->
  db.collection('tasks', (err, collection) ->
    tasksCollection = collection
    tasksCollection.ensureIndex({device: 1, timestamp: 1})
  )

  db.collection('devices', (err, collection) ->
    devicesCollection = collection
  )
)


updateDevice = (deviceId, actions, callback) ->
  if not actions?
    callback() if callback?
    return

  now = new Date(Date.now())
  updates = {}
  if actions.inform
    updates['_last_inform'] = now

  if actions.parameterValues?
    for p in actions.parameterValues
      v = sanitize(p[0], p[1])
      path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}."
      if v is undefined
        updates["#{path}_value"] = p[1]
      else
        updates["#{path}_value"] = v
        updates["#{path}_orig"] = p[1]
      updates["#{path}_timestamp"] = now

  if actions.parameterNames?
    for p in actions.parameterNames
      path = if common.endsWith(p[0], '.') then p[0] else "#{p[0]}."
      if common.endsWith(p[0], '.')
        updates["#{path}_object"] = true
      updates["#{path}_writable"] = p[1]
      updates["#{path}_timestamp"] = now

  if Object.keys(updates).length > 0
    devicesCollection.update({'_id' : deviceId}, {'$set' : updates}, {upsert: true, safe: true}, (err) ->
      callback(err) if callback?
    )
  else
    callback() if callback?
{'fraction_non_alphanumeric': 0.1564699512723335, 'fraction_numerical': 0.012181916621548458, 'mean_word_length': 3.595771144278607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '5338652', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1212, 'n_words': 296}