<commit_before>/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016  Khartec Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.khartec.waltz.jobs.generators;

import com.khartec.waltz.common.RandomUtilities;
import com.khartec.waltz.data.physical_specification.PhysicalSpecificationDao;
import com.khartec.waltz.model.Criticality;
import com.khartec.waltz.model.EntityLifecycleStatus;
import com.khartec.waltz.model.enum_value.EnumValueKind;
import com.khartec.waltz.model.physical_flow.FrequencyKind;
import com.khartec.waltz.model.physical_specification.PhysicalSpecification;
import com.khartec.waltz.schema.tables.records.PhysicalFlowRecord;
import org.jooq.DSLContext;
import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple3;
import org.springframework.context.ApplicationContext;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.khartec.waltz.common.CollectionUtilities.isEmpty;
import static com.khartec.waltz.common.ListUtilities.newArrayList;
import static com.khartec.waltz.common.MapUtilities.groupBy;
import static com.khartec.waltz.common.RandomUtilities.randomPick;
import static com.khartec.waltz.data.physical_specification.PhysicalSpecificationDao.owningEntityNameField;
import static com.khartec.waltz.schema.tables.EnumValue.ENUM_VALUE;
import static com.khartec.waltz.schema.tables.LogicalFlow.LOGICAL_FLOW;
import static com.khartec.waltz.schema.tables.PhysicalFlow.PHYSICAL_FLOW;
import static com.khartec.waltz.schema.tables.PhysicalSpecification.PHYSICAL_SPECIFICATION;


public class PhysicalFlowGenerator implements SampleDataGenerator {

    private static final Random rnd = RandomUtilities.getRandom();

    private static List<Criticality> criticalityDistribution = newArrayList(
            Criticality.NONE,
            Criticality.UNKNOWN,
            Criticality.LOW,
            Criticality.LOW,
            Criticality.LOW,
            Criticality.MEDIUM,
            Criticality.MEDIUM,
            Criticality.HIGH,
            Criticality.HIGH,
            Criticality.HIGH,
            Criticality.VERY_HIGH,
            Criticality.VERY_HIGH);


    private static List<PhysicalFlowRecord> mkPhysicalFlowRecords(PhysicalSpecification spec,
                                                                  List<Long> logicalFlowIds,
                                                                  List<String> transportKinds) {


        return IntStream.range(0, logicalFlowIds.size() - 1)
                .mapToObj(i -> {
                    Long logicalFlowId = logicalFlowIds.remove(rnd.nextInt(logicalFlowIds.size() - 1));

                    PhysicalFlowRecord record = new PhysicalFlowRecord();
                    record.setSpecificationId(spec.id().get());
                    record.setLogicalFlowId(logicalFlowId);
                    record.setDescription("Description: " + spec + " - " + logicalFlowId.toString());
                    record.setProvenance(SAMPLE_DATA_PROVENANCE);
                    record.setBasisOffset(randomPick(newArrayList(0, 0, 0, 0, 1, 1, 2, -1)));
                    record.setTransport(randomPick(transportKinds));
                    record.setFrequency(randomPick(FrequencyKind.values()).name());
                    record.setLastUpdatedBy("admin");
                    record.setCriticality(randomPick(criticalityDistribution).name());
                    record.setEntityLifecycleStatus(randomPick(EntityLifecycleStatus.ACTIVE, EntityLifecycleStatus.PENDING).name());
                    return record;
                })
                .collect(Collectors.toList());
    }


    @Override
    public Map<String, Integer> create(ApplicationContext ctx) {
        DSLContext dsl = getDsl(ctx);

        List<PhysicalSpecification> specifications = dsl
                .select(PHYSICAL_SPECIFICATION.fields())
                .select(owningEntityNameField)
                .from(PHYSICAL_SPECIFICATION)
                .fetch(PhysicalSpecificationDao.TO_DOMAIN_MAPPER);

        List<String> transportKinds = dsl<commit_msg>ChangeSets: write sample data generator<commit_after>/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016  Khartec Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.khartec.waltz.jobs.generators;

import com.khartec.waltz.common.RandomUtilities;
import com.khartec.waltz.data.physical_specification.PhysicalSpecificationDao;
import com.khartec.waltz.model.Criticality;
import com.khartec.waltz.model.EntityLifecycleStatus;
import com.khartec.waltz.model.enum_value.EnumValueKind;
import com.khartec.waltz.model.physical_flow.FrequencyKind;
import com.khartec.waltz.model.physical_specification.PhysicalSpecification;
import com.khartec.waltz.schema.tables.records.PhysicalFlowRecord;
import org.jooq.DSLContext;
import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple3;
import org.springframework.context.ApplicationContext;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.khartec.waltz.common.CollectionUtilities.isEmpty;
import static com.khartec.waltz.common.ListUtilities.newArrayList;
import static com.khartec.waltz.common.MapUtilities.groupBy;
import static com.khartec.waltz.common.RandomUtilities.randomPick;
import static com.khartec.waltz.data.physical_specification.PhysicalSpecificationDao.owningEntityNameField;
import static com.khartec.waltz.schema.tables.EnumValue.ENUM_VALUE;
import static com.khartec.waltz.schema.tables.LogicalFlow.LOGICAL_FLOW;
import static com.khartec.waltz.schema.tables.PhysicalFlow.PHYSICAL_FLOW;
import static com.khartec.waltz.schema.tables.PhysicalSpecification.PHYSICAL_SPECIFICATION;


public class PhysicalFlowGenerator implements SampleDataGenerator {

    private static final Random rnd = RandomUtilities.getRandom();

    private static List<Criticality> criticalityDistribution = newArrayList(
            Criticality.NONE,
            Criticality.UNKNOWN,
            Criticality.LOW,
            Criticality.LOW,
            Criticality.LOW,
            Criticality.MEDIUM,
            Criticality.MEDIUM,
            Criticality.HIGH,
            Criticality.HIGH,
            Criticality.HIGH,
            Criticality.VERY_HIGH,
            Criticality.VERY_HIGH);

    private static List<EntityLifecycleStatus> lifecycleStatusDistribution = newArrayList(
            EntityLifecycleStatus.ACTIVE,
            EntityLifecycleStatus.ACTIVE,
            EntityLifecycleStatus.ACTIVE,
            EntityLifecycleStatus.PENDING);


    private static List<PhysicalFlowRecord> mkPhysicalFlowRecords(PhysicalSpecification spec,
                                                                  List<Long> logicalFlowIds,
                                                                  List<String> transportKinds) {


        return IntStream.range(0, logicalFlowIds.size() - 1)
                .mapToObj(i -> {
                    Long logicalFlowId = logicalFlowIds.remove(rnd.nextInt(logicalFlowIds.size() - 1));

                    PhysicalFlowRecord record = new PhysicalFlowRecord();
                    record.setSpecificationId(spec.id().get());
                    record.setLogicalFlowId(logicalFlowId);
                    record.setDescription("Description: " + spec + " - " + logicalFlowId.toString());
                    record.setProvenance(SAMPLE_DATA_PROVENANCE);
                    record.setBasisOffset(randomPick(newArrayList(0, 0, 0, 0, 1, 1, 2, -1)));
                    record.setTransport(randomPick(transportKinds));
                    record.setFrequency(randomPick(FrequencyKind.values()).name());
                    record.setLastUpdatedBy("admin");
                    record.setCriticality(randomPick(criticalityDistribution).name());
                    record.setEntityLifecycleStatus(randomPick(lifecycleStatusDistribution).name());
                    return record;
                })
                .collect(Collectors.toList());
    }


    @Override
    public Map<String, Integer> create(ApplicationContext ctx) {
        DSLContext dsl = getDsl(ctx);

        List<PhysicalSpecification> specifications = dsl
                .select(PHYSICAL_SPECIFICATION.fields())
                .select(owningEntityNameField)
                .from(PHYSICAL_SPECIFICATION)
                .fetch(PhysicalSpecificationDao.TO_DOMAIN_MAPPER);

        List<String> transportKinds = dsl
{'fraction_non_alphanumeric': 0.08389813103306634, 'fraction_numerical': 0.0034914766892585746, 'mean_word_length': 2.8785344484269215, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6003985', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2588, 'n_words': 535}