raena_analytics_scripts/redshift/rdash_dashboard.sh

150 lines
5.3 KiB
Bash
Raw Permalink Normal View History

2024-06-24 12:26:08 +00:00
#!/bin/bash
echo -e " \n----------- ACCEPTING NUMBER OF DAYS BEFORE THE RUN DATE FOR WHICH THE REPORT IS TO BE RUN --------------\n"
backDay=$1
echo $backDay
echo -e " \n------------- DATE IN THE REQUIRED FORMAT --------------\n"
reportDate=$(date -d"$backDay day ago" "+%Y-%m-%d")
date
echo 'reportDate'=$reportDate
echo "
--------------------------Rdash -------------------------
DROP TABLE IF EXISTS raena_analytics.om_mongo_brand_category;
CREATE TABLE raena_analytics.om_mongo_brand_category AS
select rsellviewid,
json_extract_path_text(brand, 'name') as Brand_name,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'hasChild') as has_child,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'id') as actual_categoryid,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 0), 'name') as actual_category_name
from raena_analytics.mongo_baseproducts
union
select rsellviewid,
json_extract_path_text(brand, 'name') as Brand_name,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'hasChild') as has_child,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'id') as actual_categoryid,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 1), 'name') as actual_category_name
from raena_analytics.mongo_baseproducts mo
union
select rsellviewid,
json_extract_path_text(brand, 'name') as Brand_name,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'hasChild') as has_child,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'id') as actual_categoryid,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 2), 'name') as actual_category_name
from raena_analytics.mongo_baseproducts mo
union
select rsellviewid,
json_extract_path_text(brand, 'name') as Brand_name,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'hasChild') as has_child,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'id') as actual_categoryid,
json_extract_path_text(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(replace(categorypath, chr(39), ''), 3), 'name') as actual_category_name
from raena_analytics.mongo_baseproducts mo;
DROP TABLE IF exists raena_analytics.om_mongo_Order_item_details;
CREATE TABLE raena_analytics.om_mongo_Order_item_details AS
SELECT orderid,
orderitemid,
quantity,
originalprice,
rsellviewid,
productsource,
sku
FROM raena_analytics.mongo_orderitems mo
ORDER BY orderid ;
drop table if exists raena_analytics.om_mongo_Order_details;
create table raena_analytics.om_mongo_Order_details
as
select createdat,
orderid,
json_extract_path_text(storeinfo, 'name') as store,
json_extract_path_text(storeinfo, 'storeOwner') as sellername,
json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone,
storename,
marketplaceorderid,
totalamount ,orderStatus
from raena_analytics.mongo_orders;
DROP TABLE IF EXISTS raena_analytics.om_mongo_orders_summary_metabase;
CREATE TABLE raena_analytics.om_mongo_orders_summary_metabase AS
SELECT DISTINCT mod.*,
moid.orderitemid,
moid.quantity,
moid.originalprice,
moid.rsellviewid,
moid.productsource,
moid.sku,
mbc.brand_name,
mbc.has_child,
mbc.actual_categoryid,
mbc.actual_category_name,
row_number() over (partition BY mod.orderid,orderitemid,sku
ORDER BY originalprice DESC) AS R
FROM raena_analytics.om_mongo_Order_details MOD
LEFT JOIN raena_analytics.om_mongo_Order_item_details moid ON MOD.orderid=moid.orderid
LEFT JOIN raena_analytics.om_mongo_brand_category mbc ON moid.rsellviewid=mbc.rsellviewid;
DROP TABLE IF EXISTS raena_analytics.OM_Mongo_orders_dump_metabase;
CREATE TABLE raena_analytics.OM_Mongo_orders_dump_metabase AS
SELECT orderid,
status,
shippingamount,
paymenttype,
createdat,
isarchived,
invoicedata,
estimatedeliverydate,
delivereddate,
pickupdonedate,
actualshippingdate,
daystoship,
currency,
cancelreason,
cancelby,
buyercancelreason,
orderupdatedat,
ordercreatedat,
totalamount,
orderstatus,
paymenttime,
paymentstatus,
storename,
marketplacestoreid,
marketplaceorderid,
sellerbusinessprostoreid,
sellerbusinessproid,
sellerraenaemail,
sellerraenaphonenumber,
sellerraenausername,
sellerraenauserid,
fulfillmentstatus,
marketplaceid,
storeid,
orderref,
json_extract_path_text(storeinfo, 'storeOwner') as sellername,
json_extract_path_text(storeinfo, 'storeOwnerPhone') as Sellerphone
FROM raena_analytics.mongo_orders;
" > /home/ec2-user/cronjob/redshift/sql_code/360_rdash_etl.sql
psql "host=redshift-cluster-1.cdqj58hfx4p7.ap-southeast-1.redshift.amazonaws.com user=dbadmin dbname=analytics port=5439 password=5qCif6eyY3Kmg4z" -f /home/ec2-user/cronjob/redshift/sql_code/360_rdash_etl.sql