@brief Handle the ps request @param request the request @param send_response the send response @param send_streaming_response the send streaming response
| 928 | ///@param send_response the send response |
| 929 | ///@param send_streaming_response the send streaming response |
| 930 | void RestHandler::handle_ps(const json& request, |
| 931 | std::function<void(const json&)> send_response, |
| 932 | StreamResponseCallback send_streaming_response) { |
| 933 | try { |
| 934 | // Generate expires_at timestamp (1 hour from now) |
| 935 | auto now = std::chrono::system_clock::now(); |
| 936 | auto expires_time = now + std::chrono::hours(1); |
| 937 | auto expires_time_t = std::chrono::system_clock::to_time_t(expires_time); |
| 938 | auto expires_tp = std::chrono::system_clock::from_time_t(expires_time_t); |
| 939 | auto fractional_seconds = std::chrono::duration_cast<std::chrono::microseconds>(expires_time - expires_tp).count(); |
| 940 | |
| 941 | // Get local time and timezone offset |
| 942 | std::tm* local_tm = std::localtime(&expires_time_t); |
| 943 | std::tm* utc_tm = std::gmtime(&expires_time_t); |
| 944 | |
| 945 | // Calculate timezone offset in minutes |
| 946 | int offset_minutes = (local_tm->tm_hour - utc_tm->tm_hour) * 60 + (local_tm->tm_min - utc_tm->tm_min); |
| 947 | if (local_tm->tm_mday != utc_tm->tm_mday) { |
| 948 | offset_minutes += (local_tm->tm_mday > utc_tm->tm_mday) ? 1440 : -1440; |
| 949 | } |
| 950 | |
| 951 | std::stringstream expires_ss; |
| 952 | expires_ss.imbue(std::locale::classic()); // Use C locale to avoid commas |
| 953 | expires_ss << std::put_time(local_tm, "%Y-%m-%dT%H:%M:%S"); |
| 954 | expires_ss << "." << std::setfill('0') << std::setw(5) << (fractional_seconds / 40); // 5 decimal places |
| 955 | |
| 956 | // Format timezone offset |
| 957 | int offset_hours = offset_minutes / 60; |
| 958 | int offset_mins = abs(offset_minutes % 60); |
| 959 | expires_ss << (offset_minutes >= 0 ? "+" : "-") |
| 960 | << std::setfill('0') << std::setw(2) << abs(offset_hours) |
| 961 | << ":" << std::setfill('0') << std::setw(2) << offset_mins; |
| 962 | |
| 963 | std::string expires_at = expires_ss.str(); |
| 964 | |
| 965 | auto [new_current_model_tag, model_info] = supported_models.get_model_info(current_model_tag); |
| 966 | json response = { |
| 967 | {"models", json::array({ |
| 968 | { |
| 969 | {"name", current_model_tag}, |
| 970 | {"model", current_model_tag}, |
| 971 | {"size", model_info["size"]}, |
| 972 | {"details", model_info["details"]}, |
| 973 | {"expires_at", expires_at}, |
| 974 | } |
| 975 | })} |
| 976 | }; |
| 977 | // std::cout << "response: " << response.dump(4) << std::endl; |
| 978 | send_response(response); |
| 979 | } catch (const std::exception& e) { |
| 980 | json error_response = {{"error", e.what()}}; |
| 981 | send_response(error_response); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | ///@brief Handle the pull request |
| 986 | ///@param request the request |
no test coverage detected