<commit_before>// Licensed under the Apache License. See footer for details.
function Server(serverUrl, serverPort, database) {
  var
    express = require('express'),
    app = express(),
    request = require('request'),
    async = require('async'),
    path = require('path'),
    monitoring = require('./monitoring.js')(database)

  var self = this
  self.app = app
  
  var bodyParser = require('body-parser');
  app.use(bodyParser.json()); // support json encoded bodies
  app.use(bodyParser.urlencoded({
    extended: true
  })); // support encoded bodies

  // currently send a static list
  app.get("/api/1/products.json", function (req, res) {
    res.sendFile(path.resolve('products.json'));
  })

  /**
   * Returns the list of registered pricers
   */
  app.get("/api/1/pricers.json", function (req, res) {
    database.getPricers(function (err, pricers) {
      if (err) {
        res.status(400).send({
          error: err
        })
      } else {
        res.send(pricers)
      }
    })
  })

  /**
   * Registers a new pricer.
   * Returns the list of registered pricers
   */
  app.post("/api/1/registerPricer.json", function (req, res) {
    var pricer = req.body
    console.log("Register pricer", pricer)

    if (!pricer.name) {
      res.status(400).send({
        error: "'name' parameter not found"
      })
      return
    }

    if (!pricer.url) {
      res.status(400).send({
        error: "'url' parameter not found"
      })
      return
    }

    database.getPricerByName(pricer.name, function (err, existingPricer) {
      if (err) {
        res.status(400).send({
          error: err
        })
      } else if (existingPricer != null) {
        res.status(400).send({
          error: "engine name already exists"
        })
      } else {
        pricer = database.addPricer(pricer, function (err, pricer) {
          if (err) {
            res.status(400).send({
              error: err
            })
          } else {
            res.send(pricer);
          }
        })
      }
    })
  })

  /**
   * Computes pricing for the given pricer
   * /api/1/price.json?pricer=<ID>&price=<PRICE>&quantity=<QTY>&state=<LETTER_CODE>
   */
  app.get("/api/1/price.json", function (req, res) {
    var pricerId = req.query.pricer
    var price = req.query.price
    var quantity = req.query.quantity
    var state = req.query.state
    console.log("Received pricing request", req.query);

    getPrice(pricerId, price, quantity, state, res)
  });

  var getPrice = function (pricerId, price, quantity, state, res, mycurrent, next) {
    // find the engine
    database.getPricerById(pricerId, function (err, engine) {
      if (!engine) {
        res.status(404).send({
          error: "No such engine"
        })
        return
      }

      var url = engine.url + "?price=" + encodeURIComponent(price) +
        "&quantity=" + encodeURIComponent(quantity) +
        "&state=" + encodeURIComponent(state)
      console.log("[", engine.id, "] Calling", url)
      request.get(url, {
          json: true
        },
        function (error, response, body) {

          if (error) {
            console.log("[", engine.id, "]", "error:", error)
            monitoring.logPrice(engine, 0, mycurrent)
            res.send(error)
            if (next) next(error)
            return
          }

          res.status(response.statusCode)
          console.log("[", engine.id, "] Got status", response.statusCode, "body:", body)

          if (body) {
            monitoring.logPrice(engine, Number(body.totalPrice), mycurrent)
            res.send(body)
          } else {
            monitoring.logPrice(engine, 0, mycurrent)
            res.send("empty body")
          }
          if (next) next()
        })
    })
  }

  //$http.get("/api/1/monitor.json
  app.get("/api/1/monitor.json", function (req, res) {
    res.send(monitoring.monitor)        
  });
    
/**
   * Clear DB
   *    TARGET = "pricers" | "monitor"
   * /api/1/clear.json?target=<TARGET>
   */
  app.get("/api/1/clear.json", function (req, res) {
      var pricers = req.query.target == "pricers"
      var monitor = req.query.target == "monitor"
      
      if ( pricers ) {
          database.clearPricers(function(err) {
              res.send(err)
          })
      }
      
      if ( monitor ) {
            monitoring.clear(function(err) {
              res.send(err)
            }) 
      }
  });


  // TODO post complete cart
  /**
   * Log a pricing transaction
   * /api/1/logTransaction.json?value=<PRICE BEFOER TAXES>
   */
  app.get("/api/1/logTransaction.json", function (req, res) {
    monitoring.log(req.query.value)
    res.send(monitoring.monitor)
  });

  /**
   * Simulate a given scenario.
   * Returns the list of scenarios
   * /api/1/scenario.json?id=<ID>&title=<TITLE>
   */
  app.get("/api/1/scenario.json", function (req, res) {
    var scenarios = JSON.parse(require('fs').readFileSync('scenarios.json', 'utf8'))
    if (req.query.id) {
        scenarios.forEach(function (scenario) {
            if (scenario.id == req.query.id) {
                simulate(req.query.title, scenario, function(err) {
                    if (err) {
                        res.send(err)
                    } else {
                        res.send(scenarios);
                    }
                })<commit_msg>clearing sends old data back<commit_after>// Licensed under the Apache License. See footer for details.
function Server(serverUrl, serverPort, database) {
  var
    express = require('express'),
    app = express(),
    request = require('request'),
    async = require('async'),
    path = require('path'),
    monitoring = require('./monitoring.js')(database)

  var self = this
  self.app = app
  
  var bodyParser = require('body-parser');
  app.use(bodyParser.json()); // support json encoded bodies
  app.use(bodyParser.urlencoded({
    extended: true
  })); // support encoded bodies

  // currently send a static list
  app.get("/api/1/products.json", function (req, res) {
    res.sendFile(path.resolve('products.json'));
  })

  /**
   * Returns the list of registered pricers
   */
  app.get("/api/1/pricers.json", function (req, res) {
    database.getPricers(function (err, pricers) {
      if (err) {
        res.status(400).send({
          error: err
        })
      } else {
        res.send(pricers)
      }
    })
  })

  /**
   * Registers a new pricer.
   * Returns the list of registered pricers
   */
  app.post("/api/1/registerPricer.json", function (req, res) {
    var pricer = req.body
    console.log("Register pricer", pricer)

    if (!pricer.name) {
      res.status(400).send({
        error: "'name' parameter not found"
      })
      return
    }

    if (!pricer.url) {
      res.status(400).send({
        error: "'url' parameter not found"
      })
      return
    }

    database.getPricerByName(pricer.name, function (err, existingPricer) {
      if (err) {
        res.status(400).send({
          error: err
        })
      } else if (existingPricer != null) {
        res.status(400).send({
          error: "engine name already exists"
        })
      } else {
        pricer = database.addPricer(pricer, function (err, pricer) {
          if (err) {
            res.status(400).send({
              error: err
            })
          } else {
            res.send(pricer);
          }
        })
      }
    })
  })

  /**
   * Computes pricing for the given pricer
   * /api/1/price.json?pricer=<ID>&price=<PRICE>&quantity=<QTY>&state=<LETTER_CODE>
   */
  app.get("/api/1/price.json", function (req, res) {
    var pricerId = req.query.pricer
    var price = req.query.price
    var quantity = req.query.quantity
    var state = req.query.state
    console.log("Received pricing request", req.query);

    getPrice(pricerId, price, quantity, state, res)
  });

  var getPrice = function (pricerId, price, quantity, state, res, mycurrent, next) {
    // find the engine
    database.getPricerById(pricerId, function (err, engine) {
      if (!engine) {
        res.status(404).send({
          error: "No such engine"
        })
        return
      }

      var url = engine.url + "?price=" + encodeURIComponent(price) +
        "&quantity=" + encodeURIComponent(quantity) +
        "&state=" + encodeURIComponent(state)
      console.log("[", engine.id, "] Calling", url)
      request.get(url, {
          json: true
        },
        function (error, response, body) {

          if (error) {
            console.log("[", engine.id, "]", "error:", error)
            monitoring.logPrice(engine, 0, mycurrent)
            res.send(error)
            if (next) next(error)
            return
          }

          res.status(response.statusCode)
          console.log("[", engine.id, "] Got status", response.statusCode, "body:", body)

          if (body) {
            monitoring.logPrice(engine, Number(body.totalPrice), mycurrent)
            res.send(body)
          } else {
            monitoring.logPrice(engine, 0, mycurrent)
            res.send("empty body")
          }
          if (next) next()
        })
    })
  }

  //$http.get("/api/1/monitor.json
  app.get("/api/1/monitor.json", function (req, res) {
    res.send(monitoring.monitor)        
  });
    
/**
   * Clear DB
   *    TARGET = "pricers" | "monitor"
   * /api/1/clear.json?target=<TARGET>
   */
  app.get("/api/1/clear.json", function (req, res) {
      var pricers = req.query.target == "pricers"
      var monitor = req.query.target == "monitor"
      
      if ( pricers ) {
          database.clearPricers(function(err,cleared) {
              if ( err ) {
                res.send(err)
              } else {
                res.send(cleared)
              }
          })
      }
      
      if ( monitor ) {
            monitoring.clear(function(err,cleared) {
               if ( err ) {
                res.send(err)
              } else {
                res.send(cleared)
              }
          }) 
      }
  });


  // TODO post complete cart
  /**
   * Log a pricing transaction
   * /api/1/logTransaction.json?value=<PRICE BEFOER TAXES>
   */
  app.get("/api/1/logTransaction.json", function (req, res) {
    monitoring.log(req.query.value)
    res.send(monitoring.monitor)
  });

  /**
   * Simulate a given scenario.
   * Returns the list of scenarios
   * /api/1/scenario.json?id=<ID>&title=<TITLE>
   */
  app.get("/api/1/scenario.json", function (req, res) {
    var scenarios = JSON.parse(require('fs').readFileSync('scenarios.json', 'utf8'))
    if (req.query.id) {
        scenarios.forEach(function (scenario) {
            if (scenario.id == req.query.id) {
                simulate(req.query.title, scenario, function(err) {
                    if (err) {
                        res.send(err)
                    } else {
                        res.send(scenarios);
                    }
                })
{'fraction_non_alphanumeric': 0.1459211732355637, 'fraction_numerical': 0.006782768102658112, 'mean_word_length': 1.878891820580475, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6458897', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3389, 'n_words': 696}