<commit_before>/**
 * modules/analytics data store: profiles.
 *
 * Site Kit by Google, Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import invariant from 'invariant';
import { groupBy } from 'lodash';

/**
 * Internal dependencies
 */
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { isValidAccountID, isValidPropertyID, parsePropertyID } from '../util';
import { STORE_NAME, PROFILE_CREATE } from './constants';

// Actions
const FETCH_CREATE_PROFILE = 'FETCH_CREATE_PROFILE';
const START_FETCH_CREATE_PROFILE = 'START_FETCH_CREATE_PROFILE';
const FINISH_FETCH_CREATE_PROFILE = 'FINISH_FETCH_CREATE_PROFILE';
const CATCH_FETCH_CREATE_PROFILE = 'CATCH_FETCH_CREATE_PROFILE';

const FETCH_PROFILES = 'FETCH_PROFILES';
const START_FETCH_PROFILES = 'START_FETCH_PROFILES';
const FINISH_FETCH_PROFILES = 'FINISH_FETCH_PROFILES';
const CATCH_FETCH_PROFILES = 'CATCH_FETCH_PROFILES';

const RECEIVE_CREATE_PROFILE = 'RECEIVE_CREATE_PROFILE';
const RECEIVE_PROFILES = 'RECEIVE_PROFILES';

export const INITIAL_STATE = {
	isFetchingCreateProfile: {},
	isFetchingProfiles: {},
	profiles: {},
};

export const actions = {
	/**
	 * Creates a new Analytics profile.
	 *
	 * Creates a new Analytics profile for an existing Google Analytics
	 * account + property combination.
	 *
	 * @since n.e.x.t
	 *
	 * @param {string} accountID  Google Analytics account ID.
	 * @param {string} propertyID Google Analytics property ID.
	 * @return {Object} Response and error objects.
	 */
	*createProfile( accountID, propertyID ) {
		invariant( accountID, 'accountID is required.' );
		invariant( propertyID, 'propertyID is required.' );
		let response, error;

		yield {
			payload: {
				accountID,
				propertyID,
			},
			type: START_FETCH_CREATE_PROFILE,
		};

		try {
			response = yield {
				payload: {
					accountID,
					propertyID,
				},
				type: FETCH_CREATE_PROFILE,
			};
			const profile = response;

			yield actions.receiveCreateProfile( { accountID, propertyID, profile } );

			yield {
				payload: {
					accountID,
					propertyID,
				},
				type: FINISH_FETCH_CREATE_PROFILE,
			};
		} catch ( e ) {
			error = e;
			yield {
				payload: {
					accountID,
					propertyID,
					error,
				},
				type: CATCH_FETCH_CREATE_PROFILE,
			};
		}

		return { response, error };
	},
	/**
	 * Fetches profiles from the server and add them to the store.
	 *
	 * @param {string} propertyID Google Analytics property ID.
	 * @return {Object} Response and error objects.
	 */
	*fetchProfiles( propertyID ) {
		let response, error;

		yield {
			payload: { propertyID },
			type: START_FETCH_PROFILES,
		};

		try {
			response = yield {
				payload: { propertyID },<commit_msg>Add invariant to fetchProfiles.<commit_after>/**
 * modules/analytics data store: profiles.
 *
 * Site Kit by Google, Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import invariant from 'invariant';
import { groupBy } from 'lodash';

/**
 * Internal dependencies
 */
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { isValidAccountID, isValidPropertyID, parsePropertyID } from '../util';
import { STORE_NAME, PROFILE_CREATE } from './constants';

// Actions
const FETCH_CREATE_PROFILE = 'FETCH_CREATE_PROFILE';
const START_FETCH_CREATE_PROFILE = 'START_FETCH_CREATE_PROFILE';
const FINISH_FETCH_CREATE_PROFILE = 'FINISH_FETCH_CREATE_PROFILE';
const CATCH_FETCH_CREATE_PROFILE = 'CATCH_FETCH_CREATE_PROFILE';

const FETCH_PROFILES = 'FETCH_PROFILES';
const START_FETCH_PROFILES = 'START_FETCH_PROFILES';
const FINISH_FETCH_PROFILES = 'FINISH_FETCH_PROFILES';
const CATCH_FETCH_PROFILES = 'CATCH_FETCH_PROFILES';

const RECEIVE_CREATE_PROFILE = 'RECEIVE_CREATE_PROFILE';
const RECEIVE_PROFILES = 'RECEIVE_PROFILES';

export const INITIAL_STATE = {
	isFetchingCreateProfile: {},
	isFetchingProfiles: {},
	profiles: {},
};

export const actions = {
	/**
	 * Creates a new Analytics profile.
	 *
	 * Creates a new Analytics profile for an existing Google Analytics
	 * account + property combination.
	 *
	 * @since n.e.x.t
	 *
	 * @param {string} accountID  Google Analytics account ID.
	 * @param {string} propertyID Google Analytics property ID.
	 * @return {Object} Response and error objects.
	 */
	*createProfile( accountID, propertyID ) {
		invariant( accountID, 'accountID is required.' );
		invariant( propertyID, 'propertyID is required.' );
		let response, error;

		yield {
			payload: {
				accountID,
				propertyID,
			},
			type: START_FETCH_CREATE_PROFILE,
		};

		try {
			response = yield {
				payload: {
					accountID,
					propertyID,
				},
				type: FETCH_CREATE_PROFILE,
			};
			const profile = response;

			yield actions.receiveCreateProfile( { accountID, propertyID, profile } );

			yield {
				payload: {
					accountID,
					propertyID,
				},
				type: FINISH_FETCH_CREATE_PROFILE,
			};
		} catch ( e ) {
			error = e;
			yield {
				payload: {
					accountID,
					propertyID,
					error,
				},
				type: CATCH_FETCH_CREATE_PROFILE,
			};
		}

		return { response, error };
	},
	/**
	 * Fetches profiles from the server and add them to the store.
	 *
	 * @param {string} propertyID Google Analytics property ID.
	 * @return {Object} Response and error objects.
	 */
	*fetchProfiles( propertyID ) {
		invariant( isValidPropertyID( propertyID ), 'a valid property ID is required to fetch profiles for.' );
		let response, error;

		yield {
			payload: { propertyID },
			type: START_FETCH_PROFILES,
		};

		try {
			response = yield {
				payload: { propertyID },
{'fraction_non_alphanumeric': 0.09781633263535747, 'fraction_numerical': 0.0023930601256356566, 'mean_word_length': 3.766215253029223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '364834', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2066, 'n_words': 614}