I have an app where users can upvote or downvote a blog post. To implement the voting functionality, I created the below model ...
class Upvote(ndb.Model):
user_id = ndb.IntegerProperty(required=True)
blog_id = ndb.IntegerProperty(required=True)
When an user upvotes a post, I save it via:
Upvote(user_id=User.key.id(), blog_id=Blog.key.id()).put()
And to see if an user has already upvoted I query:
Upvote.query(Upvote.user_id=User.key.id(), Upvote.blog_id=Blog.key.id())
Is this the most efficient way to implement such a voting system? I want to make sure as it seems the Upvote model can get very large. Of course I am prematurely optimizing now but in a theoretical situation with millions of users I want the most efficient/cheapest method.
Well, for one I'd call the model just Vote and I'd add a BooleanProperty called upvote, set to True if it's an upvote and set to False if it's a downvote.
Otherwise you'd have to repeat the story for downvotes and without some extra logic you'd leave room for a user to cast 2 votes, one up and one down, for the same post, which IMHO doesn't make a lot of sense.
Yes, there could be many Vote entities, but they are small and not related to each-other, scaling pretty good.
Related
I want the members of an interview committee to fill out a Google Form that will help manage which questions they will each ask the candidates they are interviewing. I don't want a candidate to be asked the same question by multiple interviewers.
Step 1, they select from among 96 "competencies," e.g., "Accountability" might be a competency they believe the position requires. They can choose up to, let's say, 5 competencies.
Step 2, based on which competencies they selected, they can now choose interview questions. We have a bank of 970 potential interview questions, each one of them directly related to one of the competencies. As interviewers select questions, those questions should be eliminated as options for the other interviewers. [I found some code to support the elimination part of this step in AppScript. What I CANNOT figure out is how to make the list of questions also based on which competencies they selected in the prior step. They should be choosing from a short list, not from a list of all 970. Each competency only has a handful of questions tied to it.]
Step 3, once everyone has completed the Form, we can produce a doc or pdf so everyone on the committee can see what everyone else is asking.
Is this possible??
If I understood your question correctly, Google Forms are not suitable for your task. You can find Add-Ons and GAS example codes that can change, for example, options for some dropdown question.
The problem is those changes are reflected to all the form viewers/users, in your case interview committee. Think about it like you have a single Google Doc file, "Terms and Conditions" that you can edit and others can view. Google Form are designed with a "same for everybody" idea in mind, so you can see answer percentages right away, like you have poll on twitter.
What you can do, since you already played with GAS is to create GAS WebApp that presents different options for each user. But if you are beginner I wouldn't recommend this, you will have to be GAS + JuniorWeb developer at least. Plus, since users can work concurrently, you will have to double check if what user selected became unavailable meanwhile (after competencies are selected, while you are picking the questions someone maybe submitted one of your competencies, when you hit submit your selections are invalid, you'll have to do it again).
The simplest way is if you have a company website, to ask the website developer to make some kind of company portal (employees only, not visible to visitors). Then to create this tool you need and put it on the company portal.
I saw some solutions for "race for resources" in Google Sheets where you can have competencies listed, then one true/false checkbox column "Reserved" and one "Who Reserved" column. And the filter is set to hide Reserved=true (filters rows for everybody, "live"). Then users can "collaborate" and user is aware if something they wanted became unavailable second ago. "Who Reserved" would require GAS code to handle onEdit event. But you have a second level - questions, I have no idea how you could make this.
I have a skill that elicits a U.S. state and county from the user and then retrieves some data. The backend is working fine, but I am concerned about how to structure the conversation. So far, I have created an intent called GetInfoIntent, which has two custom slots, state_name, and county_name
There are about 3,000 U.S. counties with many duplicate names. It seems silly to me that I am asking for a county, without first "narrowing down", by states. Another way I can think of to do the conversation is to have 50 intents, "GetNewHampshireInfo, GetCaliforniaInfo, etc. If I did it this way, I'd need a custom slot type for each state, like nh_counties, ca_counties. etc.
This must be a pretty generic problem. Is there a standard approach, or best practice, I can use?
My (not necessarily best practice) practice tips:
Single slot for single data type. Meaning only have one slot for a four digit number even if you use it in more than one place for two different things in the skill.
As few intents as you need
no more no less. You certainly can and should break up the back end code with helper code, but try and not break the intents into too many smaller pieces. It can lead to difficulty when Alexa is trying to choose the intended intent.
Keep it voice focused. How would you ask in a
conversation. Voice first development is always the way to go.
For the slot filling I think it is fine to ask both state and county.
If the matching is not correct ask for confirmation.
Another option is to not use auto filling within the Alexa skill and use the dialog interface. Ask the county first and then only when it has more than one state option and is ambiguous continue the dialog to fill the state.
Even if you did have 50 separate intents you really never want to have two slots that can be filled by the same word. For example having a mo_counties and ky_counties that Clack satisfies both is ambiguous and can cause unneeded difficultly.
So for someone looking for the "best practice" I have learning that there isn't one yet (maybe never will be). Do what makes sense for the conversation and try and keep it as simple as it needs to be and no less on the back end.
I also find it helpful to find a non-developer to test your conversation flow.
This wasn't really technical and is all opinion, but that is a lot of what Alexa development is. I would suggest Tuesday Alexa office hours at https://www.twitch.tv/amazonalexa very helpful and you can ask questions about stuff like this.
I am trying to make a mock interview skill on Alexa where the skill asks the user a question for example: "tell me about your background and experiences".
The user would give an answer, and when the user is done answering, he/she can say "next question" to get the next question.
So "next question" is really the only intent the app is waiting to hear. The problem is when the user is giving an answer for example:
"My name is Bob, I am from New York, I studied biology, etc.",
the session is still live, and Alexa obviously doesn't understand the intent so AMAZON.FallbackIntent gets triggered.
Is there a way to just return an empty string when AMAZON.FallbackIntent gets called so the mock interview session doesn't get disrupted?
Thank you!
It sounds like you need to control the session and constrain the user.
IMO Alexa has a lot of trouble with long user utterances. The problem really stems from the interaction model and the unpredictability of what a user will say. This blog post sheds some light on VUI issues (https://medium.com/hackernoon/lessons-learned-moving-from-web-to-voice-development-35daa1d301db). tl;dr - you have to maintain state and context.
One approach you can take is to ask the user specific questions. "What is your name?" should map to one intent and update the session/persistence with the slot value. Then you respond with the next question you want the user to answer(i.e. "Where do you live", "what university did you attend") , having another intent ready to handle that slot value. You have to realize that users can say anything at any point in your Alexa Skill session and your skill has to handle it.
Here's an Amazon Developer blog post that can help you better understand dialog management and slot confirmation: https://developer.amazon.com/blogs/alexa/post/3a23c045-b568-4a6a-8a8c-fd5511a08053/build-advanced-alexa-skills-confirm-what-customers-want-with-dialog-management
I am making a simple app where one can post any thing and anyone can comment on that post.
Do I need to create an entity group making every post as parent and comments as children?If I am wrong how to model the data having post and comment as entities? Documentation says there a limit of 1 write/sec If my app has high traffic like many people are commenting on the same post at the same time there will be a problem of transaction failure. How to solve this ?
Before you start coding on a new platform, first do a bit of research and try to fully understand all the features and nuances first.
To answer your question, YES, you can create an Entity Group with the original post as parent and then each comment as a child, but WHY?
What are you trying to achieve? Having a Post with comments attached to it, right? Why not just save all of them in ONE entity?
Entities have attributes (fields); so you can have POST, COMMENTS as field types of the Entity Thread. The COMMENTS attribute/field can be an array of strings or text or objects as you see fit.
So when you are displaying the data, fetch the Thread entity, and display its POST field (the original post), then underneath it, display the COMMENTS field (extract strings from the string array then display them in order).
I just realized when doing basic CakePHP stuff, that there is quite bad security issue, which many don't necessarily notice. I'll just take this basic function that I think many users use while doing CakePHP driven apps.
function edit() {
if(!empty($this->data)) {
if($this->User->save($this->data)) {
}
}
}
Lets assume user has privileges to use this action. This action could be editing user information, which may have like city and number and ofcourse username. Lets assume that we want to have a form that allows us to edit just the city and number but not the username. Well what if someone just inserts that username field into that form with firebug for example? Then submits the form. Now the edit would just grab all the post information, including the username field and its value and edit them straight away. So you can change your username in this case even though there werent a field for it.
This can go even further, if someone would use saveAll(), which allows you to validate and save multiple models in one shot. If you could guess from form fields the models to use, you could easily go to other models and tables aswell and alter those information.
Now that you understand my concerns, my question is what would be the best or atleast near the best method to avoid this?
I know I could just grab the data I want from $this->data to other variable and then pass that to the save or saveAll, but because there are many forms and ajax requests, that would be quite a lot of work. But is it the only way to go or are there better ways?
Should I make or is there a behavior which could stop this? Like checking what variables some action in some controller can get from post?
After couple of days research I found that this is not really a "security hole", but rather beginners mistake.
There are two ways avoiding this type of form tampering: Security component ( http://book.cakephp.org/view/1296/Security-Component ) which automatically gets CSRF and form tampering protection by creating one-time hashes for form fields.
The other way is to give the third parameter to save() function. The save actually gets 3 parameters: data, validate, fieldlist. The fieldlist parameter acts like whitelist of fields that are allowed to be saved.
I first reported this problem as a bug to cakephp which it then wasn't but this euromark guy replied to me that he had done nice documenting about the actual problem and how to do secure saves and I really think it was quite good reading. So if you have the same problems, please see this page: http://www.dereuromark.de/2010/09/21/saving-model-data-and-security/