@julienmoumne opened this Issue on November 23rd 2010 Member

As reported on the forum, the pdf plugin needs to be updated to force weekly reports to be sent on mondays.

In plugins/PDFReports/PDFReports.

TODO: Use setDay(1) on Piwik_ScheduledTime_Weekly.

Should we set a specific hour aswell ?

@mattab commented on November 24th 2010 Member

Actually it can get tricky, as the PDF reports should really only be set once all "days" have been finished, ie. in all timezones that the websites are registered.

The ideal solution would be to send the PDF reports for each website, as soon as the website day is finished... ie. send 3 reports at midnight UTC for the websites set to UTC, no reports sent until 10AM, and then sent reports to websites set to UTC-10 at 10am... but not necessary to be that precise, the previous solution would be easier!

So I think the simpler fix would be to setHour( LATEST HOUR where a day finishes for ANY website registered), so all PDF reports are sent after we are confident that all days are finished...

Does it make sense?

@mattab commented on November 24th 2010 Member

Note: Monthly reports should be similarly sent the first day of the month (as soon as all months are finished, in the websites timezones)

@julienmoumne commented on November 24th 2010 Member

Correct me if I'm wrong.

So that all websites will have reached their local 00h00 when the report is sent, setHour should be set at :

(00h00 + time zone number - time zone number of all sites), Server local time

@julienmoumne commented on November 24th 2010 Member

With this equation, in the following case, the report would be (correctly) sent the last day of the month instead of the first :

  • Piwik Server is UTC +1
  • Only 1 site is set-up and uses UTC +15

The report could correctly be sent the last day of the month at 10AM from the Piwik Server because the site using UTC +15 will have already finished its day.

Should we implement it like that or simply send the report at 00h00 Piwik server time ?

The algorithm would then become :

timeShift = [Server time zone number] - [Minimum time zone number of all sites]
if (timeShift < 0) {
} else {
@mattab commented on November 25th 2010 Member

The server timezone is always set to UTC (piwik forces UTC in index.php)

Interesting file to look at Date/timezone manipulation helpers is tests/core/Date.test.php

A slightly similar function to look at also is setMinMaxDateAcrossWebsites in Multisites/controller

Finally, to get all websites and then the timezone, you can use the SitesManager_API.getSitesWithAtLeastViewAccess function (as the script will run as super user).

It is not the best performance wise (each call to runTasks() will result in selecting all websites and comparing all timezones), but it will work fine. To make it fast for a Piwik server with thousands of websites, we could for example record, on each website update, the earliest and latest timezone available in the _option table (but this will be a feature request when we hit problems, not yet the case :)

@julienmoumne commented on November 25th 2010 Member

I was indeed wondering if Piwik forces UTC 0, thanks for clarifying.

This issue, still applies though.

@julienmoumne commented on November 25th 2010 Member

Should CodeAdminHome scheduled task be updated as well to be triggered the first day of the month ?

@mattab commented on November 28th 2010 Member

Yes maybe this task could be ran 1st day of month as well.
Maybe the default could be that all monthly tasks run the 1st day of the month, after the 1st initial run?

@julienmoumne commented on November 29th 2010 Member

Interesting idea. In that case, for API consistency, we need to set defaults for Daily and Weekly as well.

@mattab commented on November 29th 2010 Member

Defaults should be:

  • Daily = midnight by default (server time, always UTC)
  • Weekly = monday by default
  • Monthly = 1st day of month
@julienmoumne commented on November 29th 2010 Member

(In [3387]) refs #1836 comment:12 - set defaults to setHour & setDay, updating unit tests

@julienmoumne commented on November 29th 2010 Member

Here is a working proposal to set the correct hour to the scheduled tasks.
I wanted to submit here first as I'm not entirely sure if it is the most elegant way this can be done.

If you have 3 sites with UTC+1, UTC-11, UTC-8.5, it will


If you have 3 sites with UTC+1, UTC+10, UTC+15, it will


If you have 1 site with UTC-8.5, it will

    function getScheduledTasks ( $notification )
        // Computes which hour should the tasks run according to their timezones
        $maxOffset = 0;
        $mySites = Piwik_SitesManager_API::getInstance()->getSitesWithAtLeastViewAccess();
        foreach($mySites as &$site)
            $baseDate = Piwik_Date::factory("1971-01-01");
            $offsetDate = Piwik_Date::factory("1971-01-01",  $site['timezone']);

            // Earlier means a negative timezone
            if ( $offsetDate->isEarlier($baseDate) )
                // Gets the timezone offset
                $hourOffset = (24 - date ('H', $offsetDate->getTimestamp()));

                if ( $hourOffset > $maxOffset )
                    $maxOffset = $hourOffset;

        $tasks = &$notification->getNotificationObject();

        $dailySchedule = new Piwik_ScheduledTime_Daily();
        $tasks[] = new Piwik_ScheduledTask ( $this, 'dailySchedule', $dailySchedule );

        $weeklySchedule = new Piwik_ScheduledTime_Weekly();
        $tasks[] = new Piwik_ScheduledTask ( $this, 'weeklySchedule', $weeklySchedule );

        $monthlySchedule = new Piwik_ScheduledTime_Monthly();
        $tasks[] = new Piwik_ScheduledTask ( $this, 'monthlySchedule', $monthlySchedule );
@mattab commented on December 20th 2010 Member

what is the status of the ticket? can you please post a patch, or commit if you're confident

@julienmoumne commented on December 23rd 2010 Member

(In [3524]) fixed #1836 - sets hour on scheduled tasks to wait for end of period for all configured websites timezone, should be refactored in utility class

@julienmoumne commented on January 7th 2011 Member

As forecasted in comment:7 and experienced in 1981, loading the list of all websites does not scale.

This Issue was closed on January 7th 2011
Powered by GitHub Issue Mirror