LCOV - code coverage report
Current view: top level - src/MediaService - MediaHandler.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 28 67.9 %
Date: 2025-11-04 00:47:34 Functions: 2 4 50.0 %

          Line data    Source code
       1             : #ifndef SOCIAL_NETWORK_MICROSERVICES_SRC_MEDIASERVICE_MEDIAHANDLER_H_
       2             : #define SOCIAL_NETWORK_MICROSERVICES_SRC_MEDIASERVICE_MEDIAHANDLER_H_
       3             : 
       4             : #include <chrono>
       5             : #include <iostream>
       6             : #include <string>
       7             : 
       8             : #include "../../gen-cpp/MediaService.h"
       9             : #include "../logger.h"
      10             : #include "../tracing.h"
      11             : 
      12             : // 2018-01-01 00:00:00 UTC
      13             : #define CUSTOM_EPOCH 1514764800000
      14             : 
      15             : namespace social_network {
      16             : 
      17             : class MediaHandler : public MediaServiceIf {
      18             :  public:
      19           1 :   MediaHandler() = default;
      20           0 :   ~MediaHandler() override = default;
      21             : 
      22             :   void ComposeMedia(std::vector<Media> &_return, int64_t,
      23             :                     const std::vector<std::string> &,
      24             :                     const std::vector<int64_t> &,
      25             :                     const std::map<std::string, std::string> &) override;
      26             : 
      27             :  private:
      28             : };
      29             : 
      30         200 : void MediaHandler::ComposeMedia(
      31             :     std::vector<Media> &_return, int64_t req_id,
      32             :     const std::vector<std::string> &media_types,
      33             :     const std::vector<int64_t> &media_ids,
      34             :     const std::map<std::string, std::string> &carrier) {
      35             :   // 新增:记录收到的请求
      36         800 :   LOG(info) << "Received ComposeMedia request [req_id=" << req_id 
      37         400 :             << ", media_ids count=" << media_ids.size() 
      38         400 :             << ", media_types count=" << media_types.size() << "]";
      39             : 
      40             :   // Initialize a span
      41         400 :   TextMapReader reader(carrier);
      42         400 :   std::map<std::string, std::string> writer_text_map;
      43         400 :   TextMapWriter writer(writer_text_map);
      44         400 :   auto parent_span = opentracing::Tracer::Global()->Extract(reader);
      45         400 :   auto span = opentracing::Tracer::Global()->StartSpan(
      46         800 :       "compose_media_server", {opentracing::ChildOf(parent_span->get())});
      47         200 :   opentracing::Tracer::Global()->Inject(span->context(), writer);
      48             : 
      49         200 :   if (media_types.size() != media_ids.size()) {
      50           0 :     ServiceException se;
      51           0 :     se.errorCode = ErrorCode::SE_THRIFT_HANDLER_ERROR;
      52             :     se.message =
      53           0 :         "The lengths of media_id list and media_type list are not equal";
      54           0 :     throw se;
      55             :   }
      56             : 
      57             :   // 新增:校验通过
      58         400 :   LOG(info) << "ComposeMedia parameter validation passed [req_id=" << req_id << "]";
      59             : 
      60         200 :   for (int i = 0; i < media_ids.size(); ++i) {
      61           0 :     Media new_media;
      62           0 :     new_media.media_id = media_ids[i];
      63           0 :     new_media.media_type = media_types[i];
      64           0 :     _return.emplace_back(new_media);
      65             :   }
      66             : 
      67             :   // 新增:方法结束日志
      68         800 :   LOG(info) << "ComposeMedia completed [req_id=" << req_id 
      69         400 :             << ", return media count=" << _return.size() << "]";
      70             : 
      71         200 :   span->Finish();
      72         200 : }
      73             : 
      74             : }  // namespace social_network
      75             : 
      76             : #endif  // SOCIAL_NETWORK_MICROSERVICES_SRC_MEDIASERVICE_MEDIAHANDLER_H_

Generated by: LCOV version 1.12