Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: traffic on network provider, util provider #58

Merged
merged 22 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ Zebar is split into 3 packages:
1. **Create a new config schema for your provider.**

1. Add a schema for the config under [`packages/client-api/src/user-config/window/providers`](https://github.com/glzr-io/zebar/tree/main/packages/client-api/src/user-config/window/providers).
2. Add the new provider type to the [`ProviderType`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/user-config/window/provider-type.model.ts) enum.
3. Add the schema to the [`ProviderConfigSchema`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/user-config/window/provider-config.model.ts) array.
2. Add the schema to the [`ProviderIndex`](https://github.com/glzr-io/zebar/tree/main/packages/client-api/src/user-config/window/providers/index.ts).
3. Add the new provider type to the [`ProviderType`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/user-config/window/provider-type.model.ts) enum.
4. Add the schema to the [`ProviderConfigSchema`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/user-config/window/provider-config.model.ts) array.

2. **Add the client-side logic for the provider.** Most providers aren't client-side heavy, and simply subscribe to some variables sent from the Tauri backend (eg. [`create-ip-provider.ts`](https://github.com/glzr-io/zebar/tree/main/packages/client-api/src/providers/ip/create-ip-provider.ts)).

1. Add a new provider under [`packages/client-api/src/providers`](https://github.com/glzr-io/zebar/tree/main/packages/client-api/src/providers).
2. Add the provider to the switch statement in [`createProvider(...)`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/providers/create-provider.ts#L28).
3. Add the provider to the [`ProviderIndex`](https://github.com/glzr-io/zebar/blob/main/packages/client-api/src/providers/index.ts).

3. **Add the backend logic for the provider.**

1. Add the logic for the provider under [`packages/desktop/src/providers`](https://github.com/glzr-io/zebar/tree/main/packages/desktop/src/providers).
2. Add the provider's config to the [`ProviderConfig`](https://github.com/glzr-io/zebar/blob/main/packages/desktop/src/providers/config.rs) enum.
3. Add the provider's variables to the [`ProviderVariables`](https://github.com/glzr-io/zebar/blob/main/packages/desktop/src/providers/variables.rs) enum.
4. Add the provider to the switch statement in [`createProvider(...)`](https://github.com/glzr-io/zebar/blob/main/packages/desktop/src/providers/manager.rs#L155).
5. Add the provider's module to the [`ProviderModules`](https://github.com/glzr-io/zebar/blob/main/packages/desktop/src/providers/mod.rs)
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ window/example:

- [battery](#Battery)
- [cpu](#CPU)
- [data](#Data)
- [date](#Date)
- [glazewm](#GlazeWM)
- [host](#Host)
Expand Down Expand Up @@ -301,6 +302,14 @@ window/example:
| `physicalCoreCount` | TODO | `number` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |
| `vendor` | TODO | `string` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |

## Data

## Functions

| Function | Description | Return type | Supported OS |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `convertBytes` | Convert a given number (bytes) into a custom string format. <br><br>**Parameters**<br><br> - `bytes`: _`number`_ Bytes to convert.<br> - `decimals`: _`number`_ The number of decimals to convert the bytes up to. <br> - `minUnit`: _`string`_ The min unit to convert the bytes to. There are 3 options: <br>&nbsp;&nbsp;1, bits ('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb')<br>&nbsp;&nbsp;2, bytes using SI standard ('B', 'KB', 'MB', ... , 'YB')<br>&nbsp;&nbsp;3, bytes using IEC standard (' B', 'KiB', 'MiB', ... , 'YiB').<br><br>**Examples:**<br><br> - `convertBytes(1024, 2, 'b')` -> `8.19 Kb`<br> - `convertBytes(1024, 2, 'B')` -> `1.02 KB`<br> - `convertBytes(1024, 2, ' B')` -> `1.00 KiB`<br> - `convertBytes(500, 1, ' B')` -> `500.0 B`<br> - `convertBytes(2000, 2, 'KiB')` -> `1.95 KiB` | `string` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |
lars-berger marked this conversation as resolved.
Show resolved Hide resolved

## Date

### Provider config
Expand Down Expand Up @@ -416,9 +425,21 @@ Monitors provider doesn't take any config options.

### Variables

| Variable | Description | Return type | Supported OS |
| ------------ | ----------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `interfaces` | TODO | `NetworkInterface[]` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |
| Variable | Description | Return type | Supported OS |
| ------------------ | -------------------------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `defaultInterface` | TODO | `NetworkInterface` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |
| `defaultGateway` | TODO | `Gateway` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |
| `interfaces` | TODO | `NetworkInterface[]` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |
| `traffic` | Returns the network traffic per second | `NetworkTraffic` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/005a0760-da9d-460e-b533-9b2aba7f5c03" alt="apple icon" width="24"><img src="https://github.com/glzr-io/zebar/assets/34844898/1c5d91b1-879f-42a6-945e-912a11daebb4" alt="linux icon" width="24"> |

### Return types

### NetworkTraffic

| Variable | Description | Return type |
| ------------- | ----------------------------- | ----------- |
| `received` | Received bytes per second. | `number` |
| `transmitted` | Transmitted bytes per second. | `number` |

### Self

Expand Down
3 changes: 3 additions & 0 deletions packages/client-api/src/providers/create-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Owner } from 'solid-js';

import { createBatteryProvider } from './battery/create-battery-provider';
import { createCpuProvider } from './cpu/create-cpu-provider';
import { createDataProvider } from './data/create-data-provider';
import { createDateProvider } from './date/create-date-provider';
import { createGlazewmProvider } from './glazewm/create-glazewm-provider';
import { createHostProvider } from './host/create-host-provider';
Expand Down Expand Up @@ -29,6 +30,8 @@ export async function createProvider(
return createBatteryProvider(config, owner);
case ProviderType.CPU:
return createCpuProvider(config, owner);
case ProviderType.DATA:
return createDataProvider(config, owner);
case ProviderType.DATE:
return createDateProvider(config, owner);
case ProviderType.GLAZEWM:
Expand Down
65 changes: 65 additions & 0 deletions packages/client-api/src/providers/data/create-data-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { type Owner } from 'solid-js';
import { createStore } from 'solid-js/store';

import type { DataProviderConfig } from '~/user-config';

export interface DataVariables { }

export async function createDataProvider(
config: DataProviderConfig,
owner: Owner,
) {
const [dataVariables, setDataVariables] =
createStore<DataVariables>(getDataVariables());

function getDataVariables() {
return {};
}

const bitUnits = ['b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
const byteCommonUnits = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const byteIECUnits = [' B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];

function convertBytes(bytes: number, decimals: number, minUnit: string) {
lars-berger marked this conversation as resolved.
Show resolved Hide resolved
let unitIndex = bitUnits.findIndex(u => u === minUnit);

if (unitIndex !== -1) {
bytes *= 8;
return convert(1000, bitUnits, bytes, decimals, unitIndex);
}

unitIndex = byteCommonUnits.findIndex(u => u === minUnit);

if (unitIndex !== -1) {
return convert(1000, byteCommonUnits, bytes, decimals, unitIndex);
}

unitIndex = byteIECUnits.findIndex(u => u === minUnit);

if (unitIndex !== -1) {
return convert(1024, byteIECUnits, bytes, decimals, unitIndex);
}

return 'NoUnit';
}

function convert(k: number, units: string[], bytes: number, decimals: number, unitIndex: number) {
const dm = decimals < 0 ? 0 : decimals;

if (!+bytes) {
return `${0.0.toFixed(dm)} ${units[unitIndex]}`;
}

let i = Math.floor(Math.log(bytes) / Math.log(k));

if (i < unitIndex) {
i = unitIndex;
}

return `${(bytes / Math.pow(k, i)).toFixed(dm)} ${units[i]?.trimStart()}`;
}

return {
convertBytes,
};
}
1 change: 1 addition & 0 deletions packages/client-api/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './battery/create-battery-provider';
export * from './cpu/create-cpu-provider';
export * from './data/create-data-provider';
export * from './date/create-date-provider';
export * from './glazewm/create-glazewm-provider';
export * from './ip/create-ip-provider';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface NetworkVariables {
defaultInterface: NetworkInterface | null;
defaultGateway: NetworkGateway | null;
interfaces: NetworkInterface[];
traffic: NetworkTraffic | null;
}

export interface NetworkInterface {
Expand Down Expand Up @@ -50,6 +51,11 @@ export enum InterfaceType {
BRIDGE = 'bridge',
}

export interface NetworkTraffic {
received: number | null;
transmitted: number | null;
}

export async function createNetworkProvider(
config: NetworkProviderConfig,
owner: Owner,
Expand All @@ -69,5 +75,8 @@ export async function createNetworkProvider(
get interfaces() {
return networkVariables().interfaces;
},
get traffic() {
return networkVariables().traffic;
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Prettify } from '~/utils';
import {
BatteryProviderConfigSchema,
CpuProviderConfigSchema,
DataProviderConfigSchema,
DateProviderConfigSchema,
GlazewmProviderConfigSchema,
HostProviderConfigSchema,
Expand All @@ -19,6 +20,7 @@ import {
export const ProviderConfigSchema = z.union([
BatteryProviderConfigSchema,
CpuProviderConfigSchema,
DataProviderConfigSchema,
DateProviderConfigSchema,
GlazewmProviderConfigSchema,
HostProviderConfigSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';
export enum ProviderType {
BATTERY = 'battery',
CPU = 'cpu',
DATA = 'data',
DATE = 'date',
GLAZEWM = 'glazewm',
HOST = 'host',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod';

import { ProviderType } from '../provider-type.model';

export const DataProviderConfigSchema = z.object({
type: z.literal(ProviderType.DATA),
});

export type DataProviderConfig = z.infer<typeof DataProviderConfigSchema>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './battery-provider-config.model';
export * from './cpu-provider-config.model';
export * from './data-provider-config.model';
export * from './date-provider-config.model';
export * from './glazewm-provider-config.model';
export * from './host-provider-config.model';
Expand Down
9 changes: 6 additions & 3 deletions packages/desktop/src/providers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use anyhow::{bail, Context};
use serde::Serialize;
use sysinfo::System;
use sysinfo::Networks;
use tauri::{App, AppHandle, Emitter, Manager, Runtime};
use tokio::{
sync::{
Expand Down Expand Up @@ -116,8 +117,9 @@ fn handle_provider_listen_input(
) -> Sender<ListenProviderArgs> {
let (listen_input_tx, mut listen_input_rx) =
mpsc::channel::<ListenProviderArgs>(1);

let sysinfo = Arc::new(Mutex::new(System::new_all()));
let netinfo = Arc::new(Mutex::new(Networks::new_with_refreshed_list()));

task::spawn(async move {
while let Some(input) = listen_input_rx.recv().await {
Expand Down Expand Up @@ -153,7 +155,7 @@ fn handle_provider_listen_input(
let emit_output_tx = emit_output_tx.clone();

// Attempt to create a new provider.
let new_provider = create_provider(input.config, sysinfo.clone());
let new_provider = create_provider(input.config, sysinfo.clone(), netinfo.clone());

if let Err(err) = new_provider {
_ = emit_output_tx
Expand Down Expand Up @@ -192,6 +194,7 @@ fn handle_provider_listen_input(
fn create_provider(
config: ProviderConfig,
sysinfo: Arc<Mutex<System>>,
netinfo: Arc<Mutex<Networks>>,
) -> anyhow::Result<Box<dyn Provider + Send>> {
let provider: Box<dyn Provider + Send> = match config {
ProviderConfig::Battery(config) => {
Expand All @@ -212,7 +215,7 @@ fn create_provider(
Box::new(MemoryProvider::new(config, sysinfo))
}
ProviderConfig::Network(config) => {
Box::new(NetworkProvider::new(config))
Box::new(NetworkProvider::new(config, netinfo))
}
ProviderConfig::Weather(config) => {
Box::new(WeatherProvider::new(config))
Expand Down
Loading
Loading