Application Stats

The stats application contains the actions to generate generate an automatic report.

Templates

The stats application contains 8 template :

  • variant.html : stat page where the evolution of the variants is represented in 3 graphs

  • stats_VolumetryGraph.html : stat page where the volumery of EMERGEN-DB id represented in a sankey graph

  • stats_sampling.html : stat page where a calender that reprsents how many samplings are collected each day

  • stats_public.html : stat page of the public statistics

  • stats_dashboard_labo.html : stat page where the automatic report for the laboratory dashbaord is returned

  • stats_dashboard_activities.html : stat page where the automatic report for the general dashboard is returned

  • flash.html : stat page that allows the user to filter results via the the flash

  • error.html : an error page

Key files of the « stats » application

management.commands

load_region.py

class apps.stats.management.commands.load_region.Command(stdout=None, stderr=None, no_color=False, force_color=False)
handle(*args, **kwargs)

SUMMARY LINE

Import region data

HOW TO USE

docker-compose exec web python manage.py load_region

Add -f docker-compose.prod.yml in Prod

load_departement.py

class apps.stats.management.commands.load_departement.Command(stdout=None, stderr=None, no_color=False, force_color=False)
handle(*args, **kwargs)

SUMMARY LINE

Import departement data

HOW TO USE

docker-compose exec web python manage.py load_departement

Add -f docker-compose.prod.yml in Prod

admin.py

class apps.stats.admin.DynamicCapacityAdmin(model, admin_site)

Adding the PageContent table in the Django admin and customizing the rendering

class apps.stats.admin.GroupAdmin(model, admin_site)

Adding the Flash table in the Django admin

changelist_view(request, extra_context=None)

The changelist_view function adds an UpdateFlashUploadForm to the extra_context and then calls the parent changelist_view method.

Paramètres
  • request – The request object represents the HTTP request made by the user. It contains information such as the user’s session, the requested URL, and any data sent with the request

  • extra_context – The extra_context parameter is an optional dictionary that allows you to pass additional context variables to the template when rendering the change list view. In the given code, it is used to add a new key-value pair to the existing extra_context dictionary

Renvoie

The method is returning the result of calling the changelist_view method of the GroupAdmin superclass, passing in the request and extra_context parameters. The extra_context parameter is set to the value of extra, which is a dictionary that includes an « update_flash_upload_form » key with a value of an instance of the UpdateFlashUploadForm

get_urls()

The function get_urls returns a list of additional URLs along with the existing URLs.

Renvoie

a list of additional URLs along with the existing URLs.

has_add_permission(request)

Return True if the given request has permission to add an object. Can be overridden by the user in subclasses.

has_delete_permission(request, obj=None)

Return True if the given request has permission to change the given Django model instance, the default implementation doesn’t examine the obj parameter.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to delete the obj model instance. If obj is None, this should return True if the given request has permission to delete any object of the given type.

upload_csv(request)

The function upload_csv is used to handle the uploading and processing of a CSV file, saving the data to a database and displaying appropriate messages to the user.

Paramètres

request – The request parameter is an object that represents the HTTP request made by the client. It contains information such as the request method (e.g., GET, POST), request headers, request body, and other metadata related to the request. In this code snippet, the request object is used

Renvoie

a redirect to the previous page.

models.py

class apps.stats.models.DynamicCapacity(*args, **kwargs)

Dynamic capacity model table

exception DoesNotExist
exception MultipleObjectsReturned
clean()

The function « clean » calculates the total capacity of a consortium by summing up the capacities of different components.

class apps.stats.models.departement(*args, **kwargs)

Departement model table

exception DoesNotExist
exception MultipleObjectsReturned
class apps.stats.models.flash(*args, **kwargs)

Flash model table

exception DoesNotExist
exception MultipleObjectsReturned
class apps.stats.models.region(*args, **kwargs)

Region model table

exception DoesNotExist
exception MultipleObjectsReturned
class apps.stats.models.region_dep_mat(departement_id, departement_code, departement_nom, departement_geometry, region_id, region_code, region_nom, region_geometry)
exception DoesNotExist
exception MultipleObjectsReturned

test.py

class apps.stats.tests.statsTestPage(methodName='runTest')

Stats tests

setUp()

Hook method for setting up the test fixture before exercising it.

tearDown()

Hook method for deconstructing the test fixture after testing it.

test_Flash_page_nothing()

Test get flash page with no data

test_flash_get_noLog()

Test get flash page but not log

test_flash_get_no_staff()

Test get flash page but not log

test_flash_page()

Test get flash page with default parameters

test_flash_page_post_dep()

Test post and get flash page with deparement parameter

test_flash_page_post_reg()

Test post and get flash page with region parameter

test_indication_sampling_page()

Test get indication and sampling pages

test_public_page_nothing()

Test get public page with no data

test_stats_get_noLog()

Test get stats page but not log

test_stats_page()

Test get stats page with default parameters

test_stats_page_nothing()

Test get stats page with no data

test_stats_page_post_dep()

Test post and get stats page with deparement parameter

test_stats_page_post_reg()

Test post and get stats page with region parameter

test_stats_variants_page_graphs()

Test get variants page

test_str_dep()

Test model : get str from Excel file

test_upload_flash_bad_file()

Test admin upload flash bad file

test_upload_flash_data_bad_extension()

Test admin upload flash data

test_upload_population_data()

Test admin upload flash good extension

test_varaint_get_noLog()

Test get stats page but not log

url.py

The urls.py file contains :

view.py

apps.stats.views.VolumetryGraph_view(request)

The function VolumetryGraph_view renders the Volumetry graph template.

Paramètres

request – The request object represents the HTTP request that the user made to access the view. It contains information such as the user’s browser details, the requested URL, and any data that was sent with the request

Renvoie

the rendered template “stats/stats_VolumetryGraph.html”.

apps.stats.views.dashboard_laboratoire_view(request)

The function dashboard_laboratoire_view renders the dashboard of each lab if the user is a data submitter and not a data broker or manager, otherwise it displays the external reports page with all the labs.

Paramètres

request – The request object represents the HTTP request made by the user. It contains information such as the user’s session, the requested URL, and any data sent with the request

Renvoie

If the user’s profile has the attribute data_submitter set to True and both data_broker and manager set to False, the function will return the rendered template « stats/stats_dashboard_labo.html ». Otherwise, it will return the rendered template « external_report/external_report.html ».

apps.stats.views.flash_view(request)

Request, preparate and send data to Flash template for automatic report

apps.stats.views.stats_public(request)

The function « stats_public » renders a public stats page.

Paramètres

request – The request parameter is an object that represents the HTTP request made by the client. It contains information such as the HTTP method (GET, POST, etc.), headers, user session, and any data sent with the request. In this case, it is used to render the « stats/stats_public.html »

Renvoie

the rendered template « stats/stats_public.html ».

apps.stats.views.stats_sampling(request)

The function « stats_sampling » renders the Sampling stats page.

Paramètres

request – The request parameter is an object that represents the HTTP request made by the client. It contains information such as the HTTP method (GET, POST, etc.), headers, user session, and any data sent with the request. In this case, it is used to render the « stats/stats_sampling.html »

Renvoie

the rendered HTML template « stats/stats_sampling.html ».

apps.stats.views.stats_view(request)

The stats_view function prepares and sends data to a template for generating an automatic report based on user choices and database queries.

Paramètres

request – The request parameter is an object that represents the HTTP request made by the user. It contains information such as the request method(GET or POST), the user’s session, and any data submitted with the request

Renvoie

a rendered HTML template with various context variables.

apps.stats.views.variant_view(request)

The function variant_view renders a template for an automatic report.

Paramètres

request – The request parameter is an object that represents the HTTP request made by the user. It contains information such as the user’s browser, IP address, and any data sent with the request. In this function, the request object is used to render the “variant.html” template and send it back as the

Renvoie

the rendered template “stats/variant.html”.

Create

Table

We use the datable web library to display the tables.

In view.py

To use the Datatable library in an automated way, the function sends, for each table, 3 elements :

  • A list of lists. Each sub-list corresponds to a row of the table with the name of the row in the first position. By convention, this element is called tabX where X is the table number

  • A list of column names. By convention, this element is called tabX_column_loc where X is the table number

  • A list of column numbers. By convention, this element is called tabX_column_num where X is the number of the table

The code contains several tables that can help you in adding new tables.

In template/stats/stats.html

1- Add a div in the body at the desired position

<table id="tabX" class="display nowrap" style="width:100%">
    <thead>
        <tr>
            <!-- Your custom header (see in code to have example) -->
        </tr>
        <tr>
            {% for l in tabX_column_loc %}
            <th>{{ l }}</th>
            {% endfor %}
        </tr>
    </thead>
</table>

2- Add at the end of the body with the other scripts a new JS script like the following example

<script>
    $(document).ready(function() {
        $('#tab_X').DataTable( {
            "data" : {{ tabX|safe }},
            "columns" : {{ tabX_column_num|safe }},
            "pageLength": 25,
            "order": [],
            "scrollX": true,
            dom: 'lfBrtip',
            buttons: [
                'copyHtml5',
                'excelHtml5',
                'csvHtml5',
                'pdfHtml5'
            ]
        });
    } );
</script>

Charts

We use two libraries :

The codes view.py and template/stats/stats.html contain several examples that can help you in adding new charts.

Map

To create map, we use Leaflet <https://leafletjs.com/>. The codes view.py and template/stats/stats.html contain several examples that can help you to custom map and format data.