del db2 with pr

This commit is contained in:
Jasen Qin 2024-05-20 08:07:16 +10:00
parent 6a738af3ae
commit 3d6846b554
8 changed files with 211 additions and 53 deletions

View File

@ -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)
})
})

View File

@ -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', () => {
})
})

View File

@ -1,57 +1,43 @@
import pl, { DataFrame } from 'nodejs-polars' import pl, { DataFrame, Series } from 'nodejs-polars'
import fs, { writeFile } from 'fs' import fs from 'fs'
export function test_x(params: any) { export interface CSVDatabase {
const fooSeries = pl.Series("foo", [1, 2, 3])
let res = fooSeries.sum()
console.log(res)
let df = pl.readCSV("data/iris.csv")
}
interface CSVDatabase2 {
df: DataFrame df: DataFrame
filepath: string filepath: string
} }
export function create_csv_database(filepath: string): CSVDatabase2 { export function create_csv_database(filepath: string, initialData: any): CSVDatabase {
let df = DataFrame({ 'a_key': ['1'], 'a_value': ['2'] }) let df = DataFrame(initialData)
df.writeCSV(filepath) df.writeCSV(filepath)
return { df, filepath } return { df, filepath }
} }
export function save_db(db: CSVDatabase2) { export function save_db(db: CSVDatabase) {
db.df.writeCSV(db.filepath) db.df.writeCSV(db.filepath)
} }
export function add_row(db: CSVDatabase2, row: any) { export function add_row(db: CSVDatabase, row: any) {
let d = DataFrame(row) let d = DataFrame([row])
db.df = db.df.vstack(d) db.df = db.df.vstack(d)
} }
export function print_head(db: CSVDatabase2) { export function print_head(db: CSVDatabase) {
console.log(db.df.head()) console.log(db.df.head())
} }
export function delete_row_by_key(db: CSVDatabase2, key: string) { export function delete_row_by_key(db: CSVDatabase, key: string, keyColumn: string) {
console.log("NEXT COLUMNS") db.df = db.df.filter(pl.col(keyColumn).neq(pl.lit(key)))
console.log(db.df.columns)
db.df = db.df.filter(pl.col('a_key').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) return db.df.select(...columns)
} }
export function join_dataframes(db1: CSVDatabase2, db2: CSVDatabase2, on: string): DataFrame { export function join_dataframes(db1: CSVDatabase, db2: CSVDatabase, on: string): DataFrame {
return db1.df.join(db2.df, { on, how: "inner" }) // or "left", "right", "outer" 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 const indices = Array.from(Array(db.df.height).keys()) // Create 0-N index
db.df = db.df.withColumn(pl.Series("index", indices)) db.df = db.df.withColumn(pl.Series("index", indices))
} }

View File

@ -1,5 +1,8 @@
// point of sales model core // point of sales model core
import { CSVDatabase } from "./database"
import { create_csv_database, print_head } from "./database"
interface Item { interface Item {
id: string id: string
name: string name: string
@ -19,3 +22,48 @@ interface Inventory {
itemId: string itemId: string
quantity: number 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)
}

View File

@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
}

View File

@ -1,3 +1 @@
a_key,a_value a_key,a_value
1,2
x,y

Internal Server Error - Gitea: Git with a cup of tea

Internal Server Error

Gitea Version: 1.21.1