Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions airline-data/db_scripts/balance_audit_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE `balance_audit` (
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah forgot to mention, if it's new table, we dun need a script here . As a dev, i can just run airline-data/src/main/scala/com/patson/data/QuickCreateSchema.scala :)

`airline` int(11) NOT NULL,
`cycle` int(11) NOT NULL,
`balance` mediumtext,
UNIQUE KEY `balance_audit_UN` (`airline`,`cycle`),
CONSTRAINT `balance_audit_FK` FOREIGN KEY (`airline`) REFERENCES `airline` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ object AirlineSource {
updateStatement.setInt(2, airlineId)
updateStatement.executeUpdate()
updateStatement.close()

// update balance audit table
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kinda on the fence for this, in a way it's nice that :

  1. It has the potentials to keep entries on every single adjustAirlineBalance call and not just the final balance at the end of the cycle. But this would require changing the schema - no longer be UNIQUE KEY balance_audit_UN (airline,cycle), otherwise it's kinda meaningless, as entries just overwrite eachother and at the end it will be overwritten by the simulation on airline at cycle end

But the cons are:

  1. This is not the only place that set balance (though the most common)
  2. With the current schema it's kinda meaningless to write to DB for all the updates during the week by user action, since at the end it will just be replaced by the AirlineSimulation before the week ticks

I lean towards setting it in the AirlineSimulation instead (and the purge too) - somewhere here , as for cash flow during the week, the cash_flow_item table is supposed to keep track of it.

val insertToBalanceAuditStatement = connection.prepareStatement(
s"REPLACE INTO $BALANCE_AUDIT (airline, cycle, balance) VALUES (?, (SELECT cycle + 1 FROM cycle), (SELECT balance FROM $AIRLINE_INFO_TABLE WHERE airline = ?))")
insertToBalanceAuditStatement.setInt(1, airlineId)
insertToBalanceAuditStatement.setInt(2, airlineId)
insertToBalanceAuditStatement.executeQuery();
insertToBalanceAuditStatement.close();

AirlineCache.invalidateAirline(airlineId)
} finally {
connection.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ object Constants {
val AIRLINE_CASH_FLOW_ITEM_TABLE = "airline_cash_flow_item"
val AIRLINE_BASE_SPECIALIZATION_TABLE = "airline_base_specialization"
val AIRLINE_REPUTATION_BREAKDOWN = "airline_reputation_breakdown"
val BALANCE_AUDIT = "balance_audit"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit - let's name this val BALANCE_AUDIT_TABLE, most vals follow this naming convention, i might as well fix the AIRLINE_REPUTATION_BREAKDOWN lol



val INCOME_TABLE = "income"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ object CycleSource {
insertStatement.setInt(1, cycle)
insertStatement.executeUpdate()
insertStatement.close()


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the setCycle w/o side effects.

Currently we purge stuff on the cycle simulation end :

But i think it's probably better to do it at the AirlineSimulation, right before we save the balance audit perhaps

// also clean up old (-48) balance audit entries
val cleanupStatement = connection.prepareStatement(s"DELETE FROM $BALANCE_AUDIT WHERE cycle <= ?")
cleanupStatement.setInt(1, cycle - 48)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the cost is very low, we probably want to keep balance for way longer, like maybe 1000 cycles are okay

cleanupStatement.executeQuery()
cleanupStatement.close()

connection.commit()
} finally {
connection.close()
Expand Down
20 changes: 20 additions & 0 deletions airline-data/src/main/scala/com/patson/data/Meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ object Meta {
createIp(connection)
createAdminLog(connection)
createUserUuid(connection)
createBalanceAudit(connection)

statement = connection.prepareStatement("CREATE TABLE " + AIRPORT_CITY_SHARE_TABLE + "(" +
"airport INTEGER," +
Expand Down Expand Up @@ -1916,6 +1917,25 @@ object Meta {
statement.close()
}

def createBalanceAudit(connection: Connection): Unit = {
var statement = connection.prepareStatement(s"DROP TABLE IF EXISTS $BALANCE_AUDIT")
statement.execute()
statement.close()

val sql = s"""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nicer than my other sql statements 😊

|CREATE TABLE `$BALANCE_AUDIT` (
| `airline` int(11) NOT NULL,
| `cycle` int(11) NOT NULL,
| `balance` mediumtext,
| UNIQUE KEY `balance_audit_UN` (`airline`,`cycle`),
| CONSTRAINT `balance_audit_FK` FOREIGN KEY (`$AIRLINE_TABLE`) REFERENCES `airline` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|)
|""".stripMargin
statement = connection.prepareStatement(sql)
statement.execute()
statement.close()
}

def createAdminLog(connection : Connection) {
var statement = connection.prepareStatement("DROP TABLE IF EXISTS " + ADMIN_LOG_TABLE)
statement.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ object QuickCreateSchema extends App {
// Meta.createLog(connection)
// Meta.createAlert(connection)
//Meta.createDelegate(connection)
Meta.createUserUuid(connection)
//Meta.createUserUuid(connection)
Meta.createBalanceAudit(connection)
}


Expand Down