Skip to content

Commit

Permalink
Merge branch 'refactor/2570-migrate-pages-router' of github.com:keeph…
Browse files Browse the repository at this point in the history
…q/keep into refactor/2570-migrate-pages-router
  • Loading branch information
Kiryous committed Nov 21, 2024
2 parents 33e0975 + 954e1cd commit 2af2a63
Show file tree
Hide file tree
Showing 19 changed files with 683 additions and 84 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
14 changes: 11 additions & 3 deletions keep-ui/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,20 @@ const config = {
}
return true;
},
jwt: async ({ token, user, account }): Promise<JWT> => {
jwt: async ({ token, user, account, profile }): Promise<JWT> => {
if (account && user) {
let accessToken: string | undefined;
let tenantId: string | undefined = user.tenantId;
let role: string | undefined = user.role;
// Ensure we always have an accessToken
if (authType == AuthType.AUTH0) {
accessToken = account.id_token;
if ((profile as any)?.keep_tenant_id) {
tenantId = (profile as any).keep_tenant_id;
}
if ((profile as any)?.keep_role) {
role = (profile as any).keep_role;
}
} else {
accessToken =
user.accessToken || account.access_token || account.id_token;
Expand All @@ -212,8 +220,8 @@ const config = {
}

token.accessToken = accessToken;
token.tenantId = user.tenantId;
token.role = user.role;
token.tenantId = tenantId;
token.role = role;

if (authType === AuthType.KEYCLOAK) {
token.refreshToken = account.refresh_token;
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 2af2a63

Please sign in to comment.