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

[JAVA] [EC10] Image vectors #154

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

PheonBest
Copy link

Idea: Use less heavy SVG images using less bandwidth (for Spring Boot applications, etc .)
Related issue: #111

This rule anlyzes unoptimized svg strings by using XPath.
The rule detects the following unoptimizations:

  • The attribute value length isn't reduced, e.g: id="filter4210" to id="a" (the same goes for the xlink:href attribute).
  • The attribute value isn't approximated: stdDeviation="0.57264819" to stdDeviation="0.573". Duplicated tags. Superfluous tags, e.g: sodipodi:namedview, .
  • At least two tags are redundant (same id and/or xlink:href).
  • Superfluous attributes (xmlns:dc, xmlns:cc, xmlns:rdv, xmlns: svg, xmlns, xmlns:sodipodi, xmlns:inkscape, most of id attributes, version, inkscape:version, inkscape:label, inkscape:groupmode, sodipodi:docname).

@MP-Aubay MP-Aubay added 🗃️ rule rule improvment or rule development or bug java 🏆 challenge2023 🏆 Work done during the ecoCode Challenge 2023 labels Apr 7, 2023
@dedece35 dedece35 changed the title [JAVA] Image vectors [JAVA] [EC10] Image vectors Apr 10, 2023
@dedece35 dedece35 linked an issue Apr 10, 2023 that may be closed by this pull request
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 9 Code Smells

87.1% 87.1% Coverage
0.0% 0.0% Duplication

@dedece35
Copy link
Member

dedece35 commented Apr 10, 2023

Hi @PheonBest,
thank you for issue and PR !

please could you tell us where do you find this list of superfluous attributes ? I'm curious :p
If we are ok with you, we could report it to other language rule implementation (ex for Python #158 )

thx

Copy link
Member

@dedece35 dedece35 left a comment

Choose a reason for hiding this comment

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

please add also updates with this rule implementation :

another modification to do : add a tests in ecoCode-java-test-project ==> please check others tests and make the same. as you can see test files are quite identical to unit test resources files

please make corrections on code smell problems (sonarcloud report)

@@ -30,7 +30,7 @@ services:
- "extensions:/opt/sonarqube/extensions"
- "logs:/opt/sonarqube/logs"
- "data:/opt/sonarqube/data"

restart: unless-stopped
Copy link
Member

Choose a reason for hiding this comment

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

what is the use case for this need ?
for me, if container crashes, the developer must check why on logs et make a correction or an issue for correction.
if we add this option, we could miss the real pb

@@ -0,0 +1,161 @@
package fr.greencodeinitiative.java.checks;
Copy link
Member

Choose a reason for hiding this comment

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

indeed, this new rule implementation isn't included in ecoCode plugin because there is no declaration in RulesList class : this is mandatory to include it.

import java.util.stream.Stream;

@Rule(
key = "EC53",
Copy link
Member

Choose a reason for hiding this comment

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

please, check other rule implementation to make the same :

  • create a constant with id value (to make it easier to use it in other classes)
  • tags : no bug, but ecocode, eco-design and performance (maybe other tag, please check available native tags on SonarQube

public class OptimizeImageVectorsSize extends IssuableSubscriptionVisitor {
protected final String SVG_BEGIN = "<svg";
protected final String[] SVG_END = new String[]{"</svg", "</ svg>"};
protected final List<String> SUPERFLUOUS_ATTRIBUTES = Arrays.asList("xmlns:dc", "xmlns:cc", "xmlns:rdv", "xmlns:svg", "xmlns", "xmlns:sodipodi", "xmlns:inkscape", "inkscape:version", "inkscape:label", "inkscape:groupmode", "sodipodi:docname");
Copy link
Member

Choose a reason for hiding this comment

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

please could you tell us where do you find this list of superfluous attributes ? I'm curious :p
If we are ok with you, we could report it to other language rule implementation

Copy link
Author

Choose a reason for hiding this comment

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

Hello @dedece35
I extracted the superfluous attributes from SVGGO demo page, which provides an original & compressed svg file that I compared:
compressed: blob:https://jakearchibald.github.io/09bc7d0d-2148-470d-9a25-86355304127f
original: blob:https://jakearchibald.github.io/b11f1eec-fd24-4bf2-95f2-127555093714
It's not a perfect list !

You can also define custom attributes in svg, e.g: version='1.2'
These attributes are superfluous if they're not used in the rest of the svg.

}

@Override
public void visitNode(Tree tree) {
Copy link
Member

Choose a reason for hiding this comment

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

please, explode this method in several smaller private functional methods. this is for readability and ease to maintain it.

return; // svg is invalid or is broken into multiple strings
}

// Parse svg as xml and explore its tree
Copy link
Member

Choose a reason for hiding this comment

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

why do you use XML parsing and not simplier way, for example string pattern search on all java code ?

Copy link
Author

Choose a reason for hiding this comment

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

I looked into regex matching at first, but I realized it may not suit some rules I defined.
The name of some superfluous attributes can be used as content/text instead of tags attributes, e.g: xmlns.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should consider using XPath? SVGs files being XML, this would allow us to perform the analysis independently of what language/framework is used. This is mentionned in the SonarQube documentation

@dedece35 dedece35 added the 👀 👀 review done 👀 👀 review done - waiting for changes label Apr 10, 2023
import java.util.stream.Stream;

@Rule(
key = "EC53",
Copy link
Member

Choose a reason for hiding this comment

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

wrong rule id ==> EC10

@github-actions
Copy link

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jun 24, 2023
@dedece35
Copy link
Member

dedece35 commented Sep 22, 2023

Hi @PheonBest,
do you check DoD list here : https://github.com/green-code-initiative/ecoCode-common/blob/main/doc/starter-pack.md#definition-of-done-of-a-pr ?

and please make a correction of your Jav files to inlucde mandatory licence header like other Java files (that's why the verify phase of test process is bad)

@github-actions
Copy link

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Oct 23, 2023
@github-actions github-actions bot removed the stale label Dec 4, 2023
Copy link

This PR has been automatically marked as stale because it has no activity for 60 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗃️ rule rule improvment or rule development or bug 🏆 challenge2023 🏆 Work done during the ecoCode Challenge 2023 java 👀 👀 review done 👀 👀 review done - waiting for changes stale 👀 👀 waiting commiter 👀 👀
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[EC10] [Java] Use unoptimized vector images Java
4 participants