81 lines
2.7 KiB
Python
81 lines
2.7 KiB
Python
from flask import Blueprint, current_app, render_template, send_file
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
|
|
import crudkit
|
|
|
|
from crudkit.ui.fragments import render_table
|
|
|
|
from ..models.inventory import Inventory
|
|
from ..models.work_log import WorkLog
|
|
|
|
bp_index = Blueprint("index", __name__)
|
|
|
|
def init_index_routes(app):
|
|
@bp_index.get("/")
|
|
def index():
|
|
inventory_service = crudkit.crud.get_service(Inventory)
|
|
work_log_service = crudkit.crud.get_service(WorkLog)
|
|
work_logs = work_log_service.list({
|
|
"complete__ne": 1,
|
|
"fields": [
|
|
"start_time",
|
|
"contact.label",
|
|
"work_item.label"
|
|
],
|
|
"sort": "start_time"
|
|
})
|
|
inventory_report_rows = inventory_service.list({
|
|
"fields": ["condition", "device_type.description"],
|
|
"limit": 0
|
|
})
|
|
rows = [item.as_dict() for item in inventory_report_rows]
|
|
chart_data = {}
|
|
if rows:
|
|
df = pd.DataFrame(rows)
|
|
|
|
xtab = pd.crosstab(df["condition"], df["device_type.description"]).astype(int)
|
|
|
|
top_labels = (
|
|
xtab.sum(axis=0)
|
|
.sort_values(ascending=False)
|
|
.index.tolist()
|
|
)
|
|
xtab = xtab[top_labels]
|
|
|
|
preferred_order = [
|
|
"Deployed", "Working", "Unverified",
|
|
"Partially Inoperable", "Inoperable",
|
|
"Removed", "Disposed"
|
|
]
|
|
conditions = [c for c in preferred_order if c in xtab.index] + [c for c in xtab.index if c not in preferred_order]
|
|
xtab = xtab.loc[conditions]
|
|
|
|
chart_data = {
|
|
"labels": top_labels,
|
|
"datasets": [{
|
|
"label": cond,
|
|
"data": xtab.loc[cond].to_list()
|
|
}
|
|
for cond in xtab.index]
|
|
}
|
|
|
|
columns = [
|
|
{"field": "start_time", "label": "Start", "format": "date"},
|
|
{"field": "contact.label", "label": "Contact",
|
|
"link": {"endpoint": "entry.entry", "params": {"id": "{contact.id}", "model": "user"}}},
|
|
{"field": "work_item.label", "label": "Work Item",
|
|
"link": {"endpoint": "entry.entry", "params": {"id": "{work_item.id}", "model": "inventory"}}}
|
|
]
|
|
|
|
logs = render_table(work_logs, columns=columns, opts={"object_class": "worklog"})
|
|
|
|
return render_template("index.html", logs=logs, chart_data=chart_data)
|
|
|
|
@bp_index.get("/LICENSE")
|
|
def license():
|
|
path = Path(current_app.root_path).parent / "LICENSE"
|
|
return send_file(path, mimetype="text/plain; charset=utf-8")
|
|
|
|
app.register_blueprint(bp_index)
|