<commit_before>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

 /**
 * @quotasconfig.api.js
 *     - Handlers for project quotas
 *     - Interfaces with config api server
 */
var rest        = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/common/rest.api');
var async       = require('async');
var quotasconfigapi = module.exports;
var logutils    = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/utils/log.utils');
var commonUtils = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/utils/common.utils');
var config      = process.mainModule.exports["config"];
var messages    = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/common/messages');
var global      = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/common/global');
var appErrors   = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/errors/app.errors');
var util        = require('util');
var url         = require('url');
var configApiServer = require(process.mainModule.exports["corePath"] +
                              '/src/serverroot/common/configServer.api');
var configUtilApi = require('../../../common/api/configUtil.api');

/**
 * @parseProjectQuotas
 * private function
 * 1. prepares the quota object from the project response
 */
function parseProjectQuotas(error, projData, appData, callback)
{
    var quotaData =  projData["project"]["quota"];
    if(quotaData != undefined) {
         var resQuota = {};
         resQuota.quota = quotaData;
         callback(error, resQuota);
    }
    else {
        callback(error, null);
    }
}

/**
 * @getProjectQuotasCb
 * private function
 * 1. Callback for getProjectQuotas
 * 2. Reads the response of a particular project from config api server
 */
function getProjectQuotasCb (error, projectGetData, appData, callback)
{
    if (error) {
       callback(error, null);
       return;
    }
    parseProjectQuotas(error, projectGetData, appData, callback);
}

function getDefaultProjectQuotas (projectData, appData, callback)
{
    var projUUID =
        commonUtils.getValueByJsonPath(projectData,
                                        'project;uuid', null);
    var projFqn =
        commonUtils.getValueByJsonPath(projectData,
                                        'project;fq_name', null);
    var defProjectFqn = ['default-domain', 'default-domain'];
    if (null != projFqn) {
        defProjectFqn = projFqn;
        defProjectFqn[1] = 'default-project';
    }
    var postData = {
        'appData': appData,
        'fqnReq' : {
            'fq_name': defProjectFqn,
            'type': 'project'
        }
    };
    configUtilApi.getUUIDByFQN(postData, function(error, data) {
        if ((null != error) || (null == data)) {
            callback(error, data);
            return;
        }
        var uuid = data.uuid;
        var defProjGetUrl = '/project/' + uuid;
        configApiServer.apiGet(defProjGetUrl, appData, function(error, data) {
            if ((null != error) || (null == data)) {
                callback(error, data);
                return;
            }
            var quotas =
                commonUtils.getValueByJsonPath(data, 'project;quota', null);
            if (null != quotas) {
                getProjectQuotasCb(null, data, appData, callback);
            } else {
                getDefaultQuotas(projUUID, appData, data, callback);
            }
        });
    });
}

/**
 * @readProjectQuotas
 * private function
 * 1. Needs project uuid in string format
 */
function readProjectQuotas (userData, callback)
{
    var projectIdStr = userData['id'];
    var appData = userData['appData'];
    var quotasGetURL = '/project/';
    if (projectIdStr.length) {
        quotasGetURL += projectIdStr;
    } else {
        error = new appErrors.RESTServerError('Add Project id');
        callback(error, null);
        return;
    }
    configApiServer.apiGet(quotasGetURL, appData,
                         function(error, data) {
                             if (error) {
                                 callback(error, null);
                                 return;
                             }
                             if(data["project"]["quota"] != undefined) {
                                 getProjectQuotasCb(error, data, appData, callback);
                             } else {
                                 getDefaultProjectQuotas(data, appData, callback);
                             }
                         }
    );
}

/**
 * @getDefaultQuotas
 * private function
 * 1. Needs project uuid in string format
 */
function getDefaultQuotas (appData, data, callback) {
    data["project"]["quota"] = { defaults: -1 };
    getProjectQuotasCb(null, data, appData, callback);
}

/**
 * @getProjectQuotas
 * public function
 * 1. URL /api/tenants/config/project-quotas/:id
 * 2. Gets project quotas from config api server
 * 3. Needs project id
 */
function getProjectQuotas (request, response, appData)
{
    var projectId = validateProjectId(request);
    projectQuotasAPIGet(projectId, response, appData);
}

/**
 * @updateProjectQuotas<commit_msg>Closes-Bug: #1550479 Corrected the default-project name.<commit_after>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

 /**
 * @quotasconfig.api.js
 *     - Handlers for project quotas
 *     - Interfaces with config api server
 */
var rest        = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/common/rest.api');
var async       = require('async');
var quotasconfigapi = module.exports;
var logutils    = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/utils/log.utils');
var commonUtils = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/utils/common.utils');
var config      = process.mainModule.exports["config"];
var messages    = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/common/messages');
var global      = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/common/global');
var appErrors   = require(process.mainModule.exports["corePath"] +
                          '/src/serverroot/errors/app.errors');
var util        = require('util');
var url         = require('url');
var configApiServer = require(process.mainModule.exports["corePath"] +
                              '/src/serverroot/common/configServer.api');
var configUtilApi = require('../../../common/api/configUtil.api');

/**
 * @parseProjectQuotas
 * private function
 * 1. prepares the quota object from the project response
 */
function parseProjectQuotas(error, projData, appData, callback)
{
    var quotaData =  projData["project"]["quota"];
    if(quotaData != undefined) {
         var resQuota = {};
         resQuota.quota = quotaData;
         callback(error, resQuota);
    }
    else {
        callback(error, null);
    }
}

/**
 * @getProjectQuotasCb
 * private function
 * 1. Callback for getProjectQuotas
 * 2. Reads the response of a particular project from config api server
 */
function getProjectQuotasCb (error, projectGetData, appData, callback)
{
    if (error) {
       callback(error, null);
       return;
    }
    parseProjectQuotas(error, projectGetData, appData, callback);
}

function getDefaultProjectQuotas (projectData, appData, callback)
{
    var projUUID =
        commonUtils.getValueByJsonPath(projectData,
                                        'project;uuid', null);
    var projFqn =
        commonUtils.getValueByJsonPath(projectData,
                                        'project;fq_name', null);
    var defProjectFqn = ['default-domain', 'default-project'];
    if (null != projFqn) {
        defProjectFqn = projFqn;
        defProjectFqn[1] = 'default-project';
    }
    var postData = {
        'appData': appData,
        'fqnReq' : {
            'fq_name': defProjectFqn,
            'type': 'project'
        }
    };
    configUtilApi.getUUIDByFQN(postData, function(error, data) {
        if ((null != error) || (null == data)) {
            callback(error, data);
            return;
        }
        var uuid = data.uuid;
        var defProjGetUrl = '/project/' + uuid;
        configApiServer.apiGet(defProjGetUrl, appData, function(error, data) {
            if ((null != error) || (null == data)) {
                callback(error, data);
                return;
            }
            var quotas =
                commonUtils.getValueByJsonPath(data, 'project;quota', null);
            if (null != quotas) {
                getProjectQuotasCb(null, data, appData, callback);
            } else {
                getDefaultQuotas(appData, data, callback);
            }
        });
    });
}

/**
 * @readProjectQuotas
 * private function
 * 1. Needs project uuid in string format
 */
function readProjectQuotas (userData, callback)
{
    var projectIdStr = userData['id'];
    var appData = userData['appData'];
    var quotasGetURL = '/project/';
    if (projectIdStr.length) {
        quotasGetURL += projectIdStr;
    } else {
        error = new appErrors.RESTServerError('Add Project id');
        callback(error, null);
        return;
    }
    configApiServer.apiGet(quotasGetURL, appData,
                         function(error, data) {
                             if (error) {
                                 callback(error, null);
                                 return;
                             }
                             if(data["project"]["quota"] != undefined) {
                                 getProjectQuotasCb(error, data, appData, callback);
                             } else {
                                 getDefaultProjectQuotas(data, appData, callback);
                             }
                         }
    );
}

/**
 * @getDefaultQuotas
 * private function
 * 1. Get default quotas
 */
function getDefaultQuotas (appData, data, callback) {
    data["project"]["quota"] = { defaults: -1 };
    getProjectQuotasCb(null, data, appData, callback);
}

/**
 * @getProjectQuotas
 * public function
 * 1. URL /api/tenants/config/project-quotas/:id
 * 2. Gets project quotas from config api server
 * 3. Needs project id
 */
function getProjectQuotas (request, response, appData)
{
    var projectId = validateProjectId(request);
    projectQuotasAPIGet(projectId, response, appData);
}

/**
 * @updateProjectQuotas
{'fraction_non_alphanumeric': 0.11883596893421915, 'fraction_numerical': 0.003275007017872181, 'mean_word_length': 2.0251910557599775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3949838', 'n_tokens_mistral': 3031, 'n_tokens_neox': 2981, 'n_words': 640}