
/***********************************************************************************
Name: Create_table_fw_ods 
Desc :  Script creates the FW Datatypes and  the fw_ods tables which store the final dim and measures and creates the following tables
		fw_ods   - Stores all the dim and measures for all the test types
		fw_ods_detailed  - stores the large sets of data such as steering data from IHT.
		fw_ods_tractor_specifications - tractor specifications which will be loaded manually 
Version Rev: v0.6
Create Date: 15 April 2021
Version History:
		v0.1 -  Base Version	
		v0.5 -  ADDED FW DATA types
		v0.6 - Added the fw_ods_detailed and fw_ods_tractor_specifications tables

*************************************************************************************/

set search_path to fw_ods;
-- Create FW DATA TYPEs to ensure there is no data issues.

-- SERIAL and BIGSERIAL datatypes are used from syspk columns in dim and fact tables.
-- AS UDTs can't be defined on SERIAL  data type.


CREATE DOMAIN fw_flag_bool AS
	BOOL;

	
CREATE DOMAIN fw_flag_YN AS
	BPCHAR;
	

CREATE DOMAIN fw_date AS
	DATE;


CREATE DOMAIN fw_num_dimid AS
	INTEGER;
	
CREATE DOMAIN fw_num_bigid AS
BIGINT;


CREATE DOMAIN fw_num_metric AS
	DECIMAL(15,4);
	

CREATE DOMAIN fw_num_array AS
   INTEGER ARRAY;
   

   CREATE DOMAIN fw_text AS
   TEXT;
   

CREATE DOMAIN fw_alphanum AS
   TEXT;
   

CREATE DOMAIN fw_text_array AS
   TEXT ARRAY;   
   

CREATE DOMAIN fw_timestamp AS
   TIMESTAMP;  


CREATE DOMAIN fw_json AS
   JSON;  

-- fw_ods table stores all relevant dim and metric/measures.

drop table if exists fw_ods.fw_ods;

CREATE TABLE fw_ods.fw_ods (
	syspk bigserial NOT NULL,
	client_id fw_num_dimid NOT NULL,
	function_id fw_num_dimid NOT NULL,
	test_file_mnemonic fw_text NULL,
	test_sheet_mnemonic fw_text NULL,
	test_name fw_text NULL,
	test_file_id fw_num_dimid NULL,
	test_file_name fw_text NULL,
	test_instance_id fw_num_dimid NULL,
	tractor_make fw_text NULL,
	tractor_model fw_text NULL,
	tractor_engine_hp fw_num_dimid NULL,
	wheel_drive_type fw_text NULL,
	"configuration" fw_text NULL,
	test_instance_tractor_id fw_num_dimid NULL,
	season fw_text NULL,
	test_date fw_date NULL,
	test_date_year fw_num_dimid NULL,
	test_date_quarter fw_num_dimid NULL,
	test_date_month fw_num_dimid NULL,
	test_date_dayofmonth int2 NULL,
	test_date_dayofweek int2 NULL,
	test_mode fw_text NULL,
	test_type fw_text NULL,
	test_sub_type fw_text NULL,
	test_condition fw_text NULL,
	test_purpose fw_text NULL,
	test_iteration_number fw_num_dimid NULL,
	test_ambient fw_text NULL,
	test_tractor_yn fw_text NULL,
	mahindra_model_yn fw_text NULL,
	location_name fw_text NULL,
	type_of_soil fw_text NULL,
	name_of_implement fw_text NULL,
	type_of_implement fw_text NULL,
	implement_weight_kg fw_num_dimid NULL,
	tyre_size fw_text NULL,
	tyre_ply_rating fw_num_dimid NULL,
	tyre_pressure_kg_per_cm2 fw_num_metric NULL,
	engine_speed_var fw_num_dimid NULL,
	engine_load_pct_var fw_num_dimid NULL,
	gear_used fw_text NULL,
	gear_used_up_slope_1 fw_text NULL,
	gear_used_up_slope_2 fw_text NULL,
	gear_used_down_slope fw_text NULL,
	gear_forward_reverse fw_text NULL,
	gear_pedal_effort_parameters fw_text NULL,
	sensor_body_location fw_text NULL,
	effort_type fw_text NULL,
	engine_rpm_type fw_text NULL,
	fuel_consumption_lit_per_hr fw_num_metric NULL,
	fuel_consumption_lit_per_acr fw_num_metric NULL,
	area_covered_acr_per_hr fw_num_metric NULL,
	speed_kmph fw_num_metric NULL,
	wheel_slippage_pct fw_num_metric NULL,
	field_efficiency_pct fw_num_metric NULL,
	productivity fw_num_metric NULL,
	erpmdrop_straight_1stpass_low fw_num_dimid NULL,
	erpmdrop_straight_1stpass_high fw_num_dimid NULL,
	erpmdrop_straight_2ndpass_low fw_num_dimid NULL,
	erpmdrop_straight_2ndpass_high fw_num_dimid NULL,
	erpmdrop_straight_3rdpass_low fw_num_dimid NULL,
	erpmdrop_straight_3rdpass_high fw_num_dimid NULL,
	erpmdrop_turn_1stpass_low fw_num_dimid NULL,
	erpmdrop_turn_1stpass_high fw_num_dimid NULL,
	erpmdrop_turn_2ndpass_low fw_num_dimid NULL,
	erpmdrop_turn_2ndpass_high fw_num_dimid NULL,
	erpmdrop_turn_3rdpass_low fw_num_dimid NULL,
	erpmdrop_turn_3rdpass_high fw_num_dimid NULL,
	no_of_passes fw_num_dimid NULL,
	depth_of_cut_cm_low fw_num_metric NULL,
	depth_of_cut_cm_high fw_num_metric NULL,
	total_dist_travelled_km fw_num_metric NULL,
	mileage_kmpl fw_num_metric NULL,
	power_kw fw_num_metric NULL,
	power_hp fw_num_metric NULL,
	pull_kgf fw_num_metric NULL,
	pull_kn fw_num_metric NULL,
	effort_kg fw_num_metric NULL,
	torque_kg_m fw_num_metric NULL,
	torque_nm fw_num_metric NULL,
	sfc_gms_per_hp_hr fw_num_metric NULL,
	sfc_g_per_kwh fw_num_metric NULL,
	fuelling_mm3_per_stroke_per_cyl fw_num_metric NULL,
	specific_energy_kwh_per_ltr fw_num_metric NULL,
	temp_engine_oil_c fw_num_dimid NULL,
	temp_engine_oil_high_c fw_num_dimid NULL,
	temp_trans_oil_c fw_num_dimid NULL,
	temp_trans_oil_high_c fw_num_dimid NULL,
	temp_coolant_c fw_num_dimid NULL,
	temp_coolant_high_c fw_num_dimid NULL,
	temp_fuel_in_c fw_num_dimid NULL,
	temp_fuel_high_c fw_num_dimid NULL,
	temp_fuel_out_c fw_num_dimid NULL,
	temp_water_outlet_c fw_num_dimid NULL,
	temp_exhaust_c fw_num_dimid NULL,
	temp_fuel_temp_c fw_num_dimid NULL,
	pct_of_backup_torque fw_num_dimid NULL,
	equ_crank_torque_nm fw_num_metric NULL,
	max_equ_crankshaft_torque_nm fw_num_metric NULL,
	load_pct fw_num_dimid NULL,
	load_kg fw_num_metric NULL,
	required_stopping_distance_m fw_num_metric NULL,
	actual_stopping_distance_m fw_num_metric NULL,
	decceleartoin_m_per_s2 fw_num_metric NULL,
	lifting_time_sec fw_num_dimid NULL,
	lowering_time_sec fw_num_dimid NULL,
	speed_on_high_rpm_kmph fw_num_metric NULL,
	speed_on_low_rpm_kmph fw_num_metric NULL,
	speed_on_rated_rpm_kmph fw_num_metric NULL,
	body_location_temperature fw_num_dimid NULL,
	speed_pto_rpm fw_num_dimid NULL,
	brake_pedal_travel_lh_in_mm fw_num_dimid NULL,
	brake_pedal_travel_rh_in_mm fw_num_dimid NULL,
	brake_pedal_travel_latched_in_mm fw_num_dimid NULL,
	pedal_travel_in_mm fw_num_dimid NULL,
	noise_dba fw_num_metric NULL,
	test_file_load_date fw_timestamp NULL,
	updated_by fw_text NULL ,
	created_by fw_text NULL DEFAULT CURRENT_USER,
	update_timestamp fw_timestamp NULL,
	create_timestamp fw_timestamp NULL DEFAULT now(),
	CONSTRAINT fw_ods_pkey PRIMARY KEY (syspk)
);


-- fw_ods_detailed table is used to store all big data, for example 6k records of stearing data from sensors.
DROP TABLE  if exists fw_ods.fw_ods_detailed;
CREATE TABLE fw_ods.fw_ods_detailed (
	syspk serial NOT NULL,
	client_id fw_num_dimid NOT NULL,
	function_id fw_num_dimid NOT NULL,
	test_file_mnemonic fw_num_dimid NULL,
	test_sheet_mnemonic fw_num_dimid NULL,
	test_file_id fw_num_dimid NULL,
	test_file_name fw_text NULL,
	test_instance_id fw_num_dimid NULL,
	tractor_make fw_text NULL,
	tractor_model fw_text NULL,
	tractor_engine_hp fw_num_dimid NULL,
	wheel_drive_type fw_text NULL,
	"configuration" fw_text NULL,
	test_insance_tractor_id fw_num_dimid NULL,
	season fw_text NULL,
	test_date fw_date NULL,
	test_date_year fw_num_dimid NULL,
	test_date_quarter fw_num_dimid NULL,
	test_date_month fw_num_dimid NULL,
	test_date_dayofmonth fw_num_dimid NULL,
	test_date_dayofweek fw_num_dimid NULL,
	test_mode fw_text NULL,
	test_type fw_text NULL,
	test_sub_type fw_text NULL,
	test_condition fw_text NULL,
	location_name fw_text NULL,
	test_detail_type fw_text NULL,
	time_sec fw_num_metric NULL,
	steering_angle_degree fw_num_metric NULL,
	effort_kg fw_num_metric NULL,
	test_file_load_date fw_timestamp NULL,
	updated_by text NULL ,
	created_by text NULL DEFAULT CURRENT_USER,
	update_timestamp timestamp NULL ,
	create_timestamp timestamp NULL DEFAULT now()
);


-- fw_ids_tractor_specifications table is used to store all tractor specifications created before mmt testing project starts.
-- the data into table will uploaded by the user manually.

drop table if exists fw_ods.fw_ods_tractor_specifications;
create table fw_ods.fw_ods_tractor_specifications (
	syspk serial NOT NULL,				-- PK
	Client_Id fw_num_dimid NOT NULL,				
	Function_Id fw_num_dimid NOT NULL,			
	Test_file_name fw_text NOT NULL,		
	Tractor_model fw_text, 				
	Tractor_Spec_group fw_text,			
	Tractor_spec_parameter fw_text,		
	Tractor_spec_param_value fw_text,
	Created_By fw_text NULL DEFAULT current_user,
	Updated_by fw_text NULL,
	Create_timestamp fw_timestamp NULL DEFAULT now(),
	update_timestamp fw_TIMESTAMP NULL
);


-- fw_ods.fw_ods_comments definition

-- Drop table

DROP TABLE if exists fw_ods.fw_ods_comments;

CREATE TABLE fw_ods.fw_ods_comments (
	syspk bigserial NOT NULL,
	client_id fw_ods."fw_num_dimid" NULL,
	function_id fw_ods."fw_num_dimid" NULL,
	test_file_mnemonic fw_ods."fw_text" NULL,
	test_sheet_mnemonic fw_ods."fw_text" NULL,
	test_file_id fw_ods."fw_num_dimid" NULL,
	test_file_name fw_ods."fw_text" NULL,
	test_instance_id fw_ods."fw_num_dimid" NULL,
	tractor_make fw_ods."fw_text" NULL,
	tractor_model fw_ods."fw_text" NULL,
	test_iteration_number fw_ods."fw_num_dimid" NULL,
	users fw_ods."fw_text" NULL,
	"role" fw_ods."fw_text" NULL,
	role_level fw_ods."fw_num_dimid" NULL,
	note_or_title fw_ods."fw_text" NULL,
	"timestamp" fw_ods."fw_timestamp" NULL,
	"sequence" fw_ods."fw_text" NULL,
	note_or_comment fw_ods."fw_text" NULL,
	created_by fw_ods."fw_text" NULL DEFAULT CURRENT_USER,
	updated_by fw_ods."fw_text" NULL,
	create_timestamp fw_ods."fw_timestamp" NULL DEFAULT now(),
	update_timestamp fw_ods."fw_timestamp" NULL,
	old_test_file_name text NULL,
	platform text NULL,
	benchmark_yn text NULL,
	wheel_drive_type text NULL,
	tractor_engine_hp int4 NULL,
	name_of_implement fw_ods."fw_text" NULL,
	type_of_implement fw_ods."fw_text" NULL,
	season fw_ods."fw_text" NULL
);


-- fw_ods.fw_ods_drawbar_performance definition

-- Drop table

DROP TABLE  if exists fw_ods.fw_ods_drawbar_performance;

CREATE TABLE fw_ods.fw_ods_drawbar_performance (
	syspk serial NOT NULL,
	client_id fw_ods."fw_num_dimid" NOT NULL,
	function_id fw_ods."fw_num_dimid" NOT NULL,
	test_file_mnemonic fw_ods."fw_text" NULL,
	test_sheet_mnemonic fw_ods."fw_text" NULL,
	test_file_id fw_ods."fw_num_dimid" NULL,
	test_file_name fw_ods."fw_text" NULL,
	test_name fw_ods."fw_text" NULL,
	test_instance_id fw_ods."fw_num_dimid" NULL,
	tractor_make fw_ods."fw_text" NULL,
	tractor_model fw_ods."fw_text" NULL,
	tractor_engine_hp fw_ods."fw_num_dimid" NULL,
	wheel_drive_type fw_ods."fw_text" NULL,
	"configuration" fw_ods."fw_text" NULL,
	test_instance_tractor_id fw_ods."fw_num_dimid" NULL,
	season fw_ods."fw_text" NULL,
	test_date fw_ods."fw_date" NULL,
	test_date_year fw_ods."fw_num_dimid" NULL,
	test_date_quarter fw_ods."fw_num_dimid" NULL,
	test_date_month fw_ods."fw_num_dimid" NULL,
	test_date_dayofmonth int2 NULL,
	test_date_dayofweek int2 NULL,
	test_mode fw_ods."fw_text" NULL,
	test_type fw_ods."fw_text" NULL,
	test_sub_type fw_ods."fw_text" NULL,
	test_condition fw_ods."fw_text" NULL,
	test_purpose fw_ods."fw_text" NULL,
	test_iteration_number fw_ods."fw_num_dimid" NULL,
	test_ambient fw_ods."fw_text" NULL,
	test_tractor_yn fw_ods."fw_text" NULL,
	mahindra_model_yn fw_ods."fw_text" NULL,
	location_name fw_ods."fw_text" NULL,
	tyre_size fw_ods."fw_text" NULL,
	tyre_ply_rating fw_ods."fw_num_dimid" NULL,
	tyre_pressure_kg_per_cm2 fw_ods."fw_num_metric" NULL,
	engine_speed_var fw_ods."fw_num_dimid" NULL,
	fan_speed_rpm_var fw_ods."fw_num_dimid" NULL,
	gear_used fw_ods."fw_text" NULL,
	effort_type fw_ods."fw_text" NULL,
	engine_rpm_type fw_ods."fw_text" NULL,
	fuel_consumption_lit_per_hr fw_ods."fw_num_metric" NULL,
	fuel_consumption_kg_per_kwh fw_ods."fw_num_metric" NULL,
	sfc_g_per_kwh fw_ods."fw_num_metric" NULL,
	speed_kmph fw_ods."fw_num_metric" NULL,
	wheel_slippage_pct fw_ods."fw_num_metric" NULL,
	wheel_slippage_pct_front fw_ods."fw_num_metric" NULL,
	no_of_load_lugs_front fw_ods."fw_num_dimid" NULL,
	power_kw fw_ods."fw_num_metric" NULL,
	pull_kgf fw_ods."fw_num_metric" NULL,
	pull_kn fw_ods."fw_num_metric" NULL,
	effort_kg fw_ods."fw_num_metric" NULL,
	power_hp fw_ods."fw_num_metric" NULL,
	sfc_gms_per_hp_hr fw_ods."fw_num_metric" NULL,
	specific_energy_kwh_per_ltr fw_ods."fw_num_metric" NULL,
	temp_engine_oil_c fw_ods."fw_num_dimid" NULL,
	temp_engine_oil_high_c fw_ods."fw_num_dimid" NULL,
	temp_trans_oil_c fw_ods."fw_num_dimid" NULL,
	temp_trans_oil_high_c fw_ods."fw_num_dimid" NULL,
	temp_coolant_c fw_ods."fw_num_dimid" NULL,
	temp_coolant_high_c fw_ods."fw_num_dimid" NULL,
	temp_fuel_in_c fw_ods."fw_num_dimid" NULL,
	temp_fuel_high_c fw_ods."fw_num_dimid" NULL,
	temp_fuel_out_c fw_ods."fw_num_dimid" NULL,
	temp_water_outlet_c fw_ods."fw_num_dimid" NULL,
	atm_rh_pct fw_ods."fw_num_metric" NULL,
	atm_temp_c fw_ods."fw_num_metric" NULL,
	atm_pressure_kpa fw_ods."fw_num_metric" NULL,
	test_file_load_date fw_ods."fw_timestamp" NULL,
	updated_by fw_ods."fw_text" NULL,
	created_by fw_ods."fw_text" NULL DEFAULT CURRENT_USER,
	update_timestamp fw_ods."fw_timestamp" NULL,
	create_timestamp fw_ods."fw_timestamp" NULL DEFAULT now(),
	summary_yn text NULL,
	test_report_date date NULL,
	old_test_file_name fw_ods."fw_text" NULL,
	CONSTRAINT fw_ods_drawbar_performance_pkey PRIMARY KEY (syspk)
);


-- fw_ods.fw_ods_observations definition

-- Drop table

DROP TABLE if exist fw_ods.fw_ods_observations;

CREATE TABLE fw_ods.fw_ods_observations (
	syspk bigserial NOT NULL,
	client_id fw_ods."fw_num_dimid" NULL,
	function_id fw_ods."fw_num_dimid" NULL,
	test_file_mnemonic fw_ods."fw_text" NULL,
	test_sheet_mnemonic fw_ods."fw_text" NULL,
	test_file_id fw_ods."fw_num_dimid" NULL,
	test_file_name fw_ods."fw_text" NULL,
	test_iteration_number fw_ods."fw_num_dimid" NULL,
	platform fw_ods."fw_text" NULL,
	benchmark_yn fw_ods."fw_text" NULL,
	tractor_make fw_ods."fw_text" NULL,
	tractor_model fw_ods."fw_text" NULL,
	tractor_engine_hp fw_ods."fw_num_dimid" NULL,
	wheel_drive_type fw_ods."fw_text" NULL,
	name_of_implement fw_ods."fw_text" NULL,
	type_of_implement fw_ods."fw_text" NULL,
	season fw_ods."fw_text" NULL,
	parameters fw_ods."fw_text" NULL,
	"sequence" fw_ods."fw_num_dimid" NULL,
	tractor_model1 fw_ods."fw_text" NULL,
	tractor_model2 fw_ods."fw_text" NULL,
	tractor_model3 fw_ods."fw_text" NULL,
	tractor_model4 fw_ods."fw_text" NULL,
	tractor_model5 fw_ods."fw_text" NULL,
	display_yn fw_ods."fw_text" NULL,
	created_by fw_ods."fw_text" NULL,
	updated_by fw_ods."fw_text" NULL,
	create_timestamp fw_ods."fw_timestamp" NULL,
	update_timestamp fw_ods."fw_timestamp" NULL,
	old_test_file_name fw_ods."fw_text" NULL
);

