Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
502 commits
Select commit Hold shift + click to select a range
7273110
complement
FracassandoCasualmente Jul 10, 2024
24933a2
complement
FracassandoCasualmente Jul 10, 2024
ebbb537
complement
FracassandoCasualmente Jul 10, 2024
ff19c93
complement
FracassandoCasualmente Jul 10, 2024
d2c73ed
complement
FracassandoCasualmente Jul 10, 2024
4e8a3c6
complement
FracassandoCasualmente Jul 10, 2024
1329c46
complement
FracassandoCasualmente Jul 10, 2024
2f5945e
complement
FracassandoCasualmente Jul 10, 2024
485a5d1
complement
FracassandoCasualmente Jul 10, 2024
6e2c8c0
complement
FracassandoCasualmente Jul 10, 2024
dc08c00
complement
FracassandoCasualmente Jul 10, 2024
59d0592
allele frequencies done
FracassandoCasualmente Jul 10, 2024
23b0561
allele frequencies done try to take less time
FracassandoCasualmente Jul 10, 2024
4189d9b
allele frequencies done try to take less time
FracassandoCasualmente Jul 10, 2024
4dfccb1
allele frequencies done try to take less time
FracassandoCasualmente Jul 10, 2024
2eeb64f
allele frequencies done
FracassandoCasualmente Jul 10, 2024
66615e1
trying to start on the logic
FracassandoCasualmente Jul 10, 2024
f045b5b
implement paper logic in handlers.py
FracassandoCasualmente Jul 11, 2024
0f0ecb9
try implementation of pvalue strategy
FracassandoCasualmente Jul 12, 2024
7a5f06a
contituation of budgeting
FracassandoCasualmente Jul 16, 2024
2273e0a
pvalue strategy done I guess
FracassandoCasualmente Jul 17, 2024
ae9ff7b
trying to test some things
FracassandoCasualmente Jul 18, 2024
bf82ff5
trying to test some things
FracassandoCasualmente Jul 18, 2024
448063c
trying to test some things
FracassandoCasualmente Jul 18, 2024
d200385
trying to test some things
FracassandoCasualmente Jul 18, 2024
33dec82
trying to test some things
FracassandoCasualmente Jul 18, 2024
58f9d33
trying to test some things
FracassandoCasualmente Jul 18, 2024
1259459
trying to test some things
FracassandoCasualmente Jul 18, 2024
a784975
solving allele frequency problems
FracassandoCasualmente Jul 18, 2024
f7c8ded
trying...
FracassandoCasualmente Jul 18, 2024
e018526
trying...
FracassandoCasualmente Jul 18, 2024
94ce074
trying...
FracassandoCasualmente Jul 18, 2024
08a9f28
trying...
FracassandoCasualmente Jul 18, 2024
15a2f7f
trying...
FracassandoCasualmente Jul 18, 2024
c15830e
trying...
FracassandoCasualmente Jul 18, 2024
211c492
trying...
FracassandoCasualmente Jul 18, 2024
09a4bae
trying again
FracassandoCasualmente Jul 18, 2024
7fd75f7
trying again
FracassandoCasualmente Jul 18, 2024
ea77f84
trying again
FracassandoCasualmente Jul 18, 2024
acec794
trying again
FracassandoCasualmente Jul 18, 2024
4b11760
trying again
FracassandoCasualmente Jul 18, 2024
4fdd327
complete of allele frequencies
FracassandoCasualmente Jul 18, 2024
8fa9243
try to test
FracassandoCasualmente Jul 18, 2024
8ff61f4
request file
FracassandoCasualmente Jul 18, 2024
a29eaac
try to test
FracassandoCasualmente Jul 18, 2024
8607e0c
fixing query params
FracassandoCasualmente Jul 18, 2024
a4a267d
minor changes
FracassandoCasualmente Jul 18, 2024
ecfd8b2
test: Script to test variant query.
sara-santana Jul 18, 2024
027c239
minor changes
sara-santana Jul 18, 2024
6817a97
fixing errors
sara-santana Jul 19, 2024
37f3d18
fixing errors
sara-santana Jul 19, 2024
f64fc83
fixing errors
sara-santana Jul 19, 2024
dfd01bb
fixing errors
sara-santana Jul 19, 2024
a180227
fixing errors
sara-santana Jul 19, 2024
4906c5f
fixing errors
sara-santana Jul 19, 2024
de912ac
fixing errors
sara-santana Jul 19, 2024
eaf69cd
fixing errors
sara-santana Jul 19, 2024
e112146
fixing errors
sara-santana Jul 19, 2024
a156896
fixing errors
sara-santana Jul 19, 2024
a7bb121
fixing errors
sara-santana Jul 19, 2024
0c1e247
fixing errors
sara-santana Jul 19, 2024
bc574d8
fixing errors
sara-santana Jul 19, 2024
b9607f7
fixing errors
sara-santana Jul 19, 2024
b8174c1
fixing errors
sara-santana Jul 19, 2024
ccf9f12
fixing errors
sara-santana Jul 19, 2024
4e2df21
fixing errors
sara-santana Jul 19, 2024
e250196
fixing errors
sara-santana Jul 19, 2024
35de858
fixing errors
sara-santana Jul 19, 2024
3274cc7
fixing errors
sara-santana Jul 19, 2024
88fc1ad
fixing errors
sara-santana Jul 19, 2024
711b164
fixing errors
sara-santana Jul 19, 2024
b503222
fixing errors
sara-santana Jul 19, 2024
6cf69c1
fixing errors
sara-santana Jul 19, 2024
5567bb3
fixing errors
sara-santana Jul 19, 2024
f91fd01
fixing errors
sara-santana Jul 19, 2024
4cadca4
fixing errors
sara-santana Jul 19, 2024
3a29e11
fixing errors
sara-santana Jul 19, 2024
22fc74d
fixing errors
sara-santana Jul 19, 2024
0bb7277
fixing errors
sara-santana Jul 19, 2024
c237201
fixing errors
sara-santana Jul 19, 2024
88c0e05
fixing errors
sara-santana Jul 19, 2024
92444b5
fixing errors
sara-santana Jul 19, 2024
4d3cb18
fixing errors
sara-santana Jul 19, 2024
674ba64
fixing errors
sara-santana Jul 19, 2024
059f039
fixing errors
sara-santana Jul 19, 2024
a1f4487
fixing errors
sara-santana Jul 19, 2024
5ff406c
fixing errors
sara-santana Jul 19, 2024
1a5b7d5
fixing errors
sara-santana Jul 19, 2024
9cbef1b
fixing errors
sara-santana Jul 19, 2024
aad6534
fixing errors
sara-santana Jul 19, 2024
f794444
fixing errors
sara-santana Jul 19, 2024
644eb17
fixing errors
sara-santana Jul 19, 2024
96b2d8e
minor errros
sara-santana Jul 20, 2024
c78f824
minor errros
sara-santana Jul 20, 2024
cedda2e
minor errros
sara-santana Jul 20, 2024
b52dfd0
trying the script of attacker
sara-santana Jul 20, 2024
afbc16e
trying the script of attacker
sara-santana Jul 20, 2024
1ffa232
trying the script of attacker
sara-santana Jul 20, 2024
205bcc4
trying the script of attacker
sara-santana Jul 20, 2024
312991f
log change
sara-santana Jul 30, 2024
6449e74
less variants individual
sara-santana Jul 30, 2024
5b7b395
fix some things
sara-santana Jul 30, 2024
7983f5e
fix some things
sara-santana Jul 30, 2024
2357a15
fix some things
sara-santana Jul 30, 2024
59d0b42
fix some things
sara-santana Jul 30, 2024
18a1371
history out of place
sara-santana Jul 31, 2024
b8cdf8e
history out of place
sara-santana Jul 31, 2024
89aa191
history out of place
sara-santana Jul 31, 2024
14add9f
minor errors
sara-santana Jul 31, 2024
0bfe492
handling removed individuals
sara-santana Aug 1, 2024
fa09dd7
handling removed individuals
sara-santana Aug 1, 2024
0ed1e8c
handling removed individuals
sara-santana Aug 1, 2024
5f309b5
handling removed individuals
sara-santana Aug 1, 2024
9b1d31e
handling removed individuals
sara-santana Aug 1, 2024
2e6a3ae
handling removed individuals
sara-santana Aug 1, 2024
78550fb
handling removed individuals
sara-santana Aug 1, 2024
e0c295a
handling removed individuals
sara-santana Aug 1, 2024
e0bde72
handling removed individuals
sara-santana Aug 1, 2024
0adf1b5
handling removed individuals
sara-santana Aug 1, 2024
5f76f4b
handling removed individuals
sara-santana Aug 1, 2024
b14ea8a
handling removed individuals
sara-santana Aug 1, 2024
38cc426
handling removed individuals
sara-santana Aug 1, 2024
1830be1
handling removed individuals
sara-santana Aug 1, 2024
919f4d3
handling removed individuals
sara-santana Aug 1, 2024
089b735
handling removed individuals
sara-santana Aug 1, 2024
4f3f466
handling removed individuals
sara-santana Aug 1, 2024
d4e029f
handling removed individuals
sara-santana Aug 1, 2024
9f752d0
handling removed individuals
sara-santana Aug 1, 2024
09d4435
done
sara-santana Aug 1, 2024
61faec6
get to query (name change)
sara-santana Aug 5, 2024
5a5dc26
removing unnecessairy logs
sara-santana Aug 5, 2024
b77c048
fixing some errors
sara-santana Aug 5, 2024
5b4d88e
fixing some errors
sara-santana Aug 5, 2024
cdd8d37
fixing some errors
sara-santana Aug 5, 2024
e3f0f60
fixing some errors
sara-santana Aug 5, 2024
74875c9
fixing some errors
sara-santana Aug 5, 2024
7ef090e
fixing some errors
sara-santana Aug 5, 2024
89327cb
fixing some errors
sara-santana Aug 5, 2024
06f5378
fixing some errors
sara-santana Aug 5, 2024
eb21417
fixing logs
sara-santana Aug 5, 2024
f02a1fd
start of researcher queries
sara-santana Aug 5, 2024
c02c8e0
start of researcher queries
sara-santana Aug 5, 2024
4310c2a
start of researcher queries
sara-santana Aug 5, 2024
332bc29
start of researcher queries
sara-santana Aug 5, 2024
caf1250
deleting budget and history collections before running scripts
sara-santana Aug 5, 2024
04cae6c
start of researcher queries
sara-santana Aug 5, 2024
7422dc5
start of researcher queries
sara-santana Aug 5, 2024
e390de8
start of researcher queries
sara-santana Aug 5, 2024
d28294c
preparing for scripts
sara-santana Aug 6, 2024
c7f2ebb
preparing for scripts
sara-santana Aug 6, 2024
60e8c2d
preparing for scripts
sara-santana Aug 6, 2024
d19165a
preparing for scripts
sara-santana Aug 6, 2024
88a207a
preparing for scripts
sara-santana Aug 6, 2024
facdfb4
preparing for scripts
sara-santana Aug 6, 2024
80e395f
preparing for scripts
sara-santana Aug 6, 2024
0fb685c
preparing for scripts
sara-santana Aug 6, 2024
eeae85c
preparing for scripts
sara-santana Aug 6, 2024
ad67ce5
preparing for scripts
sara-santana Aug 6, 2024
5c9bd2b
preparing for scripts
sara-santana Aug 6, 2024
b4a61b8
preparing for scripts
sara-santana Aug 6, 2024
b196698
preparing for scripts
sara-santana Aug 6, 2024
872b465
preparing for scripts
sara-santana Aug 6, 2024
4ccff89
preparing for scripts
sara-santana Aug 6, 2024
c9ae464
preparing for scripts
sara-santana Aug 6, 2024
49d7fe6
preparing for scripts
sara-santana Aug 6, 2024
97709b3
preparing for scripts
sara-santana Aug 6, 2024
4898e36
preparing for scripts
sara-santana Aug 6, 2024
d20f847
preparing for scripts
sara-santana Aug 6, 2024
f58d2dc
preparing for scripts
sara-santana Aug 6, 2024
357cadd
preparing for scripts
sara-santana Aug 6, 2024
95dea20
preparing for scripts
sara-santana Aug 6, 2024
6cf634a
running scripts
sara-santana Aug 6, 2024
d641b0a
running scripts
sara-santana Aug 6, 2024
85ff909
running scripts
sara-santana Aug 6, 2024
aa8a1fa
running scripts
sara-santana Aug 6, 2024
3dfb60c
trying N and P
sara-santana Aug 6, 2024
b9e848a
trying N and P
sara-santana Aug 6, 2024
d1f5ea1
trying N and P
sara-santana Aug 6, 2024
f000e47
normal run of script
sara-santana Aug 7, 2024
6a9c93d
100 different queries to check time spent
sara-santana Aug 7, 2024
66ab534
100 different queries to check time spent
sara-santana Aug 7, 2024
4ee1bb5
running to see time
sara-santana Aug 7, 2024
7927f8e
cleaner code
sara-santana Aug 7, 2024
3a6aacd
cleaner code
sara-santana Aug 7, 2024
3d5f4e5
cleaner code
sara-santana Aug 7, 2024
d882058
running 100k queries
sara-santana Aug 7, 2024
ac1ccf2
running 100k queries
sara-santana Aug 7, 2024
54b02c3
running 100k queries
sara-santana Aug 7, 2024
cfb127b
running 100k queries
sara-santana Aug 7, 2024
779636b
running 100k queries
sara-santana Aug 7, 2024
6de8794
running 100k queries
sara-santana Aug 7, 2024
c655396
running 100k queries
sara-santana Aug 7, 2024
4d488b0
why
sara-santana Aug 8, 2024
0895677
why
sara-santana Aug 8, 2024
84fb51b
trying again
sara-santana Aug 8, 2024
215e581
trying again
sara-santana Aug 8, 2024
0d6c78c
trying again
sara-santana Aug 8, 2024
cde02e0
trying again
sara-santana Aug 8, 2024
b1d6f9f
trying the 100k with P 01
sara-santana Aug 10, 2024
6522160
trying again the attacker
sara-santana Aug 13, 2024
80ac290
trying again the attacker
sara-santana Aug 13, 2024
ea6e0be
trying again the attacker
sara-santana Aug 13, 2024
cfd0725
trying again the attacker
sara-santana Aug 13, 2024
e5c7fbc
trying again the attacker
sara-santana Aug 13, 2024
355d6f7
trying again the attacker
sara-santana Aug 13, 2024
2fe1b87
trying again the attacker
sara-santana Aug 13, 2024
d2cc5b9
trying again the attacker
sara-santana Aug 13, 2024
787c3dd
trying again the attacker
sara-santana Aug 13, 2024
680f7b0
trying again the attacker
sara-santana Aug 13, 2024
9b3305e
trying again the attacker
sara-santana Aug 13, 2024
449958c
trying again the attacker
sara-santana Aug 13, 2024
68a91bd
trying again the attacker
sara-santana Aug 13, 2024
59cc67a
trying again the attacker
sara-santana Aug 13, 2024
b80b60a
trying again the attacker
sara-santana Aug 13, 2024
2a9889e
trying again the attacker
sara-santana Aug 13, 2024
559019f
trying again the attacker
sara-santana Aug 13, 2024
33f93c8
trying again the attacker
sara-santana Aug 13, 2024
60c107c
trying again the attacker
sara-santana Aug 13, 2024
3296e58
trying again the attacker
sara-santana Aug 13, 2024
9f5c35a
trying again the attacker
sara-santana Aug 13, 2024
f6424d6
trying again the attacker
sara-santana Aug 13, 2024
5932469
trying again the attacker
sara-santana Aug 13, 2024
79c2f9f
trying again the attacker
sara-santana Aug 13, 2024
e726f0d
trying again the attacker
sara-santana Aug 13, 2024
c099027
trying again the attacker
sara-santana Aug 13, 2024
b8a3ca2
trying again the attacker
sara-santana Aug 13, 2024
6b88f79
trying again the attacker
sara-santana Aug 13, 2024
a06684c
trying again the attacker
sara-santana Aug 13, 2024
767545f
trying again the attacker
sara-santana Aug 13, 2024
66999da
trying again the attacker
sara-santana Aug 13, 2024
904d71a
trying again the attacker
sara-santana Aug 13, 2024
e73c490
trying again the attacker
sara-santana Aug 13, 2024
ec3ad62
trying again the attacker
sara-santana Aug 13, 2024
cd42661
trying again the attacker
sara-santana Aug 13, 2024
310322e
continue to get the attacker values
sara-santana Aug 14, 2024
9038ed9
attacker with fixed individuals
sara-santana Aug 20, 2024
32af6b8
attacker with fixed individuals
sara-santana Aug 20, 2024
a7fb5d9
attacker tester
sara-santana Aug 21, 2024
64fd1d2
trying attacker 10000 01
sara-santana Aug 22, 2024
71e8f53
final code i guess (the store is not okay)
sara-santana Aug 26, 2024
1b186ef
final code i guess (the store is not okay)
sara-santana Aug 26, 2024
9b70781
try attacker 100000 05
sara-santana Aug 27, 2024
880b345
testing collusion
sara-santana Nov 25, 2024
738066f
collusion test
sara-santana Nov 25, 2024
ab44cd9
collusion test
sara-santana Nov 25, 2024
45437eb
collusion test
sara-santana Nov 25, 2024
359b753
collusion test
sara-santana Nov 26, 2024
a1caaba
collusion test
sara-santana Nov 27, 2024
59bc853
Privacy Strategy Final Commit By Sara
Jan 29, 2025
9b7a128
Adding Privacy Strategy to README and conversion files
Jan 29, 2025
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
200 changes: 165 additions & 35 deletions beacon/request/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
import asyncio
import copy
import logging
import math
from typing import Dict, List, Tuple

import numpy as np
from aiohttp import web
from aiohttp.web_request import Request
from bson import json_util
from beacon import conf
from beacon.db import client
from pymongo import ReturnDocument
import yaml
import math

from beacon.request import ontologies
from beacon.request.model import AlphanumericFilter, Granularity, RequestParams
Expand Down Expand Up @@ -60,62 +66,166 @@ async def wrapper(request: Request):

return wrapper

# support functions for the budget strategy

# update the budget of a specific individual for a user in the budget collection
def update_individual_budget(user_id, individual_id, amount):
try:
budget_collection = client.beacon['budget']
#LOG.debug(f"Updating budget for individual_id={individual_id} by amount={amount}")

# Find the document and update it, returning the updated document
updated_document = budget_collection.find_one_and_update(
{"individualId": individual_id, "userId": user_id},
{"$inc": {"budget": -amount}},
return_document=ReturnDocument.AFTER # Return the updated document
)

return updated_document

except Exception as e:
LOG.error(f"Error updating budget: {str(e)}")
return None

def pvalue_strategy(access_token, records, qparams):
helper = []
total_cases = 0
removed_individuals = []
removed = False

for record in records:
individual_ids = set()
individuals_to_remove = set()

# step 4: compute the risk for that query: ri = -log(1 - Di)
allele_frequency = record.get('alleleFrequency')
N = client.beacon.get_collection('individuals').count_documents({}) # total number of individuals !! if user requestes dataset, N = individuals in that dataset
Di = (1 - allele_frequency) ** (2 * N)
ri = -(math.log10(1 - Di))
LOG.debug(f"O CUSTO DESTA QUERY É ESTE = {ri}")

# fetch individualId from the biosample collection
case_level_data = record.get('caseLevelData', [])
for case in case_level_data:
individual_id = case.get('biosampleId') # biosampleId = individualId
individual_ids.add(individual_id)

for individualId in individual_ids:

search_criteria = {
"userId": access_token,
"individualId": individualId
}

# Step 2: check if query has been asked before
response_history = client.beacon['history'].find_one({"userId": access_token, "query": qparams.summary()})
if response_history is not None:
LOG.debug(f"Query was previously done by the same user")
return response_history["response"], helper, total_cases, removed, removed_individuals # Return stored answer if query was asked before by the same user

# Step 3: check if there are records with bj > ri
budget_info = client.beacon['budget'].find_one(search_criteria)
if not budget_info:
p_value = 0.5 # upper bound on test errors
bj = -(math.log10(p_value)) # initial budget
budget_info = {
"userId": access_token,
"individualId": individualId,
"budget": bj
}
client.beacon['budget'].insert_one(budget_info)

# re-fetch the budget_info to ensure we have the latest data
budget_info = client.beacon['budget'].find_one(search_criteria)

if budget_info and budget_info['budget'] < ri:

individuals_to_remove.add(individualId)
else:
if budget_info['budget'] >= ri:
# Step 7: reduce their budgets by ri
update_individual_budget(access_token, individualId, ri)
budget_info = client.beacon['budget'].find_one(search_criteria)

if individuals_to_remove:
# filter the individuals from the record
removed = True
removed_individuals = individuals_to_remove
LOG.debug(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
LOG.debug(f"Removed individuals: {list(individuals_to_remove)}") # signal to know which individuals have no more budget
LOG.debug(f"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
record['caseLevelData'] = [case for case in record['caseLevelData'] if case.get('biosampleId') not in individuals_to_remove]
if record['caseLevelData'] != []:
helper.append(record)
else:
helper.append(record)

total_cases += len(record['caseLevelData'])

return None, helper, total_cases, removed, removed_individuals



# handler with authentication & REMS
# mostly from BioData.pt
def generic_handler(db_fn, request=None):
async def wrapper(request:Request):

async def wrapper(request: Request):
LOG.info("-- Generic handler --")

# Get params
entry_id = request.match_info.get('id', None)
json_body = await request.json() if request.method == "POST" and request.has_body and request.can_read_body else {}
qparams:RequestParams = RequestParams(**json_body).from_request(request)
qparams: RequestParams = RequestParams(**json_body).from_request(request)

LOG.debug(f"Query Params = {qparams}")

LOG.debug(f"Headers = {request.headers}")

access_token_header = request.headers.get('Authorization')
access_token_cookies = request.cookies.get("Authorization")
LOG.debug(f"Access token header = {access_token_header}")
LOG.debug(f"Access token cookies = {access_token_cookies}")

# set access_token as the one we recieve in the header

registered = False
public = False

# set access_token as the one we receive in the header
# if not in header, get the one from cookies
if access_token_header:
access_token = access_token_header
else:
access_token = access_token_cookies

# get specified datasets
requested_datasets = qparams.query.request_parameters.get("datasets", None)
requested_datasets = qparams.query.request_parameters.get("datasets", None)
LOG.debug(f"requested_datasets = {requested_datasets}")

# Start async task to request datasets from permissions server
task_permissions = asyncio.create_task(get_accessible_datasets(access_token, requested_datasets))

# if no datasets were specified, use all in DB
if requested_datasets is None:

# get list of all datasets in DB
_, _, all_dataset_docs = get_datasets(None, RequestParams())
all_dataset_ids = [ doc["id"] for doc in all_dataset_docs]
all_dataset_ids = [doc["id"] for doc in all_dataset_docs]
target_datasets = all_dataset_ids
# else, query only the ones specified
else:
target_datasets = requested_datasets

# TODO query all datasets in parallel
tasks_dataset_queries = []
# { dataset_id:(count, records) }
datasets_query_results:Dict[str, Tuple[int,List[dict]]] = {}

db_fn_submodule = str(db_fn.__module__).split(".")[-1]
LOG.debug(f"db_fn submodule = {db_fn_submodule}")



# get response of permissions server
accessible_datasets: List[str] = [] # array of dataset ids
accessible_datasets, public, registered = await task_permissions

# TODO do this asynchronously
for dataset_id in target_datasets:
qparams_dataset = copy.deepcopy(qparams)
Expand All @@ -127,46 +237,66 @@ async def wrapper(request:Request):
if db_fn_submodule == "g_variants":
filter_dataset_id = {
"id": "_info.datasetId",
"value": dataset_id
"value": dataset_id
}
else:
filter_dataset_id = {
"id": "datasetId",
"id": "datasetId",
"value": dataset_id
}

qparams_dataset.query.filters.append(filter_dataset_id)
LOG.debug(f"Dataset Qparams = {qparams_dataset}")
entity_schema, count, records = db_fn(entry_id, qparams_dataset)
dataset_result = (count, list(records))
datasets_query_results[dataset_id] = (dataset_result)



######################## P-VALUE STRATEGY ########################
# apply the p-value strategy if user is authenticated but not registered and only if submodule is genomic variations
count = 1
LOG.debug(f"Is the user public? {public}")
LOG.debug(f"Is the user registered {registered}")
if not public and not registered and db_fn_submodule == "g_variants":
history, records, total_cases, removed, removed_individuals = pvalue_strategy(access_token, records, qparams)
dataset_result = (count, records, total_cases)
datasets_query_results[dataset_id] = (dataset_result)

if history is not None:
LOG.debug(f"Query was previously made by the same user")
return await json_stream(request, history)

LOG.debug(f"schema = {entity_schema}")

# get response of permissions server
accessible_datasets:List[str] = [] # array of dataset ids
accessible_datasets = await task_permissions

# get the max authorized granularity
requested_granularity = qparams.query.requested_granularity
max_granularity = Granularity(conf.max_beacon_granularity)
response_granularity = Granularity.get_lower(requested_granularity, max_granularity)

# build response

response = build_generic_response(
response, store = build_generic_response(
results_by_dataset=datasets_query_results,
accessible_datasets=accessible_datasets,
granularity=response_granularity,
qparams=qparams,
entity_schema=entity_schema
entity_schema=entity_schema,
registered=registered,
public=public
)
LOG.debug(f"Will the response be stored? {store}")

return await json_stream(request, response)


document = {
"userId": access_token,
"query": qparams.summary(),
"response": response
}

if store:
client.beacon['history'].insert_one(document)


return await json_stream(request, removed_individuals)

return wrapper




Expand Down
43 changes: 31 additions & 12 deletions beacon/response/build_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,60 @@ def build_response_summary(exists, num_total_results):
# receives results(count & records) of each queried dataset, authorized datasets, and granularity of results
def build_generic_response(
results_by_dataset:Dict[str,Tuple[int,list]], accessible_datasets:List[str], granularity:Granularity,
qparams, entity_schema):

qparams, entity_schema, registered:bool, public:bool):

# flag to check if we need to keep the query and result in database (if user is registered and results include non accessible datasets)
store = False

# iterate over all results to get:
# total count
# response by dataset

num_total_results = 0
response_list:List[Dict] = []
for dataset_id in results_by_dataset:
# if user is not authenticated, they cannot see aggregated data from datasets that are not public, so this will prevent
# the non public datasets to be appended to non authenticated users responses
if public and dataset_id not in accessible_datasets:
continue

num_dataset_results = results_by_dataset[dataset_id][0]
dataset_results = results_by_dataset[dataset_id][1]
total_cases = results_by_dataset[dataset_id][2]
num_total_results += num_dataset_results

dataset_response = {
"id": dataset_id,
"exists": num_dataset_results > 0,
"setType": "dataset",
"results": dataset_results,
"resultsCount": num_dataset_results
"resultsCount": total_cases
}

# if dataset is not authorized, erase the records part
if dataset_id not in accessible_datasets:
if dataset_id not in accessible_datasets or not registered:
dataset_response["results"] = []

response_list.append(dataset_response)

if dataset_id not in accessible_datasets and not registered and not public:
store = True

beacon_response = {
'meta': build_meta(qparams, entity_schema, granularity),
'responseSummary': build_response_summary(num_total_results > 0, num_total_results),
'beaconHandovers': conf.beacon_handovers,
'response': {
'resultSets': response_list
beacon_response = []

if not dataset_results == []:

beacon_response = {
'meta': build_meta(qparams, entity_schema, granularity),
'responseSummary': build_response_summary(num_total_results > 0, num_total_results),
'beaconHandovers': conf.beacon_handovers,
'response': {
'resultSets': response_list
}
}
}
return beacon_response


return beacon_response, store


def build_response_by_dataset(data, response_dict, num_total_results, qparams, func):
Expand Down
Loading