2020-06-22 07:00:45 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-non-null-assertion: "off",
|
|
|
|
@typescript-eslint/no-explicit-any: "off",
|
|
|
|
@typescript-eslint/ban-ts-ignore: "off" */
|
|
|
|
|
|
|
|
import pb from "../backend/proto";
|
|
|
|
|
2020-06-22 11:11:50 +02:00
|
|
|
async function fetchData(search: string, days: number): Promise<Uint8Array> {
|
2020-06-22 07:00:45 +02:00
|
|
|
const resp = await fetch("/_anki/graphData", {
|
|
|
|
method: "POST",
|
2020-06-22 11:11:50 +02:00
|
|
|
body: JSON.stringify({
|
|
|
|
search,
|
|
|
|
days,
|
|
|
|
}),
|
2020-06-22 07:00:45 +02:00
|
|
|
});
|
|
|
|
if (!resp.ok) {
|
|
|
|
throw Error(`unexpected reply: ${resp.statusText}`);
|
|
|
|
}
|
|
|
|
// get returned bytes
|
|
|
|
const respBlob = await resp.blob();
|
|
|
|
const respBuf = await new Response(respBlob).arrayBuffer();
|
|
|
|
const bytes = new Uint8Array(respBuf);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2020-06-22 11:11:50 +02:00
|
|
|
export async function getGraphData(
|
|
|
|
search: string,
|
|
|
|
days: number
|
|
|
|
): Promise<pb.BackendProto.GraphsOut> {
|
|
|
|
const bytes = await fetchData(search, days);
|
|
|
|
return pb.BackendProto.GraphsOut.decode(bytes);
|
|
|
|
}
|
2020-06-22 07:00:45 +02:00
|
|
|
|
2020-06-27 07:35:34 +02:00
|
|
|
export enum RevlogRange {
|
2020-06-22 11:11:50 +02:00
|
|
|
Month = 1,
|
|
|
|
Year = 2,
|
|
|
|
All = 3,
|
2020-06-22 07:00:45 +02:00
|
|
|
}
|
2020-06-23 05:43:23 +02:00
|
|
|
|
|
|
|
export interface GraphBounds {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
marginLeft: number;
|
|
|
|
marginRight: number;
|
|
|
|
marginTop: number;
|
|
|
|
marginBottom: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function defaultGraphBounds(): GraphBounds {
|
|
|
|
return {
|
|
|
|
width: 600,
|
|
|
|
height: 250,
|
2020-06-28 09:44:51 +02:00
|
|
|
marginLeft: 70,
|
|
|
|
marginRight: 30,
|
2020-06-23 05:43:23 +02:00
|
|
|
marginTop: 20,
|
|
|
|
marginBottom: 40,
|
|
|
|
};
|
|
|
|
}
|