<commit_before>import _ from "lodash";
import {kindToViewState, initialiseData} from "../common";

const initData = {
    statistic: {
        definition: null,
        summary: null,
        values: []
    },
    relatedDefinitions: null,
    summaries: [],
    navItems: [],
    selectedNavItem: null,
    parentStateRef: '.',
    visibility: {
        related: false
    }
};


function updateUrlWithoutReload($state, navItem) {
    $state.go('.', {id: navItem.id}, {notify: false});
}


function hasRelatedDefinitions(defs) {
    const relatedCount = defs.children.length
        + (defs.parent
            ? 1
            : 0)
        + defs.siblings.length;

    return relatedCount > 0;
}


function controller($q,
                    $state,
                    $stateParams,
                    bookmarkStore,
                    entityStatisticUtilities,
                    entityStatisticStore) {
    const vm = initialiseData(this, initData);

    const statId = $stateParams.statId;
    const entityKind = $stateParams.kind;
    const entityId = $stateParams.id;

    const statRef = {
        id: statId,
        kind: 'ENTITY_STATISTIC'
    };

    bookmarkStore
        .findByParent(statRef)
        .then(bs => vm.bookmarks = bs);

    const definitionPromise = entityStatisticStore
        .findRelatedStatDefinitions(statId)
        .then(ds => vm.relatedDefinitions = ds)
        .then(ds => vm.statistic.definition = ds.self)
        .then(() => vm.visibility.related = hasRelatedDefinitions(vm.relatedDefinitions));

    const navItemPromise = entityStatisticUtilities
        .findAllForKind(entityKind)
        .then(xs => vm.navItems = xs);

    $q.all([navItemPromise, definitionPromise])
        .then(() => /* boot */ vm.onSelectNavItem(_.find(vm.navItems, { id: entityId })));


    function resetValueData() {
        const clearData = _.cloneDeep(initData);
        vm.statistic.summary = clearData.statistic.summary;
        vm.statistic.values = clearData.statistic.values;
        vm.summaries = clearData.summaries;
    }


    vm.onSelectNavItem = (navItem) => {
        resetValueData();

        vm.selectedNavItem = navItem;

        const entityReference = {
            id: navItem.id,
            kind: entityKind
        };
        vm.parentRef = entityReference;


        const selector = {
            scope: 'CHILDREN',
            entityReference
        };

        entityStatisticStore
            .findStatTallies(vm.relatedDefinitions, selector)
            .then(summaries => vm.summaries = summaries)
            .then(summaries => vm.statistic.summary = _.find(summaries, { entityReference: { id: statId }}));

        entityStatisticStore
            .findStatValuesByIdSelector(statId, selector)
            .then(stats => vm.statistic.values = stats);<commit_msg>- fix for old values in ent stat view spilling over to new criteria  #339<commit_after>import _ from "lodash";
import {kindToViewState, initialiseData} from "../common";

const initData = {
    statistic: {
        definition: null,
        summary: null,
        values: []
    },
    relatedDefinitions: null,
    summaries: [],
    navItems: [],
    selectedNavItem: null,
    parentStateRef: '.',
    visibility: {
        related: false
    }
};


function updateUrlWithoutReload($state, navItem) {
    $state.go('.', {id: navItem.id}, {notify: false});
}


function hasRelatedDefinitions(defs) {
    const relatedCount = defs.children.length
        + (defs.parent
            ? 1
            : 0)
        + defs.siblings.length;

    return relatedCount > 0;
}


function controller($q,
                    $state,
                    $stateParams,
                    bookmarkStore,
                    entityStatisticUtilities,
                    entityStatisticStore) {
    const vm = initialiseData(this, initData);

    const statId = $stateParams.statId;
    const entityKind = $stateParams.kind;
    const entityId = $stateParams.id;

    const statRef = {
        id: statId,
        kind: 'ENTITY_STATISTIC'
    };

    bookmarkStore
        .findByParent(statRef)
        .then(bs => vm.bookmarks = bs);

    const definitionPromise = entityStatisticStore
        .findRelatedStatDefinitions(statId)
        .then(ds => vm.relatedDefinitions = ds)
        .then(ds => vm.statistic.definition = ds.self)
        .then(() => vm.visibility.related = hasRelatedDefinitions(vm.relatedDefinitions));

    const navItemPromise = entityStatisticUtilities
        .findAllForKind(entityKind)
        .then(xs => vm.navItems = xs);

    $q.all([navItemPromise, definitionPromise])
        .then(() => /* boot */ vm.onSelectNavItem(_.find(vm.navItems, { id: entityId })));


    function resetValueData() {
        const clearData = initialiseData({}, initData);
        vm.statistic.summary = clearData.statistic.summary;
        vm.statistic.values = clearData.statistic.values;
        vm.summaries = clearData.summaries;
    }


    vm.onSelectNavItem = (navItem) => {
        resetValueData();

        vm.selectedNavItem = navItem;

        const entityReference = {
            id: navItem.id,
            kind: entityKind
        };
        vm.parentRef = entityReference;


        const selector = {
            scope: 'CHILDREN',
            entityReference
        };

        entityStatisticStore
            .findStatTallies(vm.relatedDefinitions, selector)
            .then(summaries => vm.summaries = summaries)
            .then(summaries => vm.statistic.summary = _.find(summaries, { entityReference: { id: statId }}));

        entityStatisticStore
            .findStatValuesByIdSelector(statId, selector)
            .then(stats => vm.statistic.values = stats);
{'fraction_non_alphanumeric': 0.11202520567127604, 'fraction_numerical': 0.0015753544547523193, 'mean_word_length': 2.197537772803581, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '7148300', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1603, 'n_words': 316}