<commit_before>var _rate, _plan, _plan_id, _usaget, _appid, _prefixes = [], _tariffs = {};
db.tmp_PP_TARIFFS.find({}).forEach(
	function(obj1) {
		print("tariff name: " + obj1.PP_TARIFF_NAME);
		_rate = {};
		_prefixes = [];
		print("tariff id: " + obj1.PP_TARIFF_ID);
		db.tmp_PP_PLAN.find({PP_TARIFF_ID:obj1.PP_TARIFF_ID}).sort({TIME_TYPE:-1}).limit(1).forEach(function (plan) {_plan_id = plan.PP_PLAN_ID});
		print("plan_id: " + _plan_id);
		if (!_plan_id) return;
		switch(obj1.UNIT_TYPE) {
			case "1":
				_appid = '0';
				_usaget = 'cost';
				break;
			case "2":
				// Seconds
				_appid = '1';
				_usaget = 'call';
				break;
			case "3":
				// OCTET
				_appid = '4';
				_usaget = 'data';
				break;
			case "4":
				// SMS
				_appid = '2';
				_usaget = 'sms';
				break;
		}
		db.tmp_ACTIVITY_AND_PP_PLAN.find({PP_PLAN_ID:_plan_id}).forEach(
			function(obj2) {
				_appid = obj2.APPLICATION_ID;
				_allocations = [];
				db.tmp_SUBTYPE_TRANSLATION.find({"NEW_SUBTYPE" : obj2.NEW_SUBTYPE_ID, "APPLICATION_ID" : _appid}).forEach(
					function(obj3) {
						_allocations.push(obj3.LOCATION_ID);
					}
				);
				db.tmp_Prefix_Allocation_ID_clear.find({HOME_OPPS_ID:{$in:_allocations}}).forEach(
					function(obj4) {
						db.tmp_PPS_PREFIXES.find({BILLING_ALLOCATION:obj4.ALLOCATION_B}).forEach(
							function(obj5) {
								_prefixes.push(obj5.PPS_PREFIXES);
							}
						);
					}
				);
			}
		);
		_tariffs = {};
		_tariffs[_usaget] = {
			'access':   obj1.INITIAL_CHARGE,
			'rate':     {
				'to': 2147483647,
				'price': obj1.ADD_CHARGE,
				'interval': obj1.ADD_AMOUNT
			}
		}
		_rate = {
			'from':    ISODate('2015-12-01'),
			'to':      ISODate('2099-12-31 23:59:59'),
			'key':     obj1.PP_TARIFF_NAME,
			'params':  {
				'destination_prefixes': _prefixes
			},
			'rates': _tariffs
		};
		db.rates.insert(_rate);
	}
);<commit_msg>update migration script of rates, tariffs and plans<commit_after>// create temp indexes to make it quick
db.tmp_COS_PLANS.ensureIndex({'SUBTYPE_ID': 1, APPLICATION_ID: 1 }, { unique: false , sparse: false, background: true });
db.tmp_PP_TARIFFS.ensureIndex({"PP_TARIFF_ID": 1}, { unique: false , sparse: false, background: true });
db.tmp_COS.ensureIndex({"COS_ID": 1}, { unique: false , sparse: false, background: true });
db.tmp_Prefix_Allocation_ID_clear.ensureIndex({"ALLOCATION_B": 1}, { unique: false , sparse: false, background: true });
db.tmp_SUBTYPE_TRANSLATION.ensureIndex({"LOCATION_ID": 1}, { unique: false , sparse: false, background: true });
db.tmp_PP_PLAN.ensureIndex({"PP_PLAN_ID": 1}, { unique: false , sparse: false, background: true });

// long script to migrate the rates pricing
var _rate, _plan, _plan_id, _usaget, _appid, _prefixes = [], _tariffs = {}, _cos_id;
var _location_id, _subtype;
db.tmp_PPS_PREFIXES.aggregate({$group:{_id:"$BILLING_ALLOCATION", prefixes:{$addToSet:"$PPS_PREFIXES"}}}).forEach(
	function(obj1) {
		_rate_name = obj1._id;
		print("rate name: " + _rate_name);
		_prefixes = obj1.prefixes;
		_tariffs = {};
		db.tmp_Prefix_Allocation_ID_clear.find({ALLOCATION_B: _rate_name}).forEach(
			function(obj2) {
				if (obj2.HOME_OPPS_ID == '0' && obj2.HOME_TPPS_ID == '0') {
					return;
				}
				if (obj2.ALLOCATION_B == 'Anywhere') {
					_location_id = obj2.HOME_TPPS_ID;
				} else {
					_location_id = obj2.HOME_OPPS_ID;
				}
				print("location id: " + _location_id);
				db.tmp_SUBTYPE_TRANSLATION.find({"LOCATION_ID" : _location_id, SPECIAL_FEATURE:"0"}).forEach(
					function(obj3) {
						_subtype = obj3.NEW_SUBTYPE;
						_appid = obj3.APPLICATION_ID;
						db.tmp_COS_PLANS.find({"SUBTYPE_ID":_subtype, "APPLICATION_ID" : _appid, PP_PLAN_ID:{$exists:1, $ne:''}}).forEach(
							function(obj4) {
								db.tmp_COS.find({"COS_ID" : obj4.COS_ID}).forEach(function(obj5) {_plan_id = obj5.COS_NAME;});
								db.tmp_PP_PLAN.find({"PP_PLAN_ID" : obj4.PP_PLAN_ID}).sort({TIME_TYPE:-1}).limit(1).forEach(
									function(obj6) {
										db.tmp_PP_TARIFFS.find({"PP_TARIFF_ID":obj6.PP_TARIFF_ID}).forEach(
											function(obj7) {
												switch(obj7.UNIT_TYPE) {
													case "1":
														_usaget = 'cost';
														break;
													case "2":
														// Seconds
														_usaget = 'call';
														break;
													case "3":
														// OCTET
														_usaget = 'data';
														break;
													case "4":
														// SMS
														_usaget = 'sms';
														break;
												}
												print("usaget : " + _usaget);
												print("plan : " + _plan_id);
												if (_tariffs[_usaget] === undefined) _tariffs[_usaget] = {};
												_tariffs[_usaget][_plan_id] = {
													'access':   obj7.INITIAL_CHARGE,
													'rate':     {
														'to': 2147483647,
														'price': obj7.ADD_CHARGE,
														'interval': obj7.ADD_AMOUNT
													}
												};
											}
										);
									}
								);
							}
						);
					}
				);
			}
		);
		_rate = {
			'from':    ISODate('2015-12-01'),
			'to':      ISODate('2099-12-31 23:59:59'),
			'key':     _rate_name,
			'params':  {
				'prefixes': _prefixes
			},
			'rates': _tariffs
		};
		db.rates.insert(_rate);
	}
);

// migrate customer plans (COS)
db.tmp_COS.find().forEach(
	function(obj) {
		db.plans.insert({
			name:obj.COS_NAME,
			from:ISODate('2015-12-01'),
			to:ISODate('2099-12-31 23:59:59'),
			type:'customer',
			external_id: obj.COS_ID,
			external_code: obj.COS_CODE,
		});
	}
);
{'fraction_non_alphanumeric': 0.16127272727272726, 'fraction_numerical': 0.028181818181818183, 'mean_word_length': 2.652722443559097, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 3, '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': '6301423', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2161, 'n_words': 310}