1// Copyright 2016 The go-github AUTHORS. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6package github
7
8import (
9	"context"
10	"fmt"
11)
12
13// TrafficReferrer represent information about traffic from a referrer .
14type TrafficReferrer struct {
15	Referrer *string `json:"referrer,omitempty"`
16	Count    *int    `json:"count,omitempty"`
17	Uniques  *int    `json:"uniques,omitempty"`
18}
19
20// TrafficPath represent information about the traffic on a path of the repo.
21type TrafficPath struct {
22	Path    *string `json:"path,omitempty"`
23	Title   *string `json:"title,omitempty"`
24	Count   *int    `json:"count,omitempty"`
25	Uniques *int    `json:"uniques,omitempty"`
26}
27
28// TrafficData represent information about a specific timestamp in views or clones list.
29type TrafficData struct {
30	Timestamp *Timestamp `json:"timestamp,omitempty"`
31	Count     *int       `json:"count,omitempty"`
32	Uniques   *int       `json:"uniques,omitempty"`
33}
34
35// TrafficViews represent information about the number of views in the last 14 days.
36type TrafficViews struct {
37	Views   []*TrafficData `json:"views,omitempty"`
38	Count   *int           `json:"count,omitempty"`
39	Uniques *int           `json:"uniques,omitempty"`
40}
41
42// TrafficClones represent information about the number of clones in the last 14 days.
43type TrafficClones struct {
44	Clones  []*TrafficData `json:"clones,omitempty"`
45	Count   *int           `json:"count,omitempty"`
46	Uniques *int           `json:"uniques,omitempty"`
47}
48
49// TrafficBreakdownOptions specifies the parameters to methods that support breakdown per day or week.
50// Can be one of: day, week. Default: day.
51type TrafficBreakdownOptions struct {
52	Per string `url:"per,omitempty"`
53}
54
55// ListTrafficReferrers list the top 10 referrers over the last 14 days.
56//
57// GitHub API docs: https://developer.github.com/v3/repos/traffic/#list-referrers
58func (s *RepositoriesService) ListTrafficReferrers(ctx context.Context, owner, repo string) ([]*TrafficReferrer, *Response, error) {
59	u := fmt.Sprintf("repos/%v/%v/traffic/popular/referrers", owner, repo)
60
61	req, err := s.client.NewRequest("GET", u, nil)
62	if err != nil {
63		return nil, nil, err
64	}
65
66	var trafficReferrers []*TrafficReferrer
67	resp, err := s.client.Do(ctx, req, &trafficReferrers)
68	if err != nil {
69		return nil, resp, err
70	}
71
72	return trafficReferrers, resp, nil
73}
74
75// ListTrafficPaths list the top 10 popular content over the last 14 days.
76//
77// GitHub API docs: https://developer.github.com/v3/repos/traffic/#list-paths
78func (s *RepositoriesService) ListTrafficPaths(ctx context.Context, owner, repo string) ([]*TrafficPath, *Response, error) {
79	u := fmt.Sprintf("repos/%v/%v/traffic/popular/paths", owner, repo)
80
81	req, err := s.client.NewRequest("GET", u, nil)
82	if err != nil {
83		return nil, nil, err
84	}
85
86	var paths []*TrafficPath
87	resp, err := s.client.Do(ctx, req, &paths)
88	if err != nil {
89		return nil, resp, err
90	}
91
92	return paths, resp, nil
93}
94
95// ListTrafficViews get total number of views for the last 14 days and breaks it down either per day or week.
96//
97// GitHub API docs: https://developer.github.com/v3/repos/traffic/#views
98func (s *RepositoriesService) ListTrafficViews(ctx context.Context, owner, repo string, opt *TrafficBreakdownOptions) (*TrafficViews, *Response, error) {
99	u := fmt.Sprintf("repos/%v/%v/traffic/views", owner, repo)
100	u, err := addOptions(u, opt)
101	if err != nil {
102		return nil, nil, err
103	}
104
105	req, err := s.client.NewRequest("GET", u, nil)
106	if err != nil {
107		return nil, nil, err
108	}
109
110	trafficViews := new(TrafficViews)
111	resp, err := s.client.Do(ctx, req, &trafficViews)
112	if err != nil {
113		return nil, resp, err
114	}
115
116	return trafficViews, resp, nil
117}
118
119// ListTrafficClones get total number of clones for the last 14 days and breaks it down either per day or week for the last 14 days.
120//
121// GitHub API docs: https://developer.github.com/v3/repos/traffic/#views
122func (s *RepositoriesService) ListTrafficClones(ctx context.Context, owner, repo string, opt *TrafficBreakdownOptions) (*TrafficClones, *Response, error) {
123	u := fmt.Sprintf("repos/%v/%v/traffic/clones", owner, repo)
124	u, err := addOptions(u, opt)
125	if err != nil {
126		return nil, nil, err
127	}
128
129	req, err := s.client.NewRequest("GET", u, nil)
130	if err != nil {
131		return nil, nil, err
132	}
133
134	trafficClones := new(TrafficClones)
135	resp, err := s.client.Do(ctx, req, &trafficClones)
136	if err != nil {
137		return nil, resp, err
138	}
139
140	return trafficClones, resp, nil
141}
142