A flask based web api for acquisition of memberships data from cobot and also calculate some required metrics from them to report analysis on monthly basis.
##Requirements
- Python
2.7+
pip
installed- virtualenv
##Instructions
-
First install some system dependency
$ sudo apt-get install python-dev libssl-dev libffi-dev libmysqlclient-dev
-
Install all python package dependencies by using
pip
$ pip install -r requirements.txt
-
Also, set some required environment variables for this prototype
$ export CONFIG='local' # 'dev' for production $ export COBOT_DB_URL='<database-url>' $ export COBOT_TOKEN='<cobot-access-token>' $ export LOG_FILE_PATH='<log-file-folder-path>'
-
To run application with simple flask server
$ python manage.py runserver
-
To create database tables
$ python manage.py create_db
-
To delete database tables
$ python manage.py drop_db
-
To do database schema migrations
$ python manage.py db [command] where command can be any of these {upgrade,heads,merge,migrate,stamp,show,current,edit,init,downgrade,branches,history,revision} upgrade Upgrade to a later version heads Show current available heads in the script directory merge Merge two revisions together. Creates a new migration file migrate Alias for 'revision --autogenerate' stamp 'stamp' the revision table with the given revision; don't run any migrations show Show the revision denoted by the given symbol. current Display the current revision for each database. edit Upgrade to a later version init Generates a new migration downgrade Revert to a previous version branches Show current branch points history List changeset scripts in chronological order. revision Create a new revision file
-
To run task which gets data from cobot api and add that to database tables
$ python manage.py run_task_data [-sd START_DATE or --startDate=START_DATE] [-ed END_DATE or --endDate=END_DATE] [ -h HUB_NAME or --hub=HUB_NAME] # DATE should be in format 'YYYY-MM-DD'
Note: To run task for a specific date, then you should only pass that date as
-sd or --startDate
. -
To run task which calculate member report metrics and append them to database tables
$ python manage.py run_task_report [-sd START_DATE or --startDate=START_DATE] [-ed END_DATE or --endDate=END_DATE] [ -h HUB_NAME or --hub=HUB_NAME] # DATE should be in format 'YYYY-MM'
Note: To run task for a specific date, then you should only pass that date as
-sd or --startDate
. -
To run application within guicorn server
$ python manage.py gunicorn # OR $ python manage.py gunicorn -c gunicorn-conf.py
Note: This command also supports all gunicorn parameters
##Endpoints
A api endpoint returns all necessary information to fill some basic card's data
/api/cards
By default, returns all information regarding all hubs
-
hub_name : a hub name, to get report details regarding a particular hub
/api/cards?hub_name=91sgurgaon
Type - JSON
[
{
"card_no": 1,
"total_active_members": 520,
"new_members": {
"percent": 3.0,
"duration": 30
}
},
{
"card_no": 2,
"mrr_value": 4827354.00,
"increment_revenue": {
"percent": 4.0,
"duration": 30
}
},
{
"card_no": 3,
"new_members": {
"count": 7,
"duration": 7,
"base_percent": 0.5
}
},
{
"card_no": 4,
"leave_members": {
"count": 3,
"duration": 7,
"base_percent": 0.6
}
}
]
A api endpoint returns all details of member's report of all hubs or specific hubs to plot that data on graph.
/api/reports
By default, returns all member's report regarding all hubs
-
hub_name : a hub name, to get report details regarding a particular hub
/api/reports?hub_name=91sgurgaon
-
plan_type : a plan type, to get report details regarding a particular types of plan. It's value can be of four types (i.e
Full Time
,Part Time
,Others
andIgnore
)/api/reports?hub_name=91sgurgaon&plan_type=Full Time
-
from & to : a range of dates, to get report data of a specific duration
Note:- Date should be in
YYYY-MM
format/api/reports?hub_name=91sgurgaon&plan_type=Full Time&from=2015-03&to=2015-09
Type - JSON
[
{
"hub_plan": {
"hub": {
"name": "91springboard",
"location": {
"name": "Okhla"
}
},
"plan": {
"name": "Full Time (4+)",
"type": "FULL-TIME",
"price": 4999.00
}
},
"time": {
"month": 5,
"year": 2015,
"date": "2015-05"
},
"count": {
"new_member": 23,
"retain_member": 231,
"leave_member": 2
},
"revenue": {
"new_member": 114977.0000,
"retain_member": 1154769.0000,
"leave_member": 9998.0000
}
}
]