每周计划 - 如何将其存储在数据库中?
目前,我正在开发一个项目来管理服务器数据库等上的维护窗口。基本上,我只需要精确到小时,但允许将它们设置为允许或禁止每天一周中的。
我对如何做到这一点有一些想法,但由于我自己工作,所以我不想在没有反馈的情况下做出任何承诺。
为了形象化这一点,它就像流动的“图表”
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
-------------------------------------------
5AM |allow|allow|allow|deny |deny |allow|allow|
-------------------------------------------
6AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
7AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
8AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
9AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
... etc...
是否有执行此操作的标准方法或资源可以给我一些想法......
- 制作可以轻松保存和恢复的格式
- 使其在数据库中可搜索(例如,不必反序列化即可搜索时间)
[更新]
值得一提的是,即使不太可能,一天也可以设置为“允许,拒绝、允许、拒绝……等等……”。 不保证该跨度是一整天的唯一一个。
这也不是唯一的时间表,将会有数百个设备,每个设备都有自己的时间表,所以它会变得很麻烦......哈哈?
Rob询问是否需要每周进行跟踪 - 不需要。 这是适用于全年的通用时间表(定期维护)
Currently, I'm working on a project to manage maintenance windows on a database of servers, etc. Basically, I only need to be accurate down to the hour, but allow for them to be set to allow, or disallow, for each day of the week.
I've had a few ideas on how to do this, but since I work by myself, I'm not wanting to commit to anything without some feedback.
To visualize this, it's something like the flowing "graph"
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
-------------------------------------------
5AM |allow|allow|allow|deny |deny |allow|allow|
-------------------------------------------
6AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
7AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
8AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
9AM |allow|deny |deny |deny |deny |deny |allow|
-------------------------------------------
... etc...
Is there a standard way of doing this or a resource that might give me some ideas to...
- Make a format that can be saved and restored easily
- Make it searchable within the database (for example, not having to deserialize it to search for a time)
[Update]
It is worth mentioning that a day could, even though unlikely, be set to "allow, deny, allow, deny...etc...". The span isn't guaranteed to be the only one for the whole day.
This is also not the only schedule, there will be hundreds of devices each with their own schedule, so it's going to get hairy... lol??
Rob asked if each week needed to be tracked - It does not. This is a generic schedule that will apply to the entire year (regularly scheduled maintenance)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会考虑(1)使用包含开始时间和结束时间以及一周中的某一天的整数字段的格式。 我知道您说过区块总是一小时,但这可以通过您的代码强制执行。 此外,如果您的需求有一天发生变化,那么与将数据库语句全部编写为假设 1 小时块相比,您在步骤 (2) 中需要担心的事情会少得多。
对于 (2),如果每个记录都有与其关联的开始和结束时间,那么检查任何给定时间的窗口就非常容易:(
其中
$time
表示您想要的日期和时间查看)。一周中每一天的允许或不允许将由单独的记录处理。 恕我直言,这比一周中每一天的硬编码更灵活,因为您将使用某种 case 语句或 if-else 开关来检查您感兴趣的那一天的正确数据库列。
< strong>注意:请确保您知道您的数据库对于一周中的整数天使用哪种标准,并尝试使您的代码独立于它(始终询问数据库)。 我们在一周的开始(周日或周一)和起始索引(0 或 1)的不同标准方面获得了很多乐趣。
I would consider for (1) using a format that includes both start and end times, and an integer field for the day of the week. I know you stated the blocks will always be one hour, but that can be enforced by your code. Also, if your requirements change one day, you'll have a lot less to worry about in step (2) than if your DB statements are all written to assume 1 hour blocks.
For (2), if each record has a start and end time associated with it, then it's very easy to check for windows for any given time:
(where
$time
represents the date and time you want to check).The allowing or disallowing for each day of the week would be handled by separate records. IMHO, this is more flexible than hard-coding for each day of the week, since you'll then be using some kind of case statement or if-else switch to check the right DB column for the day you're interested in.
Note: Be sure you know which standard your DB uses for the integer day of the week, and try to make your code independent of it (always ask the DB). We've had lots of fun with different standards for the start of the week (Sunday or Monday) and the starting index (0 or 1).
如果每周都会不同,那么就这样设置桌子;
如果设置了特定日期/小时的开始时间,则假定允许,否则拒绝。
如果它是通用一周的通用配置且不会改变,请尝试此操作;
然后为每个小时/天的组合添加行。
If it is going to be different every week, then set up the table like this;
If a start time for a particular date/hour is set, then assume that it is allowed, otherwise deny.
If it is a generic configuration for a generic week that doesn't change, try this;
Then add rows for every hour/day combination.
我之前实际上已经使用过这种设计,基本上是为您想要定期安排的时间跨度除以您想要的周期数创建一个位图。 因此,在您的示例中,您需要一个包含每小时时间段的周计划,因此您将拥有一个仅 21 字节长的 168 位位图。 几个日期时间一起为 16 个字节,您需要其中的多行来表示给定一周的可能计划,因此如果您完全关心大小,我认为您无法击败它。
我承认,与之前的建议相比,它处理起来有点棘手,而且不太灵活。 考虑一下,如果您突然想要使用 1/2 小时周期,则需要将所有现有数据转码为新的 336 位位图并将值分发出去。
如果您使用 SQL,则可以将其存储为二进制博客,并进行位旋转来比较自己是否打开或关闭,或者可以将每个位存储为列。 MS SQL Server 支持高达 1024 的标准表或 30k 的宽表,因此您可以轻松地将粒度降低到 10 分钟,或者更细的 30k 表。
我希望这能为如何完成它增加一些不同的视角。 仅当您担心空间/大小或者您可能有数十或数百个数百万个时,才真正有必要。
I've actually used this design before, basically creating a bitmap for the time span you want to regularly schedule divided by the number of periods you want. So in your example you want a week schedule with hourly periods, so you'll have a 168 bit bitmap which is only 21 bytes long. A couple of datetimes are 16 bytes together and you'll need multiple rows of these to represent the possible schedules for a given week so if you care at all about size I don't think you can beat it.
I will admit it is a little trickier to deal with and less flexible than the previous suggestions. Consider if you all of a sudden wanted to use 1/2 hour periods, you would need to transcode all your existing data to a new 336 bit bitmap and distribute values out.
If you're using SQL, you can either store this as a binary blog and do the bit twiddling to compare whether a bit is on or off yourself or you can store each bit as a column. MS SQL Server supports up to 1024 for standard or 30k for wide tables so you could easily get granularity down to 10 minutes for either or a great deal finer for a 30k table.
I hope this adds a little different perspective on how it might be done. It's really only necessary if you are worried about space/size or if you have perhaps 10s or 100s of millions of them.
您可以轻松地将“允许”的时间记录在表格中。 这样,如果不存在,则不允许。 如果您需要更多变量的“时间表”,您可以轻松添加年份和月份字段。
对于 2008 年 12 月:
您可以在 2008 年 12 月的所有日期执行维护。 按照您的意愿显示。
You could easily just record the "allowed" times in a table. That way, if it isn't there, it isn't allowed. If you need to have a more variable "schedule" you could easily add a year and month field.
For December, 2008:
You have all the days for Dec 2008 on which maintenance can be performed. Display however you wish.
每个提议的解决方案对我来说都很好,无论如何,如果您面临性能和/或表大小问题,我会考虑这个解决方案。 由于您可能会在时间和实体(即服务器)之间建立关系,因此大小将增加实体编号 * 实体时间。 如果每个时间跨度都有一行,这可能会很痛苦。
这个建议在表结构方面稍微丑一点,但在磁盘空间和表扫描速度方面更有效。
考虑一下您想要在周日和周一为服务器分配 16:00 - 20:00 正常运行时间的示例,您将只有两行,例如
您将假设每个丢失的行都意味着服务器已关闭。
如果您需要此功能,您可以考虑对日期列使用 DATE 格式来设置特定日期(即仅 2013 年 10 月 2 日 16:00 到 20:00 之间的正常运行时间)。
希望能帮助到你
Every proposed solution is good to me, anyway i would consider this one should you face performance and/or table size issues. Since you would probably make a relation between time and your entity (i.e. a server), size will increase by entity_number * entity_times. If you have a row for each timespan this could be a pain.
This proposal is little uglier in terms of table structure but more efficient when it comes to disk space and table scan speed.
Consider the example where you want to assign 16:00 - 20:00 uptime to a server on sunday and monday, you will have only two rows like
You will assume that every missing row means server is down.
Should you need this, you could consider using a DATE format for the day column to set specific dates (i.e. uptime only 2013/10/02 between 16:00 and 20:00).
Hope it helps
也许像
Maybe something like
用 Python 编写,这样的模块可能会起作用 - https://github.com/AndrewPashkin/pytempo
Written in Python, a module like this might work- https://github.com/AndrewPashkin/pytempo