Code Monkey home page Code Monkey logo

weather's Introduction

Weather

Example of application, which regularly get weather predictions from different external providers, and return to users average value.

You can try it here: http://178.62.245.179/

Application architecture

Technologies

I use PHP 7.2, Postgres 11, Symfony 4.3, Docker for development environment, and DigitalOcean's droplet with ubuntu on production

Main idea

We get data from our partners via API. And we've set a threshold of 1 minute to invalidate this data. It's possible to request our partners during each request to our API and keep a cache of the response in Memcache for 1 minute. But in this case, the first request, when we don have any cache, can take a lot of time (especially, if we will have more partners). That's why I decided to udpate a partner's data to our database by using a command and do it each minute by a cronjob.

Important parts of code:

  • src/Integration/Prediction - Dummy integrations to weather predictions from partner for specific date
  • src/DTO/PredictionData.php - Data Transfer Object (DTO) for keeping data from partner in original format
  • src/DTO/Prediction.php - DTO for kepeng data from partner in common useful format
  • src/Service/PredictionDataParser/PredictionDataParser.php - Service for parse PredictionData to array of Predicitons. It's easy to add parser for new format - you just need to implement SpecificPredictionDataParserInterface and register it in PredictionDataParser
  • src/Service/ScaleConverter/ScaleConverter.php - service for convert different temperature scales to celsius and back. It's easy to add converter for new format - you just need to implement SpecificScaleConverterInterface and register it in ScaleConverter
  • src/Entity/WeatherPrediction.php - Entity for saving predicitons to database. I keep temperature in single scale (celsius), because I think it makes code more easy and clear.
  • src/Service/PredictionsActualizer.php - Service for updating predictions in database from provider's API
  • src/Repository/WeatherPredictionRepository.php - AveragePredicitons caclulate on database side in celsius scale, you can see it in repository
  • src/Controller/Api/Weather - Endpoint classes, like Controller, Validator, Mapper. Mapper can convert temperature to any available scale in ScaleConverter
  • src/Command/WeatherPredictionsCollectCommand.php - command for updating predictions
  • crontab - crontab for running command to update predicitons
  • public/index.html - place for simple SPA

TODO

  • Create tests
  • Install nelmio-bundle and create auto-documentation
  • Create simple frontend (public/index.html)

Dev-environment

Installation

  1. docker-compose up -d
  2. docker-compose exec php-fpm composer install
  3. docker-compose exec php-fpm cp /application/.env.dist /application/.env
  4. docker-compose exec php-fpm php bin/console doctrine:migtrations:migrate
  5. Check link https:/127.0.0.1:8080

Documentation

Command

weather:predictions:collect

Usage: php bin/console weather:predictions:collect [<upcomingDaysNumber>]

Arguments: upcomingDaysNumber For what number of upcoming days do you need to update a weather predictions? [default: 10]

Endpoint

GET /api/weather

GET /api/weather?city=Amsterdam&date=2019-11-29&scale=fahrenheit

Parameters:

city=Amsterdam // required
date=2019-11-29 // optional (default: current day) date for predicions in 'Y-m-d' format. It isn't be possible to search dates greater than the current day + 10 days.
scale=fahrenheit // optional (default: 'celsius'), scale like 'celsius', 'fahrenheit'

Example of response, if everything is fine: 200 OK

{
    predictions: [
        {
            city: "Amsterdam",
            predictionsDateTime: "2019-11-29 00:00:00",
            scale: "fahrenheit",
            temperature: 37,
            updatedAt: 1574271663
        },
        ...
    ]
}

Example of response, if request is incorrect: 400 Bad Request

{
    success: false,
    errors: [
        {
            propertyPath: "city",
            message: "Field can't be empty."
        },
        {
            propertyPath: "day",
            message: "Predictions are available only for today or upcoming 10 days"
        },
        {
            propertyPath: "scale",
            message: "Unsupported scale: Réaumur"
        }
    ]
}

weather's People

Contributors

rsavinkov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.