Skip to content

Commit d89872e

Browse files
committed
stocks
1 parent edddc3f commit d89872e

6 files changed

+42
-55
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ load: dockerbuild
77
portfwd:
88
kubectl port-forward service/mywebapp 8080:8088
99

10-
pgportfwd:
10+
dbforward:
1111
kubectl port-forward service/cluster-example-rw 5432:5432
1212

1313
apply-migrations:

cheatsheet.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ curl -H "Accept: application/json" localhost:8080/
77

88
hey -H "Accept: application/json" -z 5s http://localhost:8080/
99

10-
hey -z 200s -q 1 -c 2 http://localhost:8080/update
10+
hey -z 100s -q 1 -c 2 http://localhost:8080/update
1111

1212
## kubernetes
1313

@@ -19,4 +19,4 @@ pq: remaining connection slots are reserved for non-replication superuser connec
1919

2020
## app
2121

22-
PG_PASSWORD=S3xBbFUX0pQ1t8VVgYxOqVbDRDufAmdQIi5Q2AnwHx457qREWWEhDuJbIVKNP9mh PG_USER=app go run main.go
22+
PG_PASSWORD=KPzgXuDJ8P8FzmlmGO9Y7KgPXF1BszuHKWMuEPtiGhHVxVpiSc76pTD0b2V7cmOD PG_USER=app go run main.go

liquibase.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ liquibase.command.url=jdbc:postgresql://localhost:5432/app
88
liquibase.command.username: app
99

1010
# Enter the password for your Target database.
11-
liquibase.command.password: S3xBbFUX0pQ1t8VVgYxOqVbDRDufAmdQIi5Q2AnwHx457qREWWEhDuJbIVKNP9mh
11+
liquibase.command.password: KPzgXuDJ8P8FzmlmGO9Y7KgPXF1BszuHKWMuEPtiGhHVxVpiSc76pTD0b2V7cmOD
1212

1313
liquibase.hub.mode=off

main.go

+29-30
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515
_ "github.com/lib/pq"
1616
)
1717

18-
const bondTableTpl string = `
18+
const stockTableTpl string = `
1919
<html>
2020
<h3>Stonks</h3>
2121
<h3>from most recently updated</h3>
2222
<table>
2323
{{ range . }}
2424
<tr>
25-
<td>{{ .Bond }}</td>
26-
<td>{{ .Factor }}</td>
25+
<td>{{ .Stock }}</td>
26+
<td>{{ .StockValue }}</td>
2727
<td>{{ .Date }}</td>
2828
</tr>
2929
{{ end }}
@@ -33,20 +33,20 @@ const bondTableTpl string = `
3333

3434
const indexPage string = `
3535
<html>
36-
<h3>Hello</h3>
36+
<h3>Hello KubeCon!</h3>
3737
3838
<ul>
39-
<li>Get <a href="/latest">the latest bond values</a></li>
39+
<li>Get <a href="/latest">the latest stock values</a></li>
4040
<li>Add <a href="/update">random stock values</a></li>
4141
</ul>
4242
</html>
4343
`
4444

45-
// bondState represents the value of a bond at a given time
46-
type bondState struct {
47-
Bond string
48-
Factor float64
49-
Date time.Time
45+
// stockTicker represents the value of a stock at a given time
46+
type stockTicker struct {
47+
Stock string
48+
StockValue float64
49+
Date time.Time
5050
}
5151

5252
func main() {
@@ -56,7 +56,6 @@ func main() {
5656

5757
pgUser := os.Getenv("PG_USER")
5858
pgPass := os.Getenv("PG_PASSWORD")
59-
pgService := "cluster-example-rw"
6059

6160
port := 8080
6261

@@ -67,20 +66,20 @@ func main() {
6766
var dbConnString string
6867
if inside {
6968
dbConnString = fmt.Sprintf("postgres://%s:%s@%s/app?sslmode=require",
70-
pgUser, pgPass, pgService)
69+
pgUser, pgPass, "cluster-example-rw")
7170
} else {
7271
dbConnString = fmt.Sprintf("postgres://%s:%s@%s/app?sslmode=require",
7372
pgUser, pgPass, "localhost")
7473
}
7574

76-
bondTable, err := template.New("table").Parse(bondTableTpl)
75+
stockTable, err := template.New("table").Parse(stockTableTpl)
7776
if err != nil {
7877
log.Fatalf("could not parse template: %v", err)
7978
}
8079

8180
rand.Seed(time.Now().UnixNano())
8281

83-
// HTTP dispatch table
82+
// HTTP ENDPOINTS
8483

8584
// handle route using handler function
8685
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -110,31 +109,31 @@ func main() {
110109
}
111110

112111
rows, err := db.Query(`
113-
select bond, date, factor
112+
select stock, date, stock_value
114113
from (
115-
select bond, rank() over wd as rank,
114+
select stock, rank() over wd as rank,
116115
first_value(date) over wd as date,
117-
first_value(factor) over wd as factor
118-
from factors
119-
window wd as (partition by bond order by date desc)
116+
first_value(stock_value) over wd as stock_value
117+
from stock_values
118+
window wd as (partition by stock order by date desc)
120119
) as latest where rank = 1
121-
order by date desc, bond;
120+
order by date desc, stock;
122121
`)
123122
if err != nil {
124123
http.Error(w, err.Error(), http.StatusInternalServerError)
125124
return
126125
}
127126
defer rows.Close()
128127

129-
var bonds []bondState
128+
var stocks []stockTicker
130129
for rows.Next() {
131-
var bondS bondState
132-
err = rows.Scan(&bondS.Bond, &bondS.Date, &bondS.Factor)
130+
var stockT stockTicker
131+
err = rows.Scan(&stockT.Stock, &stockT.Date, &stockT.StockValue)
133132
if err != nil {
134133
http.Error(w, err.Error(), http.StatusInternalServerError)
135134
return
136135
}
137-
bonds = append(bonds, bondS)
136+
stocks = append(stocks, stockT)
138137
}
139138

140139
if rErr := rows.Err(); rErr != nil {
@@ -144,14 +143,14 @@ order by date desc, bond;
144143

145144
if r.Header.Get("Accept") == "application/json" {
146145
jsonWriter := json.NewEncoder(w)
147-
err = jsonWriter.Encode(bonds)
146+
err = jsonWriter.Encode(stocks)
148147
if err != nil {
149148
http.Error(w, err.Error(), http.StatusInternalServerError)
150149
return
151150
}
152151
} else {
153152
w.Header().Set("Content-Type", "text/html")
154-
err = bondTable.Execute(w, bonds)
153+
err = stockTable.Execute(w, stocks)
155154
if err != nil {
156155
http.Error(w, err.Error(), http.StatusInternalServerError)
157156
return
@@ -191,10 +190,10 @@ order by date desc, bond;
191190

192191
for i := 0; i < 5; i++ {
193192
n := rand.Intn(50) + 1 // between 1 and 50, like our stocks
194-
bond := fmt.Sprintf("bn_%d", n)
193+
stock := fmt.Sprintf("stock_%d", n)
195194
_, err = tx.Exec(
196-
`INSERT INTO factors(bond, date, factor) values ($1, $2, $3)`,
197-
bond, time.Now(), rand.Float64())
195+
`INSERT INTO stock_values(stock, date, stock_value) values ($1, $2, $3)`,
196+
stock, time.Now(), rand.Float64())
198197
if err != nil {
199198
http.Error(w, err.Error(), http.StatusInternalServerError)
200199
return
@@ -205,7 +204,7 @@ order by date desc, bond;
205204
})
206205

207206
// listen to port
208-
log.Println("ENV", podName, podEnv, podIP, pgPass, pgService, pgUser)
207+
log.Println("ENV", podName, podEnv, podIP, pgPass, "cluster-example-rw", pgUser)
209208
log.Printf("running the server, listening on %d\n", port)
210209
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
211210
}

migrations/example-changelog.sql

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
--liquibase formatted sql
22

33
--changeset jaime.silvela:1 labels:kubecon-demo
4-
--comment: let's start off with 50 bonds
5-
create table bonds as
6-
select 'bn_' || generate_series as bond
4+
--comment: let's start off with 50 stocks
5+
create table stocks as
6+
select 'stock_' || generate_series as stock
77
from generate_series(1, 50);
8-
--rollback DROP TABLE bonds;
8+
--rollback DROP TABLE stocks;
99

1010
--changeset jaime.silvela:2 labels:kubecon-demo
11-
--comment: lets add a bunch of cool factors. Cool beans!
12-
create table factors as
11+
--comment: lets add a bunch of random stock values
12+
create table stock_values as
1313
with dates as (
1414
SELECT generate_series as date
1515
FROM generate_series('2020-01-01 00:00'::timestamp,
1616
'2022-05-15 00:00', '24 hours')
1717
)
18-
select bond, date, random() as factor
19-
from bonds cross join dates;
20-
--rollback DROP TABLE factors;
18+
select stock, date, random() as stock_value
19+
from stocks cross join dates;
20+
--rollback DROP TABLE stock_values;

webapp-deploy.yaml

-12
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ spec:
3333
ports:
3434
- containerPort: 8080
3535
env:
36-
- name: MY_POD_NAME
37-
valueFrom:
38-
fieldRef:
39-
fieldPath: metadata.name
40-
- name: MY_POD_NAMESPACE
41-
valueFrom:
42-
fieldRef:
43-
fieldPath: metadata.namespace
44-
- name: MY_POD_IP
45-
valueFrom:
46-
fieldRef:
47-
fieldPath: status.podIP
4836
- name: PG_PASSWORD
4937
valueFrom:
5038
secretKeyRef:

0 commit comments

Comments
 (0)