Skip to content

Commit

Permalink
Reconfigure to match Russ's specifications for single measure and tim…
Browse files Browse the repository at this point in the history
…e period at once
  • Loading branch information
elliot-at-looker committed Nov 15, 2021
1 parent e48b147 commit d3c2779
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 59 deletions.
88 changes: 52 additions & 36 deletions src/FilterBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,63 @@
*/

import React, { useEffect, useState, useContext } from 'react'
import { Space, ComponentsProvider, Text, Chip, Row, SpaceVertical, Heading, Box, Button } from '@looker/components'
import { Space, ComponentsProvider, Text, Chip, SpaceVertical, Heading, Button } from '@looker/components'
import { ExtensionContext } from '@looker/extension-sdk-react'
import { content } from './StaticContent';
import { startCase, find } from 'lodash'
import { startCase, find, lowerCase } from 'lodash'
export const FilterBar = ({ executeQuery, queryStatus }) => {
const { queryBody, fieldType, dynamicFieldsDimensions, dynamicFieldMeasureTemplate, dynamicFieldsMeasuresFilterExpressions } = content;
const { queryBody,
dynamicFieldsDimensions,
dynamicFieldMeasureTemplate,
dynamicFieldsMeasuresFilterExpressions,
initialMeasures, initialDimensions, initialCustomFields } = content;
let measuresArr = [];
let dimensionsArr = [];
let customFieldsArr = [];
queryBody.fields.map(field => {
let label = startCase(field.substring(field.lastIndexOf('.') + 1, field.length).replaceAll("_", " "))
if (fieldType[field] && fieldType[field] === "measure") {
measuresArr.push({
key: field,
label: label,
selected: true
})
} else if (fieldType[field] && fieldType[field] === "dimension") {
dimensionsArr.push({
key: field,
label: label,
selected: true
})
} else if (fieldType[field] && fieldType[field] === "custom_field") {
customFieldsArr.push({
key: field,
label: label,
selected: true
})
}

initialMeasures.map((measure, index) => {
let label = startCase(measure.substring(measure.lastIndexOf('.') + 1, measure.length).replaceAll("_", " "))
measuresArr.push({
key: measure,
label: label,
selected: index === 0 ? true : false
})
})
initialDimensions.map((dimension, index) => {
let label = startCase(dimension.substring(dimension.lastIndexOf('.') + 1, dimension.length).replaceAll("_", " "))
dimensionsArr.push({
key: dimension,
label: label,
selected: index === 0 ? true : false
})
})
initialCustomFields.map((customField, index) => {
let label = startCase(customField.substring(customField.lastIndexOf('.') + 1, customField.length).replaceAll("_", " "))
customFieldsArr.push({
key: customField,
label: label,
selected: index === 0 ? true : false
})
})

const [measures, setMeasures] = useState(measuresArr)
const [dimensions, setDimensions] = useState(dimensionsArr);
const [customFields, setCustomFields] = useState(customFieldsArr);
const measuresHelper = (target, e) => {
var newMeasuresArr = measures.map((measure) => {
return target.label === measure.label ? { ...target, selected: !target.selected } : measure;
return target.label === measure.label ? { ...target, selected: !target.selected } : { ...measure, selected: false };
});
setMeasures(newMeasuresArr)
}
const dimensionsHelper = (target, e) => {
var newDimensionsArr = dimensions.map((dimension) => {
return target.label === dimension.label ? { ...target, selected: !target.selected } : dimension;
return target.label === dimension.label ? { ...target, selected: !target.selected } : dimension; //allow more than one to be selected
});
setDimensions(newDimensionsArr)
}
const customFieldsHelper = (target, e) => {
var newCustomFieldsArr = customFields.map((customField) => {
return target.label === customField.label ? { ...target, selected: !target.selected } : customField;
return target.label === customField.label ? { ...target, selected: !target.selected } : { ...customField, selected: false };
});
setCustomFields(newCustomFieldsArr)
}
Expand All @@ -96,16 +103,25 @@ export const FilterBar = ({ executeQuery, queryStatus }) => {
if (customField.selected) return customField.key
}).filter(item => item !== undefined)

let selectedDynamicFieldsDimensions = selectedCustomFields.map(item => {
return dynamicFieldsDimensions[item]

let selectedDynamicFieldsDimensions = []
selectedCustomFields.map(scf => {
selectedDynamicFieldsDimensions.push(dynamicFieldsDimensions[scf])
if (scf === "this_year") selectedDynamicFieldsDimensions.push(dynamicFieldsDimensions["last_year"])
else if (scf === "this_month") selectedDynamicFieldsDimensions.push(dynamicFieldsDimensions["last_month"])
})

let dynamicFieldsMeasures = [];
let measuresArr = [];
selectedCustomFields.map(cf => {
let measureArrForSelectedFields = [];
selectedDynamicFieldsDimensions.map(sdfd => {
selectedMeasures.map(sm => {
let label = startCase(cf.replaceAll("_", " ") + sm.substring(sm.lastIndexOf('.') + 1, sm.length).replaceAll("_", " "))
let measure = cf + "_" + sm.substring(sm.lastIndexOf('.') + 1, sm.length);
measuresArr.push(measure)
let measure = lowerCase(sdfd.label).split(" ").join("_")
measure += "_"
measure += sm.substring(sm.lastIndexOf('.') + 1, sm.length);
measureArrForSelectedFields.push(measure)

let label = startCase(sdfd.label + " " + sm.substring(sm.lastIndexOf('.') + 1, sm.length).replaceAll("_", " "))

let copy = { ...dynamicFieldMeasureTemplate }
copy.measure = measure;
copy.based_on = sm;
Expand All @@ -115,12 +131,12 @@ export const FilterBar = ({ executeQuery, queryStatus }) => {
copy.value_format_name = null;
copy._kind_hint = "measure"
copy._type_hint = "number"
copy.filter_expression = dynamicFieldsMeasuresFilterExpressions[cf]
copy.filter_expression = dynamicFieldsMeasuresFilterExpressions[sdfd.dimension]
dynamicFieldsMeasures.push(copy)
})
})

let selectedFields = [...selectedDimensions, ...measuresArr]
let selectedFields = [...selectedDimensions, ...measureArrForSelectedFields]
queryCopy.fields = selectedFields;

let dynamicFields = [...dynamicFieldsMeasures, ...selectedDynamicFieldsDimensions];
Expand Down
44 changes: 21 additions & 23 deletions src/StaticContent.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
export const content = {
"queryBody": {
"view": "order_items",
"fields": ["users.country", "users.state", "products.category", "products.sku", "users.gender", "order_items.total_sale_price", "order_items.total_returns", "order_items.total_tax_amount", "this_year", "this_month", "last_month"],
"limit": "500",
"dynamic_fields": "[{\"measure\":\"this_year\",\"based_on\":\"order_items.total_sale_price\",\"type\":\"count_distinct\",\"label\":\"This Year\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"measure\",\"_type_hint\":\"number\",\"filter_expression\":\"${is_current_year} = yes\"},{\"measure\":\"this_month\",\"based_on\":\"order_items.total_sale_price\",\"type\":\"count_distinct\",\"label\":\"This Month\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"measure\",\"_type_hint\":\"number\",\"filter_expression\":\"${is_current_month} = yes AND ${is_current_year} = yes\"},{\"measure\":\"last_month\",\"based_on\":\"order_items.total_sale_price\",\"type\":\"count_distinct\",\"label\":\"Last Month\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"measure\",\"_type_hint\":\"number\",\"filter_expression\":\"${is_last_month} = yes\"},{\"dimension\":\"is_current_year\",\"label\":\"Is Current Year\",\"expression\":\"extract_years(${order_items.created_date}) = extract_years(now())\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"yesno\"},{\"dimension\":\"is_current_month\",\"label\":\"Is Current Month\",\"expression\":\"extract_months(${order_items.created_date})\\t= extract_months(now())\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"yesno\"},{\"dimension\":\"is_last_month\",\"label\":\"Is Last Month\",\"expression\":\"if(extract_months(now()) = 1, \\n extract_months(${order_items.created_date})\\t= 12 AND extract_days(${order_items.created_date})\\t< extract_days(now())\\n , \\nextract_months(${order_items.created_date})\\t= extract_months(now()) - 1 AND extract_days(${order_items.created_date})\\t< extract_days(now()))\\n\",\"value_format\":null,\"value_format_name\":null,\"_kind_hint\":\"dimension\",\"_type_hint\":\"yesno\"}]",
"query_timezone": "America/Los_Angeles",
"model": "atom_fashion",
},
"initialMeasures": ["order_items.total_sale_price", "order_items.total_returns", "order_items.total_tax_amount",],
"initialDimensions": ["users.country", "users.state", "products.category", "products.sku", "users.gender",],
"initialCustomFields": ["this_year", "this_month"],
"resultFormat": "json_detail",
"fieldType": {
"users.country": "dimension",
"users.state": "dimension",
"products.category": "dimension",
"products.sku": "dimension",
"users.gender": "dimension",
"order_items.total_sale_price": "measure",
"order_items.total_returns": "measure",
"order_items.total_tax_amount": "measure",
"this_year": "custom_field",
"this_month": "custom_field",
"last_month": "custom_field"
},
"dynamicFieldsDimensions": {
"this_month": {
"dimension": "is_current_month",
"label": "Is Current Month",
"label": "This Month",
"expression": "extract_months(${order_items.created_date})\t= extract_months(now())",
"value_format": null,
"value_format_name": null,
Expand All @@ -33,7 +21,7 @@ export const content = {
},
"last_month": {
"dimension": "is_last_month",
"label": "Is Last Month",
"label": "Last Month",
"expression": "if(extract_months(now()) = 1, \n extract_months(${order_items.created_date})\t= 12 AND extract_days(${order_items.created_date})\t< extract_days(now())\n , \nextract_months(${order_items.created_date})\t= extract_months(now()) - 1 AND extract_days(${order_items.created_date})\t< extract_days(now()))\n",
"value_format": null,
"value_format_name": null,
Expand All @@ -42,14 +30,29 @@ export const content = {
},
"this_year": {
"dimension": "is_current_year",
"label": "Is Current Year",
"label": "This Year",
"expression": "extract_years(${order_items.created_date}) = extract_years(now())",
"value_format": null,
"value_format_name": null,
"_kind_hint": "dimension",
"_type_hint": "yesno"
},
"last_year": {
"dimension": "is_last_year",
"label": "Last Year",
"expression": "extract_years(${order_items.created_date}) = extract_years(now()) - 1",
"value_format": null,
"value_format_name": null,
"_kind_hint": "dimension",
"_type_hint": "yesno"
}
},
"dynamicFieldsMeasuresFilterExpressions": {
"is_current_month": "${is_current_month} = yes", // AND ${is_current_year} = yes //for now
"is_last_month": "${is_last_month} = yes",
"is_current_year": "${is_current_year} = yes",
"is_last_year": "${is_last_year} = yes"
},
"dynamicFieldMeasureTemplate": {
"measure": "this_year",
"based_on": "order_items.total_sale_price",
Expand All @@ -60,10 +63,5 @@ export const content = {
"_kind_hint": "measure",
"_type_hint": "number",
"filter_expression": "${is_current_year} = yes"
},
"dynamicFieldsMeasuresFilterExpressions": {
"this_year": "${is_current_year} = yes",
"this_month": "${is_current_month} = yes", // AND ${is_current_year} = yes //for now
"last_month": "${is_last_month} = yes"
}
}

0 comments on commit d3c2779

Please sign in to comment.