Skip to content

Commit

Permalink
feat: Demo mode v2 & moving background tasks outside of the server (#…
Browse files Browse the repository at this point in the history
…2561)

Signed-off-by: Matvey Kukuy <[email protected]>
Co-authored-by: Tal <[email protected]>
  • Loading branch information
Matvey-Kuk and talboren authored Nov 21, 2024
1 parent 5aea39e commit d94dabd
Show file tree
Hide file tree
Showing 18 changed files with 672 additions and 81 deletions.
5 changes: 4 additions & 1 deletion docker/Dockerfile.api
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ RUN chgrp -R 0 /app && chmod -R g=u /app
RUN chown -R keep:keep /app
RUN chown -R keep:keep /venv
USER keep
ENTRYPOINT ["gunicorn", "keep.api.api:get_app", "--bind" , "0.0.0.0:8080" , "--workers", "4" , "-k" , "uvicorn.workers.UvicornWorker", "-c", "/venv/lib/python3.11/site-packages/keep/api/config.py"]

ENTRYPOINT ["/venv/lib/python3.11/site-packages/keep/entrypoint.sh"]

CMD ["gunicorn", "keep.api.api:get_app", "--bind" , "0.0.0.0:8080" , "--workers", "4" , "-k" , "uvicorn.workers.UvicornWorker", "-c", "/venv/lib/python3.11/site-packages/keep/api/config.py"]
2 changes: 2 additions & 0 deletions docker/Dockerfile.dev.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ RUN . /venv/bin/activate && poetry install --no-root
ENV PYTHONPATH="/app:${PYTHONPATH}"
ENV PATH="/venv/bin:${PATH}"
ENV VIRTUAL_ENV="/venv"
ENV POSTHOG_DISABLED="true"

ENTRYPOINT ["/app/keep/entrypoint.sh"]

CMD ["gunicorn", "keep.api.api:get_app", "--bind" , "0.0.0.0:8080" , "--workers", "1" , "-k" , "uvicorn.workers.UvicornWorker", "-c", "./keep/api/config.py", "--reload"]
1 change: 1 addition & 0 deletions docs/deployment/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ General configuration variables control the core behavior of the Keep server. Th
| **KEEP_API_URL** | Specifies the Keep API URL | No | Constructed from HOST and PORT | Valid URL |
| **KEEP_STORE_RAW_ALERTS** | Enables storing of raw alerts | No | "false" | "true" or "false" |
| **TENANT_CONFIGURATION_RELOAD_TIME** | Time in minutes to reload tenant configurations | No | 5 | Positive integer |
| **KEEP_LIVE_DEMO_MODE** | Keep will simulate incoming alerts and other activity | No | "false" | "true" or "false" |

### Logging and Environment
<Info>
Expand Down
3 changes: 1 addition & 2 deletions keep/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import keep.api.logging
from keep.api.api import AUTH_TYPE
from keep.api.core.db_on_start import migrate_db, try_create_single_tenant
from keep.api.core.report_uptime import launch_uptime_reporting
from keep.api.core.dependencies import SINGLE_TENANT_UUID
from keep.identitymanager.identitymanagerfactory import IdentityManagerTypes

Expand All @@ -19,7 +18,6 @@ def on_starting(server=None):
logger.info("Keep server starting")

migrate_db()
launch_uptime_reporting()

# Create single tenant if it doesn't exist
if AUTH_TYPE in [
Expand Down Expand Up @@ -54,4 +52,5 @@ def on_starting(server=None):
public_url = ngrok_connection.public_url
logger.info(f"ngrok tunnel: {public_url}")
os.environ["KEEP_API_URL"] = public_url

logger.info("Keep server started")
44 changes: 44 additions & 0 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from keep.api.models.db.preset import * # pylint: disable=unused-wildcard-import
from keep.api.models.db.provider import * # pylint: disable=unused-wildcard-import
from keep.api.models.db.rule import * # pylint: disable=unused-wildcard-import
from keep.api.models.db.system import * # pylint: disable=unused-wildcard-import
from keep.api.models.db.tenant import * # pylint: disable=unused-wildcard-import
from keep.api.models.db.topology import * # pylint: disable=unused-wildcard-import
from keep.api.models.db.workflow import * # pylint: disable=unused-wildcard-import
Expand Down Expand Up @@ -4482,3 +4483,46 @@ def get_resource_ids_by_resource_type(
# Execute the query and return results
result = session.exec(query)
return result.all()

def get_or_creat_posthog_instance_id(
session: Optional[Session] = None
):
POSTHOG_INSTANCE_ID_KEY = "posthog_instance_id"
with Session(engine) as session:
system = session.exec(select(System).where(System.name == POSTHOG_INSTANCE_ID_KEY)).first()
if system:
return system.value

system = System(
id=str(uuid4()),
name=POSTHOG_INSTANCE_ID_KEY,
value=str(uuid4()),
)
session.add(system)
session.commit()
session.refresh(system)
return system.value

def get_activity_report(
session: Optional[Session] = None
):
from keep.api.models.db.user import User

last_24_hours = datetime.utcnow() - timedelta(hours=24)
activity_report = {}
with Session(engine) as session:
activity_report['tenants_count'] = session.query(Tenant).count()
activity_report['providers_count'] = session.query(Provider).count()
activity_report['users_count'] = session.query(User).count()
activity_report['last_24_hours_incidents_count'] = session.query(Incident).filter(
Incident.creation_time >= last_24_hours).count()
activity_report['last_24_hours_alerts_count'] = session.query(Alert).filter(
Alert.timestamp >= last_24_hours).count()
activity_report['last_24_hours_rules_created'] = session.query(Rule).filter(
Rule.creation_time >= last_24_hours).count()
activity_report['last_24_hours_workflows_created'] = session.query(Workflow).filter(
Workflow.creation_time >= last_24_hours).count()
activity_report['last_24_hours_workflows_executed'] = session.query(WorkflowExecution).filter(
WorkflowExecution.started >= last_24_hours).count()

return activity_report
Loading

0 comments on commit d94dabd

Please sign in to comment.