Skip to content

Conversation

@yashasingh
Copy link
Owner

@tgr Please review my PR for microtask2.

App/views.py Outdated

percentile = 100-((userrank*100)/(totalusers+1))
rating = (userrank/totalusers)*100
if(rating<=1):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This big if...else structure could be written much more simply.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced.

App/views.py Outdated
usr = request.POST['username']
SqlQuery1 = "SELECT user_editcount FROM user WHERE user_name='" + usr + "';"
SqlQuery2 = "SELECT ss_total_edits FROM site_stats"
SqlQuery3 = "select rank from (select user_name, @curRank := @curRank + 1 as rank from user p, ( select @curRank := 0 ) q order by user_editcount DESC) q where user_name='"+usr+"';"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does work but it's a bit overcomplicated. You just need to count how many users have more edits.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More problematically, the first and third queries should escape the user. Right now you could create a user called something like '; DROP TABLE user; -- and things would end badly.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have improvised the rank query.
Also, I have made the input string 'sql injection proof' :)

App/views.py Outdated
totalusers_tup = curr.fetchall()

conn.close()
useredits = useredits_tup[0][0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no guarantee this query returns any rows.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rectified.

@yashasingh
Copy link
Owner Author

yashasingh commented Oct 6, 2017

@tgr I have made all the changes as required.
Please review.

@yashasingh
Copy link
Owner Author

Tool link is - this

App/views.py Outdated
flag = 1
usr = request.POST['username']
a = 1 if re.match("^[a-zA-Z0-9_ ]*$", s) else 0
if(!a):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like valid Python. (Now that I look at it, all your ifs are in C-style. In Python you can just wrote if reqest.method=='POST' without the braces.)

Also, what's the point of the variable? You can always just inline it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

App/views.py Outdated
if(request.method=='POST'):
flag = 1
usr = request.POST['username']
a = 1 if re.match("^[a-zA-Z0-9_ ]*$", s) else 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would leave users with a non-ASCII username stranded. You should accept anything that's a valid MediaWiki username and escape it. Usually in SQL that means using parametrized queries. (Also, where does the s come from?)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

App/views.py Outdated

def main2(request):
if(request.method=='POST'):
flag = 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but if you want a nicer approach, you can wrap everything in a try...catch and throw an exception when the user is not found etc.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

App/views.py Outdated
usr = request.POST['username']
a = 1 if re.match("^[a-zA-Z0-9_ ]*$", s) else 0
if(!a):
flag=0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much point in setting this as you are exiting the function anyway.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@yashasingh
Copy link
Owner Author

@tgr Please review.

App/views.py Outdated
totaledits = totaledits_tup[0][0]
userrank = userrank_tup[0][0]
totalusers = totalusers_tup[0][0]
percentile = 100-((userrank*100)/(totalusers+1))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the logic here. In practice this will be the same as 100 - rating (since totalusers is large so adding one doesn't matter). In theory, the +1 seems wrong. The percentile is the smallest number N for which the user is included in the top N% of users - you order the users by editcount, split the list into 100 equal chunks, if the user is in the chunk with the largest editcount, they are in the first percentile, the next chunk is the second etc. So the top 1% is the users with rank 1..(totalusers/100), top 2% is (totalusers/100)..(2*totalusers/100) etc. So percentile is same as what you call rating here (or 100 minus that if you prefer - either could be called a percentile).

Copy link
Owner Author

@yashasingh yashasingh Oct 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was calculating percentile on the basis no of editcounts , so the user with highest editcounts gets 99 percentile (since 99% of users lie below him) and not the basis of userrank. In which case, the percentile of the user with highest no of editcounts would we 1, so he would be in top 1 percentile.
I'll have updated it according to userrank.

@yashasingh yashasingh changed the title Devtask2 Microtask 2 Oct 9, 2017
@yashasingh
Copy link
Owner Author

@tgr sorry sir, for the fault and delay. Please review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants