wx_sdk/mp/
datacube.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    // access_token::AccessTokenProvider,
5    error::CommonResponse,
6    wechat::WxApiRequestBuilder,
7    SdkResult,
8};
9
10// pub mod ad;
11#[derive(Debug, Serialize, Deserialize)]
12pub struct ListRes<T> {
13    pub list: Vec<T>,
14}
15
16// 冲突的实现,
17// impl<T> Into<WxResult<Vec<T>>> for WxResult<ListRes<T>> {
18//     fn into(self) -> WxResult<Vec<T>> {
19//         self.map(|ls| ls.list)
20//     }
21// }
22
23impl<T> From<ListRes<T>> for Vec<T> {
24    fn from(t: ListRes<T>) -> Self {
25        t.list
26    }
27}
28
29#[derive(Debug, Serialize)]
30pub struct TimeSpan {
31    pub begin_date: String,
32    pub end_date: String,
33}
34
35#[derive(Debug, Serialize, Deserialize)]
36pub struct UserNewCancel {
37    pub ref_date: String,
38    pub user_source: i32,
39    pub new_user: i32,
40    pub cancel_user: i32,
41}
42
43#[derive(Debug, Serialize, Deserialize)]
44pub struct UserCumulate {
45    pub ref_date: String,
46    pub cumulate_user: i64,
47}
48
49#[derive(Debug, Serialize, Deserialize)]
50pub struct ArticleSummary {
51    pub ref_date: String,
52    pub msgid: String,
53    pub title: String,
54    pub int_page_read_user: i32,
55    pub int_page_read_count: i32,
56    pub ori_page_read_user: i32,
57    pub ori_page_read_count: i32,
58    pub share_user: i32,
59    pub share_count: i32,
60    pub add_to_fav_user: i32,
61    pub add_to_fav_count: i32,
62}
63
64#[derive(Debug, Serialize, Deserialize)]
65pub struct ArticleDetail {
66    pub stat_date: String,
67    pub target_user: i64,
68    pub int_page_read_user: i32,
69    pub int_page_read_count: i32,
70    pub ori_page_read_user: i32,
71    pub ori_page_read_count: i32,
72    pub share_user: i32,
73    pub share_count: i32,
74    pub add_to_fav_user: i32,
75    pub add_to_fav_count: i32,
76    pub int_page_from_session_read_user: i32,
77    pub int_page_from_session_read_count: i32,
78    pub int_page_from_hist_msg_read_user: i32,
79    pub int_page_from_hist_msg_read_count: i32,
80    pub int_page_from_feed_read_user: i32,
81    pub int_page_from_feed_read_count: i32,
82    pub int_page_from_friends_read_user: i32,
83    pub int_page_from_friends_read_count: i32,
84    pub int_page_from_other_read_user: i32,
85    pub int_page_from_other_read_count: i32,
86    pub feed_share_from_session_user: i32,
87    pub feed_share_from_session_cnt: i32,
88    pub feed_share_from_feed_user: i32,
89    pub feed_share_from_feed_cnt: i32,
90    pub feed_share_from_other_user: i32,
91    pub feed_share_from_other_cnt: i32,
92}
93
94#[derive(Debug, Serialize, Deserialize)]
95pub struct ArticleTotal {
96    pub ref_date: String,
97    pub msgid: String,
98    pub title: String,
99    pub details: Vec<ArticleDetail>,
100}
101
102#[derive(Debug, Serialize, Deserialize)]
103pub struct UserRead {
104    pub ref_date: String,
105    pub user_source: i32,
106    pub int_page_read_user: i32,
107    pub int_page_read_count: i32,
108    pub ori_page_read_user: i32,
109    pub ori_page_read_count: i32,
110    pub share_user: i32,
111    pub share_count: i32,
112    pub add_to_fav_user: i32,
113    pub add_to_fav_count: i32,
114}
115
116#[derive(Debug, Serialize, Deserialize)]
117pub struct UserReadHour {
118    pub ref_date: String,
119    pub ref_hour: i32,
120    pub user_source: i32,
121    pub int_page_read_user: i32,
122    pub int_page_read_count: i32,
123    pub ori_page_read_user: i32,
124    pub ori_page_read_count: i32,
125    pub share_user: i32,
126    pub share_count: i32,
127    pub add_to_fav_user: i32,
128    pub add_to_fav_count: i32,
129}
130
131// pub async fn get_analyze<T: AccessTokenProvider, U: DeserializeOwned>(time: &TimeSpan, sdk: &WxSdk<T>, url: &'static str) -> SdkResult<ListRes<U>> {
132//      let res: CommonResponse<ListRes<U>> = sdk.wx_post(url).await?.json(time).send().await?.json().await?;
133//     res.into()
134// }
135
136#[derive(Debug, Serialize, Deserialize)]
137pub struct UserShare {
138    pub ref_date: String,
139    pub share_scene: i32,
140    pub share_count: i32,
141    pub share_user: i32,
142}
143
144#[derive(Debug, Serialize, Deserialize)]
145pub struct UserShareHour {
146    pub ref_date: String,
147    pub ref_hour: i32,
148    pub share_scene: i32,
149    pub share_count: i32,
150    pub share_user: i32,
151}
152
153#[derive(Debug, Serialize, Deserialize)]
154pub struct UpstreamMsg {
155    pub ref_date: String,
156    pub msg_type: i32,
157    pub msg_user: i32,
158    pub msg_count: i32,
159}
160
161#[derive(Debug, Serialize, Deserialize)]
162pub struct UpstreamMsgHour {
163    pub ref_date: String,
164    pub ref_hour: i32,
165    pub msg_type: i32,
166    pub msg_user: i32,
167    pub msg_count: i32,
168}
169
170#[derive(Debug, Serialize, Deserialize)]
171pub struct UpstreamMsgDist {
172    pub ref_date: String,
173    pub count_interval: i32,
174    pub msg_user: i32,
175}
176
177#[derive(Debug, Serialize, Deserialize)]
178pub struct ApiAnalysis {
179    pub ref_date: String,
180    pub callback_count: i32,
181    pub fail_count: i32,
182    pub total_time_cost: i64,
183    pub max_time_cost: i64,
184}
185
186#[derive(Debug, Serialize, Deserialize)]
187pub struct ApiAnalysisHour {
188    pub ref_date: String,
189    pub ref_hour: i32,
190    pub callback_count: i32,
191    pub fail_count: i32,
192    pub total_time_cost: i64,
193    pub max_time_cost: i64,
194}
195
196pub struct DataCubeModule<'a, T: WxApiRequestBuilder>(pub(crate) &'a T);
197impl<'a, T: WxApiRequestBuilder> DataCubeModule<'a, T> {
198    pub async fn get_user_summary(&self, time: &TimeSpan) -> SdkResult<ListRes<UserNewCancel>> {
199        let base_url = "https://api.weixin.qq.com/datacube/getusersummary";
200        let sdk = self.0;
201        let res: CommonResponse<ListRes<UserNewCancel>> = sdk
202            .wx_post(base_url)
203            .await?
204            .json(time)
205            .send()
206            .await?
207            .json()
208            .await?;
209        res.into()
210    }
211    pub async fn get_user_cumulate(&self, time: &TimeSpan) -> SdkResult<ListRes<UserCumulate>> {
212        let base_url = "https://api.weixin.qq.com/datacube/getusercumulate";
213        let sdk = self.0;
214        let res: CommonResponse<ListRes<UserCumulate>> = sdk
215            .wx_post(base_url)
216            .await?
217            .json(time)
218            .send()
219            .await?
220            .json()
221            .await?;
222        res.into()
223    }
224    pub async fn get_article_summary(&self, time: &TimeSpan) -> SdkResult<ListRes<ArticleSummary>> {
225        let base_url = "https://api.weixin.qq.com/datacube/getarticlesummary";
226        let sdk = self.0;
227        let res: CommonResponse<ListRes<ArticleSummary>> = sdk
228            .wx_post(base_url)
229            .await?
230            .json(time)
231            .send()
232            .await?
233            .json()
234            .await?;
235        res.into()
236    }
237    pub async fn get_article_total(&self, time: &TimeSpan) -> SdkResult<ListRes<ArticleTotal>> {
238        let base_url = "https://api.weixin.qq.com/datacube/getarticletotal";
239        let sdk = self.0;
240        let res: CommonResponse<ListRes<ArticleTotal>> = sdk
241            .wx_post(base_url)
242            .await?
243            .json(time)
244            .send()
245            .await?
246            .json()
247            .await?;
248        res.into()
249    }
250    pub async fn get_user_read(&self, time: &TimeSpan) -> SdkResult<ListRes<UserRead>> {
251        let base_url = "https://api.weixin.qq.com/datacube/getuserread";
252        let sdk = self.0;
253        let res: CommonResponse<ListRes<UserRead>> = sdk
254            .wx_post(base_url)
255            .await?
256            .json(time)
257            .send()
258            .await?
259            .json()
260            .await?;
261        res.into()
262    }
263    pub async fn get_user_share(&self, time: &TimeSpan) -> SdkResult<ListRes<UserShare>> {
264        let base_url = "https://api.weixin.qq.com/datacube/getusershare";
265        let sdk = self.0;
266        let res: CommonResponse<ListRes<UserShare>> = sdk
267            .wx_post(base_url)
268            .await?
269            .json(time)
270            .send()
271            .await?
272            .json()
273            .await?;
274        res.into()
275    }
276    pub async fn get_user_read_hour(&self, time: &TimeSpan) -> SdkResult<ListRes<UserReadHour>> {
277        let base_url = "https://api.weixin.qq.com/datacube/getuserreadhour";
278        let sdk = self.0;
279        let res: CommonResponse<ListRes<UserReadHour>> = sdk
280            .wx_post(base_url)
281            .await?
282            .json(time)
283            .send()
284            .await?
285            .json()
286            .await?;
287        res.into()
288    }
289    pub async fn get_user_share_hour(&self, time: &TimeSpan) -> SdkResult<ListRes<UserShareHour>> {
290        let base_url = "https://api.weixin.qq.com/datacube/getusersharehour";
291        let sdk = self.0;
292        let res: CommonResponse<ListRes<UserShareHour>> = sdk
293            .wx_post(base_url)
294            .await?
295            .json(time)
296            .send()
297            .await?
298            .json()
299            .await?;
300        res.into()
301    }
302    pub async fn get_upstream_msg_hour(
303        &self,
304        time: &TimeSpan,
305    ) -> SdkResult<ListRes<UpstreamMsgHour>> {
306        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsghour";
307        let sdk = self.0;
308        let res: CommonResponse<ListRes<UpstreamMsgHour>> = sdk
309            .wx_post(base_url)
310            .await?
311            .json(time)
312            .send()
313            .await?
314            .json()
315            .await?;
316        res.into()
317    }
318
319    pub async fn get_upstream_msg_week(&self, time: &TimeSpan) -> SdkResult<ListRes<UpstreamMsg>> {
320        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsgweek";
321        let sdk = self.0;
322        let res: CommonResponse<ListRes<UpstreamMsg>> = sdk
323            .wx_post(base_url)
324            .await?
325            .json(time)
326            .send()
327            .await?
328            .json()
329            .await?;
330        res.into()
331    }
332
333    pub async fn get_upstream_msg_month(&self, time: &TimeSpan) -> SdkResult<ListRes<UpstreamMsg>> {
334        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsgmonth";
335        let sdk = self.0;
336        let res: CommonResponse<ListRes<UpstreamMsg>> = sdk
337            .wx_post(base_url)
338            .await?
339            .json(time)
340            .send()
341            .await?
342            .json()
343            .await?;
344        res.into()
345    }
346
347    pub async fn get_upstream_msg(&self, time: &TimeSpan) -> SdkResult<ListRes<UpstreamMsg>> {
348        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsg";
349        let sdk = self.0;
350        let res: CommonResponse<ListRes<UpstreamMsg>> = sdk
351            .wx_post(base_url)
352            .await?
353            .json(time)
354            .send()
355            .await?
356            .json()
357            .await?;
358        res.into()
359    }
360    pub async fn get_upstream_msg_dist(
361        &self,
362        time: &TimeSpan,
363    ) -> SdkResult<ListRes<UpstreamMsgDist>> {
364        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsgdist";
365        let sdk = self.0;
366        let res: CommonResponse<ListRes<UpstreamMsgDist>> = sdk
367            .wx_post(base_url)
368            .await?
369            .json(time)
370            .send()
371            .await?
372            .json()
373            .await?;
374        res.into()
375    }
376
377    pub async fn get_upstream_msg_dist_week(
378        &self,
379        time: &TimeSpan,
380    ) -> SdkResult<ListRes<UpstreamMsgDist>> {
381        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsgdistweek";
382        let sdk = self.0;
383        let res: CommonResponse<ListRes<UpstreamMsgDist>> = sdk
384            .wx_post(base_url)
385            .await?
386            .json(time)
387            .send()
388            .await?
389            .json()
390            .await?;
391        res.into()
392    }
393
394    pub async fn get_upstream_msg_dist_month(
395        &self,
396        time: &TimeSpan,
397    ) -> SdkResult<ListRes<UpstreamMsgDist>> {
398        let base_url = "https://api.weixin.qq.com/datacube/getupstreammsgdistmonth";
399        let sdk = self.0;
400        let res: CommonResponse<ListRes<UpstreamMsgDist>> = sdk
401            .wx_post(base_url)
402            .await?
403            .json(time)
404            .send()
405            .await?
406            .json()
407            .await?;
408        res.into()
409    }
410    pub async fn get_interface_summary(&self, time: &TimeSpan) -> SdkResult<ListRes<ApiAnalysis>> {
411        let base_url = "https://api.weixin.qq.com/datacube/getinterfacesummary";
412        let sdk = self.0;
413        let res: CommonResponse<ListRes<ApiAnalysis>> = sdk
414            .wx_post(base_url)
415            .await?
416            .json(time)
417            .send()
418            .await?
419            .json()
420            .await?;
421        res.into()
422    }
423    pub async fn get_interface_summary_hour(
424        &self,
425        time: &TimeSpan,
426    ) -> SdkResult<ListRes<ApiAnalysisHour>> {
427        let base_url = "https://api.weixin.qq.com/datacube/getinterfacesummaryhour";
428        let sdk = self.0;
429        let res: CommonResponse<ListRes<ApiAnalysisHour>> = sdk
430            .wx_post(base_url)
431            .await?
432            .json(time)
433            .send()
434            .await?
435            .json()
436            .await?;
437        res.into()
438    }
439}
440#[cfg(test)]
441mod tests {
442    // use super::*;
443
444    // #[tokio::test]
445    // async fn test_summary() -> SdkResult<()> {
446    //     let time = &TimeSpan {
447    //         begin_date: "2020-04-02".to_string(),
448    //         end_date: "2020-04-02".to_string(),
449    //     };
450    //     let res = get_user_cumulate(time).await?;
451    //     println!("{:#?}", res); // --nocapture
452    //     assert_eq!(res.list[0].ref_date.as_str(), time.begin_date.as_str());
453    //     Ok(())
454    // }
455
456    // #[tokio::test]
457    // async fn test_list() -> SdkResult<()> {
458    //     let time = &TimeSpan {
459    //         begin_date: "2020-04-02".to_string(),
460    //         end_date: "2020-04-02".to_string(),
461    //     };
462    //     let res = get_interface_summary_hour(time).await?;
463    //     println!("{:#?}", res); // --nocapture
464    //     let res = get_interface_summary(time).await?;
465    //     println!("{:#?}", res); // --nocapture
466    //     let res = get_upstream_msg_dist(time).await?;
467    //     println!("{:#?}", res); // --nocapture
468    //     Ok(())
469    // }
470}