Skip to content

Commit

Permalink
Estimation of duration of MeetUp (#136)
Browse files Browse the repository at this point in the history
* Estimation of duration for WebCal added

* Update

* Regex parsing fixed

---------

Co-authored-by: Robert Brands (RiwaAdmin) <rbadmin@riwa4.de>
  • Loading branch information
rbrands and Robert Brands (RiwaAdmin) committed Jan 20, 2024
1 parent c17f5c8 commit 7f4c694
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MeetUpPlanner/Server/Controllers/UtilController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UtilController : ControllerBase
{
private readonly MeetUpFunctions _meetUpFunctions;
private readonly ILogger<UtilController> logger;
const string serverVersion = "2024-01-02";
const string serverVersion = "2024-01-20";
string functionsVersion = "tbd";

public UtilController(ILogger<UtilController> logger, MeetUpFunctions meetUpFunctions)
Expand Down
69 changes: 67 additions & 2 deletions MeetUpPlanner/Server/Controllers/WebcalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Text;
using System;
using System.Web;
using MeetUpPlanner.Client.Shared;

namespace MeetUpPlanner.Server.Controllers
{
Expand Down Expand Up @@ -66,8 +67,8 @@ public async Task<IActionResult> GetMeetUps([FromRoute] string key)
Summary = meetUp.Title,
Description = description.ToString(),
Start = new CalDateTime(meetUp.StartDate),
// Ends 1 hours later.
End = new CalDateTime(meetUp.StartDate.AddHours(1.0)),
// Calculate/estimate end of event, default is 2 hours
End = new CalDateTime(meetUp.StartDate.AddHours(GetEstimatedDurationInHours(meetUp))),
Location = meetUp.Place
};
webCal.Events.Add(icalEvent);
Expand Down Expand Up @@ -99,5 +100,69 @@ public static string StripHTML(string HTMLText, bool decode = true)
}
}

public uint GetTempoAsKilometersPerHour(ExtendedCalendarItem meetUp)
{
// Usual formats: '25 - 30km/h', '30km/h', '~20kmh'. We simply parse
// the first 2-3 digit number and interpret it as km/h. the result
// is also clamped between 0..40 to guard against unexpected inputs.
uint kmh = 0;
try
{
var match = Regex.Match(meetUp.Tempo, @"(\d{1,3})");
if (match.Success)
{
kmh = uint.Parse(match.Groups[1].ToString());
}
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
// in case of any exception ==> fallback to defaults
}

return Math.Clamp(kmh, 0, 40);
}
public uint GetDistanceAsKilometers(ExtendedCalendarItem meetUp)
{
// Usual formats include '65km / 200Hm' with or without spaces, '40
// oder 60km'. Return value is clamped between 0 and 300 to guard
// against unexpected inputs.
uint distance = 0;
try
{
var match = Regex.Match(meetUp.LevelDescription, @"(\d{1,4})\s*(?:km)?");
if (match.Success)
{
distance = uint.Parse(match.Groups[1].ToString());
}
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
// in case of any exception ==> fallback to defaults
}

return Math.Clamp(distance, 0, 300);
}
/// <summary>
/// To get a better fit for the duration (instead of 1 hour as default) this function tries to estimate the duration by parsing the tempo and length.
/// If no appropriate tempo/distance is detected ==> use 2 hours
/// Contributed by: Moritz von Göwels https://github.com/the-kenny
/// </summary>
/// <param name="meetUp"></param>
/// <returns></returns>
public double GetEstimatedDurationInHours(ExtendedCalendarItem meetUp)
{
var distance = GetDistanceAsKilometers(meetUp); // 0..inf
if (distance == 0) { distance = 50; } // default to 50km

var speed = GetTempoAsKilometersPerHour(meetUp); // 0..100
if (speed == 0) { speed = 25; } // Default to 25km/h

double duration = (double)distance / (double)speed;
// Clamp between 15 minutes and 24 hours
return Math.Clamp(duration, 0.25, 24.0);
}

}
}
2 changes: 1 addition & 1 deletion MeetUpPlanner/Server/MeetUpPlanner.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Flurl" Version="4.0.0" />
<PackageReference Include="Flurl.Http" Version="4.0.1" />
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Ical.Net" Version="4.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.15" />
Expand Down

0 comments on commit 7f4c694

Please sign in to comment.