<commit_before>/* Copyright © 2001-2014, Canal TP and/or its affiliates. All rights reserved.

This file is part of Navitia,
    the software to build cool stuff with public transport.

Hope you'll enjoy and contribute to this project,
    powered by Canal TP (www.canaltp.fr).
Help us simplify mobility and open public transport:
    a non ending quest to the responsive locomotion way of traveling!

LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Stay tuned using
twitter @navitia
channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org
https://groups.google.com/d/forum/navitia
www.navitia.io
*/

#include "departure_boards.h"

#include "request_handle.h"
#include "routing/dataraptor.h"
#include "routing/dataraptor.h"
#include "routing/get_stop_times.h"
#include "type/pb_converter.h"
#include "utils/functions.h"
#include "utils/paginate.h"

#include <boost/container/flat_set.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>

#include <utility>

namespace pt = boost::posix_time;

namespace navitia {
namespace timetables {

using RoutePointIdx = std::pair<routing::RouteIdx, routing::SpIdx>;

static bool is_last_stoptime(const nt::StopTime* stop_time) {
    return stop_time->vehicle_journey && !stop_time->vehicle_journey->stop_time_list.empty()
           && stop_time->vehicle_journey->stop_time_list.back().order() == stop_time->order();
}

static bool is_terminus_for_all_stop_times(const std::vector<routing::datetime_stop_time>& stop_times) {
    for (const auto& st : stop_times) {
        if (!is_last_stoptime(st.second)) {
            return false;
        }
    }
    return !stop_times.empty();
}

static void fill_first_last_date_times(PbCreator& pb_creator,
                                       pbnavitia::ScheduleStopTime* date_time,
                                       const std::pair<unsigned int, const navitia::type::StopTime*> stop_time,
                                       const boost::optional<const std::string> calendar_id) {
    const auto& st_calendar = navitia::StopTimeCalendar(stop_time.second, stop_time.first, calendar_id);
    pb_creator.fill(&st_calendar, date_time, 0);
}

static void fill_basic_objects(PbCreator& pb_creator,
                               pbnavitia::StopSchedule* schedule,
                               const type::Route* route,
                               const type::StopPoint* stop_point,
                               const uint32_t depth) {
    pb_creator.fill(stop_point, schedule->mutable_stop_point(), depth);
    auto m_route = schedule->mutable_route();

    pb_creator.fill(route, m_route, depth);
    if (route->line != nullptr) {
        auto m_line = m_route->mutable_line();
        pb_creator.fill(route->line, m_line, 0);
    }
}

static void render(PbCreator& pb_creator,
                   const std::map<RoutePointIdx, pbnavitia::ResponseStatus>& response_status,
                   const std::map<RoutePointIdx, vector_dt_st>& map_route_stop_point,
                   const std::map<RoutePointIdx, first_and_last_stop_time>& map_route_point_first_last_st,
                   const DateTime datetime,
                   const DateTime max_datetime,
                   const boost::optional<const std::string>& calendar_id,
                   const uint32_t depth) {
    pb_creator.action_period =
        pt::time_period(to_posix_time(datetime, *pb_creator.data), to_posix_time(max_datetime, *pb_creator.data));

    for (auto id_vec : map_route_stop_point) {
        auto schedule = pb_creator.add_stop_schedules();
        // Each schedule has a stop_point and a route
        const auto* stop_point = pb_creator.data->pt_data->stop_points[id_vec.first.second.val];
        const auto* route = pb_creator.data->pt_data->routes[id_vec.first.first.val];

        fill_basic_objects(pb_creator, schedule, route, stop_point, depth);

        auto pt_display_information = schedule->mutable_pt_display_informations();
        pb_creator.fill(route, pt_display_information, 0);

        // Now we fill the date_times
        for (auto dt_st : id_vec.second) {
            const auto& st_calendar = navitia::StopTimeCalendar(dt_st.second, dt_st.first, calendar_id);
            // terminus or partial terminus
            if (is_last_stoptime(st_calendar.stop_time)) {
                continue;
            }
            auto date_time = schedule->add_date_times();
            pb_creator.fill(&st_calendar, date_time, 0);
            if (dt_st.second != nullptr) {
                auto vj = dt_st.second->vehicle_journey;
                if (vj != nullptr) {
                    for (const auto& comment : pb_creator.data->pt_data->comments.get(*vj)) {
                        pb_creator.fill(&comment, date_time->mutable_properties()->add_notes(), 0);
                    }
                }
            }
        }

        // add first and last datetime
        const auto& first_last_it = map_route_point_first_last_st.find(id_vec.first);
        if (first_last_it != map_route_point_first_last_st.end()) {
            // first date time
            if (first_last_it->second.first) {
                fill_first_last_date_times(pb_creator, schedule->mutable_first_datetime(),
                                           *(first_last_it->second.first), calendar_id);
            }
            // last date time
            if (first_last_it->second.second) {
                fill_first_last_date_times(pb_creator, schedule->mutable_last_datetime(),
                                           *(first_last_it->second.second), calendar_id);
            }
        }

        // response status
        const auto& it = response_status.find(id_vec.first);
        if (it != response_status.end()) {
            schedule->set_response_status(it->second);
        }
    }
}

static void render(PbCreator& pb_creator,
                   const std::map<routing::JppIdx, pbnavitia::ResponseStatus>& response_status,
                   const std::map<routing::JppIdx, vector_dt_st>& map_route_stop_point,
                   const std::map<routing::JppIdx, first_and_last_stop_time>& map_route_point_first_last_st,
                   const DateTime& datetime,
                   const DateTime& max_datetime,
                   const boost::optional<const std::string> calendar_id,
                   const uint32_t depth) {
    pb_creator.action_period =
        pt::time_period(to_posix_time(datetime, *pb_creator.data), to_posix_time(max_datetime, *pb_creator.data));

    for (const auto& id_vec : map_route_stop_point) {
        auto schedule = pb_creator.add_terminus_schedules();

        // Each schedule has a stop_point and a route
        const auto& jpp_dir = pb_creator.data->dataRaptor->jp_container.get(id_vec.first);
        const auto& jp_dir = pb_creator.data->dataRaptor->jp_container.get(jpp_dir.jp_idx);
        const type::Route* route = pb_creator.data->pt_data->routes[jp_dir.route_idx.val];
        const type::StopPoint* stop_point = pb_creator.data->pt_data->stop_points[jpp_dir.sp_idx.val];

        fill_basic_objects(pb_creator, schedule, route, stop_point, depth);

        auto pt_display_information = schedule->mutable_pt_display_informations();
        pb_creator.fill(route, pt_display_information, 0);
        const auto& jpc = pb_creator.data->dataRaptor->jp_container;
        pt_display_information->set_direction(
            pb_creator.data->pt_data->stop_points[jpc.get(jp_dir.jpps.back()).sp_idx.val]->stop_area->name);

        // Now we fill the date_times
        for (auto dt_st : id_vec.second) {
            const auto& st_calendar = navitia::StopTimeCalendar(dt_st.second, dt_st.first, calendar_id);
            // terminus or partial terminus
            if (is_last_stoptime(st_calendar.stop_time)) {
                continue;
            }
            auto date_time = schedule->add_date_times();
            pb_creator.fill(&st_calendar, date_time, 0);
            if (dt_st.second != nullptr) {
                auto vj = dt_st.second->vehicle_journey;
                if (vj != nullptr) {
                    for (const auto& comment : pb_creator.data->pt_data->comments.get(*vj)) {
                        pb_creator.fill(&comment, date_time->mutable_properties()->add_notes(), 0);
                    }
                }
            }
        }

        // add first and last datetime
        const auto& first_last_it = map_route_point_first_last_st.find(id_vec.first);
        if (first_last_it != map_route_point_first_last_st.end()) {<commit_msg>kraken: second refactoring<commit_after>/* Copyright © 2001-2014, Canal TP and/or its affiliates. All rights reserved.

This file is part of Navitia,
    the software to build cool stuff with public transport.

Hope you'll enjoy and contribute to this project,
    powered by Canal TP (www.canaltp.fr).
Help us simplify mobility and open public transport:
    a non ending quest to the responsive locomotion way of traveling!

LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Stay tuned using
twitter @navitia
channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org
https://groups.google.com/d/forum/navitia
www.navitia.io
*/

#include "departure_boards.h"

#include "request_handle.h"
#include "routing/dataraptor.h"
#include "routing/dataraptor.h"
#include "routing/get_stop_times.h"
#include "type/pb_converter.h"
#include "utils/functions.h"
#include "utils/paginate.h"

#include <boost/container/flat_set.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>

#include <utility>

namespace pt = boost::posix_time;

namespace navitia {
namespace timetables {

using RoutePointIdx = std::pair<routing::RouteIdx, routing::SpIdx>;

static bool is_last_stoptime(const nt::StopTime* stop_time) {
    return stop_time->vehicle_journey && !stop_time->vehicle_journey->stop_time_list.empty()
           && stop_time->vehicle_journey->stop_time_list.back().order() == stop_time->order();
}

static bool is_terminus_for_all_stop_times(const std::vector<routing::datetime_stop_time>& stop_times) {
    for (const auto& st : stop_times) {
        if (!is_last_stoptime(st.second)) {
            return false;
        }
    }
    return !stop_times.empty();
}

static void fill_date_times(PbCreator& pb_creator,
                            pbnavitia::StopSchedule* schedule,
                            const std::pair<unsigned int, const navitia::type::StopTime*> dt_st,
                            const boost::optional<const std::string> calendar_id) {
    const auto& st_calendar = navitia::StopTimeCalendar(dt_st.second, dt_st.first, calendar_id);
    // terminus or partial terminus
    if (is_last_stoptime(st_calendar.stop_time)) {
        return;
    }
    auto date_time = schedule->add_date_times();
    pb_creator.fill(&st_calendar, date_time, 0);
    if (dt_st.second != nullptr) {
        auto vj = dt_st.second->vehicle_journey;
        if (vj != nullptr) {
            for (const auto& comment : pb_creator.data->pt_data->comments.get(*vj)) {
                pb_creator.fill(&comment, date_time->mutable_properties()->add_notes(), 0);
            }
        }
    }
}

static void fill_first_last_date_times(PbCreator& pb_creator,
                                       pbnavitia::ScheduleStopTime* date_time,
                                       const std::pair<unsigned int, const navitia::type::StopTime*> stop_time,
                                       const boost::optional<const std::string> calendar_id) {
    const auto& st_calendar = navitia::StopTimeCalendar(stop_time.second, stop_time.first, calendar_id);
    pb_creator.fill(&st_calendar, date_time, 0);
}

static void fill_basic_objects(PbCreator& pb_creator,
                               pbnavitia::StopSchedule* schedule,
                               const type::Route* route,
                               const type::StopPoint* stop_point,
                               const uint32_t depth) {
    pb_creator.fill(stop_point, schedule->mutable_stop_point(), depth);
    auto m_route = schedule->mutable_route();

    pb_creator.fill(route, m_route, depth);
    if (route->line != nullptr) {
        auto m_line = m_route->mutable_line();
        pb_creator.fill(route->line, m_line, 0);
    }
}

static void render(PbCreator& pb_creator,
                   const std::map<RoutePointIdx, pbnavitia::ResponseStatus>& response_status,
                   const std::map<RoutePointIdx, vector_dt_st>& map_route_stop_point,
                   const std::map<RoutePointIdx, first_and_last_stop_time>& map_route_point_first_last_st,
                   const DateTime datetime,
                   const DateTime max_datetime,
                   const boost::optional<const std::string>& calendar_id,
                   const uint32_t depth) {
    pb_creator.action_period =
        pt::time_period(to_posix_time(datetime, *pb_creator.data), to_posix_time(max_datetime, *pb_creator.data));

    for (auto id_vec : map_route_stop_point) {
        auto schedule = pb_creator.add_stop_schedules();
        // Each schedule has a stop_point and a route
        const auto* stop_point = pb_creator.data->pt_data->stop_points[id_vec.first.second.val];
        const auto* route = pb_creator.data->pt_data->routes[id_vec.first.first.val];

        fill_basic_objects(pb_creator, schedule, route, stop_point, depth);

        auto pt_display_information = schedule->mutable_pt_display_informations();
        pb_creator.fill(route, pt_display_information, 0);

        // Now we fill the date_times
        for (auto dt_st : id_vec.second) {
            fill_date_times(pb_creator, schedule, dt_st, calendar_id);
        }

        // add first and last datetime
        const auto& first_last_it = map_route_point_first_last_st.find(id_vec.first);
        if (first_last_it != map_route_point_first_last_st.end()) {
            // first date time
            if (first_last_it->second.first) {
                fill_first_last_date_times(pb_creator, schedule->mutable_first_datetime(),
                                           *(first_last_it->second.first), calendar_id);
            }
            // last date time
            if (first_last_it->second.second) {
                fill_first_last_date_times(pb_creator, schedule->mutable_last_datetime(),
                                           *(first_last_it->second.second), calendar_id);
            }
        }

        // response status
        const auto& it = response_status.find(id_vec.first);
        if (it != response_status.end()) {
            schedule->set_response_status(it->second);
        }
    }
}

static void render(PbCreator& pb_creator,
                   const std::map<routing::JppIdx, pbnavitia::ResponseStatus>& response_status,
                   const std::map<routing::JppIdx, vector_dt_st>& map_route_stop_point,
                   const std::map<routing::JppIdx, first_and_last_stop_time>& map_route_point_first_last_st,
                   const DateTime& datetime,
                   const DateTime& max_datetime,
                   const boost::optional<const std::string> calendar_id,
                   const uint32_t depth) {
    pb_creator.action_period =
        pt::time_period(to_posix_time(datetime, *pb_creator.data), to_posix_time(max_datetime, *pb_creator.data));

    for (const auto& id_vec : map_route_stop_point) {
        auto schedule = pb_creator.add_terminus_schedules();

        // Each schedule has a stop_point and a route
        const auto& jpp_dir = pb_creator.data->dataRaptor->jp_container.get(id_vec.first);
        const auto& jp_dir = pb_creator.data->dataRaptor->jp_container.get(jpp_dir.jp_idx);
        const type::Route* route = pb_creator.data->pt_data->routes[jp_dir.route_idx.val];
        const type::StopPoint* stop_point = pb_creator.data->pt_data->stop_points[jpp_dir.sp_idx.val];

        fill_basic_objects(pb_creator, schedule, route, stop_point, depth);

        auto pt_display_information = schedule->mutable_pt_display_informations();
        pb_creator.fill(route, pt_display_information, 0);
        const auto& jpc = pb_creator.data->dataRaptor->jp_container;
        pt_display_information->set_direction(
            pb_creator.data->pt_data->stop_points[jpc.get(jp_dir.jpps.back()).sp_idx.val]->stop_area->name);

        // Now we fill the date_times
        for (auto dt_st : id_vec.second) {
            fill_date_times(pb_creator, schedule, dt_st, calendar_id);
        }

        // add first and last datetime
        const auto& first_last_it = map_route_point_first_last_st.find(id_vec.first);
        if (first_last_it != map_route_point_first_last_st.end()) {
{'fraction_non_alphanumeric': 0.09933997091397248, 'fraction_numerical': 0.0024611254055263453, 'mean_word_length': 2.723240316534777, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 137, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2220012', 'n_tokens_mistral': 5462, 'n_tokens_neox': 5347, 'n_words': 1162}