<commit_before>/**
    Framework for building object relational database apps

    Copyright (C) 2015  <NAME>
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    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 General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

(function () {
  "use strict";
  require('./common/extend-string.js');

  var catalog, init, resolveName, getCatalog, getCurrentUser,
    doGetSettings, doGetFeather, doGetModules, doRequest,
    doGetMethod, doSaveFeather, doDeleteFeather, registerDataRoutes,
    doDeleteMethod, doDeleteWorkbook, doGetWorkbooks, doSaveWorkbook,
    doSaveMethod, doGetWorkbook,
    datasource = require("./server/datasource"),
    express = require("express"),
    bodyParser = require("body-parser"),
    qs = require("qs"),
    app = express(),
    port = process.env.PORT || 10001,
    dataRouter = express.Router(),
    featherRouter = express.Router(),
    moduleRouter = express.Router(),
    settingsRouter = express.Router(),
    workbookRouter = express.Router();

  // ..........................................................
  // CONTROLLERS
  //

  getCatalog = function (callback) {
    var payload, after;

    after = function (err, resp) {
      if (err) {
        console.error(err);
        return;
      }

      catalog = resp;
      callback();
    };

    payload = {
      method: "GET",
      name: "getSettings",
      user: "postgres",
      callback: after,
      data: {
        name: "catalog"
      }
    };

    datasource.request(payload);
  };

  getCurrentUser = function () {
    // TODO: Make this real
    return "postgres";
  };

  init = function (callback) {
    getCatalog(callback);
  };

  resolveName = function (apiPath) {
    var name,
      keys,
      found;

    if (apiPath.lastIndexOf("/") > 0) {
      name = apiPath.match("[/](.*)[/]")[1].toCamelCase(true);
    } else {
      name = apiPath.slice(1).toCamelCase(true);
    }

    if (catalog) {
      // Look for feather with same name
      if (catalog[name]) { return name; }

      // Look for plural version
      keys = Object.keys(catalog);
      found = keys.filter(function (key) {
        return catalog[key].plural === name;
      });

      if (found.length) {
        return found[0];
      }

      return;
    }
  };

  doRequest = function (req, res) {
    var callback,
      params = req.params,
      name = resolveName(req.url),
      payload = req.body || {},
      id = params.id,
      filter = params.filter ? qs.parse(params.filter) : {};

    // Handle response
    callback = function (err, resp) {
      // Handle datasource error
      if (err) {
        res.status(err.statusCode).json(err.message);
        return;
      }

      if (resp === undefined) {
        res.statusCode = 204;
      }

      // Send back a JSON response
      res.json(resp);
    };

    payload.name = name;
    payload.method = req.method;
    payload.user = getCurrentUser();
    payload.callback = callback;

    if (req.method !== "POST") {
      if (id) {
        payload.id = id;
      } else {
        payload.filter = filter;
        filter.offset = filter.offset || 0;
      }
    }

    console.log(JSON.stringify(payload, null, 2));
    datasource.request(payload);
  };

  doGetFeather = function (req, res) {
    req.params.name = req.params.name.toCamelCase(true);
    doGetMethod("getFeather", req, res);
  };

  doGetModules = function (req, res) {
    doGetMethod("getModules", req, res);
  };

  doGetSettings = function (req, res) {
    doGetMethod("getSettings", req, res);
  };

  doGetWorkbook = function (req, res) {
    doGetMethod("getWorkbook", req, res);
  };

  doGetWorkbooks = function (req, res) {
    doGetMethod("getWorkbooks", req, res);
  };

  doGetMethod = function (fn, req, res) {
    var payload, callback,
      name = req.params.name;

    callback = function (err, resp) {
      // Handle error
      if (err) {
        res.status(err.statusCode).json(err.message);
        return res;
      }

      if (!resp) { res.statusCode = 204; }
      res.json(resp);
    };

    payload = {
      method: "GET",<commit_msg>Make sure client never caches response<commit_after>/**
    Framework for building object relational database apps

    Copyright (C) 2015  <NAME>
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    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 General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

(function () {
  "use strict";
  require('./common/extend-string.js');

  var catalog, init, resolveName, getCatalog, getCurrentUser,
    doGetSettings, doGetFeather, doGetModules, doRequest,
    doGetMethod, doSaveFeather, doDeleteFeather, registerDataRoutes,
    doDeleteMethod, doDeleteWorkbook, doGetWorkbooks, doSaveWorkbook,
    doSaveMethod, doGetWorkbook,
    datasource = require("./server/datasource"),
    express = require("express"),
    bodyParser = require("body-parser"),
    qs = require("qs"),
    app = express(),
    port = process.env.PORT || 10001,
    dataRouter = express.Router(),
    featherRouter = express.Router(),
    moduleRouter = express.Router(),
    settingsRouter = express.Router(),
    workbookRouter = express.Router();

  // ..........................................................
  // CONTROLLERS
  //

  getCatalog = function (callback) {
    var payload, after;

    after = function (err, resp) {
      if (err) {
        console.error(err);
        return;
      }

      catalog = resp;
      callback();
    };

    payload = {
      method: "GET",
      name: "getSettings",
      user: "postgres",
      callback: after,
      data: {
        name: "catalog"
      }
    };

    datasource.request(payload);
  };

  getCurrentUser = function () {
    // TODO: Make this real
    return "postgres";
  };

  init = function (callback) {
    getCatalog(callback);
  };

  resolveName = function (apiPath) {
    var name,
      keys,
      found;

    if (apiPath.lastIndexOf("/") > 0) {
      name = apiPath.match("[/](.*)[/]")[1].toCamelCase(true);
    } else {
      name = apiPath.slice(1).toCamelCase(true);
    }

    if (catalog) {
      // Look for feather with same name
      if (catalog[name]) { return name; }

      // Look for plural version
      keys = Object.keys(catalog);
      found = keys.filter(function (key) {
        return catalog[key].plural === name;
      });

      if (found.length) {
        return found[0];
      }

      return;
    }
  };

  doRequest = function (req, res) {
    var callback,
      params = req.params,
      name = resolveName(req.url),
      payload = req.body || {},
      id = params.id,
      filter = params.filter ? qs.parse(params.filter) : {};

    // Handle response
    callback = function (err, resp) {
      // Handle datasource error
      if (err) {
        res.status(err.statusCode).json(err.message);
        return;
      }

      if (resp === undefined) {
        res.statusCode = 204;
      }

      // No caching... ever
      res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
      res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
      res.setHeader("Expires", "0"); //

      // Send back a JSON response
      res.json(resp);
    };

    payload.name = name;
    payload.method = req.method;
    payload.user = getCurrentUser();
    payload.callback = callback;

    if (req.method !== "POST") {
      if (id) {
        payload.id = id;
      } else {
        payload.filter = filter;
        filter.offset = filter.offset || 0;
      }
    }

    console.log(JSON.stringify(payload, null, 2));
    datasource.request(payload);
  };

  doGetFeather = function (req, res) {
    req.params.name = req.params.name.toCamelCase(true);
    doGetMethod("getFeather", req, res);
  };

  doGetModules = function (req, res) {
    doGetMethod("getModules", req, res);
  };

  doGetSettings = function (req, res) {
    doGetMethod("getSettings", req, res);
  };

  doGetWorkbook = function (req, res) {
    doGetMethod("getWorkbook", req, res);
  };

  doGetWorkbooks = function (req, res) {
    doGetMethod("getWorkbooks", req, res);
  };

  doGetMethod = function (fn, req, res) {
    var payload, callback,
      name = req.params.name;

    callback = function (err, resp) {
      // Handle error
      if (err) {
        res.status(err.statusCode).json(err.message);
        return res;
      }

      if (!resp) { res.statusCode = 204; }
      res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
      res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
      res.setHeader("Expires", "0"); //
      res.json(resp);
    };

    payload = {
      method: "GET",
{'fraction_non_alphanumeric': 0.14195616211745243, 'fraction_numerical': 0.005583126550868486, 'mean_word_length': 2.326341127922971, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5574768', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2869, 'n_words': 785}