Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:visualDust/neetbox into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
visualDust committed Dec 31, 2023
2 parents 450da85 + 1708a62 commit 23cdcae
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 194 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ build_and_reinstall_wheel.cmd
# log files
logs/
log/
for_debug.txt
.neethistory/
.neetory
.log

# others
other/
neetfiles
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pkgname=python-neetbox
pkgver=0.4.2
pkgver=0.4.3
pkgrel=1
pkgdesc="Logging/Debugging/Tracing/Managing/Facilitating long running python projects, especially a replacement of tensorboard for deep learning projects"
arch=('any')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback } from "react";
import { ECharts } from "../../../echarts";
import { GpuInfo } from "../../../../services/types";
import { TimeDataMapper } from "../../../../utils/timeDataMapper";
import { getTimeAxisOptions, green2redHue } from "./utils";
import { getTimeAxisOptions, percent2hue } from "./utils";
import { GraphWrapper } from "./graphWrapper";
import "./gpugraph.css";

Expand Down Expand Up @@ -84,7 +84,7 @@ export const GPUGraph = ({ data }: { data: TimeDataMapper<GpuInfo> }) => {
className="gpu-temperature"
style={{
backgroundColor:
"hsl(" + green2redHue(lastValue.temperature) + ", 90%, var(--temperature-bg-brightness))",
"hsl(" + percent2hue(lastValue.temperature) + ", 90%, var(--temperature-bg-brightness))",
}}
>
{lastValue.temperature}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/dashboard/project/hardware/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export function getTimeAxisOptions(mapper: TimeDataMapper) {
};
}

export function green2redHue(value) {
return ((1 - value) * 120).toString(10);
export function percent2hue(value) {
return ((100 - value) * 1.2).toString(10);
}
2 changes: 1 addition & 1 deletion frontend/src/components/dashboard/project/runSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const RunSelect = memo((props: any) => {
<>
{isOnlineRun ? (
<Tag color="green">Online</Tag>
) : runId != items[0].runId ? (
) : runId != items?.[0].runId ? (
<Tag color="orange">History</Tag>
) : (
<Tag color="red">Offline</Tag>
Expand Down
2 changes: 1 addition & 1 deletion neetbox.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "neetbox"
version = "0.4.2"
version = "0.4.3"
projectId = "3edbc5ed-8e16-44fe-8c25-949a8cb833e6"

[logging]
Expand Down
3 changes: 1 addition & 2 deletions neetbox/_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,4 @@ def merge(cls, x: Union["EventMsg", dict], y: Union["EventMsg", dict]):
IMAGE_TABLE_NAME = "image"

NEETBOX_VERSION = version("neetbox")
HISTORY_FILE_ROOT = ".neethistory"
HISTORY_FILE_TYPE_NAME = "neetory"
NEET_FILE_FOLDER = "neetfiles"
55 changes: 24 additions & 31 deletions neetbox/logging/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import json
import os
from abc import ABC, abstractmethod
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Optional

from rich.console import Console
from rich.table import Table
from rich.text import Text

from neetbox.logging.formatting import LogStyle, colored_text, styled_text
from neetbox.logging.formatting import LogStyle
from neetbox.utils import formatting
from neetbox.utils.framing import TracebackIdentity

Expand Down Expand Up @@ -89,43 +86,39 @@ def __repr__(self) -> str:
# ================== CONSOLE LOG WRITER =====================


class DefaultDictThatReturnsKeyOnMissing(defaultdict):
def __missing__(self, key):
return key


_console_prefix_2_colored_text = DefaultDictThatReturnsKeyOnMissing(
str,
{
"ok": colored_text("[ok]", "green"),
"debug": colored_text("[debug]", "cyan"),
"info": colored_text("[info]", "white"),
"warning": colored_text("[warning]", "yellow"),
"mention": colored_text("[mention]", "yellow"),
"error": colored_text("[error]", "red"),
},
)
def prefix2RichText(prefix: str):
return {
"ok": Text("[ok]", style="green"),
"debug": Text("[debug]", style="cyan"),
"info": Text("[info]", style="white"),
"warning": Text("[warning]", style="yellow"),
"mention": Text("[mention]", style="yellow"),
"error": Text("[error]", style="red"),
}.get(prefix, Text(prefix))


class __ConsoleLogWriter(LogWriter):
_console = Console()

def write(self, raw_log: RawLog):
_msg_dict = raw_log.json
_style = raw_log.style
_prefix = _msg_dict["series"]
_prefix = _console_prefix_2_colored_text[_prefix] + " " if _prefix else _prefix
msg_dict = raw_log.json
log_style = raw_log.style
style = " ".join(
[x for x in [log_style.console_color, log_style.text_style] if x is not None]
)
prefix = msg_dict["series"]
prefix = prefix2RichText(prefix) if prefix else None
table = Table(show_header=False, box=None, expand=True)
table.add_column(justify="left")
table.add_column(justify="right")
rich_msg = str(
_prefix
+ styled_text(_msg_dict["whom"], style=_style)
+ _style.split_char_cmd * min(len(_msg_dict["whom"]), 1)
+ _msg_dict["message"]
whom_text = Text(msg_dict["whom"], style)
split_text = (
Text(log_style.split_char_cmd, style="dim " + style) if len(msg_dict["whom"]) else ""
)
time_text = Text(_msg_dict["timestamp"], style="dim")
table.add_row(rich_msg, time_text)
message_text = Text(msg_dict["message"], style="default")
time_text = Text(msg_dict["timestamp"], style="default dim")
prefix_and_time = prefix + " " + time_text if prefix else time_text
table.add_row(whom_text + split_text + message_text, prefix_and_time)
self.__class__._console.print(table)


Expand Down
32 changes: 1 addition & 31 deletions neetbox/logging/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ def get_supported_colors(cls):
def get_supported_text_style(cls):
return ["bold", "italic", "blink"]

def parse(self, pattern: str):
# todo
pass

def set_color(self, color: str):
self.fore = color
return self
Expand All @@ -62,30 +58,4 @@ def randcolor(self):
# self.back = colors[(split_index + index_offset) % len(colors)]
# self.fore = colors[(split_index - index_offset) % len(colors)]
self.console_color = colors[int(random() * len(colors))]
return self


DEFAULT_STYLE = LogStyle()


def styled_text(text, style: LogStyle):
attributes = []
if style.console_color:
attributes.append(style.console_color)
if style.text_style:
attributes.append(style.text_style)
render_stack = []
_prefix = ""
_postfix = ""
while len(attributes):
attr = attributes.pop(-1)
_prefix += f"[{attr}]"
render_stack.append(attr)
while len(render_stack):
_postfix += f"[/{render_stack.pop(-1)}]"
return f"{_prefix}{text}{_postfix}"


def colored_text(text: str, color):
text = text.replace("[", r"\[")
return f"[{color}]{text}[/{color}]"
return self
17 changes: 7 additions & 10 deletions neetbox/server/_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
# Date: 20231204

from typing import Dict

from websocket_server import WebsocketServer

from neetbox._protocol import *
from neetbox.logging import LogStyle, logger
from .db.project import ProjectDB

from .db import history

logger = logger("NEETBOX", LogStyle(skip_writers=["ws"]))
logger = logger("Bridge", LogStyle(skip_writers=["ws"]))


class Bridge:
Expand Down Expand Up @@ -41,7 +38,7 @@ class Bridge:
status: dict
cli_ws_dict: dict # { run_id : client}
web_ws_list: list # since web do not have run id, use list instead of dict
historyDB: history.DBConnection
historyDB: ProjectDB

def __new__(cls, project_id: str, **kwargs) -> None:
"""Create Bridge of project id, return the old one if already exist
Expand All @@ -58,15 +55,15 @@ def __new__(cls, project_id: str, **kwargs) -> None:
[]
) # frontend ws sids. client data should be able to be shown on multiple frontend
flag_auto_load_db = kwargs["auto_load_db"] if "auto_load_db" in kwargs else True
new_bridge.historyDB = history.get_db_of_id(project_id) if flag_auto_load_db else None
new_bridge.historyDB = ProjectDB.get_db_of_id(project_id) if flag_auto_load_db else None
cls._id2bridge[project_id] = new_bridge
logger.info(f"created new Bridge for project id '{project_id}'")
return cls._id2bridge[project_id]

def __del__(self): # on delete
logger.info(f"bridge project id {self.project_id} handling on delete...")
if 0 == len(self.get_run_ids()): # if there is no active run id
self.historyDB.finialize()
self.historyDB.delete_files()
del self.historyDB # delete history db
logger.info(f"bridge of project id {self.project_id} deleted.")

Expand All @@ -90,7 +87,7 @@ def of_id(cls, project_id: str) -> "Bridge":
return bridge

@classmethod
def from_db(cls, db: history.DBConnection) -> "Bridge":
def from_db(cls, db: ProjectDB) -> "Bridge":
project_id = db.fetch_db_project_id()
target_bridge = Bridge(project_id, auto_load_db=False)
if target_bridge.historyDB is not None:
Expand All @@ -100,7 +97,7 @@ def from_db(cls, db: history.DBConnection) -> "Bridge":

@classmethod
def load_histories(cls):
db_list = history.get_db_list()
db_list = ProjectDB.get_db_list()
logger.log(f"found {len(db_list)} history db.")
for _, history_db in db_list:
cls.from_db(history_db)
Expand Down
4 changes: 1 addition & 3 deletions neetbox/server/_daemon_server_launch_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import json
import sys

from neetbox.server._server import server_process


def run(argv):
if len(argv) <= 1:
print("_daemon_: Warning: empty daemon_config")
Expand All @@ -17,6 +14,7 @@ def run(argv):
print(args.config)
daemon_config = json.loads(args.config)
print("Daemon started with config:", daemon_config)
from ._server import server_process
server_process(daemon_config)


Expand Down
7 changes: 2 additions & 5 deletions neetbox/server/_flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import time
from threading import Thread
from typing import Union

import werkzeug
from flask import Response, abort, json, redirect, request, send_from_directory

import neetbox
from neetbox._protocol import *
from neetbox.server._bridge import Bridge

from ._bridge import Bridge
from .db import QueryCondition

werkzeug_log = logging.getLogger("werkzeug")
Expand All @@ -28,7 +25,7 @@ def get_flask_server(debug=False):
from neetbox.logging import LogStyle
from neetbox.logging.logger import Logger, LogLevel

logger = Logger("NEETBOX", LogStyle(skip_writers=["ws"]))
logger = Logger("FLASK", LogStyle(skip_writers=["ws"]))

if debug:
logger.set_log_level(LogLevel.DEBUG)
Expand Down
8 changes: 4 additions & 4 deletions neetbox/server/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
import setproctitle

from neetbox._protocol import *
from neetbox.server._bridge import Bridge
from neetbox.server._flask_server import get_flask_server
from neetbox.server._websocket_server import get_web_socket_server
from ._bridge import Bridge
from ._flask_server import get_flask_server
from ._websocket_server import get_web_socket_server


def server_process(cfg, debug=False):
setproctitle.setproctitle("NEETBOX SERVER")
from neetbox.logging import LogStyle
from neetbox.logging.logger import Logger

logger = Logger("NEETBOX", LogStyle(skip_writers=["ws"]))
logger = Logger("SERVER", LogStyle(skip_writers=["ws"]))
# load bridges
Bridge.load_histories() # load history files

Expand Down
2 changes: 1 addition & 1 deletion neetbox/server/_websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_web_socket_server(config, debug=False):
from neetbox.server._bridge import Bridge

console = Console()
logger = Logger("NEETBOX", LogStyle(skip_writers=["ws"]))
logger = Logger("WS SERVER", LogStyle(skip_writers=["ws"]))
if debug:
logger.set_log_level(LogLevel.DEBUG)

Expand Down
4 changes: 1 addition & 3 deletions neetbox/server/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
# Github: github.com/visualDust
# Date: 20231201

from . import _history as history
from ._condition import DbQueryFetchType, DbQuerySortType, QueryCondition
from .condition import DbQueryFetchType, DbQuerySortType, QueryCondition

__all__ = [
"history",
"DbQueryFetchType",
"DbQuerySortType",
"QueryCondition",
Expand Down
Empty file added neetbox/server/db/_configs.py
Empty file.
Loading

0 comments on commit 23cdcae

Please sign in to comment.