deploy_feedback icon indicating copy to clipboard operation
deploy_feedback copied to clipboard

Feature request: A way for the backend to know how many read/write units have been used that day.

Open JoakimCh opened this issue 2 years ago • 3 comments

This would be a nice way to be able to have better control over spendings.

E.g. for my backend I would want to shut down certain features when I get over the free limit. Or limit the users allowed actions based on how many free units I have left.

I would love to be able to have this control!

JoakimCh avatar Sep 16 '23 22:09 JoakimCh

do you have any solution for this yet?

ooker777 avatar May 29 '24 10:05 ooker777

Nope.

JoakimCh avatar May 29 '24 17:05 JoakimCh

I come up with this solution to track the used units via signals:

// signals.ts
import { signal } from "@preact/signals";

export const kvSignal = signal<Deno.Kv>(await Deno.openKv());
export const readUnitSignal = signal<number>(0);
export const writeUnitSignal = signal<number>(0);

Replace the native kv.get and kv.set operations with these:

// kvUtils.ts
import sizeof from "npm:object-sizeof";

export function increaseReadUnit(data: any) {
  const kb = sizeof(data) / 1000;
  readUnitSignal.value += Math.ceil(kb / 4);
}
export function increaseWriteUnit(data: any) {
  const kb = sizeof(data) / 1000;
  writeUnitSignal.value += Math.ceil(kb);
}

export async function kvGet(key: Deno.KvKey) {
  const kv = kvSignal.value;
  const result = await kv.get(key);
  increaseReadUnit(result);
  return result;
}

export async function kvSet(key: Deno.KvKey, value: any) {
  const kv = kvSignal.value;
  await kv.set(key, value);
  increaseWriteUnit(value);
}

It doesn't match the result shown up in the analytic chart on the dashboard unfortunately. Here is the detail of that issue. Nevertheless I hope this gives some food for thought.

ooker777 avatar May 30 '24 04:05 ooker777