Skip to content

Commit

Permalink
Merge pull request #39 from mWater/issue-38
Browse files Browse the repository at this point in the history
adding date expressions by 3,6,12 months, closes #38
  • Loading branch information
grassick authored Oct 30, 2017
2 parents 520a173 + ebb7fc0 commit 223df23
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ Defines a language for expressions that return a value for a single row of a tab
Complex expressions involving joins, arithmetic or case statements can be created visually and then compiled to SQL or interpreted.

See the [docs](docs/Expressions.md) for more information.

### To add a new expression
1. Add tests in `ExprCompilerTests`
2. Add tests in `testExprs`
3. Add the expression in `ExprUtils`
4. Update `ExprCompiler`
5. Update `ExprEvaluator`
6. Make tests pass
78 changes: 78 additions & 0 deletions src/ExprCompiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,84 @@ module.exports = class ExprCompiler
else
return null

when 'last12months'
if not compiledExprs[0]
return null

switch expr0Type
when "date"
return {
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [compiledExprs[0], moment().subtract(11, "months").startOf('month').format("YYYY-MM-DD") ] }
{ type: "op", op: "<", exprs: [compiledExprs[0], moment().add(1, 'days').format("YYYY-MM-DD") ] }
]
}
when "datetime"
return {
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [compiledExprs[0], moment().subtract(11, "months").startOf('month').toISOString() ] }
{ type: "op", op: "<", exprs: [compiledExprs[0], moment().startOf("day").add(1, 'days').toISOString() ] }
]
}
else
return null

when 'last6months'
if not compiledExprs[0]
return null

switch expr0Type
when "date"
return {
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [compiledExprs[0], moment().subtract(5, "months").startOf('month').format("YYYY-MM-DD") ] }
{ type: "op", op: "<", exprs: [compiledExprs[0], moment().add(1, 'days').format("YYYY-MM-DD") ] }
]
}
when "datetime"
return {
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [compiledExprs[0], moment().subtract(5, "months").startOf('month').toISOString() ] }
{ type: "op", op: "<", exprs: [compiledExprs[0], moment().startOf("day").add(1, 'days').toISOString() ] }
]
}
else
return null

when 'last3months'
if not compiledExprs[0]
return null

switch expr0Type
when "date"
return {
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [compiledExprs[0], moment().subtract(2, "months").startOf('month').format("YYYY-MM-DD") ] }
{ type: "op", op: "<", exprs: [compiledExprs[0], moment().add(1, 'days').format("YYYY-MM-DD") ] }
]
}
when "datetime"
return {
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [compiledExprs[0], moment().subtract(2, "months").startOf('month').toISOString() ] }
{ type: "op", op: "<", exprs: [compiledExprs[0], moment().startOf("day").add(1, 'days').toISOString() ] }
]
}
else
return null

when 'distance'
if not compiledExprs[0] or not compiledExprs[1]
return null
Expand Down
18 changes: 18 additions & 0 deletions src/ExprEvaluator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ module.exports = class ExprEvaluator
date = values[0]
return moment(date, moment.ISO_8601).isBefore(moment().add(1, "days")) and moment(date, moment.ISO_8601).isAfter(moment().subtract(365, "days"))

when "last12months"
if hasNull
return null
date = values[0]
return moment(date, moment.ISO_8601).isBefore(moment().add(1, "days")) and moment(date, moment.ISO_8601).isAfter(moment().subtract(11, "months").startOf('month'))

when "last6months"
if hasNull
return null
date = values[0]
return moment(date, moment.ISO_8601).isBefore(moment().add(1, "days")) and moment(date, moment.ISO_8601).isAfter(moment().subtract(5, "months").startOf('month'))

when "last3months"
if hasNull
return null
date = values[0]
return moment(date, moment.ISO_8601).isBefore(moment().add(1, "days")) and moment(date, moment.ISO_8601).isAfter(moment().subtract(2, "months").startOf('month'))

when "latitude"
if hasNull
return null
Expand Down
3 changes: 3 additions & 0 deletions src/ExprUtils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ relativeDateOps = [
['last7days', 'is in last 7 days']
['last30days', 'is in last 30 days']
['last365days', 'is in last 365 days']
['last3months', 'is in last 3 months']
['last6months', 'is in last 6 months']
['last12months', 'is in last 12 months']
]
for relativeDateOp in relativeDateOps
addOpItem(op: relativeDateOp[0], name: relativeDateOp[1], resultType: "boolean", exprTypes: ['date'])
Expand Down
51 changes: 51 additions & 0 deletions test/ExprCompilerTests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,57 @@ describe "ExprCompiler", ->
}
)

it "last3months", ->
@compile(
{
type: "op"
op: "last3months"
exprs: [@datetime1]
}
{
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [@datetime1JsonQL, moment().subtract(2, "months").startOf('month').toISOString()]}
{ type: "op", op: "<", exprs: [@datetime1JsonQL, moment().startOf("day").add(1, "days").toISOString()]}
]
}
)

it "last6months", ->
@compile(
{
type: "op"
op: "last6months"
exprs: [@datetime1]
}
{
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [@datetime1JsonQL, moment().subtract(5, "months").startOf('month').toISOString()]}
{ type: "op", op: "<", exprs: [@datetime1JsonQL, moment().startOf("day").add(1, "days").toISOString()]}
]
}
)

it "last12months", ->
@compile(
{
type: "op"
op: "last12months"
exprs: [@datetime1]
}
{
type: "op"
op: "and"
exprs: [
{ type: "op", op: ">=", exprs: [@datetime1JsonQL, moment().subtract(11, "months").startOf('month').toISOString()]}
{ type: "op", op: "<", exprs: [@datetime1JsonQL, moment().startOf("day").add(1, "days").toISOString()]}
]
}
)

it "compiles days since (datetime)", ->
@compile(
{
Expand Down
12 changes: 12 additions & 0 deletions test/testExprs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ addOp(true, "last30days", literal(moment().subtract(1, "minutes").subtract(1, "d
addOp(false, "last365days", literal(moment().subtract(1, "minutes").add(3, "days").toISOString(), "datetime"))
addOp(true, "last365days", literal(moment().subtract(1, "minutes").subtract(1, "days").toISOString(), "datetime"))

addOp(false, "last12months", literal(moment().subtract(1, "minutes").add(3, "days").toISOString(), "datetime"))
addOp(false, "last12months", literal(moment().subtract(1, "minutes").subtract(12, "months").toISOString(), "datetime"))
addOp(true, "last12months", literal(moment().subtract(1, "minutes").subtract(3, "days").toISOString(), "datetime"))

addOp(false, "last6months", literal(moment().subtract(1, "minutes").add(3, "days").toISOString(), "datetime"))
addOp(false, "last6months", literal(moment().subtract(1, "minutes").subtract(6, "months").toISOString(), "datetime"))
addOp(true, "last6months", literal(moment().subtract(1, "minutes").subtract(3, "days").toISOString(), "datetime"))

addOp(false, "last3months", literal(moment().subtract(1, "minutes").add(3, "days").toISOString(), "datetime"))
addOp(false, "last3months", literal(moment().subtract(1, "minutes").subtract(3, "months").toISOString(), "datetime"))
addOp(true, "last3months", literal(moment().subtract(1, "minutes").subtract(3, "days").toISOString(), "datetime"))

addOp("1", "weekofmonth", literal("2015-05-07", "date"))
addOp("2", "weekofmonth", literal("2015-05-08", "date"))

Expand Down

0 comments on commit 223df23

Please sign in to comment.