Skip to content

Commit

Permalink
improved logging for import
Browse files Browse the repository at this point in the history
  • Loading branch information
tKe committed Apr 3, 2015
1 parent 0503c5f commit e97452a
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,45 +791,78 @@ def processPricesFile(tdenv, db, pricesPath, pricesFh=None, defaultZero=False):
warnings, items, buys, sells, numSys, numStn = processPrices(
tdenv, pricesFh, db, defaultZero
)


class Counter:
def __init__(self, tbl, skipFirstCount=False):
self.tbl = tbl
if not skipFirstCount:
self.count
@property
def count(self):
self.lastCount = db.execute("""SELECT count(*) FROM {}""".format(self.tbl)).fetchone()[0]
tdenv.DEBUG0("Count for {} at {}", self.tbl, self.lastCount)
return self.lastCount
@property
def delta(self):
count = self.lastCount
return self.count - count

itemCounter = Counter("StationItem")
db.executemany("""
DELETE FROM StationItem
WHERE station_id = ?
AND item_id = ?
AND modified < IFNULL(?, CURRENT_TIMESTAMP)
""", items)
deletedItems = 0 - itemCounter.delta

insertedItems = insertedSells = insertedBuys = 0
if items:
db.executemany("""
INSERT OR IGNORE INTO StationItem
(station_id, item_id, modified)
VALUES (?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", items)
insertedItems = itemCounter.delta
if sells:
sellCounter = Counter("StationSelling")
db.executemany("""
INSERT OR IGNORE INTO StationSelling
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", sells)
insertedSells = sellCounter.delta
if buys:
buyCounter = Counter("StationBuying")
db.executemany("""
INSERT OR IGNORE INTO StationBuying
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", buys)
insertedBuys = buyCounter.delta

db.commit()

changes = " and ".join("{} {}".format(v, k) for k, v in {
"new": insertedItems - deletedItems,
"updated": deletedItems
}.items() if v) or "0"

tdenv.NOTE(
"Import complete: "
"{:n} items ({:n} buy, {:n} sell) "
"{:s} items ({:n} buy, {:n} sell) "
"for {:n} stations "
"in {:n} systems",
len(items),
len(buys), len(sells),
changes,
insertedBuys, insertedSells,
numStn,
numSys,
)

ignoredItems = len(items) - insertedItems
if ignoredItems:
tdenv.NOTE("Ignored {} items with old data", ignoredItems)


######################################################################

Expand Down

0 comments on commit e97452a

Please sign in to comment.