import pandas as pd
import py2neo
graph = py2neo.Graph(password="password")

"""
Explanation: Analyse der Webanwendung  "PetClinic"
Priorisierung von Umbauarbeiten nach Nutzungsgrad
Technische Vorbereitung: Laden der Analysewerkzeuge
End of explanation
"""


query = """
MATCH 
  (t:Type)-[:BELONGS_TO]->(s:Subdomain),
  (t)-[:HAS_CHANGE]->(ch:Change),
  (t)-[:HAS_MEASURE]->(co:Coverage)
OPTIONAL MATCH
  (t)-[:HAS_BUG]->(b:BugInstance)
RETURN 
  s.name as ASubdomain,
  COUNT(DISTINCT t) as Types,
  COUNT(DISTINCT ch) as Changes,
  AVG(co.ratio) as Coverage,
  COUNT(DISTINCT b) as Bugs,
  SUM(DISTINCT t.lastMethodLineNumber) as Lines
ORDER BY Coverage ASC, Bugs DESC
""" 

"""
Explanation: Aggregation der Messwerte nach Subdomänen
End of explanation
"""


result = pd.DataFrame(graph.data(query))
result

"""
Explanation: Ergebnisse nach Subdomänen
End of explanation
"""


plot_data = result.copy()
plot_data = plot_data.rename(
    columns= {
        "Changes" : "Investment",
        "Coverage" : "Utilization",
        "Lines" : "Size"})
plot_data

%matplotlib inline
import matplotlib.pyplot as plt

def plot_portfolio_diagramm(plot_data, name):
    x = "Investment"
    y = "Utilization"
    
    ax = plot_data.plot.scatter(
        x,
        y,
        s=plot_data.Size * 10,
        alpha=0.7,
        title="Return on Investment ({})".format(name),
        figsize=[10,7],
        fontsize=14
    )

    ax.title.set_size(24)
    ax.title
    plt.xlabel(x, fontsize=18)
    plt.ylabel(y, fontsize=18)
    
    ax.plot(
        [plot_data[x].max()/2, plot_data[x].max()/2],
        [0, plot_data[y].max()], color='k', linestyle='--', linewidth=0.6)
    ax.plot(
        [0, plot_data[x].max()],
        [plot_data[y].max()/2,plot_data[y].max()/2], color='k', linestyle='--', linewidth=0.6)
    ax.text(plot_data[x].max()*1/4, plot_data[y].max()*3/4, "Success", ha="center", fontsize=24)
    ax.text(plot_data[x].max()*3/4, plot_data[y].max()*3/4, "Beware", ha="center", fontsize=24)
    ax.text(plot_data[x].max()*1/4, plot_data[y].max()*1/4, "Watch", ha="center", fontsize=24)
    ax.text(plot_data[x].max()*3/4, plot_data[y].max()*1/4, "Failure", ha="center", fontsize=24)

"""
Explanation: Umbenennung nach geläufigen Begriffen
End of explanation
"""


plot_portfolio_diagramm(plot_data, "Subdomains")

"""
Explanation: Vier-Felder-Matrix zur Priorisierung nach Subdomänen
End of explanation
"""


query = """
MATCH 
  (t:Type)-[:IS_A]->(ta:TechnicalAspect),
  (t)-[:HAS_CHANGE]->(ch:Change),
  (t)-[:HAS_MEASURE]->(co:Coverage)
OPTIONAL MATCH
  (t)-[:HAS_BUG]->(b:BugInstance)   
RETURN 
  ta.name as ATechnicalAspect,
  COUNT(DISTINCT t) as Types,
  COUNT(DISTINCT ch) as Investment,
  AVG(co.ratio) as Utilization,
  COUNT(DISTINCT b) as Bugs,
  SUM(DISTINCT t.lastMethodLineNumber) as Size
ORDER BY Utilization ASC, Bugs DESC
"""

"""
Explanation: Aggregation der Messwerte nach technischen Aspekten
End of explanation
"""


result = pd.DataFrame(graph.data(query))
result

"""
Explanation: Ergebnisse nach technischen Aspekten
End of explanation
"""


plot_portfolio_diagramm(result, "Technical Aspects")

"""
Explanation: Vier-Felder-Matrix zur Priorisierung nach technischen Aspekten
End of explanation
"""


