Skip to content

Commit 366c641

Browse files
committed
stateflow updates
1 parent a559376 commit 366c641

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1819
-316
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
.idea/
22
venv/
3-
__pycache__
3+
__pycache__/
44
bin/
55
*.csv
66
.ipynb_checkpoints/
77
.DS_Store
8-
.serverless/
8+
.serverless/
9+
venv3.7/
10+
venv.zip

benchmark/frontend.py

+58-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,65 @@
1-
from benchmark.hotel.user import User
2-
from benchmark.hotel.search import Search, Geo, Rate
3-
from benchmark.hotel.reservation import Reservation
4-
from benchmark.hotel.profile import Profile, HotelProfile
5-
from benchmark.hotel.recommend import RecommendType, Recommend
1+
import os
2+
import sys
3+
4+
IS_PYFLINK = bool(os.getenv("PYFLINK"))
5+
6+
7+
if IS_PYFLINK:
8+
from pyflink.user import User
9+
from pyflink.search import Search, Geo, Rate
10+
from pyflink.reservation import Reservation
11+
from pyflink.hprofile import Profile, HotelProfile
12+
from pyflink.recommend import RecommendType, Recommend
13+
14+
print("I'm here!")
15+
else:
16+
from benchmark.hotel.user import User
17+
from benchmark.hotel.search import Search, Geo, Rate
18+
from benchmark.hotel.reservation import Reservation
19+
from benchmark.hotel.profile import Profile, HotelProfile
20+
from benchmark.hotel.recommend import RecommendType, Recommend
21+
622
from typing import List
723
from random import randrange
824
import os
925
import stateflow
1026
import asyncio
1127
from stateflow.client.fastapi.kafka import KafkaFastAPIClient, StateflowFailure
12-
from stateflow.client.fastapi.aws_gateway import AWSGatewayFastAPIClient
1328
import json
1429
import argparse
1530

1631

1732
IS_KAFKA = bool(os.getenv("KAFKA"))
33+
IS_PYFLINK = bool(os.getenv("PYFLINK"))
1834

1935
AMOUNT_OF_HOTELS = 80
2036
AMOUNT_OF_USERS = 500
2137
PARTITION_COUNT = os.getenv("NUM_PARTITIONS", AMOUNT_OF_HOTELS)
38+
TIMEOUT = int(os.getenv("TIMEOUT", 5))
39+
2240
print(IS_KAFKA)
41+
print(IS_PYFLINK)
2342

2443
if IS_KAFKA and False:
25-
producer_config = json.load(open(os.environ.get("PRODUCER_CONF"), 'r'))
26-
consumer_config = json.load(open(os.environ.get("CONSUMER_CONF"), 'r'))
27-
28-
client = KafkaFastAPIClient(stateflow.init(), statefun_mode=True, producer_config=producer_config, consumer_config=consumer_config)
44+
producer_config = json.load(open(os.environ.get("PRODUCER_CONF"), "r"))
45+
consumer_config = json.load(open(os.environ.get("CONSUMER_CONF"), "r"))
46+
47+
client = KafkaFastAPIClient(
48+
stateflow.init(),
49+
statefun_mode=True,
50+
producer_config=producer_config,
51+
consumer_config=consumer_config,
52+
timeout=TIMEOUT,
53+
)
54+
elif IS_PYFLINK:
55+
client = KafkaFastAPIClient(stateflow.init(), statefun_mode=False, timeout=TIMEOUT)
2956
else:
57+
from stateflow.client.fastapi.aws_gateway import AWSGatewayFastAPIClient
58+
3059
ADDRESS = os.environ.get("ADDRESS")
31-
TIMEOUT = int(os.getenv("TIMEOUT", 5))
32-
client = AWSGatewayFastAPIClient(stateflow.init(), api_gateway_url=ADDRESS, timeout=TIMEOUT)
60+
client = AWSGatewayFastAPIClient(
61+
stateflow.init(), api_gateway_url=ADDRESS, timeout=TIMEOUT
62+
)
3363
app = client.get_app()
3464

3565

@@ -46,7 +76,7 @@ async def check_availability_hotel(hotel_id: str, in_date, out_date):
4676

4777
@app.get("/search")
4878
async def search(lat: float, lon: float, in_date: str, out_date: str):
49-
try :
79+
try:
5080
# Here we select the 'partitioned services'.
5181
partition_id = _get_partition()
5282
search: Search = await Search.by_key(f"search-service-{partition_id}")
@@ -60,7 +90,12 @@ async def search(lat: float, lon: float, in_date: str, out_date: str):
6090
# All available hotels.
6191
available_hotels: List[str] = []
6292

63-
availability = asyncio.as_completed([check_availability_hotel(hotel_id, in_date, out_date) for hotel_id in nearby_hotels])
93+
availability = asyncio.as_completed(
94+
[
95+
check_availability_hotel(hotel_id, in_date, out_date)
96+
for hotel_id in nearby_hotels
97+
]
98+
)
6499

65100
for fut in availability:
66101
hotel_id, is_available = await fut
@@ -78,15 +113,22 @@ async def search(lat: float, lon: float, in_date: str, out_date: str):
78113
async def recommend(
79114
recommendation_type: RecommendType, lat: float = 0.0, lon: float = 0.0
80115
):
116+
if IS_PYFLINK:
117+
recommendation_type = recommendation_type.value
118+
81119
# Here we select the 'partitioned services'.
82-
recommend: Recommend = await Recommend.by_key(f"recommend-service-{_get_partition()}")
120+
recommend: Recommend = await Recommend.by_key(
121+
f"recommend-service-{_get_partition()}"
122+
)
83123
profile: Profile = await Profile.by_key(f"profile-service-{_get_partition()}")
84124

85125
# Get recommendations.
86126
recommendations: List[str] = await recommend.recommend(
87-
recommendation_type, {"lat": lat, "lon": lon}
127+
recommendation_type.value, {"lat": lat, "lon": lon}
88128
)
89129

130+
print(f"Found recommendations {recommendations}")
131+
90132
# Get all profiles of recommended hotels.
91133
profiles: List[HotelProfile] = await profile.get_profiles(recommendations)
92134

benchmark/pyflink/__init__.py

Whitespace-only changes.

benchmark/pyflink/data.zip

2 KB
Binary file not shown.

benchmark/pyflink/data/geo.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"hotelId": "1",
4+
"lat": 37.7867,
5+
"lon": -122.4112
6+
},
7+
{
8+
"hotelId": "2",
9+
"lat": 37.7854,
10+
"lon": -122.4005
11+
},
12+
{
13+
"hotelId": "3",
14+
"lat": 37.7834,
15+
"lon": -122.4071
16+
},
17+
{
18+
"hotelId": "4",
19+
"lat": 37.7936,
20+
"lon": -122.3930
21+
},
22+
{
23+
"hotelId": "5",
24+
"lat": 37.7831,
25+
"lon": -122.4181
26+
},
27+
{
28+
"hotelId": "6",
29+
"lat": 37.7863,
30+
"lon": -122.4015
31+
}
32+
]

benchmark/pyflink/data/hotels.json

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
[
2+
{
3+
"id": "1",
4+
"name": "Clift Hotel",
5+
"phoneNumber": "(415) 775-4700",
6+
"description": "A 6-minute walk from Union Square and 4 minutes from a Muni Metro station, this luxury hotel designed by Philippe Starck features an artsy furniture collection in the lobby, including work by Salvador Dali.",
7+
"address": {
8+
"streetNumber": "495",
9+
"streetName": "Geary St",
10+
"city": "San Francisco",
11+
"state": "CA",
12+
"country": "United States",
13+
"postalCode": "94102",
14+
"lat": 37.7867,
15+
"lon": -122.4112
16+
}
17+
},
18+
{
19+
"id": "2",
20+
"name": "W San Francisco",
21+
"phoneNumber": "(415) 777-5300",
22+
"description": "Less than a block from the Yerba Buena Center for the Arts, this trendy hotel is a 12-minute walk from Union Square.",
23+
"address": {
24+
"streetNumber": "181",
25+
"streetName": "3rd St",
26+
"city": "San Francisco",
27+
"state": "CA",
28+
"country": "United States",
29+
"postalCode": "94103",
30+
"lat": 37.7854,
31+
"lon": -122.4005
32+
}
33+
},
34+
{
35+
"id": "3",
36+
"name": "Hotel Zetta",
37+
"phoneNumber": "(415) 543-8555",
38+
"description": "A 3-minute walk from the Powell Street cable-car turnaround and BART rail station, this hip hotel 9 minutes from Union Square combines high-tech lodging with artsy touches.",
39+
"address": {
40+
"streetNumber": "55",
41+
"streetName": "5th St",
42+
"city": "San Francisco",
43+
"state": "CA",
44+
"country": "United States",
45+
"postalCode": "94103",
46+
"lat": 37.7834,
47+
"lon": -122.4071
48+
}
49+
},
50+
{
51+
"id": "4",
52+
"name": "Hotel Vitale",
53+
"phoneNumber": "(415) 278-3700",
54+
"description": "This waterfront hotel with Bay Bridge views is 3 blocks from the Financial District and a 4-minute walk from the Ferry Building.",
55+
"address": {
56+
"streetNumber": "8",
57+
"streetName": "Mission St",
58+
"city": "San Francisco",
59+
"state": "CA",
60+
"country": "United States",
61+
"postalCode": "94105",
62+
"lat": 37.7936,
63+
"lon": -122.3930
64+
}
65+
},
66+
{
67+
"id": "5",
68+
"name": "Phoenix Hotel",
69+
"phoneNumber": "(415) 776-1380",
70+
"description": "Located in the Tenderloin neighborhood, a 10-minute walk from a BART rail station, this retro motor lodge has hosted many rock musicians and other celebrities since the 1950s. It’s a 4-minute walk from the historic Great American Music Hall nightclub.",
71+
"address": {
72+
"streetNumber": "601",
73+
"streetName": "Eddy St",
74+
"city": "San Francisco",
75+
"state": "CA",
76+
"country": "United States",
77+
"postalCode": "94109",
78+
"lat": 37.7831,
79+
"lon": -122.4181
80+
}
81+
},
82+
{
83+
"id": "6",
84+
"name": "St. Regis San Francisco",
85+
"phoneNumber": "(415) 284-4000",
86+
"description": "St. Regis Museum Tower is a 42-story, 484 ft skyscraper in the South of Market district of San Francisco, California, adjacent to Yerba Buena Gardens, Moscone Center, PacBell Building and the San Francisco Museum of Modern Art.",
87+
"address": {
88+
"streetNumber": "125",
89+
"streetName": "3rd St",
90+
"city": "San Francisco",
91+
"state": "CA",
92+
"country": "United States",
93+
"postalCode": "94109",
94+
"lat": 37.7863,
95+
"lon": -122.4015
96+
}
97+
}
98+
]

benchmark/pyflink/data/inventory.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[
2+
{
3+
"hotelId": "1",
4+
"code": "RACK",
5+
"inDate": "2015-04-09",
6+
"outDate": "2015-04-10",
7+
"roomType": {
8+
"bookableRate": 109.00,
9+
"code": "KNG",
10+
"description": "King sized bed",
11+
"totalRate": 109.00,
12+
"totalRateInclusive": 123.17
13+
}
14+
},
15+
{
16+
"hotelId": "2",
17+
"code": "RACK",
18+
"inDate": "2015-04-09",
19+
"outDate": "2015-04-10",
20+
"roomType": {
21+
"bookableRate": 139.00,
22+
"code": "QN",
23+
"description": "Queen sized bed",
24+
"totalRate": 139.00,
25+
"totalRateInclusive": 153.09
26+
}
27+
},
28+
{
29+
"hotelId": "3",
30+
"code": "RACK",
31+
"inDate": "2015-04-09",
32+
"outDate": "2015-04-10",
33+
"roomType": {
34+
"bookableRate": 109.00,
35+
"code": "KNG",
36+
"description": "King sized bed",
37+
"totalRate": 109.00,
38+
"totalRateInclusive": 123.17
39+
}
40+
}
41+
]

0 commit comments

Comments
 (0)