<commit_before>/*
 * Copyright (c) 2010-2014 LabKey Corporation
 *
 * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
 */

require("ehr/triggers").initScript(this);

function onUpsert(helper, scriptErrors, row, oldRow){

    //for compatability with the ETL
    if (!row.performedby)
        row.performedby = row.userid;

    if (
        row.feces ||
        row.menses ||
        row.behavior ||
        row.breeding ||
        row.other ||
        row.tlocation ||
        row.remark ||
        row.otherbehavior
    ){
        row.isIrregular = true;
    }
    else {
        row.isIrregular = false;
    }

    if (row.behavior && !row.otherbehavior){
        let behaviorList = row.behavior.split(';');
        for (let i=0; i<behaviorList.length; i++){
            if (behaviorList[i] === 'NPR' || behaviorList[i] === 'NOB' || behaviorList[i] === 'OSIB'){<commit_msg>Adjust obs trigger script to save displayName only LK ticket 45200<commit_after>/*
 * Copyright (c) 2010-2014 LabKey Corporation
 *
 * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
 */

require("ehr/triggers").initScript(this);
var LABKEY = require("labkey");
//create a user id to displayname map to convert userid to displayname for the performedby field.
var userIdCache = {};
var userIdSelectConfig = {
    schemaName: 'core',
    queryName: 'users',
    success: function (response) {

        var result = response.rows;
        for (i = 0; i < result.length-1; i++) {
            userIdCache[result[i].UserId] = result[i].DisplayName
        }
    },
    failure: function (f) {
        console.log('obs.js failed to get core users data');
    }
}

LABKEY.Query.selectRows(userIdSelectConfig);

function onUpsert(helper, scriptErrors, row, oldRow){

    //for compatability with the ETL
    if (!row.performedby)
        row.performedby = row.userid;

    //set the performed by to the user's display name
    if (Object.keys(userIdCache).length) {
        if (userIdCache[row.performedby]) {
            row.performedby = userIdCache[row.performedby];
        } else {
            var getUserDisplayNameConfig = {
                schemaName: 'core',
                queryName: 'users',
                filterArray: [LABKEY.Filter.create('userid', row.performedby, LABKEY.Filter.Types.EQUALS)],
                success: function(data) {
                    var rows = data.rows;
                    if (rows[0]) {
                        row.performedby = rows[0].DisplayName;
                    }
                }
            }

            LABKEY.Query.selectRows(getUserDisplayNameConfig);
        }
    }

    if (
        row.feces ||
        row.menses ||
        row.behavior ||
        row.breeding ||
        row.other ||
        row.tlocation ||
        row.remark ||
        row.otherbehavior
    ){
        row.isIrregular = true;
    }
    else {
        row.isIrregular = false;
    }

    if (row.behavior && !row.otherbehavior){
        let behaviorList = row.behavior.split(';');
        for (let i=0; i<behaviorList.length; i++){
            if (behaviorList[i] === 'NPR' || behaviorList[i] === 'NOB' || behaviorList[i] === 'OSIB'){
{'fraction_non_alphanumeric': 0.13252638112973308, 'fraction_numerical': 0.010862818125387958, 'mean_word_length': 2.1140096618357487, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '4319235', 'n_tokens_mistral': 1017, 'n_tokens_neox': 965, 'n_words': 219}