<commit_before>/*    Copyright 2016 Firewalla LLC
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict'

let log = require('../../net2/logger.js')(__filename);

let express = require('express');
let router = express.Router();
let bodyParser = require('body-parser')

let HostManager = require('../../net2/HostManager.js');
let hostManager = new HostManager('api', 'client', 'info');

let FlowManager = require('../../net2/FlowManager.js');
let flowManager = new FlowManager();

let FlowTool = require('../../net2/FlowTool');
let flowTool = new FlowTool();

let Promise = require('bluebird');

let NetBotTool = require('../../net2/NetBotTool');
let netBotTool = new NetBotTool();

let async = require('asyncawait/async');
let await = require('asyncawait/await');

router.get('/all',
           (req, res, next) => {
             let json = {};
             hostManager.getHosts(() => {
               hostManager.legacyHostsStats(json)
                 .then(() => {
                   res.json(json);
                 }).catch((err) => {
                   res.status(500).send('');
                 });
             });
           });

router.get('/:host',
           (req, res, next) => {
             let host = req.params.host;

             if(host === "system") {
               hostManager.toJson(true, (err, json) => {
                 if(err) {
                   res.status(500).send("");
                   return;
                 } else {
                   res.json(json);
                   return;
                 }
               });
             } else {
               hostManager.getHost(host, (err, h) => {
                 flowManager.getStats2(h).then(() => {
                   h.loadPolicy((err) => {
                     if(err) {
                       res.status(500).send("");
                       return;
                     }

                     let jsonObj = h.toJson();

                     Promise.all([
                       flowTool.prepareRecentFlowsForHost(jsonObj, h.getAllIPs()),
                       netBotTool.prepareTopUploadFlowsForHost(jsonObj, h.o.mac),
                       netBotTool.prepareTopDownloadFlowsForHost(jsonObj, h.o.mac),
                       netBotTool.prepareActivitiesFlowsForHost(jsonObj, h.o.mac),
                   ]).then(() => {
                       res.json(jsonObj);
                     });
                   })
                 }).catch((err) => {
                   res.status(404);
                   res.send("");
                 });
               });
             }
           });

router.get('/:host',
  (req, res, next) => {
    let host = req.params.host;


  }
)

router.get('/:host/recentFlow',
  (req, res, next) => {
    let host = req.params.host;

    flowTool.getRecentOutgoingConnections(host)
      .then((conns) => {
        res.json(conns);
      }).catch((err) => {
      res.status(500).json({error: err});
    })
  });

router.get('/:host/topDownload',
  (req, res, next) => {
    let host = req.params.host;
    let json = {};

    return async(() => {
      let h = hostManager.getHostAsync(host);
      let mac = h.o && h.o.mac;
      if(!mac) {
        return;
      }
      await (netBotTool.prepareTopDownloadFlowsForHost(json, mac));
    })()
    .then(() => res.json(json))
    .catch((err) => {
      log.error("Got error when calling topDownload:", err, {})
      res.status(404);
      res.send("");
    })
  });

router.get('/:host/topUpload',
  (req, res, next) => {
    let host = req.params.host;
    let json = {};

    return async(() => {
      let h = hostManager.getHostAsync(host);
      let mac = h.o && h.o.mac;
      if(!mac) {
        return;
      }
      await (netBotTool.prepareTopUploadFlowsForHost(json, mac));
    })()
    .then(() => res.json(json))
    .catch((err) => {
      log.error("Got error when calling topUpload:", err, {})
      res.status(404);
      res.send("");
    })
  });

module.exports = router;<commit_msg>bug fix<commit_after>/*    Copyright 2016 Firewalla LLC
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict'

let log = require('../../net2/logger.js')(__filename);

let express = require('express');
let router = express.Router();
let bodyParser = require('body-parser')

let HostManager = require('../../net2/HostManager.js');
let hostManager = new HostManager('api', 'client', 'info');

let FlowManager = require('../../net2/FlowManager.js');
let flowManager = new FlowManager();

let FlowTool = require('../../net2/FlowTool');
let flowTool = new FlowTool();

let Promise = require('bluebird');

let NetBotTool = require('../../net2/NetBotTool');
let netBotTool = new NetBotTool();

let async = require('asyncawait/async');
let await = require('asyncawait/await');

router.get('/all',
           (req, res, next) => {
             let json = {};
             hostManager.getHosts(() => {
               hostManager.legacyHostsStats(json)
                 .then(() => {
                   res.json(json);
                 }).catch((err) => {
                   res.status(500).send('');
                 });
             });
           });

router.get('/:host',
           (req, res, next) => {
             let host = req.params.host;

             if(host === "system") {
               hostManager.toJson(true, (err, json) => {
                 if(err) {
                   res.status(500).send("");
                   return;
                 } else {
                   res.json(json);
                   return;
                 }
               });
             } else {
               hostManager.getHost(host, (err, h) => {
                 flowManager.getStats2(h).then(() => {
                   h.loadPolicy((err) => {
                     if(err) {
                       res.status(500).send("");
                       return;
                     }

                     let jsonObj = h.toJson();

                     Promise.all([
                       flowTool.prepareRecentFlowsForHost(jsonObj, h.getAllIPs()),
                       netBotTool.prepareTopUploadFlowsForHost(jsonObj, h.o.mac),
                       netBotTool.prepareTopDownloadFlowsForHost(jsonObj, h.o.mac),
                       netBotTool.prepareActivitiesFlowsForHost(jsonObj, h.o.mac),
                   ]).then(() => {
                       res.json(jsonObj);
                     });
                   })
                 }).catch((err) => {
                   res.status(404);
                   res.send("");
                 });
               });
             }
           });

router.get('/:host',
  (req, res, next) => {
    let host = req.params.host;


  }
)

router.get('/:host/recentFlow',
  (req, res, next) => {
    let host = req.params.host;

    flowTool.getRecentOutgoingConnections(host)
      .then((conns) => {
        res.json(conns);
      }).catch((err) => {
      res.status(500).json({error: err});
    })
  });

router.get('/:host/topDownload',
  (req, res, next) => {
    let host = req.params.host;
    let json = {};

    return async(() => {
      let h = await (hostManager.getHostAsync(host));
      let mac = h.o && h.o.mac;
      if(!mac) {
        return;
      }
      await (netBotTool.prepareTopDownloadFlowsForHost(json, mac));
    })()
    .then(() => res.json(json))
    .catch((err) => {
      log.error("Got error when calling topDownload:", err, {})
      res.status(404);
      res.send("");
    })
  });

router.get('/:host/topUpload',
  (req, res, next) => {
    let host = req.params.host;
    let json = {};

    return async(() => {
      let h = await (hostManager.getHostAsync(host));
      let mac = h.o && h.o.mac;
      if(!mac) {
        return;
      }
      await (netBotTool.prepareTopUploadFlowsForHost(json, mac));
    })()
    .then(() => res.json(json))
    .catch((err) => {
      log.error("Got error when calling topUpload:", err, {})
      res.status(404);
      res.send("");
    })
  });

module.exports = router;
{'fraction_non_alphanumeric': 0.1540310246886607, 'fraction_numerical': 0.006991479134804457, 'mean_word_length': 1.6894829612220916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7175852', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2596, 'n_words': 592}