Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM mcr.microsoft.com/devcontainers/base:jammy
# FROM mcr.microsoft.com/devcontainers/base:jammy

ARG DEBIAN_FRONTEND=noninteractive
ARG USER=vscode

RUN DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get install -y build-essential --no-install-recommends make \
ca-certificates \
git \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
wget \
curl \
llvm \
libncurses5-dev \
xz-utils \
tk-dev \
libxml2-dev \
libxmlsec1-dev \
libffi-dev \
liblzma-dev

# Python and poetry installation
USER $USER
ARG HOME="/home/$USER"
ARG PYTHON_VERSION=3.10
# ARG PYTHON_VERSION=3.10

ENV PYENV_ROOT="${HOME}/.pyenv"
ENV PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${HOME}/.local/bin:$PATH"

RUN echo "done 0" \
&& curl https://pyenv.run | bash \
&& echo "done 1" \
&& pyenv install ${PYTHON_VERSION} \
&& echo "done 2" \
&& pyenv global ${PYTHON_VERSION} \
&& echo "done 3" \
&& curl -sSL https://install.python-poetry.org | python3 - \
&& poetry config virtualenvs.in-project true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# /bin/bash

# customize for freely
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "poetry3-poetry-pyenv",
"build": {
"dockerfile": "Dockerfile"
},

// 👇 Features to add to the Dev Container. More info: https://containers.dev/implementors/features.
// "features": {},

// 👇 Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// 👇 Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "",

// 👇 Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions":["ms-python.python", "njpwerner.autodocstring"]
}
},
"postCreateCommand": ".devcontainer/postCreateCommand.sh",

// 👇 Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# /bin/bash


# install project
poetry install --directory=electricity-rate-simulator

# for developer customized
.devcontainer/customizeCommand.sh
160 changes: 160 additions & 0 deletions serverside_challenge_1/challenges/yukikawamura/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": ".."
}
],
"settings": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# electricity-rate-simulator

## tl;dr
frontからuser情報を受け取って、電気料金をシミュレートするbackend api


## 構成

- applicationの構成
![electricity-rate-simulator-image](docs/images/electric-simulate-application.png)
- apiの構成
![api-configuration-image](docs/images/api-configuration.png)
- backend-apiの構成
![application-configuration-image](docs/images/application-configuration.png)


## 使い方

### GET /simulations

#### request
```url
<base_url>/simulations?contract=<contract>&usage=<usage>
```

|parameters|type|dscription|
|--|--|--|
|contract|int|契約アンペア数|
|usage|int|使用量|


#### response
```json
[{"provider": "会社名", "plan": "plan名", "price": "xxxxxx円"},{...}...]
```

## 開発者向け

### 事前設定
vscodeの[.devcontaier](https://code.visualstudio.com/docs/devcontainers/containers)機能を用いて開発


### projectのインストール
```bash
$ poetry install
```

### server 起動
```bash
$ cd electricity-rate-simulator/electricity_rate_simulator
$ poetry run python electricity-rate-simulator/electricity_rate_simulator/app.py
```

### apiの仕様
server起動後、以下のurlにアクセス
```bash
<base_url>/docs
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import uvicorn
from electricity_rate_simulator.core import ElectricSimulator
from electricity_rate_simulator.exception import (
ElectricSimulateClientError,
ElectricSimulationError,
)
from electricity_rate_simulator.model import UserData
from electricity_rate_simulator.utils import setup_logging
from fastapi import FastAPI, HTTPException
from pydantic import ValidationError

app = FastAPI()

lgr = setup_logging()


@app.get("/")
def get_root():
return {"app": "electricity-rate-simulator"}


@app.get("/simulations")
def electric_simulations_api(contract: int, usage: int):

try:
user_data = UserData(contract=contract, usage=usage)
except (ElectricSimulateClientError, ValidationError) as e:
raise HTTPException(status_code=400, detail=f"{e}")

try:
electric_simulator = ElectricSimulator()
return electric_simulator.simulate(user_data)
except ElectricSimulationError as e:
lgr.exception(e)
raise HTTPException(status_code=500, detail=f"{e}")


if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
Loading