-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from 5genesis/analytics
Analytics
- Loading branch information
Showing
13 changed files
with
119 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import jwt | ||
from typing import List, Tuple | ||
|
||
|
||
class Crypt: | ||
def __init__(self, secret: str): | ||
self.secret = secret | ||
|
||
def Encode(self, target: int, executions: List[int]) -> str: | ||
"""'target' is the landing experiment execution, 'executions' is | ||
the list of all executions belonging to the user""" | ||
payload = {"t": target, "l": executions} | ||
token = jwt.encode(payload, self.secret, algorithm="HS256") | ||
if isinstance(token, bytes): # Older versions of jwt return bytes | ||
token = token.decode(encoding="UTF-8") | ||
return token | ||
|
||
def Decode(self, token: str) -> Tuple[int, List[int]]: | ||
"""Returns a tuple (<landing execution>, <list of executions>)""" | ||
payload = jwt.decode(token, self.secret, algorithms=["HS256"]) | ||
return payload["t"], payload["l"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from Helper import Crypt, Config | ||
from app.models import User, Experiment | ||
from typing import List | ||
|
||
|
||
class AnalyticsApi: | ||
crypt = None | ||
url = None | ||
|
||
def __init__(self): | ||
if AnalyticsApi.crypt is None: | ||
config = Config().Analytics | ||
if config.Enabled: | ||
AnalyticsApi.url = config.Url | ||
AnalyticsApi.crypt = Crypt(config.Secret) if config.Secret is not None else None | ||
|
||
def GetToken(self, target: int, user: User) -> str: | ||
if self.crypt is not None: | ||
experiments: List[Experiment] = list(user.Experiments) | ||
executions = [] | ||
for experiment in experiments: | ||
executions.extend(experiment.experimentExecutions()) | ||
|
||
# TODO: Consider filtering the list with `execution.status == "Finished"` | ||
executions = sorted([execution.id for execution in executions]) | ||
return self.crypt.Encode(target, executions) | ||
else: | ||
return "<Analytics disabled or no Secret>" | ||
|
||
def GetUrl(self, target: int, user: User) -> str: | ||
return f"{self.url}/endpoint?token={self.GetToken(target, user)}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters