Skip to content

Commit

Permalink
Merge pull request #259 from ueckoken/impl-image-autooperation
Browse files Browse the repository at this point in the history
  • Loading branch information
Azuki-bar authored Nov 24, 2023
2 parents 9d7ca41 + 806dd7a commit 73acd10
Show file tree
Hide file tree
Showing 18 changed files with 415 additions and 131 deletions.
18 changes: 4 additions & 14 deletions .github/workflows/create-proto.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: compile_PB
name: compile PB

on:
push:
Expand All @@ -18,19 +18,9 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 20
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT
working-directory: ./frontend/dashboard
- uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn install --immutable
working-directory: ./frontend/dashboard
- name: Corepack Enable
run: corepack enable
- run: pnpm install --frozen-lockfile
- name: Set up Go
uses: actions/setup-go@v4
with:
Expand Down
1 change: 1 addition & 0 deletions backend/auto-operation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.js
45 changes: 28 additions & 17 deletions backend/auto-operation/main.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { createPromiseClient } from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-node";
import { StateManagerService } from "./proto/state/v1/state_connectweb.js";
import { StateManagerService } from "./proto/state/v1/state_connect.js";
import { BlockStateEnum } from "./proto/state/v1/block_pb.js";
import { StopStateEnum } from "./proto/state/v1/stop_pb.js";
import { PointStateEnum } from "./proto/state/v1/point_pb.js";
import { Priority } from "./proto/state/v1/train_pb.js";

let mapConfig;
type MapConfig = {
stopBlocks: { [key: string]: string },
stations: {
[key: string]: {
capacity: number,
}
}
}
let mapConfig: MapConfig;

const SERVER_ADDR = process.env['SERVER_ADDR'] ?? 'http://localhost:8080'
const transport = createConnectTransport(
{
httpVersion: "2",
baseUrl: "http://localhost:8080",
baseUrl: SERVER_ADDR,
});
const client = createPromiseClient(StateManagerService, transport);

async function loadConfig() {
mapConfig = await import("./map/chofufes-2023.json");
mapConfig = (await import("./map/chofufes-2023.json")).default;
}

loadConfig();
Expand All @@ -27,7 +36,7 @@ async function addTest() {
train: {
trainId: "test",
positionId: "shinjuku_b1",
priority: Priority.PRIORITY_HIGH,
priority: Priority.HIGH,
uuid: "test",
destination: "hashimoto_up_s1"
}
Expand All @@ -36,7 +45,7 @@ async function addTest() {
train: {
trainId: "test2",
positionId: "shinjuku_s1",
priority: Priority.PRIORITY_LOW,
priority: Priority.LOW,
uuid: "test",
destination: "hachioji_up_s1"
}
Expand Down Expand Up @@ -84,14 +93,14 @@ async function main() {
// 通過待ちができるかどうか
const stationName = train.positionId.split("_")[0] + "_" + train.positionId.split("_")[1];
const capacity = mapConfig.stations[stationName];
if (capacity > 1 && train.Priority === Priority.PRIORITY_LOW) {
if (capacity.capacity > 1 && train.priority === Priority.LOW) {
// 通過待ちが可能な駅で、PriorityがLOWの列車はPriorityがHIGHの列車が停車するまで待つ
const highPriorityTrains = trains.filter(t => (t.positionId.includes(stationName)) && t.Priority === Priority.PRIORITY_HIGH);
const highPriorityTrains = trains.filter(t => (t.positionId.includes(stationName)) && t.priority === Priority.HIGH);
if (highPriorityTrains.length > 0) continue;
}
const stop = stops.find(s => s.id === train.positionId);
// 問題ないならGOにする
if (stop.state !== StopStateEnum.STOP_STATE_GO) {
if (stop && stop.state !== StopStateEnum.STOP_STATE_GO) {
await client.updateStopState({
"state": {
"id": stop.id,
Expand All @@ -108,8 +117,8 @@ async function main() {
// 桜上水上りポイント
// デフォルトではSTRAIGHTにして、sakurajosui_up_s1がONならREVERSEにする
const sakurajosui_up_s1 = stops.find(s => s.id === "sakurajosui_up_s1");
if (sakurajosui_up_s1.state === StopStateEnum.STOP_STATE_STOP) {
if (point.state !== StopStateEnum.STOP_STATE_REVERSE) {
if (sakurajosui_up_s1 && sakurajosui_up_s1.state === StopStateEnum.STOP_STATE_STOP) {
if (point.state !== PointStateEnum.POINT_STATE_REVERSE) {
await client.updatePointState({
"state": {
"id": point.id,
Expand All @@ -118,7 +127,7 @@ async function main() {
})
}
} else {
if (point.state !== StopStateEnum.STOP_STATE_STRAIGHT) {
if (point.state !== PointStateEnum.POINT_STATE_NORMAL) {
await client.updatePointState({
"state": {
"id": point.id,
Expand All @@ -130,7 +139,7 @@ async function main() {
}
if (point.id === "sakurajosui_down_p1") {
const sakurajosui_down_s1 = stops.find(s => s.id === "sakurajosui_down_s1");
if (sakurajosui_down_s1.state === StopStateEnum.STOP_STATE_STOP) {
if (sakurajosui_down_s1 && sakurajosui_down_s1.state === StopStateEnum.STOP_STATE_STOP) {
if (point.state !== PointStateEnum.POINT_STATE_REVERSE) {
await client.updatePointState({
"state": {
Expand Down Expand Up @@ -183,7 +192,9 @@ async function main() {

addTest();

while (true) {
main();
await new Promise(resolve => setTimeout(resolve, 200));
}
(async () => {
while (true) {
main();
await new Promise(resolve => setTimeout(resolve, 200));
}
})()
18 changes: 11 additions & 7 deletions backend/auto-operation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx tsc"
},
"keywords": [],
"author": "",
"type": "module",
"license": "ISC",
"dependencies": {
"@bufbuild/buf": "^1.28.1",
"@bufbuild/connect-web": "^0.13.0",
"@bufbuild/protobuf": "^1.4.2",
"@bufbuild/protoc-gen-es": "^1.4.2",
"@connectrpc/connect": "^1.1.3",
"@connectrpc/connect-node": "^1.1.3",
"@connectrpc/connect-web": "^1.1.3",
"@connectrpc/protoc-gen-connect-es": "^1.1.3",
"install": "^0.13.0",
"install": "^0.13.0"
},
"devDependencies": {
"@types/node": "20.9.5",
"tsx": "^4.3.0",
"typescript": "5.2.2"
}
"typescript": "5.2.2",
"@connectrpc/protoc-gen-connect-es": "^1.1.3",
"@bufbuild/protoc-gen-es": "^1.4.2"
},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-connect-web v0.11.0 with parameter "target=dts+js"
// @generated by protoc-gen-connect-es v1.1.3 with parameter "target=dts+js"
// @generated from file state/v1/state.proto (package state.v1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-connect-web v0.11.0 with parameter "target=dts+js"
// @generated by protoc-gen-connect-es v1.1.3 with parameter "target=dts+js"
// @generated from file state/v1/state.proto (package state.v1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
8 changes: 4 additions & 4 deletions backend/auto-operation/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
Expand All @@ -25,9 +25,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"module": "Node16", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "Node16", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
Expand All @@ -39,7 +39,7 @@
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

Expand Down
8 changes: 8 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
group "default" {
targets = [
"state-manager",
"autooperation",
"dashboard"
]
}
Expand Down Expand Up @@ -29,6 +30,13 @@ target "state-manager" {
]
}

target "autooperation" {
dockerfile = "docker/backend/auto-operation/Dockerfile"
tags = [
GET_TAG("autooperation")
]
}

target "dashboard" {
dockerfile = "docker/frontend/dashboard/Dockerfile"
tags = [
Expand Down
24 changes: 24 additions & 0 deletions docker/backend/auto-operation/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM node:20.9-bookworm AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base AS dep-resolver

WORKDIR /auto-operation
COPY --chown=node:node pnpm-lock.yaml pnpm-workspace.yaml ./
COPY backend/auto-operation ./backend/auto-operation
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

FROM dep-resolver AS auto-operation-builder
RUN pnpm run --filter=./backend/auto-operation -r build
RUN pnpm deploy --filter=./backend/auto-operation --prod dist/auto-operation
RUN cp backend/auto-operation/main.js dist/auto-operation/main.js
WORKDIR /autooperation/dist/auto-operation


FROM gcr.io/distroless/nodejs20-debian12:nonroot AS dashboard-runner
COPY --from=auto-operation-builder --chown=nonroot:nonroot /auto-operation/dist/auto-operation /auto-operation
ENV SERVER_ADDR=''
CMD ["/auto-operation/main.js"]

2 changes: 1 addition & 1 deletion docker/frontend/dashboard/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ FROM base AS dep-resolver
WORKDIR /dashboard
COPY --chown=node:node pnpm-lock.yaml pnpm-workspace.yaml ./
COPY frontend/ ./frontend
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm install --frozen-lockfile --no-optional

FROM dep-resolver AS dashboard-builder
RUN pnpm run --filter=./frontend/dashboard -r build
Expand Down
5 changes: 2 additions & 3 deletions frontend/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"lint": "next lint"
},
"dependencies": {
"@bufbuild/connect-web": "^0.13.0",
"@bufbuild/protobuf": "^1.4.2",
"@connectrpc/connect": "^1.1.3",
"@connectrpc/connect-query": "^0.6.0",
Expand All @@ -22,7 +21,6 @@
},
"devDependencies": {
"@bufbuild/buf": "^1.28.1",
"@bufbuild/protoc-gen-connect-web": "^0.11.0",
"@bufbuild/protoc-gen-es": "^1.4.2",
"@connectrpc/protoc-gen-connect-es": "^1.1.3",
"@connectrpc/protoc-gen-connect-query": "^0.6.0",
Expand All @@ -39,5 +37,6 @@
".next/standalone",
".next/static",
"public"
]
],
"packageManager": "[email protected]"
}
2 changes: 1 addition & 1 deletion frontend/dashboard/src/app/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import {createConnectTransport} from "@bufbuild/connect-web";
import {createConnectTransport} from "@connectrpc/connect-web";
import {TransportProvider} from "@connectrpc/connect-query";
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";

Expand Down
4 changes: 2 additions & 2 deletions frontend/dashboard/src/app/test/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";
import { createPromiseClient } from "@connectrpc/connect";
import { StateManagerService } from "@/proto/state/v1/state_connectweb";
import { createConnectTransport } from "@bufbuild/connect-web";
import { StateManagerService } from "@/proto/state/v1/state_connect";
import { createConnectTransport } from "@connectrpc/connect-web";
import { GetBlockStatesRequest } from "@/proto/state/v1/block_pb";

export default function Test() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-connect-web v0.11.0 with parameter "target=ts,import_extension=none"
// @generated by protoc-gen-connect-es v1.1.3 with parameter "target=ts,import_extension=none"
// @generated from file state/v1/state.proto (package state.v1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
26 changes: 3 additions & 23 deletions frontend/dashboard/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1071,31 +1071,11 @@
"@bufbuild/buf-win32-arm64" "1.28.1"
"@bufbuild/buf-win32-x64" "1.28.1"

"@bufbuild/connect-web@^0.13.0":
version "0.13.0"
resolved "https://registry.yarnpkg.com/@bufbuild/connect-web/-/connect-web-0.13.0.tgz#87301c92d49d3c3f9acb99729c2f7505d739aa4a"
integrity sha512-Ys9VFDWYktD9yFQSLOlkpsD42LonDNMCysLCfjXFuxlupYuf4f7qg0zkT5bESyTfqk4xtRDSSGR3xygaj/ONIQ==
dependencies:
"@bufbuild/connect" "0.13.0"

"@bufbuild/[email protected]":
version "0.13.0"
resolved "https://registry.yarnpkg.com/@bufbuild/connect/-/connect-0.13.0.tgz#97a84a2cac747c7a52d4421a3382d8d165f61c99"
integrity sha512-eZSMbVLyUFtXiZNORgCEvv580xKZeYQdMOWj2i/nxOcpXQcrEzTMTA7SZzWv4k4gveWCOSRoWmYDeOhfWXJv0g==

"@bufbuild/[email protected]", "@bufbuild/protobuf@^1.2.1", "@bufbuild/protobuf@^1.3.3", "@bufbuild/protobuf@^1.4.2":
"@bufbuild/[email protected]", "@bufbuild/protobuf@^1.3.3", "@bufbuild/protobuf@^1.4.2":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@bufbuild/protobuf/-/protobuf-1.4.2.tgz#dc4faf21264a47b71a15806616043cb006e80ac8"
integrity sha512-JyEH8Z+OD5Sc2opSg86qMHn1EM1Sa+zj/Tc0ovxdwk56ByVNONJSabuCUbLQp+eKN3rWNfrho0X+3SEqEPXIow==

"@bufbuild/protoc-gen-connect-web@^0.11.0":
version "0.11.0"
resolved "https://registry.yarnpkg.com/@bufbuild/protoc-gen-connect-web/-/protoc-gen-connect-web-0.11.0.tgz#985e12d0acc804a14033916bd2cebf3d88562dd1"
integrity sha512-7GvYkQjN6LP/ixtosI4JBN8eph2kS5XNju9zFwBuJ6aHqfl1sNRkRWG8LEdtTJtyW2R3QypAUyLfpLQf/ZyRVw==
dependencies:
"@bufbuild/protobuf" "^1.2.1"
"@bufbuild/protoplugin" "^1.2.1"

"@bufbuild/protoc-gen-es@^1.4.2":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@bufbuild/protoc-gen-es/-/protoc-gen-es-1.4.2.tgz#00c8b09430dd1154e626da7c247fd6425a1cd41d"
Expand All @@ -1104,7 +1084,7 @@
"@bufbuild/protobuf" "^1.4.2"
"@bufbuild/protoplugin" "1.4.2"

"@bufbuild/[email protected]", "@bufbuild/protoplugin@^1.2.1", "@bufbuild/protoplugin@^1.3.3":
"@bufbuild/[email protected]", "@bufbuild/protoplugin@^1.3.3":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@bufbuild/protoplugin/-/protoplugin-1.4.2.tgz#abf9b0e6a3dc8b52b1d6699d7a1ce5219fa82322"
integrity sha512-5IwGC1ZRD2A+KydGXeaSOErwfILLqVtvMH/RkN+cOoHcQd4EYXFStcF7g7aR+yICRDEEjQVi5tQF/qPGBSr9vg==
Expand All @@ -1120,7 +1100,7 @@
dependencies:
stable-hash "^0.0.3"

"@connectrpc/connect-web@^1.1.3":
"@connectrpc/connect-web@^1.1.2":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@connectrpc/connect-web/-/connect-web-1.1.3.tgz#027922b4f1537ecb1eaaa31ec3a59dd471a65320"
integrity sha512-WfShOZt91duJngqivYF4wJFRbeRa4bF/fPMfDVN0MAYSX3VuaTMn8o9qgKN7tsg2H2ZClyOVQwMkZx6IdcP7Zw==
Expand Down
Loading

0 comments on commit 73acd10

Please sign in to comment.