Skip to content

analytics

main.analytics ¤

Functions for calculating team activity stats.

Functions:¤

get_funding_per_budget_type(projects) ¤

Determine the total project funding per type of budget.

Budget types are: 1. Internal (internal projects) 2. External FEC DI (directly incurred) 3. External FEC DA (directly allocated) 4. External Non-FEC 5. Other (internal funds for external projects)

Projects with status='Tentative' or 'Not done' will be excluded, as they have no analysis code.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, int]

A dictionary with budget types and the total project funding per type.

Source code in main/analytics.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def get_funding_per_budget_type(projects: list[models.Project]) -> dict[str, int]:
    """Determine the total project funding per type of budget.

    Budget types are:
    1. Internal (internal projects)
    2. External FEC DI (directly incurred)
    3. External FEC DA (directly allocated)
    4. External Non-FEC
    5. Other (internal funds for external projects)

    Projects with status='Tentative' or 'Not done' will be excluded, as they have no
    analysis code.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with budget types and the total project funding per type.
    """
    # Select only funding instances that fund one of the input queried projects
    project_fundings = models.Funding.objects.filter(project__in=projects)

    funding_per_budget_type: dict[str, float] = {}
    funding_per_budget_type["Internal"] = float(
        sum([funding.budget for funding in project_fundings.filter(source="Internal")])
    )
    names = {
        165138: "Other",
        165133: "FEC DI",
        182130: "FEC DA",
        165134: "Non-FEC",
    }
    for code, value in names.items():
        ac = models.AnalysisCode.objects.get(code=code)
        funding_per_budget_type[value] = float(
            sum(funding.budget for funding in project_fundings.filter(analysis_code=ac))
        )

    return Counter(funding_per_budget_type)

get_funding_per_department(projects) ¤

Determine the total project funding per department.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, float]

A dictionary with department names and the project funding per department.

Source code in main/analytics.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def get_funding_per_department(projects: list[models.Project]) -> dict[str, float]:
    """Determine the total project funding per department.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with department names and the project funding per department.
    """
    funding_per_department: dict[str, float] = defaultdict(float)
    for project in projects:
        funding_per_department[project.department.name] += float(
            sum([funding.budget for funding in project.funding_source.all()])
        )
    return funding_per_department

get_funding_per_faculty(projects) ¤

Determine the total project funding per faculty.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, float]

A dictionary with faculty names and the project funding per faculty.

Source code in main/analytics.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def get_funding_per_faculty(projects: list[models.Project]) -> dict[str, float]:
    """Determine the total project funding per faculty.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with faculty names and the project funding per faculty.
    """
    funding_per_faculty: dict[str, float] = defaultdict(float)
    for project in projects:
        funding_per_faculty[project.department.faculty] += float(
            sum([funding.budget for funding in project.funding_source.all()])
        )
    return funding_per_faculty

get_funding_per_funding_body(projects) ¤

Determine the total project funding per funding body.

Projects with funding body TBC are excluded, as are tentative projects.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, float]

A dictionary with funding bodies and the total project funding per body.

Source code in main/analytics.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def get_funding_per_funding_body(projects: list[models.Project]) -> dict[str, float]:
    """Determine the total project funding per funding body.

    Projects with funding body TBC are excluded, as are tentative projects.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with funding bodies and the total project funding per body.
    """
    # Select only funding instances that fund one of the input queried projects
    project_fundings = (
        models.Funding.objects.filter(project__in=projects)
        .exclude(project__status="Tentative")
        .exclude(funding_body="TBC")
    )

    # Determine total funding from each funding body across queried projects
    funding_per_body: dict[str, float] = defaultdict(float)
    for funding_body, budget in project_fundings.values_list("funding_body", "budget"):
        # Assume funding bodies are in the form "Name (...)" or simply "Name"
        body = (
            funding_body.split("(")[0].strip()
            if isinstance(funding_body, str)
            else "Internal"
        )
        funding_per_body[body] += float(budget)

    return funding_per_body

get_project_count_per_department(projects) ¤

Determine the number of projects per department.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, int]

A dictionary with department names and the number of projects per department.

Source code in main/analytics.py
 9
10
11
12
13
14
15
16
17
18
def get_project_count_per_department(projects: list[models.Project]) -> dict[str, int]:
    """Determine the number of projects per department.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with department names and the number of projects per department.
    """
    return Counter([project.department.name for project in projects])

get_project_count_per_faculty(projects) ¤

Determine the number of projects per faculty.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, int]

A dictionary with faculty names and the number of projects per faculty.

Source code in main/analytics.py
21
22
23
24
25
26
27
28
29
30
def get_project_count_per_faculty(projects: list[models.Project]) -> dict[str, int]:
    """Determine the number of projects per faculty.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with faculty names and the number of projects per faculty.
    """
    return Counter([project.department.faculty for project in projects])

get_project_count_per_funding_body(projects) ¤

Determine the number of projects per funding body.

Projects with funding body TBC are excluded, as are tentative projects.

Parameters:

Name Type Description Default
projects list[Project]

The list of projects.

required

Returns:

Type Description
dict[str, int]

A dictionary with funding bodies and the number of projects per body.

Source code in main/analytics.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def get_project_count_per_funding_body(
    projects: list[models.Project],
) -> dict[str, int]:
    """Determine the number of projects per funding body.

    Projects with funding body TBC are excluded, as are tentative projects.

    Args:
        projects: The list of projects.

    Returns:
        A dictionary with funding bodies and the number of projects per body.
    """
    # Select only funding instances that fund one of the input queried projects
    project_fundings = (
        models.Funding.objects.filter(project__in=projects)
        .exclude(project__status="Tentative")
        .exclude(funding_body="TBC")
    )

    # Assume funding bodies are named in the form "Name (...)" or simply "Name"
    return Counter(
        [
            fund.funding_body.split("(")[0].strip()
            if isinstance(fund.funding_body, str)
            else "Internal"
            for fund in project_fundings
        ]
    )

get_projects_active_between(from_date, to_date) ¤

Get the projects under active development within a time frame.

Exclude "Not done", which is projects whose start date has passed, but are not under development.

Parameters:

Name Type Description Default
from_date date

A datetime.date marking the start of the search interval.

required
to_date date

A datetime.date marking the end of the search interval.

required

Returns:

Type Description
list[Project]

The list of projects under active development within the time interval.

Source code in main/analytics.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def get_projects_active_between(from_date: date, to_date: date) -> list[models.Project]:
    """Get the projects under active development within a time frame.

    Exclude "Not done", which is projects whose start date has passed, but are not under
    development.

    Args:
        from_date: A datetime.date marking the start of the search interval.
        to_date: A datetime.date marking the end of the search interval.

    Returns:
        The list of projects under active development within the time interval.
    """
    return list(
        models.Project.objects.exclude(status="Not done").filter(
            start_date__lt=to_date, end_date__gte=from_date
        )
    )