superset icon indicating copy to clipboard operation
superset copied to clipboard

How to make Time Grain in Chart calculate day based on local time zone

Open sunnysnowhi opened this issue 6 years ago • 5 comments

I have a postgresql table with colume like this: create_time timestamp without time zone resource_id varchar(32)

Then I set the chart like this: image

I found in super set log , the sql is like this: SELECT DATE_TRUNC('day', create_time) AS __timestamp, count(resource_id) AS "COUNT(resource_id)" FROM dbadmin.de_billing WHERE create_time >= '2020-03-29 00:00:00' AND create_time <= '2020-04-29 00:00:00' GROUP BY DATE_TRUNC('day', create_time) ORDER BY "COUNT(resource_id)" DESC LIMIT 50000

which means Time Grain calculate the day in this way : DATE_TRUNC('day', create_time) , for example : UTC 2020-04-28 00:00:00 to UTC 2020-04-29 00:00:00

But what I want is showing the count of resource_id every day based on local timezone my local time zone is Asia/Shanghai, which is utc+8 hour so I want it calculate in this way , for example, UTC 2020-04-27 16:00:00 to UTC 2020-04-28 16:00:00 , which means local time 2020-04-28 00:00:00 to local time 2020-04-29 00:00:00, the day of the local time

First I tried this way: add a new calculated columu , e8_create_time = create_time + INTERVAL '8 hour' ,then select the e8_create_time as the time colume in chart. image

But I have a lot of tables! This method is very complex, I must add one or more new colomn for every table!

The I found in table settings there is a offset setting, But it did not work as I want, image image when I set 8 here, it seems not calculate a whole day from 00:00:00 to 00:00:00, it shows 08 AM here image

I search this question on internet , many anserwers say set superset config.py DRUID_TZ = tz.gettz('Asia/Shanghai'), but I did not use druid as the datasource, My database is here and druid datasources is empty image

Is there a better way to achieve my goal? Thanks!!!

sunnysnowhi avatar Apr 29 '20 09:04 sunnysnowhi

anyone help?

sunnysnowhi avatar Apr 30 '20 01:04 sunnysnowhi

Which database do you use? Maybe in the extra field you could set the timezone. image

Let me know what db you use and I can try to find the answer. Every database is different,

eugeniamz avatar May 05 '20 13:05 eugeniamz

@eugeniamz I use the postgresql

sunnysnowhi avatar May 09 '20 08:05 sunnysnowhi

You can update the base query for the data source. Go to sources > Tables > Edit record and under SQL, add:

    SELECT *, create_time at time zone 'Asia/Shanghai' at time zone 'utc' as create_time_zone
    FROM dbadmin.de_billing;

and the column create_time_zone will be available for all charts using that source. So you use that instead of create_time in your charts.

jay-finn avatar May 20 '20 00:05 jay-finn

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. For admin, please label this issue .pinned to prevent stale bot from closing the issue.

stale[bot] avatar Jul 19 '20 00:07 stale[bot]