Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions csp_bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import re
import threading
import time
from csv import reader
from datetime import datetime, timedelta
from io import StringIO
from logging import getLogger
from types import MappingProxyType
from typing import Any, Dict, List, Optional, Set, Tuple, Union

Expand All @@ -29,6 +29,7 @@
BaseCommandModel,
HelpCommand,
ScheduleCommand,
StatusCommand,
)
from .gateway import GatewayChannels, GatewayModule
from .structs import (
Expand All @@ -39,7 +40,7 @@
)
from .utils import Backend

log = logging.getLogger(__name__)
log = getLogger(__name__)

SLACK_ENTITY_REGEX = re.compile("<@.+?>")
DISCORD_ENTITY_REGEX = re.compile("<@.+?>")
Expand Down Expand Up @@ -469,10 +470,14 @@ def extract_bot_commands(self, message: Message, channel: str, text: str, entiti

command_runner = self._commands[command]

# TODO generalize by interrogating the command signature
if isinstance(command_runner, ScheduleCommand):
# Special command, gets access to schedule of commands
return command_runner.preexecute(command_instance, self._scheduled, self)
return self._commands[command].preexecute(command_instance)
elif isinstance(command_runner, StatusCommand):
# Special command, gets access to status of commands
return command_runner.preexecute(command_instance, self)
return command_runner.preexecute(command_instance)
else:
log.info(f"Defaulting to help command from message to bot with no command: {message}")
command_runner = self._commands["help"]
Expand Down
4 changes: 2 additions & 2 deletions csp_bot/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger
from pprint import pprint

import csp_gateway
Expand All @@ -8,7 +8,7 @@

from csp_bot import __version__

log = logging.getLogger(__name__)
log = getLogger(__name__)

__all__ = (
"load",
Expand Down
1 change: 1 addition & 0 deletions csp_bot/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from .echo import EchoCommand, EchoCommandModel
from .help import HelpCommand, HelpCommandModel
from .schedule import ScheduleCommand, ScheduleCommandModel
from .status import StatusCommand, StatusCommandModel
4 changes: 2 additions & 2 deletions csp_bot/commands/echo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from logging import getLogger
from typing import Optional, Type

from csp_bot.structs import BotCommand, Message

from .base import BaseCommand, BaseCommandModel, ReplyToOtherCommand

log = logging.getLogger(__name__)
log = getLogger(__name__)


class EchoCommand(ReplyToOtherCommand):
Expand Down
4 changes: 2 additions & 2 deletions csp_bot/commands/help.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from html import escape, unescape
from logging import getLogger
from typing import Mapping, Type

import pandas as pd
Expand All @@ -8,7 +8,7 @@

from .base import BaseCommand, BaseCommandModel, ReplyCommand

log = logging.getLogger(__name__)
log = getLogger(__name__)


class HelpCommand(ReplyCommand):
Expand Down
4 changes: 2 additions & 2 deletions csp_bot/commands/schedule.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from html import escape
from logging import getLogger
from typing import TYPE_CHECKING, Mapping, Type

from croniter import CroniterBadCronError, croniter
Expand All @@ -12,7 +12,7 @@
if TYPE_CHECKING:
from csp_bot import Bot

log = logging.getLogger(__name__)
log = getLogger(__name__)


class ScheduleCommand(ReplyCommand):
Expand Down
69 changes: 69 additions & 0 deletions csp_bot/commands/status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from datetime import datetime
from getpass import getuser
from logging import getLogger
from socket import gethostname
from threading import active_count
from typing import TYPE_CHECKING, Optional, Type

import psutil

from csp_bot.structs import BotCommand, Message

from .base import BaseCommand, BaseCommandModel, ReplyCommand

if TYPE_CHECKING:
from csp_bot import Bot

log = getLogger(__name__)

_HOSTNAME = gethostname()
_USER = getuser()


class StatusCommand(ReplyCommand):
def command(self) -> str:
return "status"

def name(self) -> str:
return "status"

def help(self) -> str:
return "System information. Syntax: /status [/channel <channel>]"

def preexecute(self, command: BotCommand, bot_instance: "Bot") -> BotCommand:
# TODO: pull out everything except auth keys?
self._adapters = list(bot_instance._adapters.keys())
return command

def execute(self, command: BotCommand) -> Optional[Message]:
log.info(f"Status command: {command}")

message = ""

# Time information
message += f"Now\n\t{datetime.utcnow()}\n"

# Adapter information
message += f"Backends:\n\t{', '.join(self._adapters)}\n"

# Machine information
message += f"CPU\n\t{psutil.cpu_percent()}\n"
message += f"Memory\n\t{psutil.virtual_memory().percent}\n"
message += f"Memory Available\n\t{round(psutil.virtual_memory().available * 100 / psutil.virtual_memory().total, 2)}\n"
message += f"Host\n\t{_HOSTNAME}\n"
message += f"User\n\t{_USER}\n"

# Process and thread information
current_process = psutil.Process()
message += f"PID\n\t{current_process.pid}\n"
message += f"Active Threads\n\t{active_count()}\n"

return Message(
msg=message,
channel=command.channel,
backend=command.backend,
)


class StatusCommandModel(BaseCommandModel):
command: Type[BaseCommand] = StatusCommand
4 changes: 2 additions & 2 deletions csp_bot/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logging
from logging import getLogger
from pathlib import Path
from typing import List, Optional

from ccflow import RootModelRegistry, load_config as load_config_base

from csp_bot import __version__

log = logging.getLogger(__name__)
log = getLogger(__name__)

__all__ = ("load_config",)

Expand Down
3 changes: 3 additions & 0 deletions csp_bot/config/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ echo:

schedule:
_target_: csp_bot.commands.ScheduleCommandModel

status:
_target_: csp_bot.commands.StatusCommandModel
1 change: 1 addition & 0 deletions csp_bot/config/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gateway:
- /commands/help
- /commands/echo
- /commands/schedule
- /commands/status
settings:
PORT: ${port}
AUTHENTICATE: ${authenticate}
Expand Down
4 changes: 2 additions & 2 deletions csp_bot/gateway/gateway.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from functools import wraps
from logging import getLogger
from typing import List

from csp import ts
Expand All @@ -16,7 +16,7 @@
from csp_bot.commands import BaseCommandModel
from csp_bot.structs import BotCommand, Message

log = logging.getLogger(__name__)
log = getLogger(__name__)


class GatewayChannels(GatewayChannelsBase):
Expand Down