Commit 3feae01a authored by Manuel's avatar Manuel

[reputation_calculation] added rest endpoint for trace submitting

parent 06ee0e8e
paths:
#####
# Contexts
#####
/use-cases/{use_case}/contexts:
get:
operationId: "lib.routes.context.all_for_use_case"
tags:
- "Context"
summary: "Get all contexts for a use case"
description: "Get all contexts for a use case"
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Contexts belongs to"
required: true
type: "string"
responses:
"200":
description: "OK"
schema:
type: array
$ref: '#/definitions/ContextArray'
"404":
description: "Use-Case was not found"
post:
operationId: "lib.routes.context.add"
tags:
- "Context"
summary: "Adds a new context to the system"
description: "Adds a new context to the system"
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Contexts belongs to"
required: true
type: "string"
- in: body
name: "Object"
required: true
schema:
$ref: '#/definitions/Context'
responses:
"200":
description: "Context was successfully added"
#####
# Trust Adapters
#####
/use-cases/{use_case}/contexts/{context}/trust-adapters:
......@@ -23,6 +67,9 @@ paths:
responses:
"200":
description: "OK"
schema:
type: array
$ref: '#/definitions/TrustAdapterArray'
"404":
description: "Use-Case or Context was not found"
post:
......@@ -53,7 +100,7 @@ paths:
"404":
description: "Use-Case or Context was not found"
#####
# Contexts
# Users
#####
/use-cases/{use_case}/users:
get:
......@@ -71,6 +118,9 @@ paths:
responses:
"200":
description: "OK"
schema:
type: array
$ref: '#/definitions/UserArray'
"404":
description: "Use-Case was not found"
"400":
......@@ -91,51 +141,62 @@ paths:
name: "Object"
required: true
schema:
$ref: '#/definitions/User'
$ref: '#/definitions/UserBlank'
responses:
"200":
description: "User was successfully added"
#####
# Contexts
# TRACES
#####
/use-cases/{use_case}/contexts:
get:
operationId: "lib.routes.context.all_for_use_case"
/use-cases/{use_case}/table/{table}/traces:
# get:
# operationId: "lib.routes.user.all_for_use_case"
# tags:
# - "User"
# summary: "Get all users for a use case"
# description: "Get all users for a use case"
# parameters:
# - name: "use_case"
# in: "path"
# description: "Name of the Use-Case the User belongs to"
# required: true
# type: "string"
# responses:
# "200":
# description: "OK"
# schema:
# type: array
# $ref: '#/definitions/UserArray'
# "404":
# description: "Use-Case was not found"
# "400":
# description: "Wrong or missing parameters"
post:
operationId: "lib.routes.traces.add"
tags:
- "Context"
summary: "Get all contexts for a use case"
description: "Get all contexts for a use case"
- "Trace"
summary: "Adds a new user to the system"
description: "Adds a new user to the system"
parameters:
- name: "use_case"
in: "path"
description: "Name of the Use-Case the Contexts belongs to"
description: "Name of the Use-Case the Trace belongs to"
required: true
type: "string"
responses:
"200":
description: "OK"
"404":
description: "Use-Case was not found"
post:
operationId: "lib.routes.context.add"
tags:
- "Context"
summary: "Adds a new context to the system"
description: "Adds a new context to the system"
parameters:
- name: "use_case"
- name: "table"
in: "path"
description: "Name of the Use-Case the Contexts belongs to"
description: "Name of the Table the Trace belongs to"
required: true
type: "string"
- in: body
name: "Object"
required: true
schema:
$ref: '#/definitions/Context'
$ref: '#/definitions/Trace'
responses:
"200":
description: "Context was successfully added"
description: "User was successfully added"
# DEBUG
/debug:
post:
operationId: "debug.echo"
......@@ -152,19 +213,14 @@ paths:
responses:
"200":
description: "Successful echo of request data"
#####
# DEFINITIONS
#####
definitions:
User:
type: "object"
required:
- use_case
- name
properties:
use_case:
type: string
example: "car-sharing"
name:
type: string
example: "Alice"
ContextArray:
type: "array"
items:
$ref: "#/definitions/Context"
Context:
type: "object"
required:
......@@ -177,6 +233,10 @@ definitions:
name:
type: string
example: "context1"
TrustAdapterArray:
type: "array"
items:
$ref: "#/definitions/TrustAdapter"
TrustAdapter:
type: "object"
required:
......@@ -185,6 +245,9 @@ definitions:
- table
- user_source
- volume_source
- rating_source
- rating_lower
- rating_upper
- conversion
- certainty
- cutoff
......@@ -202,10 +265,21 @@ definitions:
example: "travel"
user_source:
type: string
example: "user//id"
example: "user"
volume_source:
type: string
example: "trip//price"
example: "price"
rating_source:
type: string
example: "rating"
rating_lower:
type: number
format: integer
example: 1
rating_upper:
type: number
format: integer
example: 5
conversion:
type: number
format: float
......@@ -228,14 +302,74 @@ definitions:
type: number
format: float
example: 1
UserArray:
type: "array"
items:
$ref: "#/definitions/User"
User:
type: "object"
required:
- use_case
- name
- context_trust
properties:
use_case:
type: string
example: "car-sharing"
name:
type: string
example: "Alice"
context_trust:
type: array
items:
$ref: "#/definitions/UserTrust"
UserBlank:
type: "object"
required:
- use_case
- name
properties:
use_case:
type: string
example: "car-sharing"
name:
type: string
example: "Alice"
UserTrust:
type: "object"
required:
- use_case
- context
- user
- t
- last_update
properties:
use_case:
type: string
example: "car-sharing"
context:
type: string
example: "context1"
user:
type: string
example: "Alice"
t:
type: number
format: float
example: 0.0051
last_update:
type: string
example: "2021-07-05T17:53:20.517500"
TrustTrace:
type: "object"
required:
- use_case
- context
- user
- table
- volume
- trust
- trustworthy
- timestamp
properties:
use_case:
type: string
......@@ -245,18 +379,35 @@ definitions:
example: "context1"
user:
type: string
example: "bob"
example: "Alice"
table:
type: string
example: "travel"
volume:
type: number
format: float
example: 12.1524
trust:
trustworthy:
type: number
format: float
minimum: -1
maximum: 1
timestamp:
type: string
example: "2021-07-05T17:53:20.517500"
Trace:
type: "object"
required:
- use_case
- table
- properties
properties:
use_case:
type: string
example: "car-sharing"
table:
type: string
example: "travel"
properties:
type: object
\ No newline at end of file
......@@ -10,6 +10,7 @@ class TrustTrace(Transaction):
self.use_case = use_case
self.context = context
self.user = user
self.table = table
def to_serializable_dict(self):
return {
......@@ -17,8 +18,8 @@ class TrustTrace(Transaction):
"trustworthy": self.trustworthy,
"timestamp": self.timestamp.isoformat(),
"use_case": self.use_case,
"context": self.user,
"user": self.volume,
"context": self.context,
"user": self.user,
"table": self.table,
}
......
......@@ -44,8 +44,8 @@ class UserRepository(MongoRepositoryBase):
return result
def one_by_use_case_and_username(self, use_case: str, username: str) -> User:
result = super().get_entries(self._collection, projection={
'_id': False}, selection={"use_case": use_case, "name": username})
result = list(super().get_entries(self._collection, projection={
'_id': False}, selection={"use_case": use_case, "name": username}))
if len(result) != 1:
return None
......
from database.entities.context import Context
from database.repositories.context_repository import ContextRepository
from lib.database.entities.context import Context
from lib.database.repositories.context_repository import ContextRepository
from flask import request, Response
context_repository: ContextRepository = ContextRepository()
......
from lib.services.trust_service import TrustService
from typing import Dict
from flask import request, Response
from lib.database.repositories.trust_adapter_repository import TrustAdapterRepository
from lib.database.repositories.user_repository import UserRepository
from lib.database.repositories.user_trust_repository import UserTrustRepository
from lib.database.repositories.trust_trace_repository import TrustTraceRepository
trust_service: TrustService = TrustService(
TrustAdapterRepository(),
UserRepository(),
UserTrustRepository(),
TrustTraceRepository()
)
def add(use_case: str, table: str):
data: Dict = request.json
trust_service.updateSystem(use_case, table, data["properties"])
\ No newline at end of file
from database.repositories.context_repository import ContextRepository
from database.entities.context import Context
from database.entities.trust_adapter import TrustAdapter
from database.repositories.context_repository import ContextRepository
from database.repositories.trust_adapter_repository import TrustAdapterRepository
from lib.database.repositories.context_repository import ContextRepository
from lib.database.entities.context import Context
from lib.database.entities.trust_adapter import TrustAdapter
from lib.database.repositories.context_repository import ContextRepository
from lib.database.repositories.trust_adapter_repository import TrustAdapterRepository
from flask import request, Response
from typing import List, Dict
from typing import List
context_repository: ContextRepository = ContextRepository()
trust_adapter_repository: TrustAdapterRepository = TrustAdapterRepository()
......
from database.entities.user import User
from database.repositories.user_repository import UserRepository
from lib.database.entities.user import User
from lib.database.repositories.user_repository import UserRepository
from flask import request, Response
user_repository: UserRepository = UserRepository()
......
......@@ -19,8 +19,8 @@ class TrustService:
self._trust_trace_repository = trust_trace_repository
def updateSystem(self, use_case: str, table: str, properties: Dict):
adapters: List[TrustAdapter] = self._adapter_repository.all_for_use_case_and_table(
use_case, table)
adapters: List[TrustAdapter] = [TrustAdapter.from_serializable_dict(row) for row in self._adapter_repository.all_for_use_case_and_table(
use_case, table)]
if len(adapters) == 0:
LOGGER.warning(
......@@ -42,7 +42,7 @@ class TrustService:
if user == None:
user = User(use_case, username)
self._user_repository.add(user)
trace: TrustTrace = TrustTrace(
use_case,
adapter.context,
......
from lib.services.trust_service import TrustService
from lib.database.entities.trust_trace import TrustTrace
from lib.database.entities.user_trust import UserTrust
from lib.trust.oracle import TrustOracle
from lib.trust.transaction import Transaction
from lib.database.entities.trust_adapter import TrustAdapter
from lib.database.entities.user import User
from typing import Dict, List
from typing import Dict
import json
import logging
from datetime import datetime
LOGGER = logging.getLogger(__name__)
......@@ -52,61 +45,4 @@ class MessageHandler:
self.trust_trace_repository
)
trust_service.updateSystem(use_case, table, properties)
# adapters: List[TrustAdapter] = self.trust_adapter_repository.all_for_use_case_and_table(
# use_case, table)
# if len(adapters) == 0:
# LOGGER.warning(
# f"No Trust-Adapters found for Trace(use-case:{use_case}, table:{table}), aborting...")
# return
# for adapter in adapters:
# if adapter.user_source not in properties.keys() or adapter.volume_source not in properties.keys() or adapter.rating_source not in properties.keys():
# raise ValueError(
# f"Trace does not contain needed fields for trust-computation. Required fields: {adapter.user_source}, {adapter.volume_source}. Present fields: {properties.keys()}")
# username: str = properties[adapter.user_source]
# volume: float = properties[adapter.volume_source]
# rating: float = properties[adapter.rating_source]
# trace: TrustTrace = TrustTrace(
# use_case,
# adapter.context,
# user.name,
# adapter.table,
# volume,
# datetime.now(),
# is_trustworthy=(((rating - adapter.rating_lower) /
# (adapter.rating_upper - adapter.rating_lower)) * 2) - 1
# )
# # retrieve user (create one if it doesnt exist)
# user: User = self.user_repository.one_by_use_case_and_username(
# use_case, username)
# if user == None:
# user = User(use_case, username)
# self.user_repository.add(user)
# # retrieve user_trust for the trace (create one if it doesnt exist)
# target_trust: UserTrust = None
# for user_trust in user.context_trust:
# if user_trust.context == adapter.context:
# target_trust = user_trust
# break
# if target_trust == None:
# target_trust = UserTrust(
# use_case, adapter.context, user.name, 0, datetime(1, 1, 1, 0, 0))
# self.user_trust_repository.add(target_trust)
# user.context_trust.append(target_trust)
# oracle: TrustOracle = TrustOracle(
# adapter.conversion, adapter.certainty)
# delta_t = oracle.calculate_trust_single(trace)
# target_trust.t += delta_t
# target_trust.last_update = trace.timestamp
# self.trust_trace_repository.add(trace)
# self.user_trust_repository.update(target_trust)
trust_service.updateSystem(use_case, table, properties)
\ No newline at end of file
......@@ -34,10 +34,10 @@ class DummyTrustAdapterRepo:
return self._adapters
def all_for_use_case_and_context(self, use_case: str, context: str) -> List[Dict]:
return [adapter for adapter in self._adapters if (adapter.use_case == use_case and adapter.context == context)]
return [adapter.to_serializable_dict() for adapter in self._adapters if (adapter.use_case == use_case and adapter.context == context)]
def all_for_use_case_and_table(self, use_case: str, table: str) -> List[Dict]:
return [adapter for adapter in self._adapters if (adapter.use_case == use_case and adapter.table == table)]
return [adapter.to_serializable_dict() for adapter in self._adapters if (adapter.use_case == use_case and adapter.table == table)]
def one_for_use_case_and_context_and_table(self, use_case: str, context: str, table: str) -> Dict:
result = [adapter for adapter in self._adapters if (
......@@ -46,7 +46,7 @@ class DummyTrustAdapterRepo:
if len(result) != 1:
return None
return result[0]
return result[0].to_serializable_dict()
class DummyUserRepo:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment