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

fixed bug id 7566, allow multiple sites for image graph generation #7660

Closed
wants to merge 9 commits into from

Conversation

saleemkce
Copy link
Contributor

This bug fix is for issue #7566 So, with this fix, now it's possible to give idSite=1 (single site image graph generation), idSite=1,2,3,4 (comma separated idSites) or idSite=all(list all sites the user has view permission).

During this fix, the only tradeoff is, I needed to create a new function strokeAsImageData() to send the generated image's content back to 'plugins/ImageGraph/StaticGraph.php' because current implementation causes the browser to output single PNG image. So, we refactor the code to just pass the image data back to the caller so that it could be utilized in HTML page with N no. of images for each image graph generation.

It's still not better approach because this new function is added in pChar library file. We need to inherit the code so that when pChart library is updated, we don't lose our new strokeAsImageData() function. Please go through the code and give suggests, we can improve further.

For time being, use my fix and play with this new multi image graph page.
http://localhost.piwik/index.php?module=API&method=ImageGraph.get&idSite=all&apiModule=VisitsSummary&apiAction=get&token_auth=anonymous&graphType=evolution&period=day&date=previous30&width=500&height=250

@saleemkce
Copy link
Contributor Author

Current newly implemented page looks like this:

multiimagegraph

@saleemkce
Copy link
Contributor Author

hi all, what is the status for this commit? If you have feedback, please comment. I will do everything to get it done. Do you find issues with it or could it be moved to master?

@saleemkce
Copy link
Contributor Author

Here is a bar graph representation for multiple websites.

multiimagebargraph

Oh, how nice it looks in verticalBar, horizontalBar and as Pie. I cannot wait to see how long it will take before this commit is approved and moved to Piwik codebase. But everyone is eager to see it done sooner. And me too.

@mattab
Copy link
Member

mattab commented Apr 21, 2015

Hi @saleemkce

Thanks for the pull request! The diff looks quite big so it may take a while before we get a chance to review and consider it. Also we'd need to add some tests for this (see also: Enable static PNG image graphs during Continuous Integration #6864 )

@saleemkce
Copy link
Contributor Author

@mattab Okay. Very glad t to know. Moreover, if you take a look at this http://forum.piwik.org/read.php?9,126057, it would be helpful.

@@ -166,6 +166,17 @@ function stroke($BrowserExpire=FALSE)
header('Content-type: image/png');
imagepng($this->Picture);
}

/* Updated code to send image as encoded data */
function strokeAsImageData()
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to the ob_start etc the StaticGraph class eg in a private method strokeAsImageData? Would be good if it was possible. Otherwise we always need to make sure to apply this patch when updating the pchart library etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Thomas, I am very unsure about this. Could you put some sample code and clarify me how we could get it done better without getting affected by an update from pchart's library?

Copy link
Member

Choose a reason for hiding this comment

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

I meant can you do something like

public function getPngImageData()
{
   return 'data:image/png;base64,'.base64_encode($this->strokeAsImageData());
}

private function strokeAsImageData() 
{
     ob_start();
     $this->pImage->stroke()
     $imagedata = ob_get_contents();
     ob_end_clean();
     return $imagedata;
}

or it might be possible to do but I am unsure about that

public function getPngImageData()
{
   return 'data:image/png;base64,'.base64_encode($this->getRenderedImage());
}

@tsteur
Copy link
Member

tsteur commented May 6, 2015

The diff is quite big indeed. I wonder if it was possible to make the code simpler by leaving the behaviour of ImageGraph.get more or less as it is. Then introduce a new method like ImageGraph.getForSites which might use ImageGraph.get($idSite) for each given site and puts the graph together. Or maybe we could extract some code from ImageGraph.get into methods and reuse them in getForSites (feel free to change the name if you do it).

for ($w = 0; $w < $idSiteArrCount; $w++)
{
$idSite = $idSiteArr[$w];
Piwik::checkUserHasViewAccess($idSite);
Copy link
Member

Choose a reason for hiding this comment

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

Would be no longer needed if doing Piwik::checkUserHasViewAccess($idSite) in the beginning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's consider we have

<?php
$idSiteArr = array(1,2,3,4,5);

Is it possible to pass an array of idSites like this outside the for loop?

for example,
Piwik::checkUserHasViewAccess($idSiteArr);

I had used like this "Piwik::checkUserHasViewAccess($idSite);" because I thought this function could check idSite one at a time. Could it take array as given above?

Plus what will happen if user doesn't have view access only for idSide=3. Will it output other 4 image graphs or just stop after outputting first 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"The diff is quite big indeed. I wonder if it was possible to make the code ..."

Hi Thomas, there're actually very few changes added to existing code. Not big as you said. Github diff tool may make it seem too big. What I did in ImageGraph.get function is just put a for loop around the statements inside the function and storing the image graph outputs to an array variable so that it could be used later. It's that simple. It doesn't alter the output behavior of any other function parameters supplied to this function. If you think that it really affects the existing behavior, then we may move it to new function. Otherwise, I don't find any reason to reuse the code and write the new functionality in a new function. Moreover, that would increase the program LOC drastically which is really not useful. Please apply this patch in you app and play around with this a few more times. You may find something more constructive. What do you think?

Update:
@mattab @tsteur
For your clarification, I used an excellent diff comparison tool like TextCompare to find the no. of lines of changes, it's just 50-55 new lines of code. Previous version is this: https://github.com/piwik/piwik/blob/2effc45935c6a0b4f5d3617d0557b0b6a026b96b/plugins/ImageGraph/API.php and mine is : https://github.com/piwik/piwik/blob/6767033cfe12d577e15203bdb6d10bf90bdfaa75/plugins/ImageGraph/API.php

Please view the changes minutely or use some other GUI tool which may give better picture of changes, it's nothing super special only an enhancement of functionality.

Copy link
Member

Choose a reason for hiding this comment

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

Piwik::checkUserHasViewAccess($idSite); you can do this at the very beginning, then you don't have to do it in a for loop. Followed by this you can do $idSiteArr = Site::getIdSitesFromIdSitesString($idSite) as this will exactly do what you want and you can remove all the custom logic re $idSiteArr. I think here https://github.com/piwik/piwik/pull/7660/files?w=1#diff-b96413000922b656fd09c8adc762c5a2R133 you can remove line 133-149 as this method will take care of this.

Yes the diff is smaller now

@mattab
Copy link
Member

mattab commented May 6, 2015

The diff is quite big

Btw I just noticed if we look at the diff without whitespace (&w=1) it looks smaller: https://github.com/piwik/piwik/pull/7660/files?w=1

* @param string $imageGraphCount Number of img tags available for output
* Exits after outputting HTML content
*/
public function renderImageGraph($pngImgArr, $imageGraphCount)
Copy link
Member

Choose a reason for hiding this comment

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

This should be a private method. Otherwise it will be added to the API documentation as an API

Thanks to Thomas' brilliant suggestion which helped me achieve this without touching pchart's library. So, I remove this function from here and do the functionality at plugins/ImageGraph/SaticGraph.php file.
@saleemkce
Copy link
Contributor Author

@mattab @tsteur Thanks a lot, guys. All your suggestions successfully implemented. I have tested all new changes with 2 sites in my local machine. Everything is okay and looks nice as before. Latest changes here: https://github.com/piwik/piwik/pull/7660/files?w=1 Please notify if anything is wrong.

@mattab
Copy link
Member

mattab commented Sep 11, 2015

Dear @saleemkce

Thanks for the Pull request. At this stage, we will not merge it as we think we don't need this feature in Piwik. For example, it's quite simple for someone to include graphs from multiple websites by creating a little PHP script that loops through sites and then generates the image. Therefore we prefer not to add complexity in this part of the code. Thanks anyway for your PR and we hope you will keep contributing in the future :-)

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

Successfully merging this pull request may close these issues.

None yet

3 participants