Get the latest data for each country

The traffic data is now filtered following the business rules. It still contains the data for all the available years per country. You are only interested in the latest traffic data.

You know a simple way to get what you want. First, you group the data using the three-letter country code. Since the data is already sorted descending by year, you can get the first row of each group to get the latest available traffic data for each country. Nifty!


*** Settings ***
Documentation       Inhuman Insurance, Inc. Artificial Intelligence System robot.
...                 Produces traffic data work items.

Library             Collections
Library             RPA.HTTP
Library             RPA.JSON
Library             RPA.Tables

*** Variables ***
${TRAFFIC_JSON_FILE_PATH}=      ${OUTPUT_DIR}${/}traffic.json

*** Tasks ***
Produce traffic data work items
    Download traffic data
    ${traffic_data}=    Load traffic data as table
    ${filtered_data}=    Filter and sort traffic data    ${traffic_data}
    ${filtered_data}=    Get latest data by country    ${filtered_data}

*** Keywords ***
Download traffic data
    ...    overwrite=True

Load traffic data as table
    ${json}=    Load JSON from file    ${TRAFFIC_JSON_FILE_PATH}
    ${table}=    Create Table    ${json}[value]
    RETURN    ${table}

Filter and sort traffic data
    [Arguments]    ${table}
    ${max_rate}=    Set Variable    ${5.0}
    ${rate_key}=    Set Variable    NumericValue
    ${gender_key}=    Set Variable    Dim1
    ${both_genders}=    Set Variable    BTSX
    ${year_key}=    Set Variable    TimeDim
    Filter Table By Column    ${table}    ${rate_key}    <    ${max_rate}
    Filter Table By Column    ${table}    ${gender_key}    ==    ${both_genders}
    Sort Table By Column    ${table}    ${year_key}    False
    RETURN    ${table}

Get latest data by country
    [Arguments]    ${table}
    ${country_key}=    Set Variable    SpatialDim
    ${table}=    Group Table By Column    ${table}    ${country_key}
    ${latest_data_by_country}=    Create List
    FOR    ${group}    IN    @{table}
        ${first_row}=    Pop Table Row    ${group}
        Append To List    ${latest_data_by_country}    ${first_row}
    RETURN    ${latest_data_by_country}
  • The Get latest data by country keyword communicates the intent of the code by using clear naming.
  • The Group Table By Column keyword handles the grouping by country.
  • The Pop Table Row keyword gets the first row of a table.
  • This RPA.Tables library feels quite helpful indeed!

Preview grouped data