示例#1
0
def forecast_update(site_key):
    site = Site.get_by_key_name(site_key)
    if site is None:
        return Response(status=404)

    forecast_url = "http://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/%s?format=application/json" % site_key

    result = urlfetch.fetch(forecast_url)
    if result.status_code == 200:
        forecast = parse_forecast(result.content)
        issued_date = parse_date(forecast["@dataDate"])
        for date, day in days(forecast):
            forecast_day = ForecastDay.get_by_key_name(
                make_key_name(site, date))
            if forecast_day is None:
                forecast_day = ForecastDay(key_name=make_key_name(site, date),
                                           forecast_date=date,
                                           site=site)
            forecast_day.site = site
            for timestep, data in day_timesteps(day):
                w = Forecast()
                w.issued = issued_date
                for k, v in data.items():
                    prop_name = snake_case(k)
                    if hasattr(w, prop_name):
                        if v == "missing":
                            v = None
                        setattr(w, prop_name, v)

                forecast_day.forecasts.add(timestep, w)

            forecast_day.save()
        site.save()
    return Response(status=204)
示例#2
0
def forecast_update(site_key):
    site = Site.get_by_key_name(site_key)
    if site is None:
        return Response(status = 404)

    forecast_url = "http://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/%s?format=application/json" % site_key

    result = urlfetch.fetch(forecast_url)
    if result.status_code == 200:
        forecast = parse_forecast(result.content)
        issued_date = parse_date(forecast["@dataDate"])
        for date, day in days(forecast):
            forecast_day = ForecastDay.get_by_key_name(make_key_name(site,date))
            if forecast_day is None:
                forecast_day = ForecastDay(key_name=make_key_name(site,date), forecast_date = date, site = site)
            forecast_day.site = site
            for timestep, data in day_timesteps(day):
                w = Forecast()
                w.issued = issued_date
                for k,v in data.items():
                    prop_name = snake_case(k)
                    if hasattr(w, prop_name):
                        if v == "missing":
                            v = None
                        setattr(w, prop_name, v)

                forecast_day.forecasts.add(timestep,w)

            forecast_day.save()
        site.save()
    return Response(status = 204)
示例#3
0
        def save_forecast(user_id, game_id, forecast, team_host_goals, team_guest_goals):
            forecast_ = db.session.query(Forecast).\
                filter(Forecast.user_id == user_id).\
                filter(Forecast.game_id == game_id).\
                first()

            if not forecast_:
                forecast_ = Forecast(user_id=user_id, game_id=game_id)

            forecast_.forecast = forecast
            forecast_.team_host_goals = team_host_goals
            forecast_.team_guest_goals = team_guest_goals

            db.session.add(forecast_)
            db.session.commit()
示例#4
0
def raw_forecast_to_domain_forecast(raw_forecast) -> Forecast:
    TIME_CURRENT = 'currently'

    raw_weather = raw_forecast[TIME_CURRENT]['icon']
    weather = Weather.from_value(raw_weather)

    return Forecast(weather)
示例#5
0
    def _parse_forecast(self, request_url):
        weatherbitio_reponse = requests.get(request_url)
        weatherbitio_reponse.raise_for_status()
        json = weatherbitio_reponse.json()
        headers = weatherbitio_reponse.headers

        return Forecast(json, weatherbitio_reponse, headers)
示例#6
0
    def get_forecast(self, data, date_from, date_to):
        forecasts = []
        for data_forecast in data['data']:  
            forecast = Forecast(data_forecast['date'], data_forecast['temperature']['max'], data_forecast['temperature']['min'], data_forecast['rain']['probability'], data_forecast['rain']['precipitation'], data['id'])
            forecasts.append(forecast)

        return forecasts
def add_forecasts():
    # 5-day 3-h forecast (free)
    url = 'http://api.openweathermap.org/data/2.5/forecast?id=6167865&APPID=631d59f50ab1841ba7af0f0f706e1505'
    response = requests.get(url, verify=True)

    if response:
        r = response.json()
        session = db.session  # creates a SQLAlchemy Session
        this_hour = datetime.datetime.now().replace(microsecond=0,
                                                    second=0,
                                                    minute=0)

        Forecast.query.filter(
            Forecast.id < this_hour -
            datetime.timedelta(hours=150)).delete()  # deletes old forecasts

        for dt in r['list']:
            data = Forecast(
                id=dt['dt_txt'],
                retrieval_time=datetime.datetime.now().replace(microsecond=0,
                                                               second=0,
                                                               minute=0),
                drybulb=dt['main']['temp'] - 273.15,  # Kelvin to Celcius
                relative_humidity=dt['main']['humidity'])
            session.merge(data)  # adds new forecasts

        session.commit()

    else:
        print('Unable to add forecasts!')
示例#8
0
def get_forecast_in_location(lat, lon, days=3):
    """Generic method to get the forecast in a specified location for a variable number of days

    The weather is returned in JSON format. More info about the responses can be found in the official API documentation
    site (http://openweathermap.org/current)"""

    # Checking that the message contains actually a location
    if (lon, lat) == (-1, -1):
        raise AttributeError  # Use a custom exception in the future

    TOKEN = get_OpenWeatherAPI_token()

    # Asking for the weather
    current_weather_url = "http://api.openweathermap.org/data/2.5/forecast"
    params = {'appid': TOKEN, 'lon': lon, 'lat': lat}
    rq = requests.get(current_weather_url, params=params)

    return Forecast(api_response=rq.text, days=days)
示例#9
0
    def get(self):
        args = self.parser.parse_args()

        username = _request_ctx_stack.top.current_user['sub']
        if len(args['group']) > 0:
            username = '******'.format(
                username)

        to_coord = _split_coordinates('to', args['to'])
        from_coord = _split_coordinates('from', args['from'])
        group_users = username.split(',')

        gm_routes = get_all_routes(from_coord, to_coord, limit=8)
        ds_forecasts = get_forecast(from_coord, to_coord)

        gm_routes = _prioritize(gm_routes, ds_forecasts, group_users)

        directions = map(
            lambda x: Directions(
                mode=x['_mode'], data=x, priority=x['_priority']), gm_routes)
        directions = Directions.objects.bulk_create(list(directions))

        for idx in range(len(gm_routes)):
            gm_routes[idx]['_id'] = str(directions[idx])

        forecasts = map(
            lambda f: Forecast(lat=f['latitude'], lng=f['longitude'], data=f),
            ds_forecasts)
        forecasts = Forecast.objects.bulk_create(list(forecasts))

        recommendations = Recommendations(available=directions,
                                          forecast=forecasts,
                                          user=username,
                                          created_on=datetime.utcnow())
        recommendations_id = recommendations.save()

        return {
            'to': to_coord,
            'from': from_coord,
            'directions': gm_routes,
            'forecast': ds_forecasts,
            'recommendation_id': str(recommendations_id.pk)
        }
示例#10
0
 def get_forecasts_for_city(city_id):
     url = "http://apiadvisor.climatempo.com.br/api/v1/forecast/locale/%s/days/15?token=%s" % (
         city_id, WeatherService._api_user_token)
     response = requests.get(url)
     if response.status_code != 200:
         return None
     else:
         response_data = response.json()
         city = CityRepository.save_city(
             City(city_id, response_data["name"], response_data["state"],
                  response_data["country"]))
         return [
             ForecastRepository.save_forecast(
                 Forecast(city, data["date"], data["rain"]["probability"],
                          data["rain"]["precipitation"],
                          data["temperature"]["min"],
                          data["temperature"]["max"]))
             for data in response_data["data"]
         ]
示例#11
0
def make_forecast(response):
    return Forecast(response.json(), response, response.headers)
示例#12
0
 def __init__(self, team_name, remaining_issues=None):
     self.model = ForecastModel(team_name)
     self.remaining_issues = remaining_issues
示例#13
0
class Forecast:
    def __init__(self, team_name, remaining_issues=None):
        self.model = ForecastModel(team_name)
        self.remaining_issues = remaining_issues

    def mk_throughput_line(self):
        df = self.model.throughput_df()
        fig = go.Figure(data=go.Scatter(
            x=df['end'].tolist(),
            y=df['throughput'].tolist(),
            name='throughput'
        ))
        if self.remaining_issues:
            self.add_uncertaintity_cone(df, fig)
        return fig

    def add_uncertaintity_cone(self, throughput_df, fig):
        current_throughput = throughput_df.iloc[-1]['throughput']
        target = current_throughput + self.remaining_issues
        quick, slow = self.model.uncertainty_cone_coords(target)

        x_a, y_a, x_b, y_b = quick
        fig.add_trace(go.Scatter(
            x=[x_a, x_b],
            y=[y_a, y_b],
            name='optimistic',
            mode='lines',
            line={
                'color': 'green',
                'dash': 'dash',
                'width': 1
            },
            legendgroup='optimistic'
        ))
        fig.add_trace(go.Scatter(
            x=[x_b, x_b],
            y=[y_b, 0],
            mode='lines',
            line={
                'color': 'green',
                'dash': 'dash',
                'width': 1
            },
            showlegend=False,
            legendgroup='optimistic'
        ))
        x_a, y_a, x_b, y_b = slow
        fig.add_trace(go.Scatter(
            x=[x_a, x_b],
            y=[y_a, y_b],
            name='pesimistic',
            mode='lines',
            line={
                'color': 'red',
                'dash': 'dash',
                'width': 1
            },
            legendgroup='optimistic'
        ))
        fig.add_trace(go.Scatter(
            x=[x_b, x_b],
            y=[y_b, 0],
            mode='lines',
            line={
                'color': 'red',
                'dash': 'dash',
                'width': 1
            },
            showlegend=False,
            legendgroup='pesimistic'
        ))

    def mk_story_point_scatter(self):
        self.model.throughput_df()
        return px.scatter(
            self.model.historic_df, x="story_points", y="days_taken")

    def mk_time_per_issue_scatter(self):
        percent_80 = self.model.historic_df['days_taken'].quantile(0.8)
        percent_50 = self.model.historic_df['days_taken'].quantile(0.5)
        issue_min = self.model.historic_df['end_time'].min(numeric_only=False)
        issue_max = self.model.historic_df['end_time'].max(numeric_only=False)

        quantiles_df = pd.DataFrame({
            'x': [issue_min, issue_max, issue_min, issue_max],
            'y': [percent_50, percent_50, percent_80, percent_80],
            'name': [
                f"50% {round(percent_50)} days",
                f"50% {round(percent_50)} days",
                f"80% {round(percent_80)} days",
                f"80% {round(percent_80)} days"]
        })

        fig = px.scatter(
            self.model.historic_df, x='end_time', y="days_taken",
            hover_name="name",
            hover_data={'end_time': False, 'days_taken': True})
        for trace in px.line(
                quantiles_df, x='x', y='y', color='name',
                color_discrete_sequence=px.colors.qualitative.Vivid).data:
            fig.add_trace(trace)
        return fig

    def mk_montecarlo_plot(self, num_issues=5):
        df = self.model.run_montecarlo(num_issues)
        percent_80 = df['days_taken'].quantile(0.8)
        percent_70 = df['days_taken'].quantile(0.7)
        percent_60 = df['days_taken'].quantile(0.6)
        percent_50 = df['days_taken'].quantile(0.5)
        issue_min = df['simulation'].min(numeric_only=False)
        issue_max = df['simulation'].max(numeric_only=False)
        quantiles_df = pd.DataFrame({
            'x': [
                issue_min, issue_max,
                issue_min, issue_max,
                issue_min, issue_max,
                issue_min, issue_max
                ],
            'y': [
                percent_50, percent_50,
                percent_60, percent_60,
                percent_70, percent_70,
                percent_80, percent_80
            ],
            'name': [
                f"50% {round(percent_50)} days",
                f"50% {round(percent_50)} days",
                f"60% {round(percent_60)} days",
                f"60% {round(percent_60)} days",
                f"70% {round(percent_70)} days",
                f"70% {round(percent_70)} days",
                f"80% {round(percent_80)} days",
                f"80% {round(percent_80)} days"]
        })

        fig = px.bar(df, x=df.index, y='days_taken')
        for trace in px.line(
                quantiles_df, x='x', y='y', color='name',
                color_discrete_sequence=px.colors.qualitative.Vivid).data:
            fig.add_trace(trace)
        return fig

    def render(self):
        return [
            html.P("Input the number of remaining issues to reach your goal."),
            dbc.Input(
                id="issues-input", type="number",
                min=0, step=1,
                value=self.remaining_issues),
            dcc.Graph(
                id='throuput',
                figure=self.mk_throughput_line()
            ),
            # dcc.Graph(
            #     id="story-points",
            #     figure=self.mk_story_point_scatter()),
            # dcc.Graph(
            #     id="overview",
            #     figure=self.mk_time_per_issue_scatter())
        ]

    @classmethod
    def callback_elements(cls):
        return [
            dbc.Input(id='issues-input')
        ]