Skip to content

Commit

Permalink
Gus/restore tracksight (#1300)
Browse files Browse the repository at this point in the history
### Changelist 
<!-- Give a list of the changes covered in this PR. This will help both
you and the reviewer keep this PR within scope. -->

Restore tracksight to the matthew yung era
  • Loading branch information
gtaharaedmonds authored Jun 6, 2024
1 parent 9efe2e3 commit 6d53e94
Show file tree
Hide file tree
Showing 57 changed files with 6,190 additions and 8,802 deletions.
36 changes: 18 additions & 18 deletions software/tracksight/backend/app/flask_apps/http_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Tuple, Dict, List
from datetime import datetime, timedelta
import logging
from datetime import datetime

from influx_handler import InfluxHandler

Expand All @@ -15,8 +16,16 @@
app = Blueprint("http_app", __name__)


MAX_POINTS_HISTORIC = 1000
MAX_POINTS_LIVE = 100


def submit_query(
measurement: str, signals: List[str], start_epoch: int, end_epoch: int
measurement: str,
signals: List[str],
start_epoch: int,
end_epoch: int,
max_points: int,
) -> Dict[str, Dict]:
if (
measurement is None
Expand All @@ -41,6 +50,7 @@ def submit_query(
measurement=measurement,
signals=signals,
time_range=(start_epoch, end_epoch),
max_points=max_points,
)
except Exception as e:
return {"error": str(e)}, 500
Expand All @@ -62,15 +72,15 @@ def health() -> Tuple[Dict, int]:
return {"status": "healthy"}, 200


@app.route("/data/measurements", methods=["GET"])
@app.route("/measurements", methods=["GET"])
def return_all_measurements() -> Tuple[List[str], int]:
"""
:returns Page displaying all measurements in the database.
"""
return InfluxHandler.get_measurements(), 200


@app.route("/data/measurement/<string:measurement>/signals", methods=["GET"])
@app.route("/signals/<string:measurement>", methods=["GET"])
def return_signals_for_measurement(measurement: str) -> Tuple[List[str], int]:
"""
:param measurement: Measurement to fetch fields for.
Expand All @@ -79,22 +89,12 @@ def return_signals_for_measurement(measurement: str) -> Tuple[List[str], int]:
return InfluxHandler.get_signals(measurement=measurement), 200


@app.route("/data/measurement/wtf", methods=["GET"])
def return_live_signals() -> Tuple[List[str], int]:
"""
:param measurement: Measurement to fetch fields for.
:returns Page displaying all fields for a specific measurement.
"""
return InfluxHandler.get_signals(measurement="live"), 200


@app.route("/data/query/live", methods=["GET"])
@app.route("/query/live", methods=["GET"])
def return_live_query() -> Dict[str, Dict]:
"""
:returns Page displaying the result of a single query.
"""
params = request.args
measurement = "live"
signals: list[str] | None = params.get("signals")

start_time = datetime.now() - timedelta(hours=1)
Expand All @@ -103,17 +103,16 @@ def return_live_query() -> Dict[str, Dict]:
start_epoch = int(start_time.timestamp())
end_epoch = int(end_time.timestamp())

logger.info(f"Start request: {start_epoch}, {end_epoch}")

return submit_query(
measurement=measurement,
measurement="live",
signals=signals,
start_epoch=start_epoch,
end_epoch=end_epoch,
max_points=MAX_POINTS_HISTORIC,
)


@app.route("/data/query", methods=["GET"])
@app.route("/query", methods=["GET"])
def return_query() -> Dict[str, Dict]:
"""
:returns Page displaying the result of a single query.
Expand All @@ -129,4 +128,5 @@ def return_query() -> Dict[str, Dict]:
signals=signals,
start_epoch=start_epoch,
end_epoch=end_epoch,
max_points=MAX_POINTS_LIVE,
)
3 changes: 2 additions & 1 deletion software/tracksight/backend/app/influx_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def query(
measurement: str,
signals: List[str],
time_range: Tuple[str, str],
max_points: int = 8000, # TODO implement
max_points: int,
ms_resolution: int = 100, # TODO implement
) -> dict[str, dict]:
"""
Expand All @@ -118,6 +118,7 @@ def query(
r._measurement == "{measurement}" and
r._field == "value" and
contains(value: r.signal, set: {str(signals).replace("'", '"')}))
|> tail(n: {max_points})
"""

query_result = {signal: {"times": [], "values": []} for signal in signals}
Expand Down
6 changes: 3 additions & 3 deletions software/tracksight/backend/app/signal_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class SignalUtil:

@classmethod
def setup(cls, port: str, app):
cls.ser = serial.Serial(port=port, baudrate=57600, timeout=1)
cls.ser.reset_input_buffer()
cls.ser.reset_output_buffer()
# cls.ser = serial.Serial(port=port, baudrate=57600, timeout=1)
# cls.ser.reset_input_buffer()
# cls.ser.reset_output_buffer()
cls.is_setup = True
cls.app = app

Expand Down
2 changes: 1 addition & 1 deletion software/tracksight/backend/app/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
file = file.strip()
logger.info(f"Uploading data file: {file}")

path = os.path.join("data", file)
path = os.path.join(app_dir, "..", "data", file)
df = pd.read_csv(path)
InfluxHandler.write(df=df, measurement=file)

Expand Down
25 changes: 12 additions & 13 deletions software/tracksight/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

FROM node:18-alpine AS base

# Install dependencies only when needed
Expand All @@ -14,11 +15,11 @@ WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


# Rebuild the source code only when needed
Expand All @@ -33,11 +34,11 @@ COPY . .
ENV NEXT_TELEMETRY_DISABLED 1

RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

# Production image, copy all the files and run next
FROM base AS runner
Expand All @@ -50,8 +51,6 @@ ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
Expand All @@ -69,4 +68,4 @@ ENV PORT 3000

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD HOSTNAME="0.0.0.0" node server.js
CMD HOSTNAME="0.0.0.0" node server.js
44 changes: 28 additions & 16 deletions software/tracksight/frontend/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
# Tracksight Frontend

## Dev Server
```
npm run dev
```

## Deployment Server
Please refer to the `frontend` service of `sofware/tracksight/docker-compose.yml` for notes on how to deploy the frontend server.

## TODOs
- Move shared zoom data to a shared context
- Migrate all fetch calls to [Tanstack Query](https://tanstack.com/query/latest)
- Move from src to root directory
- Better chart library
- When auto fetch times when zooming
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

1. Install Node.js > v18.8.0. `sudo apt install node` on Linux will install an outdated version so you need to manually seek it(should probably use `nvm`).
2. Install packages with `npm install` inside this directory
3. Run the development server: `npm run dev`

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
17 changes: 0 additions & 17 deletions software/tracksight/frontend/components.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FLASK_URL } from "./constants";
import FLASK_URL from "./src/app/page"

// Function for getting data
const getRealtimeData = async (path: string) => {
Expand Down
25 changes: 25 additions & 0 deletions software/tracksight/frontend/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getDatabase } from "firebase/database";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional

// this is matt's api key, you can get your own by going to the firebase console if needed (it is free)
const firebaseConfig = {
apiKey: "AIzaSyDkLiwQaygM79Q1OfC5YHZS0lgp_mix4jo",
authDomain: "telemetry-5bc72.firebaseapp.com",
projectId: "telemetry-5bc72",
storageBucket: "telemetry-5bc72.appspot.com",
messagingSenderId: "201527675645",
appId: "1:201527675645:web:5b99c05cc6357ac6964c94",
measurementId: "G-1RSQKEB2SM"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);

export default database;
2 changes: 1 addition & 1 deletion software/tracksight/frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const nextConfig = {
output: 'standalone'
}

module.exports = nextConfig
module.exports = nextConfig
Loading

0 comments on commit 6d53e94

Please sign in to comment.