Commit 9a50d2ec authored by Alexander's avatar Alexander

cleanup and updated swagger docu

parent 1d90011e
...@@ -78,57 +78,57 @@ paths: ...@@ -78,57 +78,57 @@ paths:
400: 400:
description: "Invalid input" description: "Invalid input"
/clusters: /location-clusters:
get: get:
operationId: "rest.cluster.get" operationId: "rest.cluster.get_locations"
tags: tags:
- "Clusters" - "Clusters"
summary: "Get user communities per date per hour" summary: "Get user communities clustered by location"
parameters: [] parameters: []
responses: responses:
200: 200:
description: "Successful operation" description: "Successful operation"
schema: schema:
$ref: "#/definitions/UserClusterCollection" $ref: "#/definitions/LocationClusterCollection"
/clusters/cluster.png: # /clusters/cluster.png:
get: # get:
operationId: "rest.cluster.get_image" # operationId: "rest.cluster.get_image"
tags: # tags:
- "Clusters" # - "Clusters"
summary: "Get user communities per date per hour as image" # summary: "Get user communities per date per hour as image"
parameters: [] # parameters: []
produces: # produces:
- "image/png" # - "image/png"
responses: # responses:
200: # 200:
description: "Successful operation" # description: "Successful operation"
/agi/clusters: /time-clusters:
get: get:
operationId: "rest.agi_cluster.get" operationId: "rest.cluster.get_times"
tags: tags:
- "Clusters" - "Clusters"
summary: "Get user communities per date per hour from agi data" summary: "Get user communities clustered by time per hour"
parameters: [] parameters: []
responses: responses:
200: 200:
description: "Successful operation" description: "Successful operation"
schema: schema:
$ref: "#/definitions/UserClusterCollection" $ref: "#/definitions/TimeClusterCollection"
/agi/clusters/cluster.png: # /agi/clusters/cluster.png:
get: # get:
operationId: "rest.agi_cluster.get_image" # operationId: "rest.agi_cluster.get_image"
tags: # tags:
- "Clusters" # - "Clusters"
summary: "Get user communities per date per hour from agi data as image" # summary: "Get user communities per date per hour from agi data as image"
parameters: [] # parameters: []
produces: # produces:
- "image/png" # - "image/png"
responses: # responses:
200: # 200:
description: "Successful operation" # description: "Successful operation"
definitions: definitions:
Location: Location:
...@@ -152,8 +152,27 @@ definitions: ...@@ -152,8 +152,27 @@ definitions:
items: items:
$ref: "#/definitions/Location" $ref: "#/definitions/Location"
UserCluster: LocationCluster:
type: "object" type: object
properties:
id:
type: string
cluster_label:
type: number
clusters:
type: array
items:
$ref: "#/definitions/Location"
# example:
# 0: [1dc61b1a0602de0eaee9dba7eece9279c2844202, b4b31bbe5e12f55737e3a910827c81595fbca3eb]
LocationClusterCollection:
type: array
items:
$ref: "#/definitions/LocationCluster"
TimeCluster:
type: object
properties: properties:
id: id:
type: string type: string
...@@ -161,16 +180,16 @@ definitions: ...@@ -161,16 +180,16 @@ definitions:
type: string type: string
hour: hour:
type: number type: number
cluster_label:
type: number
clusters: clusters:
type: object
additionalProperties:
type: array type: array
items: items:
type: string $ref: "#/definitions/Location"
example: # example:
0: [1dc61b1a0602de0eaee9dba7eece9279c2844202, b4b31bbe5e12f55737e3a910827c81595fbca3eb] # 0: [1dc61b1a0602de0eaee9dba7eece9279c2844202, b4b31bbe5e12f55737e3a910827c81595fbca3eb]
UserClusterCollection: TimeClusterCollection:
type: array type: array
items: items:
$ref: "#/definitions/UserCluster" $ref: "#/definitions/TimeCluster"
\ No newline at end of file \ No newline at end of file
from __future__ import annotations
class LocationDatastore:
'''This Singelton simulates a location database'''
_instance = None
@staticmethod
def get_instance() -> LocationDatastore:
if LocationDatastore._instance == None:
LocationDatastore._instance = LocationDatastore()
return LocationDatastore._instance
def __init__(self):
if LocationDatastore._instance != None:
raise Exception("This class is a singleton!")
self.locations = []
def add(self, location):
self.locations.append(location)
def get(self):
return self.locations
\ No newline at end of file
...@@ -10,14 +10,16 @@ from typing import List ...@@ -10,14 +10,16 @@ from typing import List
class Repository(MongoRepositoryBase): class Repository(MongoRepositoryBase):
'''This repository stores and loads locations and clusters with MongoDb.'''
def __init__(self, agi_data=False): def __init__(self):
super().__init__(netconst.COMMUNITY_DETECTION_DB_HOSTNAME, super().__init__(netconst.COMMUNITY_DETECTION_DB_HOSTNAME,
netconst.COMMUNITY_DETECTION_DB_PORT, 'communityDetectionDb') netconst.COMMUNITY_DETECTION_DB_PORT,
'communityDetectionDb')
self._location_collection = 'location_agi' if agi_data else 'location' self._location_collection = 'location'
self._location_cluster_collection = 'location_cluster_agi' if agi_data else 'location_cluster' self._location_cluster_collection = 'location_cluster'
self._time_cluster_collection = 'time_cluster_agi' if agi_data else 'time_cluster' self._time_cluster_collection = 'time_cluster'
self.agi_repo = AgiRepository() self.agi_repo = AgiRepository()
...@@ -33,16 +35,17 @@ class Repository(MongoRepositoryBase): ...@@ -33,16 +35,17 @@ class Repository(MongoRepositoryBase):
return [Location(agi_loc) for agi_loc in agi_locations] return [Location(agi_loc) for agi_loc in agi_locations]
def add_location_cluster(self, cluster: LocationCluster): def add_location_cluster(self, cluster: LocationCluster):
super().insert_entry(self._location_cluster_collection, cluster.to_serializable_dict(for_db=True)) super().insert_entry(self._location_cluster_collection,
cluster.to_serializable_dict(for_db=True))
def get_location_clusters(self) -> List[LocationCluster]: def get_location_clusters(self) -> List[LocationCluster]:
clusters = super().get_entries(self._location_cluster_collection) clusters = super().get_entries(self._location_cluster_collection)
return [LocationCluster(c['cluster_label'], json.loads(c['clusters'])) for c in clusters] return [LocationCluster(location_dict=c, from_db=True) for c in clusters]
def add_time_cluster(self, cluster: TimeCluster): def add_time_cluster(self, cluster: TimeCluster):
super().insert_entry(self._time_cluster_collection, cluster.to_serializable_dict(for_db=True)) super().insert_entry(self._time_cluster_collection,
cluster.to_serializable_dict(for_db=True))
def get_time_clusters(self) -> List[TimeCluster]: def get_time_clusters(self) -> List[TimeCluster]:
clusters = super().get_entries(self._time_cluster_collection) clusters = super().get_entries(self._time_cluster_collection)
return [TimeCluster(c['cluster_label'], json.loads(c['clusters'])) for c in clusters] return [TimeCluster(time_dict=c, from_db=True) for c in clusters]
import sys
import os
modules_path = '../../../modules/'
if os.path.exists(modules_path):
sys.path.insert(1, modules_path)
from db.repository import Repository
if __name__ == "__main__":
repo = Repository()
locs = repo.get_agi_locations()
for l in locs:
repo.add_location(l)
import io
from flask import request, Response
from db.repository import Repository
from processing.clusterer import Clusterer
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
repo = Repository(agi_data=True)
clusterer = Clusterer()
def get():
clusters = repo.get_location_clusters()
return [c.to_serializable_dict() for c in clusters]
def get_image():
return Response(status=501)
# todo
locations = repo.getLocations()
fig = clusterer.draw_locations(locations)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
\ No newline at end of file
...@@ -7,11 +7,28 @@ from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas ...@@ -7,11 +7,28 @@ from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
repo = Repository() repo = Repository()
clusterer = Clusterer() clusterer = Clusterer()
def get(): def get_locations():
clusters = repo.get_location_clusters() clusters = repo.get_location_clusters()
return [c.to_serializable_dict() for c in clusters] return [c.to_serializable_dict() for c in clusters]
def get_image(): def get_times():
clusters = repo.get_time_clusters()
return [c.to_serializable_dict() for c in clusters]
def get_image_1():
return Response(status=501)
# todo
locations = repo.getLocations()
fig = clusterer.draw_locations(locations)
output = io.BytesIO()
FigureCanvas(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def get_image_2():
return Response(status=501) return Response(status=501)
# todo # todo
......
...@@ -6,17 +6,17 @@ repo = Repository() ...@@ -6,17 +6,17 @@ repo = Repository()
def post(): def post():
body = request.json body = request.json
insert_location(body) _insert_location(body)
return Response(status=201) return Response(status=201)
def post_many(): def post_many():
body = request.json body = request.json
for location in body: for location in body:
insert_location(location) _insert_location(location)
return Response(status=201) return Response(status=201)
def get(): def get():
return [l.to_serializable_dict() for l in repo.get_locations()] return [l.to_serializable_dict() for l in repo.get_locations()]
def insert_location(location_data: dict): def _insert_location(location_data: dict):
repo.add_location(Location(location_data)) repo.add_location(Location(location_data))
...@@ -14,13 +14,6 @@ DEBUG = False ...@@ -14,13 +14,6 @@ DEBUG = False
repo = Repository() repo = Repository()
# locs = repo.get_agi_locations()
# for l in locs:
# repo.add_location(l)
# exit()
def run_location_clustering(): def run_location_clustering():
user_clusterer = Clusterer() user_clusterer = Clusterer()
......
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