How would you implement a "last seen" feature for users? - scalability

On Stack Overflow, the profile page lists a "last seen" property. This doesn't seem to be updated on every page view (for performance reasons, obviously). How would you implement it in a heavy-traffic web app? Would you update it only on certain pages? Or cache the last time you logged the user's last visit and wait a specific amount of time before updating the database? Or something completely different?

On a heavy-traffic site like Stack Overflow, I would only update the "last seen" variable when a user actually does something. Lurking around and reading questions and answers shouldn't count as a user being "seen" by the system. Asking and answering questions, or voting on them should be actions that update when a user is last seen.
I won't talk about the implementation details because that's already covered by other answers (and I would probably get it wrong).

You'll probably find "What strategy would you use for tracking user recent activity?" to be helpful. The issues are similar.

I would use a SESSION. And only set it the first visit of the session. Also resetting it every hour or so for if people leave the browser open. In php something like this:
if(!isset(!_SESSION['lastSeen'])){
$_SESSION['lastSeen'] = time();
updateLastSeenInDatabaseOrSomething();
}
else{
if($_SESSION['lastSeen'] < time() + 2 * 60 * 60){ //2 hours
$_SESSION['lastSeen'] = time();
updateLastSeenInDatabaseOrSomething();
}
}
Something like that but then with OO and not doing the same thing twice.

Consider using the "Command" design pattern for this. It will help you two ways - answer the question at hand and also implement an "undo/redo" feature. You should maintain a list of command objects designed per that pattern.

Related

Send Mautic Form to different Recipients based on Formfield

I've a Mautic form with Radiobuttons where the User can select which Department he want to connect with.
o General Question (1)
o Sales (2)
o Technical Support (3)
I want to send the Request to the Person in Charge. So for example:
1: info#company.org
2: sales#company.org
3: support#company.org
I've tried different approaches, but non worked.
The most dirty one was to set the values of the radio buttons as E-Mails and send the Form to the Contact. Worked in some way, but then off course saves the Radiobutton Input to the Database as customer E-Mail. So, only smart in the first place. ;)
I've also tried campaigns, but couldn't find a nice way to use Formfields in the Campaign Templates.
I cannot accept that this couldn't be possible in a easy way. This Form gonna replace a Powermail Form in TYPO3 and there it kind of works like a charm.
Any hint is very much appreciated.
Sorry that you found the documentation bad - we are actually in the process of improving and migrating our developer docs, so please take a look at the WIP docs here: https://mautic-developer.readthedocs.io/en/latest/plugins/getting_started.html which are much improved!
(will also post this on the forum thread!)
After weeks and weeks of research and workarounds I got at the Mautic Form, here is my solution to this Challenge.
I really love Mautic and its extensiveness. It’s just a great and powerful tool.
But at this point it totally misses the market. For me it just feels so wrong and like a big error by design, when you have to create fake fields to handle formfields for such a simple task.
Not talking about the problem when the user overwrite his records before the data being send.
As mentioned this is so easy to do in TYPO3 with Powermail and so I was thinking about creating a plugin, but the documentation on this is really bad.
So here is my solution, as I was looking for simple solution for me as well as the customer.
Solution is tested and worked like a charm for me. Here’s what you can do, for everyone also looking something like this:
Create 1 custom field and label it “Owner (form)” - set Type to Text
Create 2 MySQL Trigger as follows (just copy the code 1:1, should work out of the box):
DELIMITER ;;
CREATE TRIGGER set_lead_owner_from_radiobutton_on_insert BEFORE INSERT ON leads FOR EACH ROW
IF (NEW.owner_form != NULL) THEN
IF (SELECT count(id) FROM users WHERE users.id=NEW.owner_form) > 0 THEN
SET NEW.owner_id = NEW.owner_form;
ELSE
SET NEW.owner_id = NULL;
END IF;
END IF;;
DELIMITER ;
DELIMITER ;;
CREATE TRIGGER set_lead_owner_from_radiobutton_on_update BEFORE UPDATE ON leads FOR EACH ROW
IF (NEW.owner_form != NULL) THEN
IF (SELECT count(id) FROM users WHERE users.id=NEW.owner_form) > 0 THEN
SET NEW.owner_id = NEW.owner_form;
ELSE
SET NEW.owner_id = NULL;
END IF;
END IF;;
DELIMITER ;
Create a form with a Radiobutton or Selectbox and set the “Contact Field” to our custom field “Owner (Form)”
Note: Values of the Radiobuttons / Selectboxes need to be set to the User IDs of your Mautic Instance. So you have to create an user for every Select- oder Radiobutton-Option.
Select for example “Send form results” in Actions and set “Send to owner” to yes.
That’s it.
So what does it do. It’s basically all about the MySQL Triggers. Every time a new Lead is created or updated and our custom field “Owner (form)” is not null and has a valid entry (User ID), the trigger copies the value from our field to the original Owner Field of the lead. So we can then use Owner of the Lead (in my case a Department) to send him a E-Mail.
I hope this is helpfull to someone. But even more I hope that Mautic is gonna fix this in the future, as I believe this a very essential task when it come to enterprise Websites.
Cheers,
Lufi
Mautic Forum Discussion: https://forum.mautic.org/t/send-mautic-form-to-different-recipients-based-on-formfield/24363/13

Xtext - Temporarily tolerate unresolvable references and make the reference name user readable

Let's say I have a language that models a part of stackoverflow. Users are held in one resource, questions in another.
Users:
user fred : fred#foobar.com
user notfred : notfred#foobar.com
Questions:
question 123 by fred message "smart question"
question 124 by notfred message "not so smart question"
Now, the user "fred" wants to remove his account, but this wouldn't work because after loading both resources into my ResourceSet I would have a non-empty Resource#getErrors().
I can work around this by filtering XtextLinkingDiagnostic from the errors, but still other users reading the "smart question" cannot tell anymore that it was asked by someone called "fred". The info is still there, I can access it for example when I set a LinkingDiagnosticMessageProvider with LazyLinkingResource#setDiagnosticMessageProvider(...); however, the best thing I can now do is, show other users validation errors that "fred" was deleted, but they wouldn't know "fred" wrote the message they are just reading. Knowing this would help them a lot because everybody knows that "fred" writes great questions, right?
Long story short, I have an application into which users can load a declaration and a definition file. In very few cases something goes wrong and both files don't match perfectly, which means the definition has entries that are not declared in the declaration. However, I know that ~95% of the entries will still match!
Users cannot fix this quickly, but it is likely that they are happy just editing the 95% definitions, but they still need to be able to read the names of the 5% declarations without editing them!
I am not currently using any UI-parts of Xtext to edit the definitions, but rather a custom UI in form of a table. The current state with the missing declarations is that everything except a value column will be empty. The reference ID would be in another column, and knowing this ID would help the user a lot! Is there a clean way to achieve this?
Have a look at the 'Node Model' e.g. org.eclipse.xtext.nodemodel.util.NodeModelUtils.findNodesForFeature(EObject, EStructuralFeature) allows you to access the text that is written in the file

Keeping track of multiple ID's in a Quiz using Sessions

Im making a quiz in MVC. After all the questions have been answered, the results is written to the database when the quiz is completed. So far so good.
There are both multichoice and singlechoice questions.
The answers and questions are taken from a database, and are going to change over time.
During the quiz i keep track of the replies from the user, by using a few sessions.
I have a int (index) to keep track of which question is the current, the prev. and the next.
I have a List<-int-> with QuestionID's.
I have a List<-List<-int->-> with answers-ids so i can see what answers fit what question.
Everything is going very smooth as long as the user use my "previous question" and "next question" buttons, but if the user uses the "back" and "forward" buttons it ruins my index.
So my question is how do i prevent this from happening?
Can i force the user back to "home", clear the sessions and start again, if he presses the back-button?
Let me know if you need more information...
In webforms I would add the "current index" to the ViewState and reset my Session index to that value, if present.
MVC doesn't have ViewState, so maybe you can use a <input type=hidden> for a similar effect.

Does this Rails 3 Controller method make me look fat?

This is a new application, and I have an index method on a Search controller. This also serves as the home page for the application, and I'm trying to decide if I am headed down the wrong path from a design pattern perspective.
The method is already 35 lines long. Here is what the method does:
3 lines of setting variables to determine what "level" of hierarchical data is being searched.
Another 10 lines to populate some view variables based on whether a subdomain was in the request or not.
A 10 line section to redirect to one of two pages based on:
1) If the user does not have access, and is signed in, and has not yet requested access, tell them "click here to request access to this brand".
2) If the user does not have access, is signed in, and has already requested access, tell them "so and so is reviewing your request".
Another 10 lines to build the dynamic arel.
I can't get it straight in my head how to separate these concerns, or even if they should be separated. I appreciate any help you can offer!
Summarizing what you've said in something codelike (sorry, don't know ruby; consider it pseudocode):
void index() {
establishHierarchyLevel();
if (requestIncludedSubdomain())
fillSubdomainFields();
else
fillNonsubdomainFields();
if (user.isSignedIn() && !user.hasAccess()) {
if (user.hasRequestedAccess())
letUserIn();
else
adviseUserOfRequestUnderReview();
}
buildDynamicArelWhateverThatIs();
}
14 lines instead of 35 (of course, the bodies of the extracted methods will lengthen the overall code, but you can look at this and know what it's doing). Is it worth doing? That really depends on whether it's clearer to you or subsequent programmers. My guess is it's worth doing, that splitting out little code blocks into their own method will make the code easier to maintain.
That's a lot of variables being set. Maybe this is a good opportunity for a module of some kind? Perhaps your module can make a lot of these decisions for you, as well as acting as a wrapper for a lot of these variables. Sorry I don't have a more specific answer.
Without your code it's somewhat difficult to suggest actual fixes, but it definitely sounds like a really wrong approach and that you're making things much harder than they need to be:
3 lines of setting variables to
determine what "level" of hierarchical
data is being searched
if there is a search form, I would think you would want to pass those straight from the params hash into scopes or Model.where() calls. Setup scopes on your model as appropriate.
Another 10 lines to populate some view variables based on whether a subdomain was in the request or not.
This seems to me like it should be at most 1 line. or that in your view, you should use if statements to change what you'd like your output to be depending on your subdomain.
A 10 line section to redirect to one of two pages based on:
the only thing different in your explanation of the 2 views is "whether the user has requested access" surely this is just a boolean variable? You only need 1 view. Wrap the differences into 2 partials and then in your view and write one if statement to choose between them.
Another 10 lines to build the dynamic arel.
It might be necessary to go into Arel, but I highly highly doubt it. Your actual search call can in most cases (and should aim to be) 1 line, done through the standard ActiveRecord query interface. You want to setup strong scopes in your models that take care of joining to other models/narrowing conditions, etc. through the ActiveRecord Query interface.

NHibernate IQuery.List Error "Invalid index 4 for this SqlParameterCollection with Count=4."

Long title, I know but I searched all over and couldn't find that error message coming from that function call so I thought this might be more useful.
This is the code snippet:
string hql = " from LabRequest r where 1 = 1 ";
hql += " and 0 < (select count(rs) ";
hql += " from r.Statuses rs ";
hql += " where rs.StatusType.Description IN ('Assigned','Submitted')";
hql += " ) ";
//Session.Clear();
IQuery query = Session.CreateQuery(hql);
IQueryable<LabRequest> requests = query.List<LabRequest>().AsQueryable<LabRequest>();
This is a function (or most of it) in my Data Access Object in an MVC app I'm working on. It's for a search page and when the page runs this function gets called exactly like you see in the code and works.
Then, without changing anything, I refresh the page which goes through the same steps and calls this code, exactly as you see it, again. But the second time through it crashes on the query.List() portion of the last line with the error in the subject.
Session is defined in another DAO as:
session = NHibernateHelper.GetCurrentSession();
I know this is hard to analyze without the actual DB but I just wanted to see if anyone could maybe point me in the right direction, or maybe point out something obvious about NHibernate since I know basically nothing about it.
Edit: forgot to mention that when I uncomment the Session.Clear() it works fine, so was thinking the answer has something to do with that, and if it does how I should handle when to clear()?
Edit 2: This is part of the answer, but I call a very similar function prior to this one the second time around. What I can't figure out is why that one is affecting the one I posted. The 'query' variable is local, so it seems to be something with Session.CreateQuery. Anyone know what that would be?
Thanks,
Jeff
While I'm not sure why exactly it seems the 'Statistics' property on the Session has data on it from the first query and I think this is what's causing the error because if I do a Session.Clear it removes the collections in the Statistics property.
As my current, and possibly temporary fix, I just created an extension method for the CreateQuery function that takes a bool asking whether to clear the Session and am just using this instead of the one provided.
If anyone else has any real answer to this please add it.
Relating to your "Edit 1" and "Edit 2" notes, yes, it has to do with the session that is shared (assuming you're using one of the standard methods of handling sessions in NHibernate).
Is there a good reason for using Session.Clear()? In general, Clear is only used after a flush, to make sure the Session cache doesn't get too big causing a performance hit. Are you using it that way, or for some business reason not mentioned in your question?

Resources