Commit 9a50d2ec authored by Alexander's avatar Alexander

cleanup and updated swagger docu

parent 1d90011e
......@@ -78,57 +78,57 @@ paths:
400:
description: "Invalid input"
/clusters:
/location-clusters:
get:
operationId: "rest.cluster.get"
operationId: "rest.cluster.get_locations"
tags:
- "Clusters"
summary: "Get user communities per date per hour"
summary: "Get user communities clustered by location"
parameters: []
responses:
200:
description: "Successful operation"
schema:
$ref: "#/definitions/UserClusterCollection"
$ref: "#/definitions/LocationClusterCollection"
/clusters/cluster.png:
get:
operationId: "rest.cluster.get_image"
tags:
- "Clusters"
summary: "Get user communities per date per hour as image"
parameters: []
produces:
- "image/png"
responses:
200:
description: "Successful operation"
# /clusters/cluster.png:
# get:
# operationId: "rest.cluster.get_image"
# tags:
# - "Clusters"
# summary: "Get user communities per date per hour as image"
# parameters: []
# produces:
# - "image/png"
# responses:
# 200:
# description: "Successful operation"
/agi/clusters:
/time-clusters:
get:
operationId: "rest.agi_cluster.get"
operationId: "rest.cluster.get_times"
tags:
- "Clusters"
summary: "Get user communities per date per hour from agi data"
summary: "Get user communities clustered by time per hour"
parameters: []
responses:
200:
description: "Successful operation"
schema:
$ref: "#/definitions/UserClusterCollection"
$ref: "#/definitions/TimeClusterCollection"
/agi/clusters/cluster.png:
get:
operationId: "rest.agi_cluster.get_image"
tags:
- "Clusters"
summary: "Get user communities per date per hour from agi data as image"
parameters: []
produces:
- "image/png"
responses:
200:
description: "Successful operation"
# /agi/clusters/cluster.png:
# get:
# operationId: "rest.agi_cluster.get_image"
# tags:
# - "Clusters"
# summary: "Get user communities per date per hour from agi data as image"
# parameters: []
# produces:
# - "image/png"
# responses:
# 200:
# description: "Successful operation"
definitions:
Location:
......@@ -152,8 +152,27 @@ definitions:
items:
$ref: "#/definitions/Location"
UserCluster:
type: "object"
LocationCluster:
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:
id:
type: string
......@@ -161,16 +180,16 @@ definitions:
type: string
hour:
type: number
cluster_label:
type: number
clusters:
type: object
additionalProperties:
type: array
items:
type: string
example:
0: [1dc61b1a0602de0eaee9dba7eece9279c2844202, b4b31bbe5e12f55737e3a910827c81595fbca3eb]
UserClusterCollection:
type: array
items:
$ref: "#/definitions/Location"
# example:
# 0: [1dc61b1a0602de0eaee9dba7eece9279c2844202, b4b31bbe5e12f55737e3a910827c81595fbca3eb]
TimeClusterCollection:
type: array
items:
$ref: "#/definitions/UserCluster"
\ No newline at end of file
$ref: "#/definitions/TimeCluster"
\ 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
class Repository(MongoRepositoryBase):
'''This repository stores and loads locations and clusters with MongoDb.'''
def __init__(self, agi_data=False):
super().__init__(netconst.COMMUNITY_DETECTION_DB_HOSTNAME,
netconst.COMMUNITY_DETECTION_DB_PORT, 'communityDetectionDb')
def __init__(self):
super().__init__(netconst.COMMUNITY_DETECTION_DB_HOSTNAME,
netconst.COMMUNITY_DETECTION_DB_PORT,
'communityDetectionDb')
self._location_collection = 'location_agi' if agi_data else 'location'
self._location_cluster_collection = 'location_cluster_agi' if agi_data else 'location_cluster'
self._time_cluster_collection = 'time_cluster_agi' if agi_data else 'time_cluster'
self._location_collection = 'location'
self._location_cluster_collection = 'location_cluster'
self._time_cluster_collection = 'time_cluster'
self.agi_repo = AgiRepository()
......@@ -33,16 +35,17 @@ class Repository(MongoRepositoryBase):
return [Location(agi_loc) for agi_loc in agi_locations]
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]:
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):
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]:
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
repo = Repository()
clusterer = Clusterer()
def get():
def get_locations():
clusters = repo.get_location_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)
# todo
......
......@@ -6,17 +6,17 @@ repo = Repository()
def post():
body = request.json
insert_location(body)
_insert_location(body)
return Response(status=201)
def post_many():
body = request.json
for location in body:
insert_location(location)
_insert_location(location)
return Response(status=201)
def get():
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))
......@@ -14,13 +14,6 @@ DEBUG = False
repo = Repository()
# locs = repo.get_agi_locations()
# for l in locs:
# repo.add_location(l)
# exit()
def run_location_clustering():
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