<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This file tests the API for retrieving meters, by artificially
 * inserting meters prior to executing the test code. */

// The test line below does not work if you cannot properly connect
// to the database.
// let { chai, mocha, expect, app, testDB, testUser } = require('../common');
mocha = require('mocha');
chai = require('chai');
const ini = require('ini');
const fs = require('fs');
const Meter = require('../../models/Meter');
const ConfigFile = require('../../models/obvius/Configfile');

mocha.describe('Obvius meter config processing', () => {
    mocha.it('should create the right meters based on the config file', () => {
        const configSerialNumber = 'mb-001';
        const configFilePath = `./obvius/${configSerialNumber}.ini`;
        const config = ini.parse(fs.readFileSync(configFilePath, 'utf-8'));
        const regularExpression = /([0-9][0-9])/;
        // For the metersHash we assume each key corresponds 
        // to a hash of structure { NAME: <alternative name>, UNITS: <units>, LOW:<>, HIGHT:<>, CONSOLE:<> }
        const metersHash = {};
        // Array of Meter (from models) objects
        const metersArray = [];
        for (key in config) {
            const [, number, characteristic] = key.split(regularExpression);
            const internalMeterName = `${configSerialNumber}.${parseInt(number)}`
            const meter = metersHash[internalMeterName];
            metersHash[internalMeterName] = { ...meter, [characteristic]: config[key] }
        };
        for (internalMeterName in metersHash) {
            metersArray.push(new Meter(undefined, internalMeterName, undefined, undefined, undefined, undefined, metersHash[internalMeterName].NAME));
        }
        console.log('', metersHash);
        console.log(metersArray);
    });
    mocha.it('should consume a ConfigFile Object', () => {
        const configSerialNumber = 'mb-001';
        const configFilePath = `./obvius/${configSerialNumber}.ini`;
        const contents = (fs.readFileSync(configFilePath, 'utf-8'))
        //const blob = new Blob([content], { type: "text/plain;charset=utf-9"});
        //const file = new File(blob, 'mb-001.ini');
        // const req = {params: {serialNumber: 'mb-001'}, files: [file]}
        const configFile = new ConfigFile(undefined, configSerialNumber, undefined, undefined, undefined, contents, false);
        console.log(processConfigFile(configFile));
    });
});

/**
 * Creates meters from a config file 
 * @param {ConfigFile} configFile
 */
function processConfigFile(configFile) {
    const config = ini.parse(configFile.contents);
    const regularExpression = /([0-9][0-9])/;
    // For the metersHash we assume each key corresponds 
    // to a hash of structure { NAME: <alternative name>, UNITS: <units>, LOW:<>, HIGHT:<>, CONSOLE:<> }
    const metersHash = {};
    // Array of Meter (from models) objects
    const metersArray = [];
    for (key in config) {
        const [, number, characteristic] = key.split(regularExpression);
        const internalMeterName = `${configFile.serialId}.${parseInt(number)}`
        const meter = metersHash[internalMeterName];
        metersHash[internalMeterName] = { ...meter, [characteristic]: config[key] }
    };
    for (internalMeterName in metersHash) {
        metersArray.push(new Meter(undefined, internalMeterName, undefined, undefined, undefined, undefined, metersHash[internalMeterName].NAME));
    }
    return metersArray;
}<commit_msg>Add returns param<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This file tests the API for retrieving meters, by artificially
 * inserting meters prior to executing the test code. */

// The test line below does not work if you cannot properly connect
// to the database.
// let { chai, mocha, expect, app, testDB, testUser } = require('../common');
mocha = require('mocha');
chai = require('chai');
const ini = require('ini');
const fs = require('fs');
const Meter = require('../../models/Meter');
const ConfigFile = require('../../models/obvius/Configfile');

mocha.describe('Obvius meter config processing', () => {
    mocha.it('should create the right meters based on the config file', () => {
        const configSerialNumber = 'mb-001';
        const configFilePath = `./obvius/${configSerialNumber}.ini`;
        const config = ini.parse(fs.readFileSync(configFilePath, 'utf-8'));
        const regularExpression = /([0-9][0-9])/;
        // For the metersHash we assume each key corresponds 
        // to a hash of structure { NAME: <alternative name>, UNITS: <units>, LOW:<>, HIGHT:<>, CONSOLE:<> }
        const metersHash = {};
        // Array of Meter (from models) objects
        const metersArray = [];
        for (key in config) {
            const [, number, characteristic] = key.split(regularExpression);
            const internalMeterName = `${configSerialNumber}.${parseInt(number)}`
            const meter = metersHash[internalMeterName];
            metersHash[internalMeterName] = { ...meter, [characteristic]: config[key] }
        };
        for (internalMeterName in metersHash) {
            metersArray.push(new Meter(undefined, internalMeterName, undefined, undefined, undefined, undefined, metersHash[internalMeterName].NAME));
        }
        console.log('', metersHash);
        console.log(metersArray);
    });
    mocha.it('should consume a ConfigFile Object', () => {
        const configSerialNumber = 'mb-001';
        const configFilePath = `./obvius/${configSerialNumber}.ini`;
        const contents = (fs.readFileSync(configFilePath, 'utf-8'))
        //const blob = new Blob([content], { type: "text/plain;charset=utf-9"});
        //const file = new File(blob, 'mb-001.ini');
        // const req = {params: {serialNumber: 'mb-001'}, files: [file]}
        const configFile = new ConfigFile(undefined, configSerialNumber, undefined, undefined, undefined, contents, false);
        console.log(processConfigFile(configFile));
    });
});

/**
 * Creates meters from a config file 
 * @param {ConfigFile} configFile
 * @returns {Meter[]} an array of Meter objects
 */
function processConfigFile(configFile) {
    const config = ini.parse(configFile.contents);
    const regularExpression = /([0-9][0-9])/;
    // For the metersHash we assume each key corresponds 
    // to a hash of structure { NAME: <alternative name>, UNITS: <units>, LOW:<>, HIGHT:<>, CONSOLE:<> }
    const metersHash = {};
    // Array of Meter (from models) objects
    const metersArray = [];
    for (key in config) {
        const [, number, characteristic] = key.split(regularExpression);
        const internalMeterName = `${configFile.serialId}.${parseInt(number)}`
        const meter = metersHash[internalMeterName];
        metersHash[internalMeterName] = { ...meter, [characteristic]: config[key] }
    };
    for (internalMeterName in metersHash) {
        metersArray.push(new Meter(undefined, internalMeterName, undefined, undefined, undefined, undefined, metersHash[internalMeterName].NAME));
    }
    return metersArray;
}
{'fraction_non_alphanumeric': 0.13947226709746904, 'fraction_numerical': 0.007269789983844911, 'mean_word_length': 3.240296803652968, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, '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': '4764511', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2066, 'n_words': 625}