-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
36 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,50 @@ | ||
import argparse | ||
from random import randint | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from argus.incident.constants import MIN_INCIDENT_LEVEL, MAX_INCIDENT_LEVEL | ||
from argus.incident.models import create_fake_incident | ||
|
||
|
||
class Range(argparse.Action): | ||
def __init__(self, minimum=None, maximum=None, *args, **kwargs): | ||
self.min = minimum | ||
self.max = maximum | ||
kwargs["metavar"] = "[%d-%d]" % (self.min, self.max) | ||
super(Range, self).__init__(*args, **kwargs) | ||
|
||
def __call__(self, parser, namespace, value, option_string=None): | ||
if not (self.min <= value <= self.max): | ||
msg = "invalid choice: %r (choose from [%d-%d])" % (value, self.min, self.max) | ||
raise argparse.ArgumentError(self, msg) | ||
setattr(namespace, self.dest, value) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Create fake Incident" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("-t", "--tags", nargs="+", type=str, help="Add the listed tags to the incident") | ||
parser.add_argument("-b", "--batch-size", type=int, help="Create <batch size> incidents in one go") | ||
parser.add_argument("-d", "--description", type=str, help="Use this description for the incident") | ||
parser.add_argument( | ||
"-l", | ||
"--level", | ||
type=int, | ||
action=Range, | ||
minimum=MIN_INCIDENT_LEVEL, | ||
maximum=MAX_INCIDENT_LEVEL, | ||
default=0, | ||
help="Set level to <level>, otherwise a random number within the correct range is used", | ||
) | ||
parser.add_argument("-t", "--tags", nargs="+", type=str, help="Add the listed tags to the incident") | ||
parser.add_argument("--stateless", action="store_true", help="Create a stateless incident (end_time = None)") | ||
|
||
def handle(self, *args, **options): | ||
tags = options.get("tags") or [] | ||
description = options.get("description") or None | ||
batch_size = options.get("batch_size") or 1 | ||
level = options.get("level") or randint(MIN_INCIDENT_LEVEL, MAX_INCIDENT_LEVEL) | ||
stateful = False if options.get("stateless") else True | ||
for i in range(batch_size): | ||
create_fake_incident(tags=tags, description=description, stateful=stateful) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Prevent import loops | ||
# DO NOT import anything here, ever | ||
|
||
INCIDENT_LEVELS = (1, 2, 3, 4, 5) | ||
MIN_INCIDENT_LEVEL = 1 # Do not override | ||
MAX_INCIDENT_LEVEL = 5 | ||
INCIDENT_LEVELS = tuple(range(MIN_INCIDENT_LEVEL, MAX_INCIDENT_LEVEL + 1)) | ||
INCIDENT_LEVEL_CHOICES = zip(INCIDENT_LEVELS, map(str, INCIDENT_LEVELS)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters