-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mongobi-driver): implement
convert_tz
as a simple hour shift
Fixes issue #50
- Loading branch information
1 parent
1f109dc
commit c97e451
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const moment = require('moment-timezone'); | ||
|
||
const MysqlQuery = require('./MysqlQuery'); | ||
|
||
class MongoBiQuery extends MysqlQuery { | ||
convertTz(field) { | ||
const tz = moment().tz(this.timezone); | ||
// TODO respect day light saving | ||
const [hour, minute] = tz.format('Z').split(':'); | ||
const [hourInt, minuteInt] = [parseInt(hour, 10), parseInt(minute, 10) * Math.sign(parseInt(hour, 10))]; | ||
let result = field; | ||
if (hourInt !== 0) { | ||
result = `TIMESTAMPADD(HOUR, ${hourInt}, ${result})`; | ||
} | ||
if (minuteInt !== 0) { | ||
result = `TIMESTAMPADD(HOUR, ${minuteInt}, ${result})`; | ||
} | ||
return result; | ||
} | ||
|
||
timeStampCast(value) { | ||
return `TIMESTAMP(${value})`; | ||
} | ||
} | ||
|
||
module.exports = MongoBiQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const MongoBiQuery = require('../adapter/MongoBiQuery'); | ||
const PrepareCompiler = require('./PrepareCompiler'); | ||
require('should'); | ||
|
||
const { prepareCompiler } = PrepareCompiler; | ||
|
||
describe('MongoBiQuery', () => { | ||
const { compiler, joinGraph, cubeEvaluator } = prepareCompiler(` | ||
cube(\`visitors\`, { | ||
sql: \` | ||
select * from visitors | ||
\`, | ||
measures: { | ||
count: { | ||
type: 'count' | ||
} | ||
}, | ||
dimensions: { | ||
createdAt: { | ||
type: 'time', | ||
sql: 'created_at' | ||
} | ||
} | ||
}); | ||
`); | ||
|
||
it('convert_tz implementation', () => { | ||
return compiler.compile().then(() => { | ||
const query = new MongoBiQuery({ joinGraph, cubeEvaluator, compiler }, { | ||
measures: [ | ||
'visitors.count' | ||
], | ||
timeDimensions: [{ | ||
dimension: 'visitors.createdAt', | ||
granularity: 'date', | ||
dateRange: ['2017-01-01', '2017-01-30'] | ||
}], | ||
timezone: 'America/Los_Angeles', | ||
order: [{ | ||
id: 'visitors.createdAt' | ||
}] | ||
}); | ||
|
||
const queryAndParams = query.buildSqlAndParams(); | ||
console.log(queryAndParams); | ||
queryAndParams[0].should.match(/TIMESTAMPADD\(HOUR, -7, visitors\.created_at\)/); | ||
}); | ||
}); | ||
}); |