diff --git a/api/app.py b/api/app.py index de2807b..e95d251 100755 --- a/api/app.py +++ b/api/app.py @@ -10,6 +10,7 @@ import uvicorn import redis from subprocess import Popen, PIPE +from fastapi.params import Form from starlette.responses import Response app = FastAPI() @@ -21,15 +22,79 @@ def read_root(): return {"Hello": "World"} -HOUR = 3600 -DAY = HOUR * 24 -FLAMEGRAPH_TTL = DAY * 7 +TTL = 60 * 60 -@app.get("/flame_graph/{key}") -def flame_graph_get(key: str): - data = rdb.get("toolbox:FlameGraph:" + key) - return Response(content=data, media_type="image/svg+xml") +@app.get("/data/{key}") +def flame_graph_get(key: str, type: str): + data = rdb.get("toolbox:data:" + key) + return Response(content=data, media_type=type) + + +class SpectrographRequest: + x: int + y: int + z: int + label: str + window: str + + def __init__(self, x, y, z, label, window, type): + self.x = x + self.y = y + self.z = z + self.label = label + self.window = window + self.type = type + + def valid(self): + if self.x < 100 or self.x > 200000: + return False + + if self.y < 100 or self.y > 10000: + return False + + if self.z < 20 or self.z > 180: + return False + + if self.window not in ("Hann", "Hamming", "Bartlett", "Rectangular", "Kaiser", "Dolph"): + return False + return True + + +@app.post("/spectrograph") +def spectrograph(file: bytes = File(...), x: int = Form(...), y: int = Form(...), z: int = Form(...), label=Form(...), + window=Form(...), type=Form(...)): + key = str(uuid.uuid4()) + + req = SpectrographRequest(x, y, z, label, window, type) + if not req.valid(): + return { + "err": "Invalid request" + } + + p = Popen( + ["sox", + "-t", type, + "-", + "-n", "remix", "1", "spectrogram", + "-t", label, + "-x", str(x), "-y", str(y), "-z", str(z), + "-w", window, + "-o", "-"], + cwd="./FlameGraph", + stdin=PIPE, stdout=PIPE, stderr=PIPE, + ) + + p.stdin.write(file) + p.stdin.close() + + out = p.stdout.read() + rdb.set("toolbox:data:" + key, out, ex=TTL) + + return { + "key": key + } + @app.post("/flame_graph") def flame_graph(file: bytes = File(...), width: int = 1200): @@ -64,7 +129,7 @@ def flame_graph(file: bytes = File(...), width: int = 1200): p3.stdin.close() out = p3.stdout.read() - rdb.set("toolbox:FlameGraph:" + key, out, ex=FLAMEGRAPH_TTL) + rdb.set("toolbox:data:" + key, out, ex=TTL) return { "key": key, diff --git a/toolbox-web/src/assets/Spectrograph.png b/toolbox-web/src/assets/Spectrograph.png new file mode 100644 index 0000000..88a3675 Binary files /dev/null and b/toolbox-web/src/assets/Spectrograph.png differ diff --git a/toolbox-web/src/components/FlameGraph.vue b/toolbox-web/src/components/FlameGraph.vue index 5cfa94b..6d03702 100644 --- a/toolbox-web/src/components/FlameGraph.vue +++ b/toolbox-web/src/components/FlameGraph.vue @@ -30,7 +30,7 @@ export default class FlameGraph extends Vue { public apiUrl = API_URL getGraphUrl() { - return `${API_URL}/flame_graph/${this.key}` + return `${API_URL}/data/${this.key}?type=image/svg%2Bxml` } onUpload() { diff --git a/toolbox-web/src/components/Home.vue b/toolbox-web/src/components/Home.vue index d19130c..e7bd66e 100644 --- a/toolbox-web/src/components/Home.vue +++ b/toolbox-web/src/components/Home.vue @@ -1,13 +1,17 @@ diff --git a/toolbox-web/src/components/Spectrograph.vue b/toolbox-web/src/components/Spectrograph.vue new file mode 100644 index 0000000..1ac381f --- /dev/null +++ b/toolbox-web/src/components/Spectrograph.vue @@ -0,0 +1,90 @@ + + + + + diff --git a/toolbox-web/src/router/index.ts b/toolbox-web/src/router/index.ts index 0dce81b..bb65871 100644 --- a/toolbox-web/src/router/index.ts +++ b/toolbox-web/src/router/index.ts @@ -2,12 +2,14 @@ import Vue from 'vue' import VueRouter, {RouteConfig} from 'vue-router' import Home from '../components/Home.vue' import FlameGraph from '@/components/FlameGraph.vue' +import Spectrograph from '@/components/Spectrograph.vue'; Vue.use(VueRouter) const routes: Array = [ {path: '/', component: Home}, - {path: '/tool/FlameGraph', component: FlameGraph} + {path: '/tool/FlameGraph', component: FlameGraph}, + {path: '/tool/Spectrograph', component: Spectrograph} ] const router = new VueRouter({ diff --git a/toolbox-web/src/tools.ts b/toolbox-web/src/tools.ts index a4218c2..47f5606 100644 --- a/toolbox-web/src/tools.ts +++ b/toolbox-web/src/tools.ts @@ -1,7 +1,8 @@ import {Tool} from "@/models"; export const tools: Tool[] = [ - {name: "FlameGraph", icon: "mdi-bug"} + {name: "FlameGraph", icon: "mdi-bug"}, + {name: "Spectrograph", icon: "mdi-music"} ] export const toolByName = (name: string) => { diff --git a/toolbox-web/tsconfig.json b/toolbox-web/tsconfig.json index a489b8f..d345dff 100644 --- a/toolbox-web/tsconfig.json +++ b/toolbox-web/tsconfig.json @@ -9,6 +9,7 @@ "experimentalDecorators": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, + "strictNullChecks": false, "sourceMap": true, "baseUrl": ".", "types": [