Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Sub status new branch #993

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
SubscriptionSafelistSendView,
SubscriptionsPagedView,
SubscriptionsStatusView,
SubscriptionsStatusEndingSoonView,
SubscriptionsStatusInProgressView,
SubscriptionsStatusStoppedView,
SubscriptionsView,
SubscriptionTestView,
SubscriptionValidView,
Expand Down Expand Up @@ -169,6 +172,18 @@
SubscriptionsPagedView,
),
("/subscriptions/status/", SubscriptionsStatusView),
(
"/subscriptions/status/ending_soon/<page>/<pagesize>/<sortby>/<sortorder>/",
SubscriptionsStatusEndingSoonView
),
(
"/subscriptions/status/in_progress/<page>/<pagesize>/<sortby>/<sortorder>/",
SubscriptionsStatusInProgressView
),
(
"/subscriptions/status/stopped/<page>/<pagesize>/<sortby>/<sortorder>/",
SubscriptionsStatusStoppedView
),
("/subscriptions/valid/", SubscriptionValidView),
("/subscription/<subscription_id>/", SubscriptionView),
("/subscription/<subscription_id>/launch/", SubscriptionLaunchView),
Expand Down
231 changes: 231 additions & 0 deletions src/api/views/subscription_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,238 @@ def get(self):
subscription["_id"] = str(subscription["_id"])

return jsonify(subscriptions)
class StatusDefaults():
"""StatusDefaults Helper Class."""

def generateDefaultSortValues(sortby, sortorder):
"""
Generate the default values for the subscription
status page paged aggregate queries.
"""
defaults = {}
sortdirection = 1
if sortorder == "desc":
sortdirection = -1

defaults['sortdirection'] = sortdirection

sort_dict = OrderedDict()
if sortby != "name":
sort_dict = OrderedDict([(sortby,sortdirection)])
else:
sort_dict["name_no_inc"] = sortdirection
sort_dict["created"] = sortdirection

defaults['sort_dict'] = sort_dict

# Defaults to not show archived items in the subscription status
archived = [{"archived": {"$exists": False}}, {"archived": False}]

defaults['archived'] = archived

return defaults

def generateDefaultpPipeline(page, pagesize, sortby, sortorder):
"""Generate the default pipeline used by subscription status aggregations."""

sortdirection = 1
if sortorder == "desc":
sortdirection = -1

sort_dict = OrderedDict()
if sortby != "name":
sort_dict[sortby] = sortdirection
else:
sort_dict["name_no_inc"] = sortdirection
sort_dict["created"] = sortdirection

pipeline = [
{
"$addFields": {
"subscription_id": {"$toString": "$_id"},
"customer_object_id": {"$toObjectId": "$customer_id"},
}
},
{
"$lookup": {
"from": "cycle",
"let": {"subscription_id": "$subscription_id"},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": ["$subscription_id","$$subscription_id"]
}
}
}
],
"as": "cycle",
},
},
{
"$lookup": {
"from": "customer",
"localField": "customer_object_id",
"foreignField": "_id",
"as": "customer",
}
},
{
"$addFields": {
"cycle_end_date": {"$max": "$cycle.end_date"},
"cycle_start_date": {"$max": "$cycle.start_date"},
}
},
{
"$sort": sort_dict
},
{"$skip": int(page) * int(pagesize)},
{"$limit": int(pagesize)},
{
"$project":{
"name": "$name",
"endDate": "$cycle_end_date",
"startDate": "$cycle_start_date",
"appendixDate": {"$max": "$customer.appendix_a_date"},
"isContinuous": "$continuous_subscription",
"lastUpdated": "$updated",
}
}
]

return pipeline

class SubscriptionsStatusEndingSoonView(MethodView):
"""SubscriptionStatusEndingSoonView."""

def get(self, page, pagesize, sortby, sortorder):
"""
Get data for the Overview Subscription Status Page.
For the 'Ending Soon' table.
Ending Soon defined as ending within 20 days from current time.
"""

pipeline = StatusDefaults.generateDefaultpPipeline(page,pagesize,sortby,sortorder)

endingSoonDayRange = 45

now = datetime.now()
compareDate = datetime.now()
compareDate += timedelta(days=endingSoonDayRange)

archived = [{"archived": {"$exists": False}}, {"archived": False}]

pipeline.insert(
4,
{
"$match": {
"$and":[
{"$expr": {
"$lt": ["$cycle_end_date",compareDate]
}},
{"$expr": {
"$gte": ["$cycle_end_date",now]
}},
{
"$expr": {
"$eq": [
"$status",
"running",
]
}
},
{"$or": archived},
]
}
},
)


subscriptions = subscription_manager.aggregate(pipeline)
for subscription in subscriptions:
subscription["_id"] = str(subscription["_id"])

return subscriptions
# return pipeline

class SubscriptionsStatusInProgressView(MethodView):
"""SubscriptionStatusInProgressView."""

def get(self, page, pagesize, sortby, sortorder):
"""
Get data for the Overview Subscription Status Page.
For the 'In Progress' table.
In Progress defined as any with 'status' value of 'running'.
"""

pipeline = StatusDefaults.generateDefaultpPipeline(page,pagesize,sortby,sortorder)

archived = [{"archived": {"$exists": False}}, {"archived": False}]

pipeline.insert(
4,
{
"$match": {
"$and":[
{
"$expr": {
"$eq": [
"$status",
"running",
]
}
},
{"$or": archived},
]
}
},
)


subscriptions = subscription_manager.aggregate(pipeline)
for subscription in subscriptions:
subscription["_id"] = str(subscription["_id"])

return subscriptions

class SubscriptionsStatusStoppedView(MethodView):
"""SubscriptionStatusStoppedView."""

def get(self, page, pagesize, sortby, sortorder):
"""
Get data for the Overview Subscription Status Page.
For the 'Stopped' table.
Stopped defined as any with 'status' value of 'stopped'.
"""

pipeline = StatusDefaults.generateDefaultpPipeline(page,pagesize,sortby,sortorder)

archived = [{"archived": {"$exists": False}}, {"archived": False}]

pipeline.insert(
4,
{
"$match": {
"$and":[
{
"$expr": {
"$eq": [
"$status",
"stopped",
]
}
},
{"$or": archived},
]
}
},
)

subscriptions = subscription_manager.aggregate(pipeline)
for subscription in subscriptions:
subscription["_id"] = str(subscription["_id"])

return subscriptions

class SubscriptionLaunchView(MethodView):
"""SubscriptionLaunchView."""
Expand Down