Skip to content

Commit

Permalink
Merge pull request #28 from Som-Energia/NEW_endpoint_reports
Browse files Browse the repository at this point in the history
New endpoint reports
  • Loading branch information
JoanaFigueira authored Feb 24, 2021
2 parents b39d2c4 + df2b7fe commit 019b9de
Show file tree
Hide file tree
Showing 19 changed files with 1,143 additions and 279 deletions.
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ JWT_EXPIRATION_DELTA=3600

# Mongo configuration
MONGO_CONF="mongodb://username:password@server:port/database"

# BeeData API access configuration
CERT_FILE = '/path/to/cerfile/cert.crt'
KEY_FILE = '/path/to/keyfile/cert.key'
COMPANY_ID = 1234567
BASE_URL = 'https://baseurl.com'
APIVERSION = 'v1'
USERNAME = "test@test"
PASSWORD = "test1234"

# Number of workers to download reports from BeeData
N_WORKERS = 2
6 changes: 5 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ pytz = "*"
passlib = "*"
pony = "*"
environs = "*"
uvicorn = "*"
uvicorn = "==0.11.5"
aioredis = "*"
pymongo = "*"
motor = "*"
pool-transport = {git = "https://github.com/Som-Energia/pool_transport.git"}
aiohttp = {extras = ["speedups"],version = "*"}
pytest-aiohttp = "*"
fakeredis = "*"

[requires]
python_version = "3.8"
850 changes: 595 additions & 255 deletions Pipfile.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions config/settings/devel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@
},
}
}

#Access BeeData API:
CERT_FILE = env.str('CERT_FILE')
KEY_FILE = env.str('KEY_FILE')
COMPANY_ID = env.int('COMPANY_ID')
BASE_URL = env.str('BASE_URL')
APIVERSION = env.str('APIVERSION')
USERNAME = env.str('USERNAME')
PASSWORD = env.str('PASSWORD')

#Number of workers for process Reports
N_WORKERS = env.int('N_WORKERS')
12 changes: 12 additions & 0 deletions config/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@
},
}
}

#Access BeeData API:
CERT_FILE = env.str('CERT_FILE')
KEY_FILE = env.str('KEY_FILE')
COMPANY_ID = env.int('COMPANY_ID')
BASE_URL = env.str('BASE_URL')
APIVERSION = env.str('APIVERSION')
USERNAME = env.str('USERNAME')
PASSWORD = env.str('PASSWORD')

#Number of workers for process Reports
N_WORKERS = env.int('N_WORKERS')
12 changes: 12 additions & 0 deletions config/settings/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@
},
}
}

#Access BeeData API:
CERT_FILE = env.str('CERT_FILE')
KEY_FILE = env.str('KEY_FILE')
COMPANY_ID = env.int('COMPANY_ID')
BASE_URL = env.str('BASE_URL')
APIVERSION = env.str('APIVERSION')
USERNAME = env.str('USERNAME')
PASSWORD = env.str('PASSWORD')

#Number of workers for process Reports
N_WORKERS = env.int('N_WORKERS')
89 changes: 86 additions & 3 deletions docs/infoenergia-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ tags:
- name: f1
description: All information about contract f1 measurements
- name: cch
description: cch curves (F5D and P5D)
description: cch curves
- name: reports
description: Reports to infoenergia
- name: measurements
description: All information about contract energy measurements

paths:
/contracts:
get:
Expand Down Expand Up @@ -193,7 +196,7 @@ paths:
- name: type
in: query
description: Filter by different types of contract modification
required: false
required: true
style: form
explode: false
schema:
Expand All @@ -217,7 +220,7 @@ paths:
- name: to_
in: query
description: Filter contract amendments until this date. Format YYYY-MM-DD
required: false
required: true
style: form
explode: true
schema:
Expand Down Expand Up @@ -658,6 +661,28 @@ paths:
format: date
default: None
example: '2019-08-10'
- name: downloaded_from
in: query
description: Get all CCH downloaded from this date on at 00:00 hours. In format YYYY-MM-DD
required: false
style: form
explode: true
schema:
type: string
format: date
default: None
example: '2019-08-01'
- name: downloaded_to
in: query
description: Get all CCH downloaded until this date at 00:00 hours. In format YYYY-MM-DD
required: false
style: form
explode: true
schema:
type: string
format: date
default: None
example: '2019-08-10'
- name: limit
in: query
description: How many contracts to return at one time
Expand Down Expand Up @@ -708,6 +733,40 @@ paths:
content: {}
security:
- jwt: []
/reports:
post:
tags:
- reports
summary: uploads report for all contracts
operationId: server.reports
requestBody:
description: json with contracts Ids and type of report to be processed
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/reports'
responses:
200:
description: successful operation
content:
application/json:
schema:
type: object
properties:
reports:
type: integer
description: number of submited reports
example: 3
unprocessed_reports:
type: array
description: reports that could not be processed
items:
type: string
example: ["0000001", "0000002"]
400:
description: Invalid username/password supplied
content: {}
/auth:
post:
tags:
Expand Down Expand Up @@ -739,6 +798,30 @@ paths:
content: {}
components:
schemas:
reports:
type: object
properties:
id:
type: integer
format: int64
contract_ids:
type: array
items:
type: string
example: ["0000001", "0000002"]
type:
type: string
description: type of report
example: "infoenergia"
create_at:
type: string
format: date-time
description: json creation date
example: "2020-01-01"
month:
type: string
description: month of json data
example: "202001"
User:
type: object
properties:
Expand Down
3 changes: 2 additions & 1 deletion infoenergia_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__version__ = VERSION = '1.3.2'

from .api import contracts, f1_measures, cch, modcontracts, registration
from .api import (contracts, cch, f1_measures, modcontracts, registration,
reports)
from .app import app
1 change: 1 addition & 0 deletions infoenergia_api/api/reports/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .reports import bp_reports
39 changes: 39 additions & 0 deletions infoenergia_api/api/reports/reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from sanic import Blueprint
from sanic.log import logger
from sanic.response import json
from sanic.views import HTTPMethodView
from sanic_jwt.decorators import protected, inject_user

from infoenergia_api.contrib import beedataApi, get_report_ids
from infoenergia_api.contrib import PaginationLinksMixin


bp_reports = Blueprint('reports')

class ReportsView(PaginationLinksMixin, HTTPMethodView):
decorators = [
inject_user(),
protected(),
]

endpoint_name = 'reports.reports'

async def post(self, request, user):
logger.info("Uploading contracts")
report_ids, month = await get_report_ids(request)

request.app.loop.create_task(
beedataApi().process_reports(report_ids, month)
)

response = {
'reports': len(report_ids),
}
return json(response)


bp_reports.add_route(
ReportsView.as_view(),
'/reports',
name='reports',
)
2 changes: 2 additions & 0 deletions infoenergia_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from infoenergia_api.api.f1_measures import bp_f1_measures
from infoenergia_api.api.cch import bp_cch_measures
from infoenergia_api.api.modcontracts import bp_modcontracts
from infoenergia_api.api.reports import bp_reports
from infoenergia_api.api.registration.login import (InvitationUrlToken,
authenticate, extra_views)
from infoenergia_api.api.registration.models import db, retrieve_user
Expand All @@ -34,6 +35,7 @@ def build_app():
app.blueprint(bp_f1_measures)
app.blueprint(bp_modcontracts)
app.blueprint(bp_cch_measures)
app.blueprint(bp_reports)

app.add_route(
InvitationUrlToken.as_view(),
Expand Down
1 change: 1 addition & 0 deletions infoenergia_api/contrib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .f1 import Invoice, get_invoices
from .pagination import Pagination, PaginationLinksMixin
from .cch import Cch
from .reports import beedataApi, get_report_ids
18 changes: 13 additions & 5 deletions infoenergia_api/contrib/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ async def _pagination_links(self, request, request_id, pagination_list, **kwargs
url = request.url_for(self.endpoint_name, **kwargs)
url_cursor = await self._next_cursor(request_id, pagination_list.next_cursor)

next_page = '{url}?cursor={cursor}&limit={limit}'.format(
url=url,
cursor=url_cursor,
limit=pagination_list.page_size
) if url_cursor else False
if 'cch' in self.endpoint_name:
next_page = '{url}?type={cch_type}&cursor={cursor}&limit={limit}'.format(
url=url,
cch_type=request.args['type'][0],
cursor=url_cursor,
limit=pagination_list.page_size
) if url_cursor else False
else:
next_page = '{url}?cursor={cursor}&limit={limit}'.format(
url=url,
cursor=url_cursor,
limit=pagination_list.page_size
) if url_cursor else False

return dict(
cursor=url_cursor,
Expand Down
Loading

0 comments on commit 019b9de

Please sign in to comment.