Statcomm 1.7.30 is coming: New Features(II)

While we are struggling to keep plugin complexity at the minimum, we’ve been looking ways to simplify the internal processes. To some degree, we succeed… in a way.

Templating system: Mustache

If there is something that is bothering us, is how the current plugin renders the data in the Overview page. Although we are using PHP classes,  we inherited some sort of spaghetti-code hard to maintain, understand and improve. In short, it sucks is very poor.

In the search of splitting data from the format, we came accross with Mustache, a remarkable piece of software. After playing a while, we think Mustache is close to be wonderful. Dreadful mixture of HTML and PHP code can be easily untangled in two simple files and then blended together to render properly. It is like magic.

An example: this is the code need it to render  the Last Referrers table. The first file is a class extending the Mustach class where we declare all the data we’ll going to use. We return strings and arrays of data to be displayed:

class lastReferrers extends statcommMustache
    public function templateName(){return "lastreferrers";}

    public function title()     { return __('Last referrers', 'statcomm'); }
    public function date()      { return __('Date', 'statcomm'); }
    public function time()      { return __('Time', 'statcomm'); }
    public function from()      { return __('From', 'statcomm'); }
    public function result()    { return __('Result', 'statcomm'); }

    public function rows()
        $qry = mySql::get_results(mySql::QRY_lastReferrers,array(get_option('home'), 20));
        foreach ($qry as $rk)
            $row= array();
            $row['url'] = utilities::irigetblogurl() .
                ((strpos($rk->urlrequested, 'index.php') === FALSE) ? $rk->urlrequested : '');
            $row['url_ellipsis'] = utilities::makeEllipsis($row['url'], 70);
            $row['date'] = utilities::conv2Date($rk->date);
            $row['time'] = $rk->time;
            $row['referrer'] = $rk->referrer;
            $row['referrer_ellipsis'] = utilities::makeEllipsis($rk->referrer, 70);
        return $results;

Notice not HTML code involved, just the necessary text fields, and an array of results.

And this is the template to combine with. We only declare where the data goes and also the loops (see #rows tags) using the data in arrays:

Template subsystem

We rebuilt the Overview page, assembling it as a composite of modules where each one does one thing and one thing only. Currently, the page is assembled with 8 modules: tableOverview (header with resume data), drawChart (renders bar charts), lastHits (table hits ) and so on. Each module is composed with 2 files: a class which extracts data from the database (or any other data resource), and a template which decides how to display the data. All the concept can be easily extended to any plugin page.

Rendering the page

After dividing the Overview page in view modules, we made a manageable render system to complete it. The final goal (to be accomplished around 1.7.50 version) is to make a configurable Overview page (and other pages we hope) where you can easily decide what to include or exclude from the current view. What’s best, you could add and customize modules to include in the view.

The render process based its inner working from a customizable xml file (one xml per page) where we declare what modules and the order to be displayed(see the statcomm.xml file in the image above):

The above XML file represents one page and the subsequent modules with the order to be displayed.

If we suppress or rearrange the modules you can easily change the current Overview page in a breeze. Subsequent Statcomm version will provide a graphic way to handle the views.

All the mustache configuration is added into a mustache  folder under the statcomm plugin installation.

Unexpected drawback: performance

Everything is nice and lovely, the code works fine and we’ll live happily ever after… and that’s what we thought after many hours of dealing with the transition from spaghetti code to template conversion. But then we realized (too late) a problem we missed: performance.

After we ended it the conversion and we could taste our ‘Oh!’ moment, we starting to see our ‘oh-oh-ups’ final job. As you may know, we are struggling to deliver the best code we can make, knowing your site deserve no less. And going back to deal with performance numbers is a misstep we want to fix.

Is that bad? In our tests we consistently got around 2,6 secs to render the Overview page without using mustache (graphics with max columns (50), and 200k records on the table). Using mustache, we got around 5,6 seconds to render the page, 3 seconds more. We remind you the tests were in a slower machine, you should have better figures on a normal web server. Also, the final time is hard to perceive, since the complete Overview page is far bigger than the entire screen.

We are working in including a module capable of  measure the render times of each module. In this way, you will have a better understanding where is time is spent in the page(s). (probably in 1.7.40)

Aside this issue that we are confident to improve,  using a template system simplifies complex mixed coding, enabling you to play safely with it and improve it if you wanted to.

When it would be available?

We currently started testing the new implementation. The 1.7.30 version will be ready before July 15th.

Thank you everyone and enjoy the day. Don’t forget the read the First Part!

No Comments

Leave a Reply

Your email address will not be published Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>