# Connection Statistics

Since we have map and dots, they should be colored according to their connectivity status. Country data can be static (statistics is being imported manually via CSV file) or realtime (which is updated much more often from different sources).

![](https://3948420328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Mb6-VPCnfVXN___pYrz%2F-MbAkuQJo4Qg1OVTTQhe%2F-MbAw_G2SsAdxesaC1JQ%2Fproco_statistics.png?alt=media\&token=fbc516a1-ef1f-4898-9f22-0340f0fe2eb6)

To avoid complex calculations for every request we aggregate information for every day and week per school/country into corresponding models. Aggregation models are being updated in background every few hours.

### Measurement units

* **connectivity speed** - bytes per second
* **connectivity latency** - milliseconds

### Connectivity/Coverage mapping logic

Logic for connectivity and coverage calculations is described with draw\.io scheme. In general, every single school mapping depends from country overall status (for example realtime data availability or static connection information provided).

{% embed url="<https://drive.google.com/file/d/1ZC7GSNxwD2bVjVdbvOEI6LNwws6wCbjf/view?usp=sharing>" %}

### Connectivity aggregations

As already said, information is being aggregated on different levels from most granular (real time entries) to less detailed (weekly statistics for country). Entry point for data aggregations is `proco.connection_statistics.tasks.update_real_time_data` task.

1. First, realtime data is being fetched from all possible sources: UNICEF database, Brazil api (simet.nic.br), etc.
2. for every country aggregation task is being executed in parallel
3. For every school having realtime data, daily data is being populated (speed = average value from all realtime values for date)
4. All daily entries from schools are being aggregated to country daily statistics row. Same logic here, speed for country equals to average between all country schools.
5. Weekly school level aggregation is being performed: we update week entry for every school with average value from past seven days based on daily statistics.
6. Country level weekly entry is being updated. We take all weekly entries for school level and calculate average between them. And also, we update country connectivity and coverage statuses based on mapping above.
7. Invalidate all cached records that can be potentially affected: global statistics, countries list, country details, schools list.
