From 3d6846b55477a7b7c8787996fd98ae6de0b2f911 Mon Sep 17 00:00:00 2001 From: Jasen Qin Date: Mon, 20 May 2024 08:07:16 +1000 Subject: [PATCH] del db2 with pr --- pos-frontend/__test__/db.test.jsx | 22 ------ pos-frontend/__test__/db.test.ts | 32 +++++++++ pos-frontend/app/database.ts | 44 ++++-------- pos-frontend/app/model.tsx | 48 +++++++++++++ pos-frontend/babel.config.js | 6 ++ pos-frontend/csv_db/x.csv | 2 - pos-frontend/package.json | 1 + pos-frontend/pnpm-lock.yaml | 109 ++++++++++++++++++++++++++++++ 8 files changed, 211 insertions(+), 53 deletions(-) delete mode 100644 pos-frontend/__test__/db.test.jsx create mode 100644 pos-frontend/__test__/db.test.ts create mode 100644 pos-frontend/babel.config.js diff --git a/pos-frontend/__test__/db.test.jsx b/pos-frontend/__test__/db.test.jsx deleted file mode 100644 index ff296cf..0000000 --- a/pos-frontend/__test__/db.test.jsx +++ /dev/null @@ -1,22 +0,0 @@ -import '@testing-library/jest-dom' -import pl from 'nodejs-polars' -import { test_x, create_csv_database, add_row, print_head, delete_row_by_key, save_db } from '../app/database' - -describe('DATABASE', () => { - it('DATA', () => { - test_x() - }) - - it('create csv', () => { - let res = create_csv_database('csv_db/x.csv') - print_head(res) - - add_row(res, { "a_key": ["x"], "a_value": ["y"] }) - print_head(res) - - save_db(res) - - delete_row_by_key(res, "x") - print_head(res) - }) -}) diff --git a/pos-frontend/__test__/db.test.ts b/pos-frontend/__test__/db.test.ts new file mode 100644 index 0000000..e999313 --- /dev/null +++ b/pos-frontend/__test__/db.test.ts @@ -0,0 +1,32 @@ +import '@testing-library/jest-dom' +import pl from 'nodejs-polars' +import { create_csv_database, add_row, print_head, delete_row_by_key, save_db } from '../app/database' + +describe('DATABASE', () => { + it('DATA', () => { + const fooSeries = pl.Series("foo", [1, 2, 3]) + + let res = fooSeries.sum() + + console.log(res) + + let df = pl.readCSV("data/iris.csv") + }) + + it('create csv', () => { + let res = create_csv_database('csv_db/x.csv', { "a_key": [], "a_value": [] }) + print_head(res) + + add_row(res, { "a_key": ["x"], "a_value": ["y"] }) + print_head(res) + + save_db(res) + + delete_row_by_key(res, "x", "a_key") + print_head(res) + }) + + it('creates pos model', () => { + + }) +}) diff --git a/pos-frontend/app/database.ts b/pos-frontend/app/database.ts index 6e1a4b7..d271b9c 100644 --- a/pos-frontend/app/database.ts +++ b/pos-frontend/app/database.ts @@ -1,57 +1,43 @@ -import pl, { DataFrame } from 'nodejs-polars' -import fs, { writeFile } from 'fs' +import pl, { DataFrame, Series } from 'nodejs-polars' +import fs from 'fs' -export function test_x(params: any) { - const fooSeries = pl.Series("foo", [1, 2, 3]) - - let res = fooSeries.sum() - - console.log(res) - - let df = pl.readCSV("data/iris.csv") -} - -interface CSVDatabase2 { +export interface CSVDatabase { df: DataFrame filepath: string } -export function create_csv_database(filepath: string): CSVDatabase2 { - let df = DataFrame({ 'a_key': ['1'], 'a_value': ['2'] }) - +export function create_csv_database(filepath: string, initialData: any): CSVDatabase { + let df = DataFrame(initialData) df.writeCSV(filepath) - return { df, filepath } } -export function save_db(db: CSVDatabase2) { +export function save_db(db: CSVDatabase) { db.df.writeCSV(db.filepath) } -export function add_row(db: CSVDatabase2, row: any) { - let d = DataFrame(row) +export function add_row(db: CSVDatabase, row: any) { + let d = DataFrame([row]) db.df = db.df.vstack(d) } -export function print_head(db: CSVDatabase2) { +export function print_head(db: CSVDatabase) { console.log(db.df.head()) } -export function delete_row_by_key(db: CSVDatabase2, key: string) { - console.log("NEXT COLUMNS") - console.log(db.df.columns) - db.df = db.df.filter(pl.col('a_key').neq(pl.lit(key))) +export function delete_row_by_key(db: CSVDatabase, key: string, keyColumn: string) { + db.df = db.df.filter(pl.col(keyColumn).neq(pl.lit(key))) } -export function select_columns(db: CSVDatabase2, columns: string[]): DataFrame { +export function select_columns(db: CSVDatabase, columns: string[]): DataFrame { return db.df.select(...columns) } -export function join_dataframes(db1: CSVDatabase2, db2: CSVDatabase2, on: string): DataFrame { - return db1.df.join(db2.df, { on, how: "inner" }) // or "left", "right", "outer" +export function join_dataframes(db1: CSVDatabase, db2: CSVDatabase, on: string): DataFrame { + return db1.df.join(db2.df, { on, how: "inner" }) } -export function add_simple_index(db: CSVDatabase2) { +export function add_simple_index(db: CSVDatabase) { const indices = Array.from(Array(db.df.height).keys()) // Create 0-N index db.df = db.df.withColumn(pl.Series("index", indices)) } diff --git a/pos-frontend/app/model.tsx b/pos-frontend/app/model.tsx index 232bd04..ad5920d 100644 --- a/pos-frontend/app/model.tsx +++ b/pos-frontend/app/model.tsx @@ -1,5 +1,8 @@ // point of sales model core +import { CSVDatabase } from "./database" +import { create_csv_database, print_head } from "./database" + interface Item { id: string name: string @@ -19,3 +22,48 @@ interface Inventory { itemId: string quantity: number } + +export function create_item_dataframe(items: Item[]): CSVDatabase { + const filepath = 'data/items.csv' + return create_csv_database(filepath, items) +} + +export function create_sale_dataframe(sales: Sale[]): CSVDatabase { + const filepath = 'data/sales.csv' + const data = sales.map(sale => ({ + ...sale, + timestamp: sale.timestamp.toISOString() // Convert Date to string + })) + return create_csv_database(filepath, data) +} + +export function create_inventory_dataframe(inventories: Inventory[]): CSVDatabase { + const filepath = 'data/inventory.csv' + return create_csv_database(filepath, inventories) +} + + +function create_models() { + const items: Item[] = [ + { id: '1', name: 'Item 1', price: 100 }, + { id: '2', name: 'Item 2', price: 200 } + ] + + const sales: Sale[] = [ + { id: '1', itemId: '1', quantity: 2, timestamp: new Date() }, + { id: '2', itemId: '2', quantity: 1, timestamp: new Date() } + ] + + const inventories: Inventory[] = [ + { itemId: '1', quantity: 50 }, + { itemId: '2', quantity: 30 } + ] + + const itemDB = create_item_dataframe(items) + const saleDB = create_sale_dataframe(sales) + const inventoryDB = create_inventory_dataframe(inventories) + + print_head(itemDB) + print_head(saleDB) + print_head(inventoryDB) +} diff --git a/pos-frontend/babel.config.js b/pos-frontend/babel.config.js new file mode 100644 index 0000000..ae0b3eb --- /dev/null +++ b/pos-frontend/babel.config.js @@ -0,0 +1,6 @@ +module.exports = { + presets: [ + ['@babel/preset-env', { targets: { node: 'current' } }], + '@babel/preset-typescript', + ], +} \ No newline at end of file diff --git a/pos-frontend/csv_db/x.csv b/pos-frontend/csv_db/x.csv index b4e3165..37bd13c 100644 --- a/pos-frontend/csv_db/x.csv +++ b/pos-frontend/csv_db/x.csv @@ -1,3 +1 @@ a_key,a_value -1,2 -x,y diff --git a/pos-frontend/package.json b/pos-frontend/package.json index e538a12..4764bd5 100644 --- a/pos-frontend/package.json +++ b/pos-frontend/package.json @@ -49,6 +49,7 @@ "rocksdb": "^5.2.1" }, "devDependencies": { + "@babel/preset-typescript": "^7.24.1", "@next/eslint-plugin-next": "^14.2.0", "@testing-library/jest-dom": "^6.4.2", "@testing-library/react": "^14.2.2", diff --git a/pos-frontend/pnpm-lock.yaml b/pos-frontend/pnpm-lock.yaml index f642395..c83d1b9 100644 --- a/pos-frontend/pnpm-lock.yaml +++ b/pos-frontend/pnpm-lock.yaml @@ -117,6 +117,9 @@ importers: specifier: ^5.2.1 version: 5.2.1 devDependencies: + '@babel/preset-typescript': + specifier: ^7.24.1 + version: 7.24.1(@babel/core@7.24.5) '@next/eslint-plugin-next': specifier: ^14.2.0 version: 14.2.3 @@ -216,10 +219,20 @@ packages: resolution: {integrity: sha512-x32i4hEXvr+iI0NEoEfDKzlemF8AmtOP8CcrRaEcpzysWuoEb1KknpcvMsHKPONoKZiDuItklgWhB18xEhr9PA==} engines: {node: '>=6.9.0'} + '@babel/helper-annotate-as-pure@7.22.5': + resolution: {integrity: sha512-LvBTxu8bQSQkcyKOU+a1btnNFQ1dMAd0R6PyW3arXes06F6QLWLIrd681bxRPIXlrMGR3XYnW9JyML7dP3qgxg==} + engines: {node: '>=6.9.0'} + '@babel/helper-compilation-targets@7.23.6': resolution: {integrity: sha512-9JB548GZoQVmzrFgp8o7KxdgkTGm6xs9DW0o/Pim72UDjzr5ObUQ6ZzYPqA+g9OTS2bBQoctLJrky0RDCAWRgQ==} engines: {node: '>=6.9.0'} + '@babel/helper-create-class-features-plugin@7.24.5': + resolution: {integrity: sha512-uRc4Cv8UQWnE4NXlYTIIdM7wfFkOqlFztcC/gVXDKohKoVB3OyonfelUBaJzSwpBntZ2KYGF/9S7asCHsXwW6g==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0 + '@babel/helper-environment-visitor@7.22.20': resolution: {integrity: sha512-zfedSIzFhat/gFhWfHtgWvlec0nqB9YEIVrpuwjruLlXfUSnA8cJB0miHKwqDnQ7d32aKo2xt88/xZptwxbfhA==} engines: {node: '>=6.9.0'} @@ -232,6 +245,10 @@ packages: resolution: {integrity: sha512-wGjk9QZVzvknA6yKIUURb8zY3grXCcOZt+/7Wcy8O2uctxhplmUPkOdlgoNhmdVee2c92JXbf1xpMtVNbfoxRw==} engines: {node: '>=6.9.0'} + '@babel/helper-member-expression-to-functions@7.24.5': + resolution: {integrity: sha512-4owRteeihKWKamtqg4JmWSsEZU445xpFRXPEwp44HbgbxdWlUV1b4Agg4lkA806Lil5XM/e+FJyS0vj5T6vmcA==} + engines: {node: '>=6.9.0'} + '@babel/helper-module-imports@7.24.3': resolution: {integrity: sha512-viKb0F9f2s0BCS22QSF308z/+1YWKV/76mwt61NBzS5izMzDPwdq1pTrzf+Li3npBWX9KdQbkeCt1jSAM7lZqg==} engines: {node: '>=6.9.0'} @@ -242,14 +259,28 @@ packages: peerDependencies: '@babel/core': ^7.0.0 + '@babel/helper-optimise-call-expression@7.22.5': + resolution: {integrity: sha512-HBwaojN0xFRx4yIvpwGqxiV2tUfl7401jlok564NgB9EHS1y6QT17FmKWm4ztqjeVdXLuC4fSvHc5ePpQjoTbw==} + engines: {node: '>=6.9.0'} + '@babel/helper-plugin-utils@7.24.5': resolution: {integrity: sha512-xjNLDopRzW2o6ba0gKbkZq5YWEBaK3PCyTOY1K2P/O07LGMhMqlMXPxwN4S5/RhWuCobT8z0jrlKGlYmeR1OhQ==} engines: {node: '>=6.9.0'} + '@babel/helper-replace-supers@7.24.1': + resolution: {integrity: sha512-QCR1UqC9BzG5vZl8BMicmZ28RuUBnHhAMddD8yHFHDRH9lLTZ9uUPehX8ctVPT8l0TKblJidqcgUUKGVrePleQ==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0 + '@babel/helper-simple-access@7.24.5': resolution: {integrity: sha512-uH3Hmf5q5n7n8mz7arjUlDOCbttY/DW4DYhE6FUsjKJ/oYC1kQQUvwEQWxRwUpX9qQKRXeqLwWxrqilMrf32sQ==} engines: {node: '>=6.9.0'} + '@babel/helper-skip-transparent-expression-wrappers@7.22.5': + resolution: {integrity: sha512-tK14r66JZKiC43p8Ki33yLBVJKlQDFoA8GYN67lWCDCqoL6EMMSuM9b+Iff2jHaM/RRFYl7K+iiru7hbRqNx8Q==} + engines: {node: '>=6.9.0'} + '@babel/helper-split-export-declaration@7.24.5': resolution: {integrity: sha512-5CHncttXohrHk8GWOFCcCl4oRD9fKosWlIRgWm4ql9VYioKm52Mk2xsmoohvm7f3JoiLSM5ZgJuRaf5QZZYd3Q==} engines: {node: '>=6.9.0'} @@ -352,6 +383,24 @@ packages: peerDependencies: '@babel/core': ^7.0.0-0 + '@babel/plugin-transform-modules-commonjs@7.24.1': + resolution: {integrity: sha512-szog8fFTUxBfw0b98gEWPaEqF42ZUD/T3bkynW/wtgx2p/XCP55WEsb+VosKceRSd6njipdZvNogqdtI4Q0chw==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0-0 + + '@babel/plugin-transform-typescript@7.24.5': + resolution: {integrity: sha512-E0VWu/hk83BIFUWnsKZ4D81KXjN5L3MobvevOHErASk9IPwKHOkTgvqzvNo1yP/ePJWqqK2SpUR5z+KQbl6NVw==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0-0 + + '@babel/preset-typescript@7.24.1': + resolution: {integrity: sha512-1DBaMmRDpuYQBPWD8Pf/WEwCrtgRHxsZnP4mIy9G/X+hFfbI47Q2G4t1Paakld84+qsk2fSsUPMKg71jkoOOaQ==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0-0 + '@babel/runtime@7.24.5': resolution: {integrity: sha512-Nms86NXrsaeU9vbBJKni6gXiEXZ4CVpYVzEjDH9Sb8vmZ3UljyA1GSOJl/6LGPO8EHLuSF9H+IxNXHPX8QHJ4g==} engines: {node: '>=6.9.0'} @@ -5513,6 +5562,10 @@ snapshots: '@jridgewell/trace-mapping': 0.3.25 jsesc: 2.5.2 + '@babel/helper-annotate-as-pure@7.22.5': + dependencies: + '@babel/types': 7.24.5 + '@babel/helper-compilation-targets@7.23.6': dependencies: '@babel/compat-data': 7.24.4 @@ -5521,6 +5574,19 @@ snapshots: lru-cache: 5.1.1 semver: 6.3.1 + '@babel/helper-create-class-features-plugin@7.24.5(@babel/core@7.24.5)': + dependencies: + '@babel/core': 7.24.5 + '@babel/helper-annotate-as-pure': 7.22.5 + '@babel/helper-environment-visitor': 7.22.20 + '@babel/helper-function-name': 7.23.0 + '@babel/helper-member-expression-to-functions': 7.24.5 + '@babel/helper-optimise-call-expression': 7.22.5 + '@babel/helper-replace-supers': 7.24.1(@babel/core@7.24.5) + '@babel/helper-skip-transparent-expression-wrappers': 7.22.5 + '@babel/helper-split-export-declaration': 7.24.5 + semver: 6.3.1 + '@babel/helper-environment-visitor@7.22.20': {} '@babel/helper-function-name@7.23.0': @@ -5532,6 +5598,10 @@ snapshots: dependencies: '@babel/types': 7.24.5 + '@babel/helper-member-expression-to-functions@7.24.5': + dependencies: + '@babel/types': 7.24.5 + '@babel/helper-module-imports@7.24.3': dependencies: '@babel/types': 7.24.5 @@ -5545,12 +5615,27 @@ snapshots: '@babel/helper-split-export-declaration': 7.24.5 '@babel/helper-validator-identifier': 7.24.5 + '@babel/helper-optimise-call-expression@7.22.5': + dependencies: + '@babel/types': 7.24.5 + '@babel/helper-plugin-utils@7.24.5': {} + '@babel/helper-replace-supers@7.24.1(@babel/core@7.24.5)': + dependencies: + '@babel/core': 7.24.5 + '@babel/helper-environment-visitor': 7.22.20 + '@babel/helper-member-expression-to-functions': 7.24.5 + '@babel/helper-optimise-call-expression': 7.22.5 + '@babel/helper-simple-access@7.24.5': dependencies: '@babel/types': 7.24.5 + '@babel/helper-skip-transparent-expression-wrappers@7.22.5': + dependencies: + '@babel/types': 7.24.5 + '@babel/helper-split-export-declaration@7.24.5': dependencies: '@babel/types': 7.24.5 @@ -5650,6 +5735,30 @@ snapshots: '@babel/core': 7.24.5 '@babel/helper-plugin-utils': 7.24.5 + '@babel/plugin-transform-modules-commonjs@7.24.1(@babel/core@7.24.5)': + dependencies: + '@babel/core': 7.24.5 + '@babel/helper-module-transforms': 7.24.5(@babel/core@7.24.5) + '@babel/helper-plugin-utils': 7.24.5 + '@babel/helper-simple-access': 7.24.5 + + '@babel/plugin-transform-typescript@7.24.5(@babel/core@7.24.5)': + dependencies: + '@babel/core': 7.24.5 + '@babel/helper-annotate-as-pure': 7.22.5 + '@babel/helper-create-class-features-plugin': 7.24.5(@babel/core@7.24.5) + '@babel/helper-plugin-utils': 7.24.5 + '@babel/plugin-syntax-typescript': 7.24.1(@babel/core@7.24.5) + + '@babel/preset-typescript@7.24.1(@babel/core@7.24.5)': + dependencies: + '@babel/core': 7.24.5 + '@babel/helper-plugin-utils': 7.24.5 + '@babel/helper-validator-option': 7.23.5 + '@babel/plugin-syntax-jsx': 7.24.1(@babel/core@7.24.5) + '@babel/plugin-transform-modules-commonjs': 7.24.1(@babel/core@7.24.5) + '@babel/plugin-transform-typescript': 7.24.5(@babel/core@7.24.5) + '@babel/runtime@7.24.5': dependencies: regenerator-runtime: 0.14.1