search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

OnBeforeCellRender optimization

Related article: Scheduler for ASP.NET MVC 4 Razor (C#, VB.NET, SQL Server)
Asked by Janardan Chaugule
6 years ago.

Hello Team,

I have worked on day pilot scheduler but I am stuck at OnBeforeCellRender this server side method because it's taking too much time(around 4-5 mins taking for loading) for render the events.
I have used this event for setting style to CSS according to data (color).I have also mentioned the code snippet for your reference. Please help us on this for optimization.

protected override void OnBeforeCellRender(BeforeCellRenderArgs e)
{
if (e.ResourceId == "0" || listavailability.Count == 0)
{
e.BackgroundColor = "#FFD9CC";
}
else
{
var data = listavailability.Where(x => DateTime.Parse(x.Date.ToString("yyyy-MM-dd ") + x.StartTime.TimeOfDay.ToString()) <= e.Start && DateTime.Parse(x.Date.ToString("yyyy-MM-dd ") + x.EndTime.TimeOfDay.ToString()) >= e.End && x.ClinicRoomId == Convert.ToInt32(e.ResourceId.Split(',')[2]) && x.TherapistId == Convert.ToInt32(e.ResourceId.Split(',')[0])).ToList();
if (data.Count > 0)
{
e.BackgroundColor = "#fff";
}
else
{
e.BackgroundColor = "#f0f0f0";
}
}

}

Answer posted by Dan Letecky [DayPilot]
6 years ago.

This event is called once for every cell. Therefore the implementation should be as fast as possible.

You should avoid making database calls from there. Instead, load the required data in advance and process it if needed. In OnBeforeCellRender, just look up the results of the preprocessing.

Another approach would be to display the availability data as events - this method is used in the Doctor Appointment Scheduling tutorial:
https://code.daypilot.org/55027/asp.net-doctor-appointment-scheduling

This tutorial is for ASP.NET WebForms but you should get the idea.

Comment posted by Janardan Chaugule
6 years ago.

Thanks for the reply, I agree with your first approach but we haven't fetch the data from the database it was already fetched. We just filtering the list.Can you please check again and try to give another approach. list availability variable its a list object which contains 1000 object, and we are manipulating it on every cell of this event.

Comment posted by Dan Letecky [DayPilot]
6 years ago.

You need to find where the problem really is.

First, you need to make sure that those 4 minutes (which is a lot!) are spent in OnBeforeCellRender.

Ideally, you move all processing out of the OnBeforeCellRender to have better control over the processing. The output of the preprocessing could be a hash map (Dictionary) with keys something like datetime + resource id (to uniquely identify each cell). Use the desired background color as the value.

Your OnBeforeCellRender would then look like this:

protected override void OnBeforeCellRender(BeforeCellRenderArgs e) 
{ 
  string key = e.Start.ToString("s") + "_" + e.ResourceId;
  e.BackgroundColor = map[key];
}

When creating the hash map try to avoid inefficient operations like converting a DateTime to string and parsing it again, splitting the same string over and over again, etc. Especially inside loops.

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.