How to define schema for timeseries data
Hi @paveltiunov ,
I'm using a postgresql database to store timeseries data. I created a view cpu_usage1m_v2 with two columns;
- time (one_min_bucket)
- cpu average usage (double precision float8 values)
In cubejs, I created the below schema.
cube(`CpuUsage1mV2`, {
sql: `SELECT * FROM public.cpu_usage1m_v2`,
joins: {
},
measures: {
count: {
type: `count`,
drillMembers: []
},
cpu_percent: {
sql: `${CUBE}.avg_value * 100`,
type: `number`,
format: `percent`
}
},
dimensions: {
avgValue: {
sql: `avg_value`,
type: `number`
},
oneMinBucket: {
sql: `one_min_bucket`,
type: `time`
}
}
});
The goal is to create a chart with time as the x-axis and the cpu percentage as the y-axis.
When I select cpu_percent in the measures, I get this error: Error: Error: column "cpu_usage1m_v2.avg_value" must appear in the GROUP BY clause or be used in an aggregate function. So I added avg_value in the dimensions and ended up with the below query that kind of works.
{
"measures": [
"CpuUsage1mV2.cpu_percent"
],
"timeDimensions": [
{
"dimension": "CpuUsage1mV2.oneMinBucket",
"granularity": "hour"
}
],
"dimensions": [
"CpuUsage1mV2.avgValue",
"CpuUsage1mV2.oneMinBucket"
],
"filters": []
}
With this query, I see chart with time in the x-axis. On the y-axis, I see both cpu_percentage and avg_value. I'm not sure if I'm doing this correctly. Please advise what is the best way to achieve this.

Hey @devopsprosiva ! You should use avg measure instead of number: https://cube.dev/docs/types-and-formats#measures-types-avg.