Commit 31c9beed authored by sebastien letort's avatar sebastien letort

launched plot became create plot and time period has its format in iso in all...

launched plot became create plot and time period has its format in iso in all metrics (previously only per_user). Test updated in consequence.
parent c8ce19f2
Pipeline #66075 canceled with stage
......@@ -17,7 +17,6 @@ from django.http import JsonResponse
from main.models import AllgoUser
from api.v1.views import Metrics
from api.v1.metrics import DATE_FMT
from . import populate_db
......@@ -29,19 +28,14 @@ class ApiMetricsTestCase(TestCase):
API_URL = '/api/v1/metrics'
@classmethod
def setUpClass(cls):
log.info(":> setUpClass.")
def setUpTestData(cls):
log.info(":> setUpTestData.\nCreate 4 users, one own the app, and various jobs are created.")
populate_db.with_vitals()
populate_db.with_users([ 'Bob', 'Liz', 'Zaza', 'Lol' ])
populate_db.with_webapps({ 'Bob': [cls.APP_NAME] })
populate_db.with_various_jobs_from_one_app()
@classmethod
def tearDownClass(cls):
# needed, but useless, the entire database is destroyed at the end
pass
def setUp(self):
log.info(":> setUp.")
# Every test needs access to the request factory.
......@@ -61,8 +55,7 @@ class ApiMetricsTestCase(TestCase):
api_url = cls.API_URL + "=?" + "&".join(l_args)
return api_url
def launch_view(self, what):
def create_view(self, what):
SLEEP_ID = '1'
return Metrics.as_view()(self.request, SLEEP_ID, what)
......@@ -84,7 +77,7 @@ class ApiMetricsTestCase(TestCase):
self.request = self.req_factory.get(url)
self.request.user = User.objects.get(username='Bob')
o_response = self.launch_view(what)
o_response = self.create_view(what)
self.assertIsInstance(o_response, JsonResponse)
str_response = o_response.content.decode('utf-8')
......@@ -103,7 +96,7 @@ class ApiMetricsTestCase(TestCase):
url = self._build_url()
self.request = self.req_factory.get(url)
o_response = self.launch_view('per_user')
o_response = self.create_view('per_user')
self.assertIsInstance(o_response, JsonResponse)
self.assertEqual(o_response.status_code, 401, "without user = not authorized.")
......@@ -113,7 +106,7 @@ class ApiMetricsTestCase(TestCase):
self.request = self.req_factory.get(url)
self.request.user = User.objects.get(username='Lol')
o_response = self.launch_view('per_user')
o_response = self.create_view('per_user')
self.assertIsInstance(o_response, JsonResponse)
self.assertEqual(o_response.status_code, 403, "user without app = permission denied.")
......@@ -134,7 +127,7 @@ class ApiMetricsTestCase(TestCase):
o_user = User.objects.get(username='Bob')
self.request.user = o_user
o_response = self.launch_view('peureuse')
o_response = self.create_view('peureuse')
self.assertIsInstance(o_response, JsonResponse)
self.assertEqual(o_response.status_code, 404, "Unknown metrics type = not found.")
......@@ -145,8 +138,6 @@ class ApiMetricsTestCase(TestCase):
# I cannot manage to make "01-01-1 00:00" be validated with this.
# It seems that only 4 digits years are allowed.
# ~ print( DATE_FMT )
# ~ print( d_apps['from'] )
# ~ with not self.assertRaises(ValueError):
# ~ datetime.strptime( d_apps['from'], DATE_FMT )
# ~ with not self.assertRaises(ValueError):
......@@ -158,9 +149,9 @@ class ApiMetricsTestCase(TestCase):
self.check_api( 'per_state', d_format )
@tag( 'format' )
def test_launched(self):
def test_created(self):
d_format = { 'n': int, 'time_period': str }
self.check_api( 'launched', d_format )
self.check_api( 'created', d_format )
def check_results(self, what, ld_expected):
......@@ -168,7 +159,7 @@ class ApiMetricsTestCase(TestCase):
self.request = self.req_factory.get(url)
self.request.user = User.objects.get(username='Bob')
o_response = self.launch_view(what)
o_response = self.create_view(what)
str_response = o_response.content.decode('utf-8')
d_json = json.loads(str_response)
l_data = d_json[self.APP_NAME]['data']
......@@ -181,44 +172,44 @@ class ApiMetricsTestCase(TestCase):
@tag( 'results' )
def test_per_state_results(self):
ld_expected = [
{'time_period': '2019-01-01T00:00:00Z', 'result': 'SUCCESS', 'n': 2},
{'time_period': '2019-01-01T00:00:00Z', 'result': 'ERROR', 'n': 1},
{'time_period': '2019-02-01T00:00:00Z', 'result': 'SUCCESS', 'n': 2},
{'time_period': '2019-02-01T00:00:00Z', 'result': 'ERROR', 'n': 1},
{'time_period': '2019-02-01T00:00:00Z', 'result': 'ABORTED', 'n': 2},
{'time_period': '2019-03-01T00:00:00Z', 'result': 'SUCCESS', 'n': 3},
{'time_period': '2019-03-01T00:00:00Z', 'result': 'TIMEOUT', 'n': 1},
{'time_period': '2019-04-01T00:00:00Z', 'result': 'SUCCESS', 'n': 7},
{'time_period': '2019-04-01T00:00:00Z', 'result': 'ERROR', 'n': 2},
{'time_period': '2019-04-01T00:00:00Z', 'result': 'ABORTED', 'n': 2},
{'time_period': '2019-01-01', 'result': 'SUCCESS', 'n': 2},
{'time_period': '2019-01-01', 'result': 'ERROR', 'n': 1},
{'time_period': '2019-02-01', 'result': 'SUCCESS', 'n': 2},
{'time_period': '2019-02-01', 'result': 'ERROR', 'n': 1},
{'time_period': '2019-02-01', 'result': 'ABORTED', 'n': 2},
{'time_period': '2019-03-01', 'result': 'SUCCESS', 'n': 3},
{'time_period': '2019-03-01', 'result': 'TIMEOUT', 'n': 1},
{'time_period': '2019-04-01', 'result': 'SUCCESS', 'n': 7},
{'time_period': '2019-04-01', 'result': 'ERROR', 'n': 2},
{'time_period': '2019-04-01', 'result': 'ABORTED', 'n': 2},
]
self.check_results('per_state', ld_expected)
@tag( 'result' )
def test_per_user_results(self):
ld_expected = [
{'uname': 'Bob', 'user': 1, 'n': 3, 'time_period': '2019-01-01T00:00:00Z'},
{'uname': 'Bob', 'user': 1, 'n': 1, 'time_period': '2019-02-01T00:00:00Z'},
{'uname': 'Liz', 'user': 2, 'n': 2, 'time_period': '2019-02-01T00:00:00Z'},
{'uname': 'Zaza', 'user': 3, 'n': 2, 'time_period': '2019-02-01T00:00:00Z'},
{'uname': 'Liz', 'user': 2, 'n': 1, 'time_period': '2019-03-01T00:00:00Z'},
{'uname': 'Zaza', 'user': 3, 'n': 3, 'time_period': '2019-03-01T00:00:00Z'},
{'uname': 'Bob', 'user': 1, 'n': 5, 'time_period': '2019-04-01T00:00:00Z'},
{'uname': 'Liz', 'user': 2, 'n': 1, 'time_period': '2019-04-01T00:00:00Z'},
{'uname': 'Zaza', 'user': 3, 'n': 3, 'time_period': '2019-04-01T00:00:00Z'},
{'uname': 'Lol', 'user': 4, 'n': 2, 'time_period': '2019-04-01T00:00:00Z'},
{'uname': 'Bob', 'user': 1, 'n': 3, 'time_period': '2019-01-01'},
{'uname': 'Bob', 'user': 1, 'n': 1, 'time_period': '2019-02-01'},
{'uname': 'Liz', 'user': 2, 'n': 2, 'time_period': '2019-02-01'},
{'uname': 'Zaza', 'user': 3, 'n': 2, 'time_period': '2019-02-01'},
{'uname': 'Liz', 'user': 2, 'n': 1, 'time_period': '2019-03-01'},
{'uname': 'Zaza', 'user': 3, 'n': 3, 'time_period': '2019-03-01'},
{'uname': 'Bob', 'user': 1, 'n': 5, 'time_period': '2019-04-01'},
{'uname': 'Liz', 'user': 2, 'n': 1, 'time_period': '2019-04-01'},
{'uname': 'Zaza', 'user': 3, 'n': 3, 'time_period': '2019-04-01'},
{'uname': 'Lol', 'user': 4, 'n': 2, 'time_period': '2019-04-01'},
]
self.check_results('per_user', ld_expected)
@tag( 'results' )
def test_launched_results(self):
def test_created_results(self):
ld_expected = [
{'time_period': '2019-01-01T00:00:00Z', 'n': 3},
{'time_period': '2019-02-01T00:00:00Z', 'n': 5},
{'time_period': '2019-03-01T00:00:00Z', 'n': 4},
{'time_period': '2019-04-01T00:00:00Z', 'n': 11},
{'time_period': '2019-01-01', 'n': 4},
{'time_period': '2019-02-01', 'n': 6},
{'time_period': '2019-03-01', 'n': 3},
{'time_period': '2019-04-01', 'n': 10},
]
self.check_results('launched', ld_expected)
self.check_results('created', ld_expected)
if __name__ == '__main__':
......
......@@ -19,13 +19,10 @@ from django.db.models.functions import TruncHour,TruncMonth, Trunc, Extract
from main.models import Job #, Webapp, JobQueue
log = logging.getLogger('allgo')
# ~ log = logging.getLogger(__name__)
# ======================================
# ~ DATE_FMT = "%d-%m-%Y %H:%M"
def __fmt_date(date):
"""date is datetime object (for from/to)"""
# ~ return datetime.strftime( date, DATE_FMT )
return date.date().isoformat()
DATE_IN_FMT = "%Y-%m-%d"
......@@ -60,7 +57,7 @@ def __api_dict(from_, to, step, l_records ):
}
def nb_jobs_per_user(app_id, from_=None, to=None, step=None):
"""compute for the webapp app_id, for each user the number of jobs "updated" (created,launched,terminated)
"""compute for the webapp app_id, for each user the number of jobs "updated" (launched,terminated)
per step period between [from\_ ; to[.
:param app_id: webapp id
......@@ -95,7 +92,7 @@ def nb_jobs_per_user(app_id, from_=None, to=None, step=None):
return __api_dict( from_, to, step, list(res) );
def nb_jobs_per_state(app_id, from_=None, to=None, step=None):
"""compute for the webapp app_id, for each state (result) the number of jobs "updated" (created,launched,terminated)
"""compute for the webapp app_id, for each state (result) the number of jobs "updated" (launched,terminated)
per step period between [from\_ ; to[.
:param app_id: webapp id
......@@ -124,10 +121,11 @@ def nb_jobs_per_state(app_id, from_=None, to=None, step=None):
for d_res in res:
d_res['result'] = Job.JOB_RESULT_CHOICES[d_res['result']][1]
d_res['time_period'] = __fmt_date( d_res.pop('time_period') )
return __api_dict( from_, to, step, list(res) );
def nb_jobs_launched(app_id, from_=None, to=None, step=None):
def nb_jobs_created(app_id, from_=None, to=None, step=None):
"""compute for the webapp app_id, the number of jobs created
per step period between [from\_ ; to[.
......@@ -156,13 +154,16 @@ def nb_jobs_launched(app_id, from_=None, to=None, step=None):
.values('time_period') \
.annotate(n=Count('id'))
for d_res in res:
d_res['time_period'] = __fmt_date( d_res.pop('time_period') )
return __api_dict( from_, to, step, list(res) );
# ======================================
d_actions = {
"per_user" : nb_jobs_per_user,
"per_state": nb_jobs_per_state,
"launched" : nb_jobs_launched,
"created" : nb_jobs_created,
# ~ "": ,
}
......@@ -289,7 +289,7 @@ class PerStatePlot extends Metrics
}
class LaunchedPlot extends Metrics
class CreatedPlot extends Metrics
{
constructor( app_name, d_app )
{
......@@ -344,7 +344,7 @@ const D_CHARTS = {
// api keyword : [ html elt id, js class, object ]
'per_user' : [ '#per_user_plot', PerUserPlot, null ],
'per_state': [ '#per_state_plot', PerStatePlot, null ],
'launched' : [ '#launched_plot', LaunchedPlot, null ],
'created' : [ '#created_plot', CreatedPlot, null ],
};
function build_API_url( which_chart )
......
......@@ -48,7 +48,7 @@
</select>
</fieldset>
<input type="submit" />
<canvas id="launched_plot" width="400px" height="200px"></canvas>
<canvas id="created_plot" width="400px" height="200px"></canvas>
<canvas id="per_user_plot" width="400px" height="200px"></canvas>
<canvas id="per_state_plot" width="400px" height="200px"></canvas>
</form>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment