Skip to content

Commit

Permalink
monitoring optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MaKyOtOx committed Jan 18, 2023
1 parent 4adcca1 commit 3187692
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 101 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM python:3.7-slim
LABEL Name="PatrowlHears" Version="1.3.2"
LABEL Name="PatrowlHears" Version="1.3.3"

ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/patrowl-hears/
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.2 // Community Edition
1.3.3 // Community Edition
2 changes: 1 addition & 1 deletion backend_app/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.2
1.3.3
2 changes: 1 addition & 1 deletion backend_app/common/utils/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def get_current_organization(user, org_id=None):
if org_id is None:
org = Organization.objects.first()
else:
org = Organization.objects.get(id=org_id)
org = Organization.objects.select_related('org_monitoring_list').get(id=org_id)
else:
# standard user
_org = OrganizationUser.objects.filter(user_id=user.id, organization_id=org_id).first()
Expand Down
17 changes: 17 additions & 0 deletions backend_app/cves/migrations/0015_auto_20230117_1547.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.1.13 on 2023-01-17 15:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cves', '0014_auto_20210409_1930'),
]

operations = [
migrations.AddIndex(
model_name='vendor',
index=models.Index(fields=['name'], name='kb_vendor_name_43938a_idx'),
),
]
17 changes: 17 additions & 0 deletions backend_app/cves/migrations/0016_auto_20230118_1701.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.1.13 on 2023-01-18 17:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cves', '0015_auto_20230117_1547'),
]

operations = [
migrations.AddIndex(
model_name='product',
index=models.Index(fields=['name'], name='kb_product_name_ef6a33_idx'),
),
]
6 changes: 6 additions & 0 deletions backend_app/cves/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Vendor(models.Model):

class Meta:
db_table = "kb_vendor"
indexes = [
models.Index(fields=['name'])
]

def __unicode__(self):
return self.name
Expand Down Expand Up @@ -66,6 +69,9 @@ class Product(models.Model):
class Meta:
db_table = "kb_product"
unique_together = (('name', 'vendor'),)
indexes = [
models.Index(fields=['name'])
]

def __unicode__(self):
return self.name
Expand Down
21 changes: 11 additions & 10 deletions backend_app/monitored_assets/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def toggle_monitor_product(self):
if self.data['monitored'] is False and product in org.org_monitoring_list.products.all():
org.org_monitoring_list.products.remove(product)

product.save()
# product.save()
return JsonResponse("toggled.", safe=False)


Expand All @@ -40,7 +40,7 @@ def toggle_monitor_vendor(self):
if set(['vendor_name', 'monitored', 'organization_id']).issubset(self.data.keys()) is False:
return JsonResponse("error.", safe=False, status=500)

vendor = Vendor.objects.filter(name=self.data['vendor_name']).first()
vendor = Vendor.objects.filter(name=self.data['vendor_name']).prefetch_related('product_set').first()
if vendor is None:
return JsonResponse("error.", safe=False, status=500)
else:
Expand All @@ -49,16 +49,17 @@ def toggle_monitor_vendor(self):

if self.data['monitored'] is True and vendor not in org.org_monitoring_list.vendors.all():
org.org_monitoring_list.vendors.add(vendor)
for product in vendor.product_set.all():
if self.data['monitored'] is True and product not in org.org_monitoring_list.products.all():
org.org_monitoring_list.products.add(product)
for product in vendor.product_set.exclude(id__in=org.org_monitoring_list.products.all()):
org.org_monitoring_list.products.add(product)
if self.data['monitored'] is False and vendor in org.org_monitoring_list.vendors.all():
org.org_monitoring_list.vendors.remove(vendor)
for product in vendor.product_set.all():
if self.data['monitored'] is False and product in org.org_monitoring_list.products.all():
org.org_monitoring_list.products.remove(product)
# for product in vendor.product_set.all():
# if product in org.org_monitoring_list.products.all():
# org.org_monitoring_list.products.remove(product)
for product in vendor.product_set.filter(id__in=org.org_monitoring_list.products.all()):
org.org_monitoring_list.products.remove(product)

vendor.save()
# vendor.save()
return JsonResponse("toggled.", safe=False)


Expand All @@ -79,7 +80,7 @@ def toggle_monitor_package(self):
if self.data['monitored'] is False and package in org.org_monitoring_list.packages.all():
org.org_monitoring_list.packages.remove(package)

package.save()
# package.save()
return JsonResponse("toggled.", safe=False)


Expand Down
70 changes: 44 additions & 26 deletions backend_app/users/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from django_filters import rest_framework as filters
from organizations.models import Organization, OrganizationUser, OrganizationOwner
# from organizations.backends.tokens import RegistrationTokenGenerator
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from common.utils import get_api_default_permissions
from common.utils.pagination import StandardResultsSetPagination
Expand All @@ -26,6 +25,8 @@
from .backends import InvitationBackend, CustomInvitations

import json
import logging
logger = logging.getLogger(__name__)


class UserSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -80,7 +81,7 @@ def get_queryset(self):
if org.is_owner(current_user) or org.is_admin(current_user):
org_admins.append(org)
except Exception as e:
print(e)
logger.exception(e)
pass
return OrganizationUser.objects.filter(organization__in=org_admins).order_by('id')

Expand Down Expand Up @@ -129,7 +130,7 @@ def activate_user(self, token):
user = get_user_model().objects.get(id=user_id, is_active=False)
except get_user_model().DoesNotExist:
raise Http404(_("Your URL may have expired."))
# if not RegistrationTokenGenerator().check_token(user, user_token):

if not PasswordResetTokenGenerator().check_token(user, user_token):
raise Http404(_("Your URL may have expired."))

Expand Down Expand Up @@ -184,6 +185,7 @@ def activate_user(self, token):

return JsonResponse({'status': 'success'}, safe=False)
except Exception as e:
logger.exception(e)
return JsonResponse({'status': 'error', 'reason': str(e)}, safe=False)
return JsonResponse({'status': 'valid', 'email': user.email}, safe=False)

Expand Down Expand Up @@ -268,7 +270,8 @@ def add_user(self):
return JsonResponse({'status': 'success', 'user': user_dict}, safe=False)
except Exception as e:
print(e)
return JsonResponse({'status': 'error', 'reason': 'no'}, safe=False)
logger.exception(e)
return JsonResponse({'status': 'error', 'reason': str(e)}, safe=False)


@api_view(['GET', 'DELETE'])
Expand All @@ -284,7 +287,8 @@ def delete_user(self, user_id):
return JsonResponse({'status': 'success'}, safe=False)
except Exception as e:
print(e)
return JsonResponse({'status': 'error', 'reason': 'no'}, safe=False)
logger.exception(e)
return JsonResponse({'status': 'error', 'reason': str(e)}, safe=False)


@api_view(['POST'])
Expand Down Expand Up @@ -334,7 +338,8 @@ def invite_user(self, organization_id):
organization=org
)
org_user.save()
except Exception:
except Exception as e:
logger.exception(e)
pass

return JsonResponse({'status': 'success'}, safe=False)
Expand All @@ -357,8 +362,9 @@ def update_user_profile(self):
try:
self.user.save()
return JsonResponse({'status': 'success'}, safe=False)
except Exception:
return JsonResponse({'status': 'error'}, status=500, safe=False)
except Exception as e:
logger.exception(e)
return JsonResponse({'status': 'error', 'reason': str(e)}, status=500, safe=False)


@api_view(['POST'])
Expand All @@ -380,8 +386,12 @@ def update_user_profile_admin(self, user_id):
try:
user.save()
return JsonResponse({'status': 'success'}, safe=False)
except Exception:
return JsonResponse({'status': 'error'}, status=500, safe=False)
except Exception as e:
logger.exception(e)
return JsonResponse(
{'status': 'error', 'reason': str(e)},
status=500, safe=False
)


@api_view(['POST'])
Expand All @@ -401,7 +411,7 @@ def create_organization(self):
raise PermissionDenied(_("Sorry, (org) admins only"))

if set(['name', 'is_active', 'email']).issubset(self.data.keys()) is False:
return JsonResponse("error.", safe=False, status=500)
return JsonResponse({"status": "error", "reason": "required values: 'name', 'is_active', 'email'"}, safe=False, status=500)
org_name = self.data.get('name')
is_active = self.data.get('is_active', None) == "true"
owner_email = self.data.get('email')
Expand All @@ -410,8 +420,9 @@ def create_organization(self):
try:
org = Organization.objects.create(name=org_name, is_active=is_active)
org.save()
except Exception:
return JsonResponse("error.", safe=False, status=400)
except Exception as e:
logger.exception(e)
return JsonResponse({"status": "error", "reason": str(e)}, safe=False, status=400)

# Create or activate owner
try:
Expand Down Expand Up @@ -687,9 +698,9 @@ def set_org(self, org_id):
self.session['org_id'] = org.id
self.session['org_name'] = org.name
return JsonResponse({
'status': 'set',
'org_id': org.id,
'org_name': org.name
'status': 'set',
'org_id': org.id,
'org_name': org.name
},
safe=False
)
Expand All @@ -706,9 +717,9 @@ def set_default_org(self):
self.session['org_id'] = user.organization.id
self.session['org_name'] = user.organization.name
return JsonResponse({
'status': 'set',
'org_id': user.organization.id,
'org_name': user.organization.name
'status': 'set',
'org_id': user.organization.id,
'org_name': user.organization.name
}, safe=False
)

Expand Down Expand Up @@ -753,7 +764,8 @@ def get_curruser_authtoken(request):
try:
token = Token.objects.filter(user=request.user).first()
return JsonResponse({"status": "success", "token": token.key})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand All @@ -766,7 +778,8 @@ def get_user_authtoken(request, user_id):
uid = get_object_or_404(get_user_model(), id=user_id)
token = Token.objects.filter(user=uid).first()
return JsonResponse({"status": "success", "token": token.key})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand All @@ -779,7 +792,8 @@ def delete_curruser_authtoken(request):
for token in Token.objects.filter(user=request.user):
token.delete()
return JsonResponse({"status": "success"})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand All @@ -793,7 +807,8 @@ def delete_user_authtoken(request, user_id):
for token in Token.objects.filter(user=uid):
token.delete()
return JsonResponse({"status": "success"})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand All @@ -807,7 +822,8 @@ def renew_curruser_authtoken(request):
token.delete()
token = Token.objects.get_or_create(user=request.user)[0]
return JsonResponse({"status": "success", "token": token.key})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand All @@ -822,7 +838,8 @@ def renew_user_authtoken(request, user_id):
token.delete()
token = Token.objects.get_or_create(user=uid)[0]
return JsonResponse({"status": "success", "token": token.key})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand All @@ -841,7 +858,8 @@ def renew_user_password(request, user_id):
user.set_password(new_password)
user.save()
return JsonResponse({"status": "success", "password": new_password})
except Exception:
except Exception as e:
logger.exception(e)
pass
return JsonResponse({
"status": "error",
Expand Down
1 change: 0 additions & 1 deletion backend_app/users/authentication.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from rest_framework import authentication


Expand Down
Loading

0 comments on commit 3187692

Please sign in to comment.