Spaces:
Sleeping

kriyatrack / data /generate.py
budijuarto's picture
Publish KriyaTrack sewing studio, forecast evaluation and interactive 3D scene
d091b2c verified
Raw History Blame Contribute Delete
4.55 kB
"""Create a deterministic synthetic order ledger for chiessewby."""
import calendar
import json
import math
import random
from pathlib import Path
SEED = 2882026
MONTHS = 72
START_YEAR, START_MONTH = 2020, 11
PRODUCTS = [
("pouch-bordir", "Pouch bordir inisial", 85000, 7.5),
("totebag-sulam", "Totebag sulam", 160000, 5.5),
("gantungan-rajut", "Gantungan rajut", 35000, 9.0),
("dompet-kain", "Dompet kain motif", 95000, 6.0),
("pouch-linen", "Pouch serut linen", 70000, 6.0),
("sampul-jurnal", "Sampul jurnal bordir", 120000, 4.5),
]
SEASON = (0.98, 0.82, 0.91, 1.12, 1.10, 0.94, 0.88, 0.92, 1.05, 1.10, 1.25, 1.36)
def calendar_month(offset):
index = START_YEAR * 12 + START_MONTH - 1 + offset
year, month = divmod(index, 12)
return year, month + 1
def make_ledger():
random_source = random.Random(SEED)
orders, history = [], []
for offset in range(MONTHS):
year, month = calendar_month(offset)
period = f"{year}-{month:02d}"
days = calendar.monthrange(year, month)[1]
trend = 0.88 + 0.0105 * offset
shared = 1 + random_source.uniform(-0.045, 0.045)
# Small operational variation is independent of the calendar pattern.
disruption = 0.88 if offset in (17, 38, 59) else 1.0
monthly = []
for p, (product_id, product_name, base_price, volume) in enumerate(PRODUCTS):
category = 1 + 0.065 * math.sin(2 * math.pi * (month - 1 + 2 * p) / 12)
expected = volume * trend * SEASON[month - 1] * shared * disruption * category
count = max(1, round(expected + random_source.uniform(-0.8, 0.8)))
for position in range(count):
quantity = 1 + int(random_source.random() < (0.24 if p == 2 else 0.12))
unit_price = base_price + (offset // 18) * 2000 + random_source.choice((-1000, 0, 0, 1000))
day = random_source.randint(1, days)
settled = position % 13 != 12 and (offset + p + position) % 17 != 16
status = "paid" if settled else ("partial" if (position + p) % 3 else "cancelled")
total = quantity * unit_price
dp = total // 3 if status in {"paid", "partial"} else 0
order = {
"id": f"KT-SIM-{len(orders) + 1:05d}",
"orderDate": f"{period}-{day:02d}",
"productId": product_id,
"productName": product_name,
"quantity": quantity,
"unitPrice": unit_price,
"total": total,
"status": status,
"deposit": dp,
"paidAt": f"{period}-{min(days, day + random_source.randrange(5)):02d}" if settled else None,
}
orders.append(order)
monthly.append(order)
paid = [row for row in monthly if row["status"] == "paid"]
history.append({"month": period, "value": sum(row["total"] for row in paid),
"paidOrders": len(paid), "units": sum(row["quantity"] for row in paid)})
return {
"schema": "kriyatrack-ledger/v1", "synthetic": True, "currency": "IDR", "seed": SEED,
"coverage": {"from": history[0]["month"], "to": history[-1]["month"]},
"products": [{"id": row[0], "name": row[1]} for row in PRODUCTS],
"orders": sorted(orders, key=lambda row: (row["orderDate"], row["id"])),
"history": history,
}
def validate(ledger):
orders = ledger["orders"]
assert len(orders) >= 1000 and len({row["id"] for row in orders}) == len(orders)
assert len(ledger["history"]) == MONTHS
for row in orders:
assert row["total"] == row["quantity"] * row["unitPrice"]
assert (row["paidAt"] is not None) == (row["status"] == "paid")
by_month = {}
for row in orders:
if row["status"] == "paid":
month = row["orderDate"][:7]
by_month[month] = by_month.get(month, 0) + row["total"]
assert [by_month[row["month"]] for row in ledger["history"]] == [row["value"] for row in ledger["history"]]
if __name__ == "__main__":
ledger = make_ledger()
validate(ledger)
output = Path(__file__).resolve().with_name("ledger.json")
output.write_text(json.dumps(ledger, ensure_ascii=False, separators=(",", ":")) + "\n",
encoding="utf-8", newline="\n")
print(f'{len(ledger["orders"])} unique synthetic transactions, {len(ledger["history"])} closed months, {output.name}')