Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

top whorl values #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ def get_whorl_value_count(self, variable, value, epoched):
finally:
c.close()

def get_top_whorl_value_counts(self, whorl_name, limit, epoched):
c = self.cxn.cursor()
try:
c.execute("SELECT value, " + self.epoch_prefix(epoched) +
"total FROM totals WHERE variable=%s ORDER BY " +
self.epoch_prefix(epoched) + "total DESC LIMIT %s",
(whorl_name, limit))
return c.fetchall()
finally:
c.close()

def get_total_count(self, epoched):
c = self.cxn.cursor()
try:
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ secrets:
services:

db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
image: mysql:lts
command: --mysql-native-password=ON
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/mysql-root-password
MYSQL_PASSWORD_FILE: /run/secrets/mysql-password
Expand Down
22 changes: 22 additions & 0 deletions entropy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ def single_whorl_uniqueness(whorl_name, whorl_value):

return uniqueness

@staticmethod
def get_top_whorl_values(whorl_name):
db = Db()
db.connect()

try:
try:
top_whorl_value_counts = db.get_top_whorl_value_counts(whorl_name, 25, config.epoched)
except TypeError:
return {'status': "Error: no values have been recorded for '" + whorl_name + "'"}

if len(top_whorl_value_counts) == 0:
return {'status': "Error: no values have been recorded recently for '" + whorl_name + "'"}

total = db.get_total_count(config.epoched)
finally:
db.close()

top_whorl_values = [[item[0], item[1] / total] for item in top_whorl_value_counts]

return top_whorl_values

@staticmethod
def _fetch_counts(whorls, whorl_names):
db = Db()
Expand Down
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ def api_v1_ua_uniqueness():
whorl = json.loads(request.data)
return jsonify(EntropyHelper.single_whorl_uniqueness(whorl['name'], whorl['value']))

# API endpoint for getting the top 25 values for a given whorl and the frequency of each value
@app.route("/api/v1/top-whorl-values", methods=['POST'])
def api_v1_top_whorl_values():
whorl = json.loads(request.data)
return jsonify(EntropyHelper.get_top_whorl_values(whorl['name']))

@app.route("/privacy")
def privacy():
Expand Down