<commit_before>var ad = require('../config/ad.json');
var ldapClient = require('../lib/ldap-client');
var request = require('request');
var fs = require('fs');
var moment = require('moment');

var debug = require('debug')('dataU');

var dataconfig = require('../config/data.json');
var pending_photo;
var options = {
  root: __dirname + '/../public/images/staff/',
  maxAge: 30 * 24 * 3600 * 1000
};

var update = {};

var plotUpdate = [];

var summary = {};

var pvs = [{
  name: 'K5MN_LVL_LCKD'
}, {
  name: 'K12_VLTSEC_IND'
}, {
  name: 'XFR_M_DOOR_LED'
}, {
  name: 'S1_VAULT_SECUR'
}, {
  name: 'S3_VAULT_SECUR'
}, {
  name: 'N3_VAULT_SECUR'
}, {
  name: 'N4_VAULT_SECUR'
}, {
  name: 'STRCHAN27',
  type: 'string'
}, {
  name: 'K8FREQ'
}];

function fetch_from_ad(id) {
  var searchFilter = ad.searchFilter.replace('_id', id);
  var opts = {
    filter: searchFilter,
    attributes: ad.rawAttributes,
    scope: 'sub'
  };
  ldapClient.search(ad.searchBase, opts, true, function (err, result) {
    var res_list = pending_photo[id];
    delete pending_photo[id];
    if (err) {
      console.error(err);
      res_list.forEach(function (res) {
        res.status(500).json({
          error: 'ldap error'
        });
      });
    } else {
      if (result.length === 0) {
        res_list.forEach(function (res) {
          res.status(500).json({
            error: id + ' is not found'
          });
        });
      }
      if (result.length > 1) {
        res_list.forEach(function (res) {
          res.status(500).json({
            error: id + ' is not unique!'
          });
        });
      } else {
        fs.writeFile(options.root + id + '.jpg', result[0].thumbnailPhoto, function (err) {
          if (err) {
            console.error(err);
            res_list.forEach(function (res) {
              res.status(500).json({
                error: 'cannot access file.'
              });
            });
          } else {
            res_list.forEach(function (res) {
              res.sendFile(id + '.jpg', options);
            });
          }
        });
      }
    }
  });
}

function getPV(pv, now) {
  request({
    url: dataconfig.pvdataurl + '.csv',
    qs: {
      pv: pv.name,
      from: now.toISOString()
    },
    timeout: 30 * 1000
  }, function (error, response, body) {
    var firstLine;
    if (error) {
      console.error(error);
    } else {<commit_msg>send file from memory then save to file<commit_after>var ad = require('../config/ad.json');
var ldapClient = require('../lib/ldap-client');
var request = require('request');
var fs = require('fs');
var moment = require('moment');

var debug = require('debug')('dataU');

var dataconfig = require('../config/data.json');
var pending_photo;
var options = {
  root: __dirname + '/../public/images/staff/',
  maxAge: 30 * 24 * 3600 * 1000
};

var update = {};

var plotUpdate = [];

var summary = {};

var pvs = [{
  name: 'K5MN_LVL_LCKD'
}, {
  name: 'K12_VLTSEC_IND'
}, {
  name: 'XFR_M_DOOR_LED'
}, {
  name: 'S1_VAULT_SECUR'
}, {
  name: 'S3_VAULT_SECUR'
}, {
  name: 'N3_VAULT_SECUR'
}, {
  name: 'N4_VAULT_SECUR'
}, {
  name: 'STRCHAN27',
  type: 'string'
}, {
  name: 'K8FREQ'
}];

function fetch_from_ad(id) {
  var searchFilter = ad.searchFilter.replace('_id', id);
  var opts = {
    filter: searchFilter,
    attributes: ad.rawAttributes,
    scope: 'sub'
  };
  ldapClient.search(ad.searchBase, opts, true, function (err, result) {
    var res_list = pending_photo[id];
    delete pending_photo[id];
    if (err) {
      console.error(err);
      res_list.forEach(function (res) {
        res.status(500).json({
          error: 'ldap error'
        });
      });
    } else {
      if (result.length === 0) {
        res_list.forEach(function (res) {
          res.status(500).json({
            error: id + ' is not found'
          });
        });
      }
      if (result.length > 1) {
        res_list.forEach(function (res) {
          res.status(500).json({
            error: id + ' is not unique!'
          });
        });
      } else {
        res_list.forEach(function (res) {
          res.set('Content-Type', 'image/jpeg');
          res.send(result[0].thumbnailPhoto);
        });
        fs.writeFile(options.root + id + '.jpg', result[0].thumbnailPhoto, function (err) {
          if (err) {
            console.error(err);
          }
        });
      }
    }
  });
}

function getPV(pv, now) {
  request({
    url: dataconfig.pvdataurl + '.csv',
    qs: {
      pv: pv.name,
      from: now.toISOString()
    },
    timeout: 30 * 1000
  }, function (error, response, body) {
    var firstLine;
    if (error) {
      console.error(error);
    } else {
{'fraction_non_alphanumeric': 0.168875587858059, 'fraction_numerical': 0.01795639162035058, 'mean_word_length': 2.1938566552901024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1249766', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1627, 'n_words': 332}