Query editor
WITH req AS (SELECT'{"query":"query Fees { positionFeesInfoWithPeriods(where: { period_eq: \"1d\" }, orderBy: timestamp_DESC, limit: 400) { period timestamp totalBorrowingFeeUsd totalLiquidationFeeUsd totalPositionFeeUsd } swapFeesInfoWithPeriods(where: { period_eq: \"1d\" }, orderBy: timestamp_DESC, limit: 400) { period timestamp totalFeeReceiverUsd } }"}' AS body),raw AS (SELECT json_parse(http_post('https://gmx.squids.live/gmx-synthetics-{{chain}}:prod/api/graphql',body,ARRAY['Content-Type: application/json'])) AS jFROM req),position_rows AS (SELECTCAST(json_extract_scalar(x, '$.timestamp') AS bigint) AS ts,CAST(json_extract_scalar(x, '$.totalBorrowingFeeUsd') AS double) / 1e30 AS borrowing_fee_usd,CAST(json_extract_scalar(x, '$.totalLiquidationFeeUsd') AS double) / 1e30 AS liquidation_fee_usd,CAST(json_extract_scalar(x, '$.totalPositionFeeUsd') AS double) / 1e30 AS position_fee_usdFROM rawCROSS JOIN UNNEST(CAST(json_extract(j, '$.data.positionFeesInfoWithPeriods') AS array(json))) AS t(x)),swap_rows AS (SELECTCAST(json_extract_scalar(x, '$.timestamp') AS bigint) AS ts,CAST(json_extract_scalar(x, '$.totalFeeReceiverUsd') AS double) / 1e30 AS swap_fee_usdFROM rawCROSS JOIN UNNEST(CAST(json_extract(j, '$.data.swapFeesInfoWithPeriods') AS array(json))) AS t(x)),daily AS (SELECTCAST(from_unixtime(p.ts) AS date) AS day,p.position_fee_usd,p.borrowing_fee_usd,p.liquidation_fee_usd,COALESCE(s.swap_fee_usd, 0) AS swap_fee_usd,p.position_fee_usd+ p.borrowing_fee_usd+ p.liquidation_fee_usd+ COALESCE(s.swap_fee_usd, 0) AS daily_fee_usdFROM position_rows pLEFT JOIN swap_rows sON p.ts = s.ts),bucketed AS (SELECTday,position_fee_usd,borrowing_fee_usd,liquidation_fee_usd,swap_fee_usd,daily_fee_usd,date_add('day', -mod(day_of_week(day) - 3 + 7, 7), day) AS week_start_wed,date_trunc('Month', day) AS month_startFROM daily