Ruby algorithm help using cron jobs - ruby-on-rails-3

I'm determined to get this algorithm (if you want to call it that) figured out over the weekend and I think I'll need the help of cron jobs or maybe clockwork.
Here's my logic:
When adding a book, it needs to be stuck in a "queue line" or "queue status" and will only be "published" at the top of the week. At the top of the week, if no book has been "queued", add a new "published" book & make the form available again (the form will get hidden once a book has been queued to publish next). When I say top of the week, I mean that a book must be published every 7 days, going from the date the user's account was created.
To give some background, I'm creating an app where users are encouraged to read one book per week over the course of 2013 (yeah this thing has to launch by Monday!). They are able to add a book that they're going to read the next week and change the status of that book to 'read' once they read it. If they don't change the status to 'read' within 7 days of adding it, it automatically gets moved down the line and displays as 'unread' and the new blank book gets added (like what I explained above).
The whole adding of books and changing the status to read/unread is already complete. I'm just looking for help setting up the 7 day incremental book adding and "queue line".
Some insight into my code:
I have a books controller & model that belongs_to a user. My user model has_many books.
I'm adding and displaying the users' books through the user show view.
I'm sure there are things that I'm leaving out that will help better explain my environment, so just ask me :) Thanks!

I don't think you need a chron job for this.
What I would do is add a status to each book of read/unread. And then write a method in the model that checks on page load and modifies the view or controller data for display.
Rails/Ruby has some nice date/time helpers where you can say date1 <= 7.days.ago.
Is there any reason why this approach won't work?

Related

Database design for recurring events with exceptions

I'm building a system that needs to store/manage different types of events. For simplicity, I will focus on designing a calendar (I'm building something slightly different, but calendar is a good analogy and it's easy to reason about). I'd like to hear about possible database/schema design ideas.
Problem Description
I have a calendar with different types of events (for simplicity sake, say there is only 1 type of event: Task). User can add new event for a particular date, edit (change some details, like title or move to another date) or delete. There can be one-time events and recurring events (with different types of recurrence: every X days, every 15th day of the month, every week on Monday; kind of like simple cron). When user moves recurring event, all other instances of this event are moved in the same manner (e.g: +3 days). Important part: recurring events can have exceptions. So, for example, let's say I have an recurring event A which is repeated every 7 days. But I want to change it's date for next week, so instead of Tuesday, it's be assigned to Friday, after that it'll still occur on Tuesday. This "exception" event shouldn't be affected when "parent" event is moved.
Also, every recurring event can have additional info, that is related only to 1 particular instance, e.g: I have the same recurring event A repeated every 7 days, I want to add a note for this week instance that says "X", and I want to add another note for the event A next month that says "Y" - those fields are only visible to that single instances.
Ideas
System with regular, one-time events is pretty straightforward so I won't discuss that and focus only on recurring events.
1. One possible solution is the one that resembles OOP: I can have an Event "class" with fields such as start_date, end_date (can be null), recurrence_type (something like enum with possible values of EVERY_X_DAYS, DAY_OF_WEEK, DAY_OF_MONTH) and recurrence_value (say 7). When user adds new recurring event, I just create such Event in the database. When user wants to change 1 occurrence of this event, I add new entry to the DB of the type/class MovedEvent that "inherits" from Event with different date and has additional field related_to that points to the ID (or UUID, if you will) of the Event that it's related to. But at the same time, I need to keep track of all the MovedEvents (otherwise I'd have 2 events displayed in the same week), so I need to have an array moved_events of IDs that point to all MovedEvents.
Disadvantage: every time I want to display the calendar I need to get Event and select all events from the moved_events, which is not optimal if I'll have a lot of moved events.
2. Another idea is to store every event as a separate record. IMO it's a terrible idea, but I just mentioning it because it's a possibility. Disadvantages: every time I want to edit the main event (e.g: I want to change the event from occurring "every 7 days" to "every 9 days") I need to change every single occurrence of the event. "Exceptions" (changing single instance) is easier, though.
SQL/NoSQL? Scale details
I'm using PostgreSQL in my project, but I have basic knowledge in NoSQL databases and if they are better suited for this kind of a problem, I can use it.
Scale: Let's say I have 5k users, and each will have on average 150 events/week, 40% of which can be "exceptions". Therefore I want to design this system to be efficient.
Similar Questions & Other Resources
I've just started reading Martin Fowler's "Recurring Events for Calendars" (http://martinfowler.com/apsupp/recurring.pdf) but I'm not sure if it applies to my problem and if so, how one would design database schema according to this document (suggestions are welcome).
There are similar questions, but I didn't see any mention of "exceptions" (changing 1 event instance without affecting other), but maybe someone will find these links useful:
Design question: How would you design a recurring event system?
Optimal design for a Database with recurring event
Design option for 'recurring tasks'
Calendar Recurring/Repeating Events - Best Storage Method
What is the best way to represent "Recurring Events" in database?
Sorry for a long question, I wanted to describe the problem well. Yet, I feel that's pretty chaotic, so if you have additional questions, I will happily provide more details. Again, I'd like to hear about possible database/schema design ideas plus any other suggestions. Thank you!
Use iCalendar RRules and ExDates
If it's a recurring event, just store the start/end datetimes and RRules and ExDates for the event.
Use a Materialized View to pre-calculate upcoming actual events, say for the next 30 days or 365 days.
As you are using Postgres, you can use existing python, perl, or javascript RRule libraries (such as dateutil) inside pg function for calculating future events based on the rrules and exdates
UPDATE: check out pg_rrule extension: https://github.com/petropavel13/pg_rrule

Difference between 2 meetings overlapping each other

I'm having this issue:
I'm using VBS to extract all meetings from our conference rooms.
Sometimes I get from the same room 2 meetings that overlap each other for a certain amount of time. But, outlook shows only one of them.
I've tried to check all the item.fields to see what seems to be the criteria by which a meeting is shown in shared calendar, or not, but all seems to be the same for both.
I would have uploaded my code here, but it is very long, about 350 code lines.
So, my question is, what property is used by outlook to show to other people in a shared calendar, a meeting, if it overlaps with another one?
I`ve found the answer.
Microsoft uses this model for recurrent appointments:
When a recurring event is created, the item.isRecurring is set to True.
then the colection occurrences is added to item, and reccuring pattern object.
If you delete or modify one or more occurrences, anther object is added to occurences, exception. All deleted, or modified occurrences can be found here.
the strange thing is that even if an occurence is deleted, you can still find it as active, and thus, overlaying with the appointments created afterwards.
The trick is to check all the way, even in exceptions, in order to be able to get same view as in Outlook.
if you need additional details, pm me.

Linking actions on two tables

Sorry the title isn't very descriptive, but here is what I'm trying to do:
I am making a website that handles student group funding requests. A particular request for funding could involve any number of items. I have two tables: requests and items. A group begins a request by filling out a "request" form, which contains a brief summary of the request, a category (speaker, advertising, etc.), and some other fields necessary for the bureaucratic process. That works fine and was handled pretty much entirely by scaffolding. The problem I'm having is with the "items". I would like the user to click the "submit" button on the new request form and be taken to another page at which he can add any number of items to the items table, all containing in a field the id of the request he just submitted.
The front end appearance doesn't really matter - it could all be on one page - what's important is that the user submits information to one table and then submits information to a second table, with the info sent to the second table containing something that connects it to the information just submitted to the first table.
As you can probably guess, I'm pretty new at rails (I went through the book Head First Rails and I'm working on my first project), so I'm looking for a general explanation about how this can be accomplished.
I recommend you to read / watch this tutorial. It is quite good.
UPDATE:
http://railscasts.com/episodes/196-nested-model-form-part-1
Hope it helps.

Number of participants in Mturk?

I'm an absolute newbie in Mturk,and I'm definitely not a person who understands smth in programming. thus,I would appreciate a lot a simple answer on my question saying which button and where I should press(this is an upper limit of my computer skills).
the question is the following one:I want 200 people to participate in my survey. I want 1 pseron to answer on my survey just 1 time. where should I set those requirements? should I put the number 200 here:Number of assignments per HIT or there will be some other space where I will specify the number of my participants?
thanks a lot in advance!!!
I think you are looking for the MaxAssignments option of CreateHIT operation.
Here is the attached image for the developers guide
It says each worker can complete a HIT only once, so multiple assignments are guaranteed to be completed by multiple workers.
Hope this helps
I have seen workers try to complete my HITs more than once, even though there not meant to see the HIT again. Here are some workarounds to prevent this form happening.
Republish the same HIT. Workers can access an identical HIT if it's published under a different name.
You can add a qualification for every worker that has already taken your survey. Even without programming, this is fairly simple. You can download a list of your workers, add the qualification on the spreadsheet for all of them, and then upload the spreadsheet into MTurk. When you set up your HIT, you can add this qualification. In this case, you would add: (Name of your qualification) Has Not Been Granted.
Good luck.

Trac plugin to send email number of new and closed tickets and their details based on define schedule

I am looking for a way or a plugin so that trac sends me email about the number of new or closed tickets (and some information about these tickets also ) for a specific duration lets say for the last three days.
Basically I need to know how many tickets have been created in last week and how many of them have been closed at the end of week.
Of course the email only should be sent to the admin and not to all the users.
For additional Trac funcionality we have Trac plugins, yes. And the first place to look for them is trac-hacks.org .
The excellent TagsPlugin in use overthere already delivers some hints on resources tagged with notification or notifications. The most comprehensive and mature solution is certainly TracAnnouncer with a just reworked configuration interface providing a highly sophisticated opt-in and opt-out subscription system. Unfortunately digest notification are not integrated today.
Still there are other plugins, that fill in the gap, i.e. check the XMailPlugin. It claims to do configurable instant, daily and weekly notifications, so this may be for you. Since this is a relativly new plugin, you should expect some pending issues, but the author might be very open to your suggestion. If you're becoming a heavy user giving valuable test feedback and a bit lucky too, asking kindly could be enought to make things happen.
There's a slightly different way to solve this problem that doesn't require any plugins. First, create a custom "timeline" view that displays the information that you want. In your example, this would be all "opened and closed tickets" starting from "today" and going back three days. When viewing this custom view, you should see a link at the bottom of the page that says "RSS Feed" (on my system, the resulting URL looks something like this: http://myserver/timeline?ticket=on&max=50&authors=&daysback=3&format=rss). Click on this link to subscribe to the feed using your web browser, email client, or other program capable of reading feeds. Now, you can view the results live at any time. What you can do at this point is only limited by the capabilities of your feed reader app, but most can at least be configured to notify you when the feed is updated.