Skip to content

Commit

Permalink
feat: add support foor db pools
Browse files Browse the repository at this point in the history
  • Loading branch information
christyjacob4 committed Nov 17, 2022
1 parent de73c02 commit 2f6ba45
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
65 changes: 65 additions & 0 deletions src/lib/stores/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Client, Models } from "@aw-labs/appwrite-console";

type Payload = {
[key: string]: any;
}

class Service {
static CHUNK_SIZE = 5*1024*1024; // 5MB

client: Client;

constructor(client: Client) {
this.client = client;
}

static flatten(data: Payload, prefix = ''): Payload {
let output: Payload = {};

for (const key in data) {
let value = data[key];
let finalKey = prefix ? `${prefix}[${key}]` : key;

if (Array.isArray(value)) {
output = Object.assign(output, this.flatten(value, finalKey));
}
else {
output[finalKey] = value;
}
}

return output;
}
}

export class Project extends Service {
constructor(client: Client)
{
super(client);
}

/**
* Get usage stats for a project
*
*
* @param {string} range
* @throws {AppwriteException}
* @returns {Promise}
*/
async getUsage(range?: string): Promise<Models.UsageProject> {
let path = '/project/usage';
let payload: Payload = {};

console.log("Getting usage")
console.log(range)

if (typeof range !== 'undefined') {
payload['range'] = range;
}

const uri = new URL(this.client.config.endpoint + path);
return await this.client.call('get', uri, {
'content-type': 'application/json',
}, payload);
}
}
3 changes: 3 additions & 0 deletions src/lib/stores/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
Users
} from '@aw-labs/appwrite-console';

import { Project } from './project';

const endpoint =
import.meta.env.VITE_APPWRITE_ENDPOINT?.toString() ?? `${window?.location?.origin}/v1`;
const clientConsole = new Client();
Expand Down Expand Up @@ -42,6 +44,7 @@ const sdkForProject = {
functions: new Functions(clientProject),
health: new Health(clientProject),
locale: new Locale(clientProject),
project: new Project(clientProject),
projects: new Projects(clientProject),
storage: new Storage(clientProject),
teams: new Teams(clientProject),
Expand Down
4 changes: 2 additions & 2 deletions src/routes/console/project-[project]/overview/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
afterNavigate(handle);
async function handle() {
const promise = usage.load(projectId, period);
const promise = usage.load(period);
if ($usage) {
await promise;
Expand All @@ -51,7 +51,7 @@
function changePeriod(newPeriod: UsagePeriods) {
period = newPeriod;
usage.load(projectId, period);
usage.load(period);
}
</script>

Expand Down
9 changes: 5 additions & 4 deletions src/routes/console/project-[project]/overview/store.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { sdkForConsole } from '$lib/stores/sdk';
import { sdkForProject } from '$lib/stores/sdk';
import { cachedStore } from '$lib/helpers/cache';
import type { Models } from '@aw-labs/appwrite-console';

export const usage = cachedStore<
Models.UsageProject,
{
load: (projectId: string, range: string) => Promise<void>;
load: (range: string) => Promise<void>;
}
>('projectUsage', function ({ set }) {
return {
load: async (projectId, range) => {
const usages = await sdkForConsole.projects.getUsage(projectId, range);
load: async (range) => {
const usages = await sdkForProject.project.getUsage(range);
console.log(usages)
set(usages);
}
};
Expand Down

0 comments on commit 2f6ba45

Please sign in to comment.