awsmatrix/queries.js
2025-01-10 04:27:56 +05:30

185 lines
7.5 KiB
JavaScript

import dotenv from "dotenv";
dotenv.config();
// /accounts
export const accountsQuery = `SELECT DISTINCT
line_item_usage_account_id AS accountId FROM ${process.env.ATHENA_CU_TABLE};`;
// /accounts/:accountId/regions
export const regionsQuery = `SELECT DISTINCT
line_item_usage_account_id AS accountId,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode
FROM ${process.env.ATHENA_CU_TABLE}
WHERE line_item_usage_account_id = '%accountId%';`;
// /accounts/:accountId/regions/:regionCode/products
export const productsByRegionQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId
FROM ${process.env.ATHENA_CU_TABLE}
WHERE line_item_usage_account_id = '%accountId%' AND LOWER(product_region_code) = LOWER('%regionCode%');`;
// /accounts/:accountId/regions/:regionCode/products/:productCode
export const productByRegionQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId
FROM ${process.env.ATHENA_CU_TABLE}
WHERE
line_item_usage_account_id = '%accountId%' AND
product_region_code = '%regionCode%' AND
LOWER(line_item_product_code) = LOWER('%productCode%');`;
// /accounts/:accountId/regions/:regionCode/products/:productCode/usage
export const productUsageByRegionQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId,
line_item_usage_type AS usageType,
line_item_usage_amount AS usageAmount,
line_item_unblended_rate AS unblendedRate,
line_item_unblended_cost AS unblendedCost,
line_item_blended_rate AS blendedRate,
line_item_blended_cost AS blendedCost,
pricing_term AS pricingTerm,
pricing_unit AS pricingUnit,
pricing_rate_code AS pricingRateCode,
pricing_currency AS pricingCurrency,
line_item_usage_start_date AS startDate,
line_item_usage_end_date AS endDate
FROM ${process.env.ATHENA_CU_TABLE}
WHERE line_item_usage_account_id = '%accountId%'
AND product_region_code = '%regionCode%'
AND LOWER(line_item_product_code) = LOWER('%productCode%');`;
// /accounts/:accountId/regions/:regionCode/products/:productCode/usage/:year
export const productUsageByRegionYearQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId,
line_item_usage_type AS usageType,
line_item_usage_amount AS usageAmount,
line_item_unblended_rate AS unblendedRate,
line_item_unblended_cost AS unblendedCost,
line_item_blended_rate AS blendedRate,
line_item_blended_cost AS blendedCost,
pricing_term AS pricingTerm,
pricing_unit AS pricingUnit,
pricing_rate_code AS pricingRateCode,
pricing_currency AS pricingCurrency,
line_item_usage_start_date AS startDate,
line_item_usage_end_date AS endDate
FROM ${process.env.ATHENA_CU_TABLE}
WHERE line_item_usage_account_id = '%accountId%'
AND product_region_code = '%regionCode%'
AND LOWER(line_item_product_code) = LOWER('%productCode%')
AND year = '%year%';`;
// /accounts/:accountId/regions/:regionCode/products/:productCode/usage/:year/:month
export const productUsageByRegionYearMonthQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId,
line_item_usage_type AS usageType,
line_item_usage_amount AS usageAmount,
line_item_unblended_rate AS unblendedRate,
line_item_unblended_cost AS unblendedCost,
line_item_blended_rate AS blendedRate,
line_item_blended_cost AS blendedCost,
pricing_term AS pricingTerm,
pricing_unit AS pricingUnit,
pricing_rate_code AS pricingRateCode,
pricing_currency AS pricingCurrency,
line_item_usage_start_date AS startDate,
line_item_usage_end_date AS endDate
FROM ${process.env.ATHENA_CU_TABLE}
WHERE line_item_usage_account_id = '%accountId%'
AND product_region_code = '%regionCode%'
AND LOWER(line_item_product_code) = LOWER('%productCode%')
AND year = '%year%'
AND month = '%month%';`;
// /products
export const productQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId
FROM ${process.env.ATHENA_CU_TABLE};`;
export const productByCodeQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId
FROM ${process.env.ATHENA_CU_TABLE}
WHERE LOWER(line_item_product_code) = LOWER('%productCode%');`;
export const productByCodeUsageQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId,
line_item_usage_type AS usageType,
line_item_usage_amount AS usageAmount,
line_item_unblended_rate AS unblendedRate,
line_item_unblended_cost AS unblendedCost,
line_item_blended_rate AS blendedRate,
line_item_blended_cost AS blendedCost,
pricing_term AS pricingTerm,
pricing_unit AS pricingUnit,
pricing_rate_code AS pricingRateCode,
pricing_currency AS pricingCurrency,
line_item_usage_start_date AS startDate,
line_item_usage_end_date AS endDate
FROM ${process.env.ATHENA_CU_TABLE}
WHERE LOWER(line_item_product_code) = LOWER('%productCode%');`;
export const productByCodeUsageYearQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId,
line_item_usage_type AS usageType,
line_item_usage_amount AS usageAmount,
line_item_unblended_rate AS unblendedRate,
line_item_unblended_cost AS unblendedCost,
line_item_blended_rate AS blendedRate,
line_item_blended_cost AS blendedCost,
pricing_term AS pricingTerm,
pricing_unit AS pricingUnit,
pricing_rate_code AS pricingRateCode,
pricing_currency AS pricingCurrency,
line_item_usage_start_date AS startDate,
line_item_usage_end_date AS endDate
FROM ${process.env.ATHENA_CU_TABLE}
WHERE LOWER(line_item_product_code) = LOWER('%productCode%')
AND year = '%year%';`;
export const productByCodeUsageYearMonthQuery = `SELECT DISTINCT
line_item_product_code AS productCode,
COALESCE(NULLIF(TRIM(product_region_code), ''), 'global') AS regionCode,
line_item_usage_account_id AS accountId,
line_item_resource_id AS resourceId,
line_item_usage_type AS usageType,
line_item_usage_amount AS usageAmount,
line_item_unblended_rate AS unblendedRate,
line_item_unblended_cost AS unblendedCost,
line_item_blended_rate AS blendedRate,
line_item_blended_cost AS blendedCost,
pricing_term AS pricingTerm,
pricing_unit AS pricingUnit,
pricing_rate_code AS pricingRateCode,
pricing_currency AS pricingCurrency,
line_item_usage_start_date AS startDate,
line_item_usage_end_date AS endDate
FROM ${process.env.ATHENA_CU_TABLE}
WHERE LOWER(line_item_product_code) = LOWER('%productCode%')
AND year = '%year%'
AND month = '%month%';`;