Skip to content
Snippets Groups Projects
Unverified Commit 2eddf076 authored by Tom Robinson's avatar Tom Robinson
Browse files

Fix month/quarter param -> MBQL

parent 648d972b
Branches
Tags
No related merge requests found
......@@ -145,7 +145,7 @@ describe("metabase/meta/Card", () => {
card: assocIn(
dissoc(card, "id"),
["dataset_query", "query", "filter"],
["AND", ["=", ["datetime-field", 1, "month"], "FIXME: what should this be?"]]
["AND", ["=", ["datetime-field", 3, "month"], "2017-05-01"]]
)
});
});
......
......@@ -5,6 +5,8 @@ import type { TemplateTag, LocalFieldReference } from "./types/Query";
import type { Parameter, ParameterTarget, ParameterValues } from "./types/Parameter";
import type { FieldId } from "./types/Field";
import moment from "moment";
import Q from "metabase/lib/query";
import { mbqlEq } from "metabase/lib/query/util";
......@@ -62,29 +64,45 @@ export function getParameterTargetFieldId(target: ?ParameterTarget, datasetQuery
const timeParameterValueDeserializers: [{ testRegex: RegExp, deserialize: Deserializer}] = [
{testRegex: /^past([0-9]+)([a-z]+)s$/, deserialize: (matches) => {
return ["time-interval", noopRef, -parseInt(matches[0]), matches[1]]
}},
{testRegex: /^next([0-9]+)([a-z]+)s$/, deserialize: (matches) => {
return ["time-interval", noopRef, parseInt(matches[0]), matches[1]]
}},
{testRegex: /^this([a-z]+)$/, deserialize: (matches) => ["time-interval", noopRef, "current", matches[0]] },
{testRegex: /^~([0-9-T:]+)$/, deserialize: (matches) => ["<", noopRef, matches[0]]},
{testRegex: /^([0-9-T:]+)~$/, deserialize: (matches) => [">", noopRef, matches[0]]},
{testRegex: /^([0-9-T:]+)$/, deserialize: (matches) => ["=", noopRef, matches[0]]},
{testRegex: /^past([0-9]+)([a-z]+)s$/, deserialize: (matches, field = noopRef) =>
["time-interval", field, -parseInt(matches[0]), matches[1]]
},
{testRegex: /^next([0-9]+)([a-z]+)s$/, deserialize: (matches, field = noopRef) =>
["time-interval", field, parseInt(matches[0]), matches[1]]
},
{testRegex: /^this([a-z]+)$/, deserialize: (matches, field = noopRef) =>
["time-interval", field, "current", matches[0]]
},
{testRegex: /^~([0-9-T:]+)$/, deserialize: (matches, field = noopRef) =>
["<", field, matches[0]]
},
{testRegex: /^([0-9-T:]+)~$/, deserialize: (matches, field = noopRef) =>
[">", field, matches[0]]
},
{testRegex: /^(\d{4}-\d{2})$/, deserialize: (matches, field = noopRef) =>
["=", ["datetime-field", field, "month"], moment(matches[0], "YYYY-MM").format("YYYY-MM-DD")]
},
{testRegex: /^(Q\d-\d{4})$/, deserialize: (matches, field = noopRef) =>
["=", ["datetime-field", field, "quarter"], moment(matches[0], "[Q]Q-YYYY").format("YYYY-MM-DD")]
},
{testRegex: /^([0-9-T:]+)$/, deserialize: (matches, field = noopRef) =>
["=", field, matches[0]]
},
// TODO 3/27/17 Atte Keinänen
// Unify BETWEEN -> between, IS_NULL -> is-null, NOT_NULL -> not-null throughout the codebase
// $FlowFixMe
{testRegex: /^([0-9-T:]+)~([0-9-T:]+)$/, deserialize: (matches) => ["BETWEEN", noopRef, matches[0], matches[1]]},
{testRegex: /^([0-9-T:]+)~([0-9-T:]+)$/, deserialize: (matches, field = noopRef) =>
["BETWEEN", field, matches[0], matches[1]]
},
];
export function timeParameterValueToMBQL(urlEncoded: UrlEncoded): ?FieldFilter {
export function timeParameterValueToMBQL(urlEncoded: UrlEncoded, field: ConcreteField = noopRef): ?FieldFilter {
const deserializer =
timeParameterValueDeserializers.find((des) => urlEncoded.search(des.testRegex) !== -1);
timeParameterValueDeserializers.find((des) => des.testRegex.test(urlEncoded));
if (deserializer) {
const substringMatches = deserializer.testRegex.exec(urlEncoded).splice(1);
return deserializer.deserialize(substringMatches);
return deserializer.deserialize(substringMatches, field);
} else {
return null;
}
......@@ -102,8 +120,7 @@ export function parameterToMBQLFilter(parameter) {
let filter;
if (parameter.type.indexOf("date/") === 0) {
filter = timeParameterValueToMBQL(parameter.value);
filter[1] = field;
filter = timeParameterValueToMBQL(parameter.value, field);
} else {
// FIXME: we don't have the field type so we don't know when the value should be a number or a string
// assuming string for now
......
import { timeParameterValueToMBQL } from "./Parameter";
describe("metabase/meta/Parameter", () => {
describe("timeParameterValueToMBQL", () => {
it ("should parse past30days", () => {
expect(timeParameterValueToMBQL("past30days")).toEqual(["time-interval", null, -30, "day"])
})
it ("should parse next2years", () => {
expect(timeParameterValueToMBQL("next2years")).toEqual(["time-interval", null, 2, "year"])
})
it ("should parse thisday", () => {
expect(timeParameterValueToMBQL("thisday")).toEqual(["time-interval", null, "current", "day"])
})
it ("should parse ~2017-05-01", () => {
expect(timeParameterValueToMBQL("~2017-05-01")).toEqual(["<", null, "2017-05-01"])
})
it ("should parse 2017-05-01~", () => {
expect(timeParameterValueToMBQL("2017-05-01~")).toEqual([">", null, "2017-05-01"])
})
it ("should parse 2017-05", () => {
expect(timeParameterValueToMBQL("2017-05")).toEqual(["=", ["datetime-field", null, "month"], "2017-05-01"])
})
it ("should parse Q1-2017", () => {
expect(timeParameterValueToMBQL("Q1-2017")).toEqual(["=", ["datetime-field", null, "quarter"], "2017-01-01"])
})
it ("should parse 2017-05-01", () => {
expect(timeParameterValueToMBQL("2017-05-01")).toEqual(["=", null, "2017-05-01"])
})
it ("should parse 2017-05-01~2017-05-02", () => {
expect(timeParameterValueToMBQL("2017-05-01~2017-05-02")).toEqual(["BETWEEN", null, "2017-05-01", "2017-05-02"])
})
})
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment