在 Rails 中对随时间变化的资源可用性进行建模(存在异常情况)

发布于 2024-09-11 03:56:07 字数 384 浏览 2 评论 0原文

我正在努力解决如何使用 Rails 中可用的工具、gems 或插件对资源及其可用性进行建模。

基本上,每个资源每周每天 (0,1,2,3,4,5,6) 的典型可用性为 0..24 小时。但是,需要考虑一些例外情况,例如假期或特殊事件,这些例外情况应该推翻一周中这一天的默认设置。

我可以想到两种广泛的方法(尽管我确信还有更多)。它会根据每个请求查询数据库并测试逻辑以查看资源当时是否可用。随着系统的增长,这可能会变得很麻烦(耗时)。另一个将每 x(5?) 分钟运行一个进程(cron?),以根据其可用性规则派生的每个资源设置布尔值(AVAILABLE)。

这些都是可以想象的、可行的广泛方法,但如何处理模型/类才是我真正可以使用一些建议的地方。

有什么想法吗?经验?我应该注意的宝石/插件?

I'm struggling with how to model a resource and its availability using the tools, gems or plugins available to us in Rails.

Basically each resource will have a typical availability of 0..24 hours for each day (0,1,2,3,4,5,6) of the week. BUT then there will be exceptions that need to be considered for example a holiday or special event that should overrule the default for that day of the week.

I can think of two broad approaches (though I'm sure there are more). One which would query the DB on every request and test the logic to see if the resource is available at that moment. This may become cumbersome (time consuming) as system grows. The other would run a process (cron?) every x (5?) min to set a boolean (AVAILABLE) on each resource that is derived depending on the rules for its availability.

Those are broad approaches that could, conceivably, work but how to approach the model/classes is where I could really use some advice.

Any thoughts? Experience? Gems/Plugins I should be aware of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2024-09-18 03:56:07

不确定在您的应用程序中的适用性,但有一个非常好的 Ruby gem,名为 ice_cube ,可以用于对重复日期和时间进行建模。您可以为资源设置计划,指定其可用性规则和例外情况。对于每个资源,计划可以作为 YAML(例如在文本字段中)序列化到数据库。

当您需要查看特定资源是否可用时,您只需拉动它的时间表,将其反序列化,然后询问 ice_cube 相关日期/时间是否与其规则相交。它也非常快速和高效。

Not sure of the applicability in your application but there is a pretty nice Ruby gem called ice_cube with can be used to model recurring dates and times. You could set up a schedule for your resources specifying their availability rules and the exception cases. The schedule can be serialized to the database as YAML (say in a text field) for each resource.

When you need to see if a particular resource is available you just pull it's schedule de-serialized it and ask ice_cube if a date/time in question intersects with it's rules or not. It's very fast and efficient too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文