Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data generation logic #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion data_rows/DataRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import stringify from "csv-stringify";
import fs from "graceful-fs";

export default class DataRow {
oddsNew() {
static oddsNew() {
return 1;
}

Expand Down
90 changes: 90 additions & 0 deletions data_rows/DatabaseStorageUsageHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import DataRow from "./DataRow";
import TYPES from "./Types";
import { randomFromArray } from "../helpers";

class InitializeError extends Error {}

/**
* DATABASE_STORAGE_USAGE_HISTORY View
*
* https://docs.snowflake.net/manuals/sql-reference/account-usage/database_storage_usage_history.html
*/
export default class DatabaseStorageUsageHistory extends DataRow {
constructor(databaseID, databaseName, date) {
super();
if (!this.constructor._sizes) {
throw new InitializeError(
`Must call ${this.constructor.name}.initialize() first`
);
}
this.USAGE_DATE = date.toISOString();
this.DATABASE_ID = databaseID;
this.DATABASE_NAME = databaseName;
this._setAverageDatabaseBytes();
this._setAverageFailsafeBytes();
}

_setAverageDatabaseBytes() {
let size = this.constructor._sizes[this.DATABASE_NAME];
if (Math.random() < 0.1) {
const changeDegree = Math.random() * (0.2 - 0.05) + 0.05;
const change = Math.round(size * changeDegree);
if (Math.random() < 0.3) {
size -= change;
} else {
size += change;
}
}
this.constructor._sizes[this.DATABASE_NAME] = size;
this.AVERAGE_DATABASE_BYTES = size;
}

_setAverageFailsafeBytes() {
this.AVERAGE_FAILSAFE_BYTES = 0;
if (Math.random() < 0.1) {
this.AVERAGE_FAILSAFE_BYTES = Math.round(
Math.random() * this.AVERAGE_DATABASE_BYTES
);
}
}

static initialize(dbs) {
if (this._sizes) {
throw new InitializeError(
`Must call ${this.name}.initialize() only once`
);
}
const seedSizes = [1.1e15, 1.2e15, 1.3e15, 1.4e15];
const sizes = {};
for (const db of dbs) {
const seed = randomFromArray(seedSizes);
sizes[db.DATABASE_NAME] = Math.round(Math.random() * seed);
}
this._sizes = sizes;
}

static types() {
return [
{
name: "USAGE_DATE",
type: TYPES.timestamp
},
{
name: "DATABASE_ID",
type: TYPES.integer
},
{
name: "DATABASE_NAME",
type: TYPES.string
},
{
name: "AVERAGE_DATABASE_BYTES",
type: TYPES.integer
},
{
name: "AVERAGE_FAILSAFE_BYTES",
type: TYPES.integer
}
];
}
}

Choose a reason for hiding this comment

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

👍

40 changes: 40 additions & 0 deletions data_rows/Databases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import DataRow from "./DataRow";
import TYPES from "./Types";

/**
* DATABASES View
*
* https://docs.snowflake.net/manuals/sql-reference/account-usage/databases.html
*/
export default class Databases extends DataRow {
constructor(name, id) {
super();
this.DATABASE_NAME = name;
this.DATABASE_ID = id;
}

static generate() {
const names = [
"squiggly_database",
"jims_database",
"jacksonbase",
"prod",
"staging_db",
"dev1"
];
return names.map((name, idx) => new this(name, idx + 1));
}

static types() {
return [
{
name: "DATABASE_NAME",
type: TYPES.string
},
{
name: "DATABASE_ID",
type: TYPES.integer
}
];
}
}

Choose a reason for hiding this comment

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

👍

44 changes: 44 additions & 0 deletions data_rows/LoadHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import DataRow from "./DataRow";
import TYPES from "./Types";

const ODDS_NEW = 0.2;

/**
* LOAD_HISTORY View
*
* https://docs.snowflake.net/manuals/sql-reference/account-usage/load_history.html
*/
export default class LoadHistory extends DataRow {
constructor(tableID, date) {
super();
this.TABLE_ID = tableID;
this.LAST_LOAD_TIME = date.toISOString();
this.ROW_COUNT = Math.round(Math.random() * 100);
this.ERROR_COUNT = Math.random() < 0.1 ? Math.round(Math.random() * 10) : 0;
}

static oddsNew() {

Choose a reason for hiding this comment

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

personally I would define this as a const at the top of the module, const ODDS_NEW 0.2 but I don't really have any good reason to do it one way or the other.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Early on I thought I'd have a lot more "dice rolling" to create an object than I ended up with. So I made this DataRow.rollDice() / SubClass.oddsNew() construct that ended up not being used very much (and actually DataRow.oddsNew() should be static). What I'd like is class level properties but doesn't seem like those exist in JS?

I could set const ODDS_NEW = 0.2 at the module level and then reference it in the static oddsNew() method in each class to help with the "magic number" issue, is that what you're referring to? or would you do the check differently?

return ODDS_NEW;
}

static types() {
return [
{
name: "TABLE_ID",
type: TYPES.integer
},
{
name: "LAST_LOAD_TIME",
type: TYPES.timestamp
},
{
name: "ROW_COUNT",
type: TYPES.integer
},
{
name: "ERROR_COUNT",
type: TYPES.integer
}
];
}
}
54 changes: 54 additions & 0 deletions data_rows/LoginHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import DataRow from "./DataRow";
import TYPES from "./Types";
import * as helpers from "../helpers";

/**
* LOGIN_HISTORY View
*
* https://docs.snowflake.net/manuals/sql-reference/account-usage/login_history.html
*/
export default class LoginHistory extends DataRow {
constructor(date) {
super();
this.EVENT_ID = helpers.getID();
this.EVENT_TIMESTAMP = date.toISOString();
const user = helpers.randomFromArray(this.constructor.getUsers());
this.USER_NAME = user.name;
this.REPORTED_CLIENT_TYPE = user.driver;
this.IS_SUCCESS = Math.random() < 0.9 ? "YES" : "NO";
}

static getUsers() {
return [
{ name: "WEB_CLIENT", driver: "JAVASCRIPT_DRIVER", querySpeed: 1.2 },
{ name: "BOB", driver: "OTHER", querySpeed: 2.0 },
{ name: "BI_APP", driver: "JDBC_DRIVER", querySpeed: 0.8 },
{ name: "JANE", driver: "SNOWFLAKE_UI", querySpeed: 1.0 }
];
}

static types() {
return [
{
name: "EVENT_ID",
type: TYPES.string
},
{
name: "EVENT_TIMESTAMP",
type: TYPES.timestamp
},
{
name: "USER_NAME",
type: TYPES.string
},
{
name: "REPORTED_CLIENT_TYPE",
type: TYPES.string
},
{
name: "IS_SUCCESS",
type: TYPES.string
}
];
}
}

Choose a reason for hiding this comment

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

👍

Loading