Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Weekly PDF reports not sent on mondays, Monthly reports not sent the 1st of month #1836

Closed
JulienMoumne opened this Issue · 15 comments

2 participants

@JulienMoumne
Collaborator

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
Owner

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
Owner

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
Collaborator

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
Collaborator

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) {
    setHour(0)
} else {
    setHour(timeShift)
}
@mattab
Owner

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
Collaborator

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

This issue, still applies though.

@JulienMoumne
Collaborator

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

@mattab
Owner

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
Collaborator

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

@mattab
Owner

Defaults should be:

  • Daily = midnight by default (server time, always UTC)
  • Weekly = monday by default
  • Monthly = 1st day of month
@JulienMoumne
Collaborator

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

@JulienMoumne
Collaborator

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

->setHour(11);

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

->setHour(0);

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

->setHour(9);
    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();
        $dailySchedule->setHour($maxOffset);
        $tasks[] = new Piwik_ScheduledTask ( $this, 'dailySchedule', $dailySchedule );

        $weeklySchedule = new Piwik_ScheduledTime_Weekly();
        $weeklySchedule->setHour($maxOffset);
        $tasks[] = new Piwik_ScheduledTask ( $this, 'weeklySchedule', $weeklySchedule );

        $monthlySchedule = new Piwik_ScheduledTime_Monthly();
        $monthlySchedule->setHour($maxOffset);
        $tasks[] = new Piwik_ScheduledTask ( $this, 'monthlySchedule', $monthlySchedule );
    }
@mattab
Owner

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

@JulienMoumne
Collaborator

(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
Collaborator

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

@JulienMoumne JulienMoumne added this to the Piwik 1.1 milestone
@JulienMoumne JulienMoumne self-assigned this
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.