Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new event to define units for metrics #13640

Merged
merged 2 commits into from Dec 11, 2018
Merged

Adds new event to define units for metrics #13640

merged 2 commits into from Dec 11, 2018

Conversation

sgiehl
Copy link
Member

@sgiehl sgiehl commented Oct 22, 2018

Evolution charts and row evolution uses the method Metrics::getUnit to determine the unit that should be shown with any metric.
For new plugins having custom metrics there is currently no possibility to manipulate that behavior. This causes all metrics, that don't match the format handled by the core method, to be shown as numbers only, even if they should be percent or time values.
The new event will make that possible.

@sgiehl sgiehl added c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Needs Review PRs that need a code review labels Oct 22, 2018
@sgiehl sgiehl added this to the 3.7.0 milestone Oct 22, 2018
core/Metrics.php Outdated
* @param string $idSite id of the current site
* @param string $unit should hold the unit (e.g. %, €, s or empty string)
*/
Piwik::postEvent('Metrics.getUnit', [$column, $idSite, &$unit]);
Copy link
Member

Choose a reason for hiding this comment

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

I think usually we post the metric that can be changed and is passed by reference first. Then all following parameters that may change the metric which makes it also easier to add more params later if needed.

The method seems to be only used by evolution yet seeing the event name you could think it would be applied to all reports etc. Maybe the event name could be renamed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've adjusted the code. Wondering if we really should mention it in the changelog. Imho it would be nice if wouldn't need it at all. But guess that would require some more refactoring, so we are able to determine the unit based on the column name. Maybe we even could get a "Metric" instance based on the column name and have a method getUnit in such metric classes or similar. Or we have a metric type and do the formatting based on the type (using the existing formatters)

Copy link
Member

Choose a reason for hiding this comment

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

I don't know how much work it is, and whether it cause any trouble... but may be worth a try to check if the metric ID can be found by https://github.com/matomo-org/matomo/blob/3.x-dev/core/Columns/MetricsList.php and the formatting could be done eg through https://github.com/matomo-org/matomo/blob/3.x-dev/core/Plugin/ArchivedMetric.php#L130 but as you say this would require potentially some more work and not sure how much it is doable/not doable.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't simply do the formatting in PHP as the yaxsis are grouped based on the defined units. Also the JS imho needs the unformatted numbers to work. So we need some kind of definition which column has which metric.

if (!empty($unit)) {
return $unit;
}

Copy link
Member

Choose a reason for hiding this comment

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

Could this method get called a lot? If so might make sense to cache the value in the transient cache to avoid posting the event too many times.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's called in JqplotDataGenerator. Not sure if it's worth to cache that, as cache would need to be based on columnname and idsite. So likely a lot entries would be created

@diosmosis
Copy link
Member

LGTM, not sure if there's more to the getUnit() method vs event discussion

@diosmosis diosmosis merged commit 569e071 into 3.x-dev Dec 11, 2018
@diosmosis diosmosis deleted the customunits branch December 11, 2018 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants