File size: 3,880 Bytes
c34c3c8
94ed9e1
 
 
 
 
 
 
 
 
 
 
c34c3c8
d5312ce
c34c3c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94ed9e1
d5312ce
c34c3c8
d5312ce
 
 
 
 
 
 
c34c3c8
d5312ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c34c3c8
 
d5312ce
94ed9e1
 
 
 
d5312ce
94ed9e1
 
 
 
 
 
 
 
d5312ce
94ed9e1
 
 
 
 
 
 
 
 
 
 
 
 
d5312ce
94ed9e1
 
 
 
d5312ce
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
import os
from http import HTTPStatus
from fastapi.responses import JSONResponse
from restful.services import cryptocurrency_svc
from restful.schemas import CryptocurrencyPredictionSchema


# Cryptocurrency Controller
class cryptocurrency_controller:
    # Cryptocurrency Service
    __SERVICE = cryptocurrency_svc()

    # Cryptocurrency List
    async def crypto_list(self) -> JSONResponse:
        try:
            DATASETS_PATH = './datasets'
            DATASETS = sorted(
                [
                    item.replace(".csv", "") for item in os.listdir(DATASETS_PATH)
                    if os.path.isfile(os.path.join(DATASETS_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
            )

    # Cryptocurrency Controller
    async def prediction(self, payload: CryptocurrencyPredictionSchema) -> JSONResponse:
        try:
            DATASETS_PATH = './datasets'
            DATASETS = sorted(
                [
                    item.replace(".csv", "") for item in os.listdir(DATASETS_PATH)
                    if os.path.isfile(os.path.join(DATASETS_PATH, item)) and item.endswith('.csv')
                ]
            )

            # Validation
            if (payload.days > 31) or (payload.days < 1):
                return JSONResponse(
                    content = {
                        'message': 'prediction 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.currency not in DATASETS:
                return JSONResponse(
                    content = {
                        'message': f'cryptocurrency {payload.currency} is not available.',
                        'status_code': HTTPStatus.BAD_REQUEST,
                        'data': None
                    },
                    status_code = HTTPStatus.BAD_REQUEST
                )


            prediction: dict = await self.__SERVICE.prediction(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
            )