aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/sendgrid/helpers/stats/stats.py
blob: b866093b50938b220de40f36a2190db6f4932eb1 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
class Stats(object):
    """
    Object for building query params for a global email statistics request
    """
    def __init__(
            self, start_date=None):
        """Create a Stats object

        :param start_date: Date of when stats should begin in YYYY-MM-DD format, defaults to None
        :type start_date: string, optional
        """
        self._start_date = None
        self._end_date = None
        self._aggregated_by = None
        self._sort_by_metric = None
        self._sort_by_direction = None
        self._limit = None
        self._offset = None

        # Minimum required for stats
        if start_date:
            self.start_date = start_date

    def __str__(self):
        """Get a JSON representation of this object.

        :rtype: string
        """
        return str(self.get())

    def get(self):
        """
        Get a JSON-ready representation of Stats

        :returns: This GlobalStats, ready for use in a request body.
        :rtype: response stats dict
        """
        stats = {}
        if self.start_date is not None:
            stats["start_date"] = self.start_date
        if self.end_date is not None:
            stats["end_date"] = self.end_date
        if self.aggregated_by is not None:
            stats["aggregated_by"] = self.aggregated_by
        if self.sort_by_metric is not None:
            stats["sort_by_metric"] = self.sort_by_metric
        if self.sort_by_direction is not None:
            stats["sort_by_direction"] = self.sort_by_direction
        if self.limit is not None:
            stats["limit"] = self.limit
        if self.offset is not None:
            stats["offset"] = self.offset
        return stats

    @property
    def start_date(self):
        """Date of when stats should begin in YYYY-MM-DD format

        :rtype: string
        """        
        return self._start_date

    @start_date.setter
    def start_date(self, value):
        """Date of when stats should begin in YYYY-MM-DD format

        :param value: Date representing when stats should begin
        :type value: string
        """
        self._start_date = value

    @property
    def end_date(self):
        """Date of when stats should end in YYYY-MM-DD format

        :rtype: string
        """  
        return self._end_date

    @end_date.setter
    def end_date(self, value):
        """Date of when stats should end in YYYY-MM-DD format

        :param value: Date representing when stats should end
        :type value: string
        """
        self._end_date = value

    @property
    def aggregated_by(self):
        """Chosen period (e.g. 'day', 'week', 'month') for how stats get grouped

        :rtype: string
        """        
        return self._aggregated_by

    @aggregated_by.setter
    def aggregated_by(self, value):
        """Chosen period (e.g. 'day', 'week', 'month') for how stats get grouped

        :param value: Period for how keys will get formatted
        :type value: string
        """        
        self._aggregated_by = value

    @property
    def sort_by_metric(self):
        """Metric to sort stats by

        :rtype: string
        """        
        return self._sort_by_metric

    @sort_by_metric.setter
    def sort_by_metric(self, value):
        """Metric to sort stats by

        :param value: Chosen metric stats will by sorted by
        :type value: string
        """        
        self._sort_by_metric = value

    @property
    def sort_by_direction(self):
        """Direction data will be sorted, either 'asc' or 'desc'

        :rtype: string
        """        
        return self._sort_by_direction

    @sort_by_direction.setter
    def sort_by_direction(self, value):
        """Direction data will be sorted, either 'asc' or 'desc'

        :param value: Direction of data, either 'asc' or 'desc'
        :type value: string
        """        
        self._sort_by_direction = value

    @property
    def limit(self):
        """Max amount of results to be returned

        :rtype: int
        """        
        return self._limit

    @limit.setter
    def limit(self, value):
        """Max amount of results to be returned

        :param value: Max amount of results
        :type value: int
        """        
        self._limit = value

    @property
    def offset(self):
        """Number of places a starting point of a data set will move

        :rtype: int
        """        
        return self._offset

    @offset.setter
    def offset(self, value):
        """Number of places a starting point of a data set will move

        :param value: Number of positions to move from starting point
        :type value: int
        """        
        self._offset = value


class CategoryStats(Stats):
    """
    object for building query params for a category statistics request
    """
    def __init__(self, start_date=None, categories=None):
        """Create a CategoryStats object

        :param start_date: Date of when stats should begin in YYYY-MM-DD format, defaults to None
        :type start_date: string, optional
        :param categories: list of categories to get results of, defaults to None
        :type categories: list(string), optional
        """        
        self._categories = None
        super(CategoryStats, self).__init__()

        # Minimum required for category stats
        if start_date and categories:
            self.start_date = start_date
            for cat_name in categories:
                self.add_category(Category(cat_name))

    def get(self):
        """
        Get a JSON-ready representation of this CategoryStats.

        :return: response category stats dict
        """
        stats = {}
        if self.start_date is not None:
            stats["start_date"] = self.start_date
        if self.end_date is not None:
            stats["end_date"] = self.end_date
        if self.aggregated_by is not None:
            stats["aggregated_by"] = self.aggregated_by
        if self.sort_by_metric is not None:
            stats["sort_by_metric"] = self.sort_by_metric
        if self.sort_by_direction is not None:
            stats["sort_by_direction"] = self.sort_by_direction
        if self.limit is not None:
            stats["limit"] = self.limit
        if self.offset is not None:
            stats["offset"] = self.offset
        if self.categories is not None:
            stats['categories'] = [category.get() for category in
                                   self.categories]
        return stats

    @property
    def categories(self):
        """List of categories

        :rtype: list(Category)
        """        
        return self._categories

    def add_category(self, category):
        """Appends a category to this object's category list

        :param category: Category to append to CategoryStats
        :type category: Category
        """
        if self._categories is None:
            self._categories = []
        self._categories.append(category)


class SubuserStats(Stats):
    """
    object of building query params for a subuser statistics request
    """    
    def __init__(self, start_date=None, subusers=None):
        """Create a SubuserStats object

        :param start_date: Date of when stats should begin in YYYY-MM-DD format, defaults to None
        :type start_date: string, optional
        :param subusers: list of subusers to get results of, defaults to None
        :type subusers: list(string), optional
        """        
        self._subusers = None
        super(SubuserStats, self).__init__()

        # Minimum required for subusers stats
        if start_date and subusers:
            self.start_date = start_date
            for subuser_name in subusers:
                self.add_subuser(Subuser(subuser_name))

    def get(self):
        """
        Get a JSON-ready representation of this SubuserStats.

        :return: response subuser stats dict
        """
        stats = {}
        if self.start_date is not None:
            stats["start_date"] = self.start_date
        if self.end_date is not None:
            stats["end_date"] = self.end_date
        if self.aggregated_by is not None:
            stats["aggregated_by"] = self.aggregated_by
        if self.sort_by_metric is not None:
            stats["sort_by_metric"] = self.sort_by_metric
        if self.sort_by_direction is not None:
            stats["sort_by_direction"] = self.sort_by_direction
        if self.limit is not None:
            stats["limit"] = self.limit
        if self.offset is not None:
            stats["offset"] = self.offset
        if self.subusers is not None:
            stats['subusers'] = [subuser.get() for subuser in
                                 self.subusers]
        return stats

    @property
    def subusers(self):
        """List of subusers

        :rtype: list(Subuser)
        """
        return self._subusers

    def add_subuser(self, subuser):
        """Appends a subuser to this object's subuser list

        :param subuser: Subuser to append to SubuserStats
        :type subuser: Subuser
        """
        if self._subusers is None:
            self._subusers = []
        self._subusers.append(subuser)


class Category(object):
    """
    Represents a searchable statistics category to be used in a CategoryStats object
    """
    def __init__(self, name=None):
        """Create a Category object

        :param name: name of category, defaults to None
        :type name: string, optional
        """        
        self._name = None
        if name is not None:
            self._name = name

    @property
    def name(self):
        """Get name of category

        :rtype: string
        """
        return self._name

    @name.setter
    def name(self, value):
        """Set name of category

        :param value: name of the statistical category
        :type value: string
        """        
        self._name = value

    def get(self):
        """
        Get a string representation of Category.

        :return: string of the category's name
        """
        return self.name


class Subuser(object):
    """
    Represents a searchable subuser to be used in a SubuserStats object
    """    
    def __init__(self, name=None):
        """Create a Subuser object

        :param name: name of subuser, defaults to None
        :type name: string, optional
        """        
        self._name = None
        if name is not None:
            self._name = name

    @property
    def name(self):
        """Get name of the subuser

        :rtype: string
        """        
        return self._name

    @name.setter
    def name(self, value):
        """Set name of the subuser

        :param value: name of the subuser
        :type value: string
        """        
        self._name = value

    def get(self):
        """
        Get a string representation of Subuser.

        :return: string of the subuser's name
        """
        return self.name