Collecting data with iTop in action

  • avril 12, 2023

In this tutorial, we will cover data synchronization with iTop and see how to adapt it to a specific need thanks to data collectors.

We will use the open APIs available on https://api.gouv.fr/ in order to synchronise national education institutions with our iTop server.
You can later collect more data using other APIs of the website.

This site references public service APIs, made available to local authorities, government departments and companies to build IT services for everyone.

api.gouv.fr

⏱️Approximate duration of the tutorial: 4 hours

Key steps in this tutorial

1/ Data structuring
2/ Creation of an iTop extension
3/ Creation of an iTop data collector
4/ Industrialisation of the solution
5/ Exercise
6/ Quizz

Prerequisites

An iTop community server version 2.7 or higher installed locally or on a remote machine on your network with user rights to make changes on setup.
A PHP runtime environment compatible with the iTop version and some knowledge of PHP.
A text editor like Notepad++ or similar.

Principle of synchronisation with iTop

Scheme of operation

The role of the collector is to convert external data to CSV format, to update the replicas with this data and finally to synchronise them with iTop data.
An abstraction layer handles the import and synchronisation phase for us.


For our specific REST collector, we simply need to extract data from the API and convert it to CSV.

Tutorial steps

1/ Data structuring

We would like to synchronise the list of French national education institutions with iTop. The data will be read-only on the iTop side.
We will use the Education Directory API: https://api.gouv.fr/les-api/api-annuaire-education.

The analysis

The first step consists of analysing the API and identifying the data we want to upload to iTop.
By going to the API overview page, we can see the main data available.

Overview of data provided by the API

Selection of data to be synchronised

We select the following information from the institutions:

· Unique identifier (UAI)
· Name
· Mailing address
· Type of institution
· Public or private information
· Academy code
· Number of pupils
· Availability of a catering service
· Open date

Several data types (text, number, date, enumeration and Boolean) will be synchronised.

Data source

The API request we will use is as follows:

GET /catalog/datasets/fr-en-annuaire-education/records

We will use select, limit and offset parameters in order to select only the data we are interested in, so that we can go through all the records in several iterations. It is indeed impossible to select all the institutions, as the limit of the number of results set by the API is 100 institutions per query.

You can run some tests to get familiar with the API here : https://api.gouv.fr/documentation/api-annuaire-education.

API testing page

2/ Creation of an iTop extension

We will create an extension to allow us to customise the iTop data model and thus be able to collect data from educational institutions. We will extend the location class and add specific information for national education institutions. We will also be making some changes to the iTop side menu to accommodate the menus associated with the api.gouv.fr APIs.

Skeleton generation

Start by generating an extension skeleton by entering the following information in the creation form.
Click on the button Generate and then download the generated archive.
Unzip the downloaded archive into the extensions folder on the iTop server.

Company name: MyCompany
Extension name: gouv-api-extension
Extension label: api.gouv.fr data synchronisation
Version: 1.0.0
Dependencies: itop-structure/3.0.0

Skeleton generation form

To go further in the creation of extensions, visit the official documentation.

Creation of the data model class

We will create our class to store school information. We choose to derive the class Location to inherit the location data.

General information

Initializing the class Establishment which derives from the class Location.

Add the general class information to the data model file as follows:

datamodel.gouv-edu-extension.xml

XMgggL

💡The tag <icon> allows you to associate an image with the class.

Adding attributes to the data model class

Declaration of the attributes of the Establishment class. For each of these attributes we specify its data type xsi:type="AttributeXXX".
All attributes identifier, type and private_public are set as mandatory.

Add the attributes of the class to the data model file as follows:

datamodel.gouv-edu-extension.xml

datamodel.gouv-edu-extension.xml

Add the attributes of the class to the data model file as follows:
By default, only the English translation file is created, add other languages if required.

en.dict.gouv-api-extension.php

en.dict.gouv-api-extension.php

💡The + symbol allows you to attach a hovering tooltip to the form for a given attribute: ‘Class:Establishment/Attribute:identifier+

Adding presentation data

Declaration of the different display modes of our class.
Section détails sets the display of the class when viewing or editing an object.
Section list defines the attributes of the class used as columns in lists.
Section search defines the attributes used as criteria for searches.

Add the display modes as follows:

datamodel.gouv-edu-extension.xml

XML
Adding reconciliation data

Declaration of the reconciliation attributes of the class.
We use the school ID (UAI) for reconciliation because it is unique.

🎓Reconciliation defines one or more attributes that ensure the identification of an object.

Add the class reconciliation information to the data model file as follows:

datamodel.gouv-edu-extension.xml

datamodel.gouv-edu-extension.xml

iTop menu customisation

We will organise data retrieved from api.gouv.fr in a menu integrated in the iTop side menu.
You can then add as many entries to this menu as you implement APIs.

api.gouv.fr menu overview

Add the class menu information to the data model file as follows:

datamodel.gouv-edu-extension.xml

datamodel.gouv-edu-extension.xml

💡Tag <decoration_classes> allows you to set an icon for the menu group.

Codes for the icons are available on Font Awesome website: https://fontawesome.com/.

Add the translations of your menus to the internationalisation file as follows:

en.dict.gouv-api-extension.php

en.dict.gouv-api-extension.php

Installing the extension

Copy the extension to the extensions folder on your iTop server.
Add write permissions to the iTop configuration file located in conf/production/config-itop.php then go to the following web address of your iTop: http://{adresse-serveur-itop}/setup in a web browser to start the installation.

Confirm the installation steps and make sure that our new extension is checked at the end of the installation wizard.

Extension selection screen

And that’s it! You can now host national education institutions in your iTop.
Try to create an institution to check that everything is up and running.

Navigate to the api.gouv.fr > National education establishments menu and click on create an establishment.

Form for the creation of a new establishment

In the next chapter, we will see how to synchronise data using a collector.

3/ Creation of an iTop data collector

Installation iTop data collector

Download the extension data collector base.
And unzip the contents into a folder of your choice. (Do not unzip into the folder www of your server to avoid exposing its contents)
It is perfectly possible to unzip on another machine on the network as long as it has a PHP 7.x runtime environment.

Read all about Data collector base in our official documentation.

Data source configuration

Data source configuration is described in JSON format.
The easiest way to do this is to set up the data source in iTop’s console user interface and transform it into JSON format via a script provided by the iTop data collector extension.

Create a new data source in the configuration menu> sources of synchronisation data.
Enter the following information and leave the other fields unchanged.

Name: Synchro national education establishments
Description: https://api.gouv.fr/documentation/api-annuaire-education
Target class: Establishment
Contact to notify: Your_email
Datatable: data_national_education_establishment

Data source configuration screen (Properties)

Choose the attribute identified for reconciliation.
Choose the reconciliation key Full name for the Owner organization attribute, which will allow us to indicate the organisation by name and thus be more readable.

Data source configuration screen (Attributes)

Then generate the JSON definition of this data source in the collectors directory.

Shell

For further information on this topic, please refer to our official documentation.

Collector settings

We need to prepare a settings file for the use of the collector.

Duplicate the configuration file params.distrib.xml and name it params.local.xml.
Update the configuration file with your settings as follows:

conf/params.local.xml

conf/params.local.xml

Collector class creation

Creation of the class DataEducationCollector derived from the class Collector.
The presence of curl has to be checked and you initialise it by deleting the old data collected.

DataEducationCollector.class.inc.php

DataEducationCollector.class.inc.php

Declare the main function for the API call.
This method returns a boolean to indicate that there is still data to collect.
You iterate by bunches of 100 elements (max defined by the API) and you limit yourself to 300 results for the moment.

DataEducationCollector.class.inc.php

DataEducationCollector.class.inc.php

The basic mechanism of collection.
The prepare function is called to collect a batch and then the records of this batch are unstacked by adding them in csv files respecting a maximum data size.
You will repeat the process as long as there is data to be collected.

DataEducationCollector.class.inc.php

DataEducationCollector.class.inc.php

Collector registration

The next step is to declare the new collector to the orchestrator.

Create a file main.php to the root of the collectors folder and initialise it as follows:

collectors/main.php

collectors/main.php

Launching the synchronisation

You can now trigger the synchronisation with the following command.

Shell

Browse the menu api.gouv.fr > National educational establishments.
You should get your 300 establishments synchronised.

4/ Industrialisation of the solution

We now want to finalise our solution by externalising parameters from our collector into the settings file.

Adding parameters

Add class members as follows:

DataEducationCollector.class.inc.php

DataEducationCollector.class.inc.php

Versioning the collector

Create a module declaration file at the collectors folder root and initialise it as follows:

collectors/module.gouv-api-collectors-extension.php

collectors/module.gouv-api-collectors-extension.php

5/ Exercise on your own!

List of AFPA establishments throughout France
https://api.gouv.fr/documentation/api_etablissements_publics

Add a new entry in the sidebar menu.

6/ Quizz

– How to create an extension skeleton quickly
🔲 By creating it with a text editor
🔲 Thanks to the wiki pages
🔲 Based on another extension

– What is reconciliation?
🔲 The primary key of a class
🔲 Displaying the intelligible name of an object
🔲 One or a set of identifying attributes

– Where are translations of the text defined?
🔲 In the class tag of the data model
🔲 In the dictionaries tag of the data model
🔲 In the PHP function Dict::Add

– How to install an extension
🔲 With iTop hub
🔲 By running an installation script
🔲 By putting the extension in the extensions folder of the iTop server.

– How to release a version of your collector
🔲 By naming the folder with the version
🔲 By creating a module file
🔲 By pushing it to Git

Blog Post

Related Articles

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique.

La collecte de données avec iTop en action

avril 12, 2023
Dans ce tutoriel, nous allons aborder la synchronisation de données avec iTop et nous allons voir comment l’adapter à...

Cybersécurité des collectivités : menaces et solutions

mars 12, 2024
Les enjeux de sécurité évoluent rapidement, et les collectivités font face à des risques uniques en matière de...

Use case: TIMS steps up to the next level with iTop Professional

mars 9, 2023
Use case: TIMS steps up to the next level with iTop Professional by Denis Perotto | Mar 10, 2023 | Extensibility, iTop,...
Blog Post CTA

H2 Heading Module

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique.