oncall-engine/engine/common/insight_log/chatops_insight_logs.py
Joey Orlando e9969f4bd0
feat: convert organization.general_log_channel_id to organization.default_slack_channel (#5191)
# What this PR does

Related to https://github.com/grafana/oncall-private/issues/2947

Right now `general_log_channel_id` is just a string value representing
the Slack Channel ID (ex. `C043HQ70QMB`). This PR migrates this instead
to be a foreign key relationship on the `slack_slackchannel` table and
updates all references to `general_log_channel_id`.

Tested migrations locally:
```bash
Operations to perform:
  Apply all migrations: [redacted secret grafana-admin-creds:admin-user], alerts, auth, auth_token, base, contenttypes, email, exotel, fcm_django, google, heartbeat, labels, mobile_app, oss_installation, phone_notifications, schedules, sessions, slack, social_django, telegram, twilioapp, user_management, webhooks, zvonok
Running migrations:
  Applying user_management.0024_organization_general_log_slack_channel... OK
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Starting migration to populate general_log_slack_channel field.
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Total organizations to process: 1
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Organization 1 updated with SlackChannel 2 (slack_id: C043LL6RTS7).
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Finished migration. Total organizations processed: 1. Organizations updated: 1. Missing SlackChannels: 0.
  Applying user_management.0025_auto_20241017_1919... OK
```

## Future incoming PRs

- Drop `Organization.general_log_channel_id` column
- Migrate `ChannelFilter.slack_channel_id` and
`ResolutionNoteSlackMessage.slack_channel_id` to use foreign key
relationships

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-11-01 06:41:38 +01:00

46 lines
1.6 KiB
Python

import enum
import json
import logging
import typing
from .insight_logs_enabled_check import is_insight_logs_enabled
if typing.TYPE_CHECKING:
from apps.user_management.models import User
insight_logger = logging.getLogger("insight_logger")
logger = logging.getLogger(__name__)
class ChatOpsEvent(enum.Enum):
WORKSPACE_CONNECTED = "workspace_connected"
WORKSPACE_DISCONNECTED = "workspace_disconnected"
CHANNEL_CONNECTED = "channel_connected"
CHANNEL_DISCONNECTED = "channel_disconnected"
USER_LINKED = "user_linked"
USER_UNLINKED = "used_unlinked"
DEFAULT_CHANNEL_CHANGED = "default_channel_changed"
class ChatOpsTypePlug(enum.Enum):
# ChatOpsTypePlug provides backend_id string for chatops integration not supporting messaging_backends.
SLACK = "slack"
TELEGRAM = "telegram"
def write_chatops_insight_log(author: "User", event_name: ChatOpsEvent, chatops_type: str, **kwargs):
try:
organization = author.organization
if is_insight_logs_enabled(organization):
tenant_id = organization.stack_id
user_id = author.public_primary_key
username = json.dumps(author.username)
log_line = f"tenant_id={tenant_id} author_id={user_id} author={username} action_type=chat_ops action_name={event_name.value} chat_ops_type={chatops_type.lower()}" # noqa
for k, v in kwargs.items():
log_line += f" {k}={json.dumps(v)}"
insight_logger.info(log_line)
except Exception as e:
logger.warning(f"insight_log.failed_to_write_chatops_insight_log exception={e}")