Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

feat(legacy-plugin-chart-pivot-table): add support for timestamp format #734

Merged
merged 4 commits into from
Aug 19, 2020

Conversation

villebro
Copy link
Contributor

@villebro villebro commented Aug 12, 2020

🏆 Enhancements

This adds support for formatted dates on pivot-table. This is done by prefixing date values with __timestamp:, after which they can be identified and parsed into formattable date objects. In addition sorting is done based on the epoch value, meaning that the format won't affect the sorting order. Other changes:

  • Add date format
  • Move visual options to own tab (currently on Query pane)
  • use time grain for formatting if smart formatting is selected
  • make sorting of numeric values based on the parsed float value, not the text value

AFTER:

image

BEFORE:

image

@villebro villebro requested a review from a team as a code owner August 12, 2020 14:02
@vercel
Copy link

vercel bot commented Aug 12, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/superset/superset-ui/b1g9da0a0
✅ Preview: https://superset-ui-git-fork-preset-io-villebro-pivot-date.superset.vercel.app

@codecov
Copy link

codecov bot commented Aug 13, 2020

Codecov Report

Merging #734 into master will increase coverage by 0.02%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #734      +/-   ##
==========================================
+ Coverage   24.35%   24.38%   +0.02%     
==========================================
  Files         340      340              
  Lines        7637     7662      +25     
  Branches      929      935       +6     
==========================================
+ Hits         1860     1868       +8     
- Misses       5704     5721      +17     
  Partials       73       73              
Impacted Files Coverage Δ
.../legacy-plugin-chart-pivot-table/src/PivotTable.js 0.00% <0.00%> (ø)
...acy-plugin-chart-pivot-table/src/transformProps.js 0.00% <0.00%> (ø)
packages/superset-ui-core/src/utils/logging.ts 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d323ce8...89edfba. Read the comment docs.

@@ -44,25 +45,37 @@ const propTypes = {
};

function PivotTable(element, props) {
const { data, height, columnFormats, numberFormat, numGroups, verboseMap } = props;
const { data, height, columnFormats, dateFormat, numberFormat, numGroups, verboseMap } = props;

const { html, columns } = data;
const container = element;
const $container = $(element);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery!? 😭

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately pivot table still uses jQuery.dataTables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, if it works it works! 😄


const { html, columns } = data;
const container = element;
const $container = $(element);
const dateFormatter =
dateFormat === 'smart_date' ? smartDateFormatter : getTimeFormatter(dateFormat);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getTimeFormatter('smart_date') should work just fine. But since we are here, can you update it to follow the same logic as the table chart?

// Use granularity for "Adaptive Formatting" (smart_date)
const timeFormat = format || tableTimestampFormat;
formatter = getTimeFormatter(timeFormat);
if (timeFormat === smartDateFormatter.id) {
if (isTimeColumn(key)) {
formatter = getTimeFormatterForGranularity(granularity);
} else if (format) {
formatter = getTimeFormatter(format);
} else {
// return the identity string when datasource level formatter is not set
// and table timestamp format is set to Adaptive Formatting
formatter = String;
}
}

It has fallbacks to d3-format in pre-defined columns and uses Time Grain for smart_date.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code was hard to follow for you, I can also update this later and refactor these logics into some kind of common utilities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this reference! I didn't have strong opinions here aka. I was just out to make this work. I'll digest the table chart logic and see if it makes sense for me to streamline here 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktmud I implemented a slightly modified version of your formatter that felt most suitable for this slightly simpler case. Does it make sense to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for the update. Do you mind adding a datetime column to Storybook as well?

Copy link
Member

@rusackas rusackas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktmud's comments are duly noted (and appreciated!)... I'll leave it up to @villebro to address them, but this looks functionally sufficient to warrant the green checkmark.

@villebro
Copy link
Contributor Author

villebro commented Aug 14, 2020

Thanks for your reviews @ktmud and @rusackas ! ❤️

Copy link
Contributor

@kristw kristw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @ktmud 's comment make sense. At minimum I would recommend updating this line.

const dateFormatter =
    dateFormat === 'smart_date' ? smartDateFormatter : getTimeFormatter(dateFormat);

to

const dateFormatter = getTimeFormatter(dateFormat);

@villebro
Copy link
Contributor Author

Oh, one more thing @ktmud and @rusackas . Currently the sorting carets are broken (displays a black box, see the screenshots). I wasn't sure how to fix this, do you guys know how to make the icon work properly?

@villebro
Copy link
Contributor Author

Thanks @kristw , I'll update accordingly 👍

@ktmud
Copy link
Contributor

ktmud commented Aug 14, 2020

Oh, one more thing @ktmud and @rusackas . Currently the sorting carets are broken (displays a black box, see the screenshots). I wasn't sure how to fix this, do you guys know how to make the icon work properly?

I used react-icons in the table chart.

function SortIcon<D extends object>({ column }: { column: ColumnInstance<D> }) {
const { isSorted, isSortedDesc } = column;
let sortIcon = <FaSort />;
if (isSorted) {
sortIcon = isSortedDesc ? <FaSortDesc /> : <FaSortAsc />;
}
return sortIcon;
}

In the future we should probably move the SVG icons from superset-frontend to superset-ui so they can be reused by visualizations.

@villebro
Copy link
Contributor Author

villebro commented Aug 18, 2020

I used react-icons in the table chart.

function SortIcon<D extends object>({ column }: { column: ColumnInstance<D> }) {
const { isSorted, isSortedDesc } = column;
let sortIcon = <FaSort />;
if (isSorted) {
sortIcon = isSortedDesc ? <FaSortDesc /> : <FaSortAsc />;
}
return sortIcon;
}

In the future we should probably move the SVG icons from superset-frontend to superset-ui so they can be reused by visualizations.

@ktmud I was unsure how to apply this snippet to this code, as it doesn't seem to be natively React. Perhaps it's best if we merge this as-is and leave this fix for later?

@ktmud
Copy link
Contributor

ktmud commented Aug 18, 2020

@ktmud I was unsure how to apply this snippet to this code, as it doesn't seem to be natively React. Perhaps it's best if we merge this as-is and leave this fix for later?

Sounds good

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants