db.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import pandas as pd
  2. from flask import request, Flask, json
  3. from sqlalchemy import create_engine
  4. app = Flask(__name__)
  5. engine = create_engine("mysql+mysqldb://python:sqlalchemy@GC-SERVER1/tasks")
  6. @app.route("/tickets", methods=["POST", "GET"])
  7. def tickets():
  8. data = request.args
  9. if not data.get("id") is None:
  10. if data.get("id") == "":
  11. query = (
  12. f"INSERT INTO tickets (datum, benutzer, kunde, kontakt, beschreibung, termin, dauer, status, prioritaet, "
  13. f"fortschritt, kategorie, programm, kommentar) "
  14. f"VALUES ('{data['datum']}', '{data['benutzer']}', '{data['kunde']}', '{data['kontakt']}', '{data['beschreibung']}', "
  15. f"'{data['termin']}', '{data['dauer']}', "
  16. f"'{data['status']}', '{data['prioritaet']}', '{data['fortschritt']}', "
  17. f"'{data['kategorie']}', '{data['programm']}', '{data['kommentar']}')"
  18. )
  19. else:
  20. query = (
  21. f"UPDATE tickets SET datum = '{data['datum']}', benutzer = '{data['benutzer']}', kunde = '{data['kunde']}', "
  22. f"kontakt = '{data['kontakt']}', beschreibung = '{data['beschreibung']}', termin = '{data['termin']}', "
  23. f"dauer = '{data['dauer']}', status = '{data['status']}', "
  24. f"prioritaet = '{data['prioritaet']}', fortschritt = '{data['fortschritt']}', "
  25. f"kategorie = '{data['kategorie']}', programm = '{data['programm']}', "
  26. f"kommentar = '{data['kommentar']}' WHERE id = '{data['id']}' "
  27. )
  28. with engine.connect() as con:
  29. con.execute(query)
  30. df = pd.read_sql("SELECT * FROM tickets", engine)
  31. return df.to_json()
  32. @app.route("/benutzer", methods=["POST", "GET"])
  33. def benutzer():
  34. df = pd.read_sql("SELECT * FROM benutzer", engine)
  35. return df.to_json()
  36. @app.route("/kunden", methods=["POST", "GET"])
  37. def kunden():
  38. data = request.args
  39. kunde = request.args.items()
  40. if not data.get("id") is None:
  41. for kunde in data:
  42. if kunde.get("kunde") != "":
  43. query = (
  44. f"UPDATE kunden SET system = '{kunde['system']}', start_soll = '{kunde['start_soll']}', "
  45. f"ende_soll = '{kunde['ende_soll']}', "
  46. f"erster_status = '{kunde['erster_status']}', aktiv = '{kunde['aktiv']}', woche = '{kunde['woche']}', "
  47. f"bundesland = '{kunde['bundesland']}', plz = '{kunde['plz']}', ort = '{kunde['ort']}' "
  48. f"WHERE kunde = '{kunde['kunde']}' "
  49. )
  50. with engine.connect() as con:
  51. con.execute(query)
  52. df = pd.read_sql("SELECT * FROM kunden", engine)
  53. return df.to_json()
  54. @app.route("/fehlerbericht", methods=["POST", "GET"])
  55. def fehlerbericht():
  56. data = request.args
  57. kunde = request.args.get("kunde")
  58. datum = request.args.get("datum")
  59. start = request.args.get("start")
  60. if data.get("id") is None:
  61. query = (
  62. f"SELECT sm.*, k.whitelist FROM status_meldung sm INNER JOIN kunden k USING (kunde) "
  63. f"WHERE sm.datum = '{datum}' AND sm.kunde = '{kunde}' AND sm.start = '{start}' LIMIT 1"
  64. )
  65. df = pd.read_sql(query, engine)
  66. df["fehlerbericht"] = df["fehlerbericht"].apply(json.loads)
  67. # df["whitelist"] = df["whitelist"].apply(json.loads)
  68. if df.get("kommentar_id") != "":
  69. query = f"SELECT sk.* FROM status_kommentar sk WHERE sk.id = '{df['kommentar_id']}' "
  70. else:
  71. query = (
  72. f"SELECT sk.* FROM status_kommentar sk "
  73. f"WHERE sk.datum = '{datum}' AND sk.kunde = '{kunde}' AND sk.start = '{start}' "
  74. "ORDER BY cdate DESC LIMIT 1"
  75. )
  76. with engine.connect() as con:
  77. con.execute(query)
  78. response = app.response_class(
  79. response=df.to_json(indent=2), status=200, mimetype="application/json"
  80. )
  81. return response
  82. @app.route("/whitelist", methods=["POST", "GET"])
  83. def whitelist():
  84. kunde = request.args.get("kunde")
  85. pd.read_sql(f"SELECT * FROM kunden WHERE kunde = '{kunde}' ", engine)
  86. @app.route("/zeit", methods=["POST", "GET"])
  87. def zeit():
  88. start_soll = request.args.get("start_soll")
  89. ende_soll = request.args.get("ende_soll")
  90. kunde = request.args.get("kunde")
  91. with engine.connect() as con:
  92. con.execute(
  93. f"UPDATE kunden SET start_soll = '{start_soll}', ende_soll = '{ende_soll}' WHERE kunde = '{kunde}'"
  94. )
  95. return 1
  96. @app.route("/kommentar", methods=["POST", "GET"])
  97. def kommentar():
  98. pass
  99. @app.route("/config", methods=["POST", "GET"])
  100. def config():
  101. df = pd.read_sql(
  102. "SELECT kunde, datum FROM kunden_config ORDER BY 1, 2 DESC", engine
  103. )
  104. return df.to_json
  105. @app.route("/liste", methods=["POST", "GET"])
  106. def liste():
  107. datum_req = request.args.get("datum")
  108. if not datum_req or datum_req == "":
  109. datum = "a.datum > date_add(now(), INTERVAL -32 DAY)"
  110. else:
  111. datum = f"a.datum >= '{datum_req}' "
  112. datum_bis_req = request.args.get("datum_bis")
  113. if not datum_bis_req or datum_bis_req == "":
  114. datum_bis = "a.datum > date_add(now(), INTERVAL -32 DAY)"
  115. else:
  116. datum_bis = f"a.datum >= '{datum_bis_req}' "
  117. kunde_req = request.args.get("kunde")
  118. if not kunde_req or kunde_req == "":
  119. kunde = "1"
  120. else:
  121. kunde = f"k.kunde LIKE '%{kunde_req}%'"
  122. query = (
  123. f"SELECT DISTINCT a.datum, k.kunde, sm.aufgabe, k.system, a.bundesland, a.feiertag, k.start_soll, k.ende_soll, "
  124. f"sm.start, sm.ende, sm.anzahl, if(sk2.id IS NULL, sm.bearbeitet, '1') as bearbeitet,"
  125. f" case"
  126. f" when woche = 'Mo-Fr' then mofr"
  127. f" when woche = 'Mo-Sa' then mosa"
  128. f" when woche = 'Mo-So' then moso"
  129. f" when woche = 'Di-So' then diso"
  130. f" else 1"
  131. f" end * if(sk.status <= 1, 0, 1) * if(sm.anzahl > 0 or (sm.anzahl is null and now() > date_add(concat(a.datum, ' ', k.ende_soll), "
  132. f"INTERVAL 1 HOUR)), 1, 0) as fehler,"
  133. f" if(sm.anzahl is null and now() > date_add(concat(a.datum, ' ', k.ende_soll), INTERVAL 1 HOUR), 1, 0) as fehlend,"
  134. f" ifnull(sk.benutzer, '') as benutzer"
  135. f" FROM arbeitstage a"
  136. f" INNER JOIN kunden k USING (bundesland)"
  137. f" LEFT JOIN status_meldung sm USING (datum, kunde)"
  138. f" LEFT JOIN status_kommentar sk ON sm.kommentar_id = sk.id"
  139. f" LEFT JOIN status_kommentar as sk2 ON a.datum = sk2.datum AND k.kunde = sk2.kunde"
  140. f" WHERE {datum} AND {datum_bis} AND {kunde} AND a.datum <= now() AND k.erster_status <= a.datum"
  141. f" AND k.aktiv = 1"
  142. f" ORDER BY a.datum, k.ende_soll"
  143. )
  144. df = pd.read_sql(query, engine)
  145. return df.to_json()
  146. @app.route("/events", methods=["POST", "GET"])
  147. def events():
  148. pass
  149. if __name__ == "__main__":
  150. app()