Skip to content

Commit

Permalink
Refactoring in preparation to play with Vue
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrTopa committed Jul 7, 2022
1 parent 6761b3a commit 247fdb5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 52 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func apiRxPacketsGet(w http.ResponseWriter, req *http.Request, db *sql.DB) {
return
}

list, err := model.FetchRxPacketList(from, to, db)
list, err := model.FetchRxPacketListByTime(db, from, to)
if err != nil {
logger.Sugar().Errorw(
"Cannot fetch RxPacket records from DB",
Expand Down
16 changes: 11 additions & 5 deletions model/rxPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var (
SQL_RX_PACKET_INSERT = "INSERT INTO `RX_PACKET` (`TIMESTAMP`, `TYPE`, `CHANNEL`, `DIAL`, `FREQ`, `OFFSET`, `SNR`, `MODE`, `TIME_DRIFT`, `GRID`, `FROM`, `TO`, `TEXT`, `COMMAND`, `EXTRA`) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
SQL_RX_PACKET_LIST_BY_TIMESTAMP = "SELECT * FROM `RX_PACKET` WHERE `TIMESTAMP` > ? AND `TIMESTAMP` < ?"
SQL_RX_PACKET_INSERT = "INSERT INTO `RX_PACKET` (`TIMESTAMP`, `TYPE`, `CHANNEL`, `DIAL`, `FREQ`, `OFFSET`, `SNR`, `MODE`, `SPEED`, `TIME_DRIFT`, `GRID`, `FROM`, `TO`, `TEXT`, `COMMAND`, `EXTRA`) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
SQL_RX_PACKET_LIST_BY_TIMESTAMP = "SELECT `ID`, `TIMESTAMP`, `TYPE`, `CHANNEL`, `DIAL`, `FREQ`, `OFFSET`, `SNR`, `MODE`, `SPEED`, `TIME_DRIFT`, `GRID`, `FROM`, `TO`, `TEXT`, `COMMAND`, `EXTRA` FROM `RX_PACKET` WHERE `TIMESTAMP` > ? AND `TIMESTAMP` < ?"
)

type RxPacketObj struct {
Expand Down Expand Up @@ -80,6 +80,7 @@ func (obj *RxPacketObj) Insert(db *sql.DB) error {
&obj.Offset,
&obj.Snr,
&obj.Mode,
&obj.Speed,
&obj.TimeDrift,
&obj.Grid,
&obj.From,
Expand Down Expand Up @@ -112,6 +113,7 @@ func (obj *RxPacketObj) Scan(rows *sql.Rows) error {
&obj.Offset,
&obj.Snr,
&obj.Mode,
&obj.Speed,
&obj.TimeDrift,
&obj.Grid,
&obj.From,
Expand All @@ -128,16 +130,16 @@ func (obj *RxPacketObj) Scan(rows *sql.Rows) error {
return err
}

func FetchRxPacketList(from time.Time, to time.Time, db *sql.DB) ([]RxPacketObj, error) {
func fetchRxPackets(db *sql.DB, query string, args ...any) ([]RxPacketObj, error) {
l := make([]RxPacketObj, 0)

stmt, err := db.Prepare(SQL_RX_PACKET_LIST_BY_TIMESTAMP)
stmt, err := db.Prepare(query)
if err != nil {
return l, fmt.Errorf("error preparing SQL, caused by %w", err)
}
defer stmt.Close()

rows, err := stmt.Query(from, to)
rows, err := stmt.Query(args...)
if err != nil {
log.Fatal(err)
}
Expand All @@ -159,3 +161,7 @@ func FetchRxPacketList(from time.Time, to time.Time, db *sql.DB) ([]RxPacketObj,

return l, nil
}

func FetchRxPacketListByTime(db *sql.DB, from time.Time, to time.Time) ([]RxPacketObj, error) {
return fetchRxPackets(db, SQL_RX_PACKET_LIST_BY_TIMESTAMP, from, to)
}
7 changes: 4 additions & 3 deletions res/initDb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CREATE TABLE `RX_PACKET` (
`OFFSET` INTEGER NOT NULL,
`SNR` INTEGER NOT NULL,
`MODE` TEXT NOT NULL,
`SPEED` TEXT NOT NULL,
`TIME_DRIFT` INTEGER NOT NULL,
`GRID` TEXT,
`FROM` TEXT,
Expand All @@ -25,7 +26,7 @@ CREATE TABLE `RX_PACKET` (
`EXTRA` TEXT
);

CREATE INDEX RX_PACKET_DAY_IDX ON `RX_PACKET`(date(`TIMESTAMP`));
CREATE INDEX RX_PACKET_TIMESTAMP_IDX ON `RX_PACKET`(`TIMESTAMP`);

CREATE TABLE `RX_SPOT` (
`ID` INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -39,7 +40,7 @@ CREATE TABLE `RX_SPOT` (
`OFFSET` INTEGER NOT NULL
);

CREATE INDEX RX_SPOT_DAY_IDX ON `RX_SPOT`(date(`TIMESTAMP`));
CREATE INDEX RX_SPOT_TIMESTAMP_IDX ON `RX_SPOT`(`TIMESTAMP`);

CREATE TABLE `TX_FRAME` (
`ID` INTEGER PRIMARY KEY AUTOINCREMENT,
Expand All @@ -54,7 +55,7 @@ CREATE TABLE `TX_FRAME` (
`TONES` TEXT
);

CREATE INDEX TX_FRAME_DAY_IDX ON `TX_FRAME`(date(`TIMESTAMP`));
CREATE INDEX TX_FRAME_TIMESTAMP_IDX ON `TX_FRAME`(`TIMESTAMP`);


CREATE TABLE `STATION_INFO` (
Expand Down
43 changes: 43 additions & 0 deletions webapp/app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import axios from 'axios'

export default {
data() {
return {
stationInfo: {},
rigStatus: {},
days: {},
}
},
methods: {
fetchData() {
axios.get('/api/station-info').then(response => {
this.stationInfo = response.data;
});
axios.get('/api/rig-status').then(response => {
this.rigStatus = response.data;
});
axios.get('/api/rx-packets/list-days').then(response => {
this.days = response.data;
})
}
},
template: `
<h3>Rig status</h3>
<p>
{{ rigStatus }}
</p>
<br />
<h3>Station info</h3>
<p>
{{ stationInfo }}
</p>
<br />
<button @click="fetchData()">
Update
</button>`
}

51 changes: 8 additions & 43 deletions webapp/index.html
Original file line number Diff line number Diff line change
@@ -1,56 +1,21 @@
<header>
<script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js",
"axios": "https://cdn.skypack.dev/axios"
}
}
</script>

</script>
</header>
<body>
<div id="app">
<h3>Rig status</h3>
<p>
{{ rigStatus }}
</p>

<br />

<h3>Station info</h3>
<p>
{{ stationInfo }}
</p>

<br />

<button @click="fetchData()">
Update
</button>
</div>

<script type="module">
import { createApp } from 'vue'
import axios from 'axios'

createApp({
data() {
return {
stationInfo: {},
rigStatus: {},
days: {},
}
},
methods: {
fetchData() {
axios.get('/api/station-info').then(response => {
this.stationInfo = response.data;
});
axios.get('/api/rig-status').then(response => {
this.rigStatus = response.data;
});
axios.get('/api/rx-packets/list-days').then(response => {
this.days = response.data;
})
}
}
}).mount('#app')
</script>
import app from './app.mjs'
createApp(app).mount('#app')
</script>
</body>

0 comments on commit 247fdb5

Please sign in to comment.