0

An application I've inherited maintenance of is timing out when attempting to load the events JSON stream for a Fullcalendar render.

I'm hoping that I'll get approval to throw some more computing resources at the problem to hopefully resolve it, but other than that I'm having a hard time figuring out how to resolve this problem.

One idea I had was to split the data request up in to smaller pieces, requesting 1 week of data (instead of 1 month) at a time.

This seems like it should be doable, but I can't figure out how to catch the timeframe change, split it into smaller chunks and then make multiple requests out of it instead of just one.

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
  • sounds like a job for an asynchronous process. but hard to give specifics without knowing what platform you are working with...is this a web app? What platform/language are you working with? – Eric Oct 16 '14 at 19:19
  • Are you sure the problem isn't that you're using an old version of fullcalendar? I saw this freezing issue here: http://jsfiddle.net/jcornelius/pba56nf1/, which was resolved in newer versions here: http://jsfiddle.net/pba56nf1/2/ – Richard Löwenström Oct 16 '14 at 22:07
  • Sorry for the lack of clarity, I haven't used StackOverflow very much yet. This is a web application, built with Ruby on Rails. – CallMeEsmale Oct 17 '14 at 02:15

1 Answers1

0

It seems that you are trying to get around the main issue which is why is it timing out? Not knowing many details, it could be as simple as a database index or some other process on the server is consuming to many resources. I suggest you profile your database and framework / server to resolve the issue.

That said, you can do two things:

  1. Disable month view to force FullCalendar to use agendaWeek or agendaDay. Something like this:

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next',
            center: 'title',
            right: 'agendaWeek,agendaDay'
        },
        defaultView: 'agendaWeek'
    });
    

    See an example.

  2. If you really need month view, you can use events as a function. You'll have to calculate the start and end days for each week of the month and then call the callback. Something like this:

    $('#calendar').fullCalendar({
        events: function(start, end, timezone, callback) {
            // do this in chunks only when view is month
            if ($("#calendar").fullCalendar( 'getView' ).name == 'month') {
                // You'll need to get the start and end day of each week.
                // The parameters `start` amd `end` relate to the first day of
                // the month and the last day of the month.
    
                // You should get an array of objects, like this
                var weeks = [
                    {start: '2014-09-28', end: '2014-10-04'},
                    {start: '2014-10-05', end: '2014-10-11'},
                    {start: '2014-10-12', end: '2014-10-18'},
                    {start: '2014-10-19', end: '2014-10-25'},
                    {start: '2014-10-26', end: '2014-11-01'}
                ];
    
                // When you have that:
                for(var i in weeks) {
                    getEvents(weeks[i].start, weeks[i].end, callback);
                }
            }
            // if its another view, get all events at once
            else {
                getEvents(start, end, callback);
            }
        }
    });
    
    function getEvents(start, end, callback) {
        $.ajax({
            url: 'events',
            dataType: 'json',
            async: false,
            data: {
                start: start,
                end: end
            },
            success: function(events) {
                callback(events);
            }
        });
    }
    

    In order to calculate the week start and end, you can have a look at this question: get next week start and end using jquery and moment js, it should help you out.

    Note that I've added async: false to $.ajax in order to have only one request at a time.

    Also note that this code works with FullCalendar 2.* . Since you're talking about an existing application, some things will be different. The documentation for events of version 1.* states that the signature is a bit different: function( start, end, callback ) { }

Community
  • 1
  • 1
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
  • I doubt that the issue is really the number of fetched events unless there's like thousands of events, still doubtful even then. Rendering is what should take time. – Richard Löwenström Oct 16 '14 at 22:10
  • @RichardHermanson as do I, that's why I suggest the OP to do some profiling. Without addicional details it will be nearly impossible to state what is causing the issue. – Luís Cruz Oct 16 '14 at 22:15
  • Upon further investigation, I have a hunch as to why the request is timing out. It's happening for a specific user that is trying to go back in the calendar to March 2013. She's hitting the 'previous month' button multiple times, and the calendar fires of a request for events for each month. So by the time she gets to March 2013, the server is essentially trying to pull events for multiple months. I have the 'prev year' button enabled, but is there any way to jump directly to a specified month, instead of having to traverse each month in between? – CallMeEsmale Oct 17 '14 at 02:19
  • @CallMeEsmale yes it's possible. However, there isn't a default option in FullCalendar so you will have to build it. [Check this jsfiddle](http://jsfiddle.net/milz/x4k3ztL0/1/), that has what you need. – Luís Cruz Oct 17 '14 at 11:00
  • @milz, that's awesome, and gets me most of the way there I think. But if the user has hit the previous year button, and then uses the dropdown to change the month, it pushes the year back up to 2014. I've been looking through the documentation, and am not having any luck finding a way to get the 'current' year from fullCalendar. Is that an option anywhere? Or do I need to save that out to my own variable somewhere? – CallMeEsmale Oct 17 '14 at 14:28
  • @CallMeEsmale, my bad. [Here you go](http://jsfiddle.net/milz/x4k3ztL0/2/). It has a prevYear button, and selects the correct month from the start. When you change the month, it will take the current FullCalendar date into account. – Luís Cruz Oct 17 '14 at 14:42
  • Thank you so much for the help, hopefully this will help alleviate the problems I'm seeing. – CallMeEsmale Oct 17 '14 at 15:35
  • @CallMeEsmale I'm glad I could help. Please accept my answer as it helped you. Please visit [this meta post](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) to see how you can accept it. If you have any additional issues, please post a new question. – Luís Cruz Oct 17 '14 at 15:46