File size: 5,154 Bytes
0a7b47e
 
 
d7df8d0
 
0a7b47e
 
d7df8d0
 
0a7b47e
d7df8d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7b47e
45f7ad7
d7df8d0
0a7b47e
d7df8d0
 
0a7b47e
 
 
 
 
 
 
d7df8d0
0a7b47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7df8d0
 
 
 
 
 
0a7b47e
45f7ad7
d7df8d0
0a7b47e
d7df8d0
 
0a7b47e
 
 
d7df8d0
 
 
 
 
 
 
 
 
 
 
0a7b47e
 
 
d7df8d0
0a7b47e
 
 
 
 
 
d7df8d0
 
0a7b47e
 
d7df8d0
0a7b47e
 
 
 
 
 
 
d7df8d0
0a7b47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from http import HTTPStatus
from fastapi.responses import JSONResponse
from restful.services import ForecastingService
from restful.schemas import ForecastingServiceSchema


""" Forecasting Controller """
class ForecastingControllers:

    __SERVICE: ForecastingService = ForecastingService()


    """
        Algorithms Controller
    """
    async def algorithms_controller(self) -> JSONResponse:
        try:
            algorithms: list = sorted(os.listdir("resources/algorithms"))
            return JSONResponse(
                content = {
                    'message': 'Success',
                    'status_code': HTTPStatus.OK,
                    'data': algorithms
                },
                status_code = HTTPStatus.OK
            )

        except Exception as error_message:
            print(error_message)
            return JSONResponse(
                content = {
                    'message': 'Internal Server Error',
                    'status_code': HTTPStatus.INTERNAL_SERVER_ERROR,
                    'data': None
                },
                status_code = HTTPStatus.INTERNAL_SERVER_ERROR
            )



    """
        Currency Controller
    """
    async def currencies_controller(self) -> JSONResponse:
        try:
            path: str = 'resources/datasets'
            datasets: list = sorted(
                [
                    item.replace(".csv", "") for item in os.listdir(path)
                    if os.path.isfile(os.path.join(path, item)) and item.endswith('.csv')
                ]
            )

            return JSONResponse(
                content = {
                    'message': 'Success',
                    'status_code': HTTPStatus.OK,
                    'data': datasets
                },
                status_code = HTTPStatus.OK
            )

        except Exception as error_message:
            print(error_message)
            return JSONResponse(
                content = {
                    'message': 'Internal Server Error',
                    'status_code': HTTPStatus.INTERNAL_SERVER_ERROR,
                    'data': None
                },
                status_code = HTTPStatus.INTERNAL_SERVER_ERROR
            )



    """ 
        Forecasting Controller
    """
    async def forecasting_controller(self, payload: ForecastingServiceSchema) -> JSONResponse:
        try:
            path: str = 'resources/datasets'
            datasets: list = sorted(
                [
                    item.replace(".csv", "") for item in os.listdir(path)
                    if os.path.isfile(os.path.join(path, item)) and item.endswith('.csv')
                ]
            )

            if payload.currency not in datasets:
                return JSONResponse(
                    content = {
                        'message': f'symbols "{payload.currency}" is not available.',
                        'status_code': HTTPStatus.BAD_REQUEST,
                        'data': None
                    },
                    status_code = HTTPStatus.BAD_REQUEST
                )


            if (payload.days > 31) or (payload.days < 1):
                return JSONResponse(
                    content = {
                        'message': 'days cannot be more than a month and cannot be less than 1',
                        'status_code': HTTPStatus.BAD_REQUEST,
                        'data': None
                    },
                    status_code = HTTPStatus.BAD_REQUEST
                )


            if payload.algorithm not in os.listdir("resources/algorithms"):
                return JSONResponse(
                    content = {
                        'message': f'algorithm "{payload.algorithm}" is not available.',
                        'status_code': HTTPStatus.BAD_REQUEST,
                        'data': None
                    },
                    status_code = HTTPStatus.BAD_REQUEST
                )


            prediction: dict = await self.__SERVICE.forecasting(payload)

            if not prediction :
                return JSONResponse(
                    content = {
                        'message': 'prediction could not be generated, please try again.',
                        'status_code': HTTPStatus.BAD_REQUEST,
                        'data': None
                    },
                    status_code = HTTPStatus.BAD_REQUEST
                )

            return JSONResponse(
                content = {
                    'message': 'prediction success',
                    'status_code': HTTPStatus.OK,
                    'data': {
						'currency': payload.currency,
						'predictions': prediction
					}
                },
                status_code = HTTPStatus.OK
            )

        except Exception as error_message:
            print(error_message)
            return JSONResponse(
                content = {
                    'message': 'internal server error',
                    'status_code': HTTPStatus.INTERNAL_SERVER_ERROR,
                    'data': None
                },
                status_code = HTTPStatus.INTERNAL_SERVER_ERROR
            )