Commit 78bbb3a3 authored by Knut Behrends's avatar Knut Behrends
Browse files

substantial rewrite, try to use JS classes and getters

parent 56251341
node_modules/
scratch.js
# LICENSE
All rights reserved
Copyright 2020 ICDP-OSG
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// tbc
// shell commands to run:
/*
cd src
export token="fjhadkfj" && node -e "const config = require('./config.js');console.log(config)"
*/
......@@ -33,6 +33,11 @@
"debug": "=3.1.0"
}
},
"lodash": {
"version": "4.17.19",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
},
"moment": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz",
......
......@@ -11,6 +11,7 @@
"dependencies": {
"axios": "^0.19",
"faker": "^4.1",
"lodash": "^4.17.19",
"moment": "^2.27"
},
"devDependencies": {}
......
const axios = require("axios")
const config = require("./config.js")
let Core = require("./fake-cores.js")
const mdis = axios.create({
baseURL: "https://jet.rundis.com/",
timeout: 1000,
headers: config.auth_header,
})
let num_cores = 2
let endpoint = "/api/v1/form?name=core&per-page=1&sort=-id"
let current_next_core_id = null
core_0 = {}
mdis
.get(endpoint)
.then(function (response) {
core_0 = response.data.items[0]
current_next_core_id = core_0.id + 1 // assume
//console.log(core_0)
console.warn("###################")
})
.catch(function (error) {
console.error(error)
})
.then(function () {
let top_depth = null
for (let i = 0; i < num_cores; i++) {
top_depth = top_depth ? top_depth : core_0.bottom_depth
// use core_0 as a template for the next record, and overwrite some values
let core = new Core(core_0, current_next_core_id, i, top_depth)
console.table(core.toJSON()._props)
top_depth = core_0.bottom_depth
console.warn("###################")
mdis
.post(endpoint, core._props)
.then(function (response) {
//console.log(response)
setTimeout((_) => console.log(`inserted core #${i}`), 1000)
})
.catch(function (error) {
console.error(error.data)
})
}
})
let token = process.env.token || "2Wh0EhUH8PSYpF6XeBioK8ddTVuXO4i6"
let config = {
token: token,
auth_header: { "Authorization": `Bearer ${token}` },
}
module.exports = config
lodash = require("lodash")
class CoreDefaults {}
CoreDefaults.core_types = [
"B",
"H",
"L",
"M",
"N",
"O",
"P",
"R",
"S",
"T",
"W",
"X",
"Z",
]
CoreDefaults.continuity = [
"broken",
"continuous",
"continuous",
"continuous",
"rubble",
"fractures",
]
CoreDefaults.curator = ["KB", "CK", "KH"]
CoreDefaults.methods_core = [
["MSCL", "Core Section Scan"],
["MSCL", "Core Section Scan"],
["MSCL", "Core Section Scan"],
["MSCL"],
["Core Section Scan"],
]
CoreDefaults.fluid_type = ["salty", "fresh", "fresh", "fresh", "fresh", "fresh"]
CoreDefaults.drilled_length = [1, 2, 2, 2]
.concat(lodash.split(lodash.repeat(3, 50), ""))
.map((_) => parseInt(_))
CoreDefaults.barrel_length = [1, 2, 2, 2]
.concat(lodash.split(lodash.repeat(3, 50), ""))
.map((_) => parseInt(_))
CoreDefaults.core_run_duration = [23, 42, 66]
CoreDefaults.last_section = [1, 2, 2, 2, 3, 4, 4]
CoreDefaults.core_loss_reason = ["unknown", "fallback", "-", "-", "-", "-"]
module.exports = CoreDefaults
#!/usr/bin/env node
"use strict"
var faker = require("faker")
var axios = require("axios")
var util = require("./fake-util")
// var randomName = faker.name.findName(); // Rowan Nikolaus
// var randomEmail = faker.internet.email(); // Kassandra.Haley@erich.biz
// var randomCard = faker.util..createCard(); // random contact card containing many properties
const faker = require("faker")
const util = require("./fake-util")
const CoreDefaults = require("./fake-core-defaults.js")
const lodash = require("lodash")
faker.seed(112)
let mydate = faker.date.recent()
let curdate = faker.date.recent()
let mins = 0
let my_core_type = util.find_core_type()
let top_depth = 0.0
let max_temp = 12.0001
let barrel_length = 3
let core_diameter = "HQ"
let current_next_core_id = 7
let bottom_depth = 8.47
const instance = axios.create({
baseURL: "https://jet.rundis.com/",
timeout: 1000,
headers: { "Authorization": "Bearer cP9x6eOP3GQ9Qnqp03ky6oynglqR71ir" },
})
for (let i = 0; i < 300; i++) {
let row = {}
let drilled_length =
Math.round(
util.shuffle([1, 2, 2, 2, 3, 3, 3, 3]).pop() * Math.random() * 100,
2
) / 100
let recovered_length = util.frac_above(drilled_length) * drilled_length
row["core"] = i + current_next_core_id
if (typeof row["bottom_depth"]) {
row["top_depth"] = bottom_depth
} else {
row["top_depth"] = row["bottom_depth"]
}
row["hole_id"] = 2
row["analyst"] = util.shuffle(["KB", "CK", "KH"]).pop()
curdate = util.add_date_offset(curdate)
row["core_ondeck"] = util.find_core_on_deck(
curdate,
util.shuffle([23, 42, 66])
)
row["core_type"] = `${my_core_type}`
row["drilled_length"] = drilled_length
row["core_recovery"] = Math.round(recovered_length * 100, 2) / 100
row["bottom_depth"] = bottom_depth + row["core_recovery"]
top_depth = Math.round(row["bottom_depth"] * 100, 2) / 100
row["core_recovery_pc"] = Math.abs(
Math.round((recovered_length / drilled_length) * 100, 2) / 100
)
row["continuity"] = util
.shuffle([
"broken",
"continuous",
"continuous",
"continuous",
"rubble",
"fractures",
])
.pop()
row["last_section"] = util.shuffle([1, 2, 2, 2, 3, 4, 4]).pop()
row["core_catcher"] = 0
row["core_diameter"] = core_diameter
row["oriented"] = 0
row["core_loss_reason"] = util
.shuffle(["unknown", "fallback", "-", "-", "-", "-"])
.pop()
row["mcd_offset"] = 0
row["rqd_abundance"] = "abundant"
row["rqd_intensity"] = "intense"
max_temp +=
Math.random() < 0.005 ? Math.round(Math.random() * 100, 4) / 10000 : 0
row["temperature"] = max_temp
row["comments"] = faker.name.findName()
row["igsn"] = null
row["fluid_type"] = util
.shuffle(["salty", "fresh", "fresh", "fresh", "fresh", "fresh"])
.pop()
row["bit_type"] = Math.random() < 0.005 ? "new" : "used"
row["barrel_length"] = barrel_length
row["drillers_depth"] = row["bottom_depth"]
row["comments_2"] = "check drillers depth"
row["mcd_top_depth"] = parseInt(row["top_depth"])
row["methods_core"] = util
.shuffle([
["MSCL", "Core Section Scan"],
["MSCL", "Core Section Scan"],
["MSCL", "Core Section Scan"],
["MSCL"],
["Core Section Scan"],
])
.pop()
row["igsn_ukbgs"] = null
bottom_depth += drilled_length
/*
for (let [key, value] of Object.entries(row)) {
console.log(`${key}: ${value}`);
}*/
console.log(JSON.stringify(row))
/*
console.log(Object.values(row).map(_ => {
if( Number.parseFloat(_) || Number.parseInt(_) ){
return _
} else {
return `"${_}"`
}
}).join(", "))
*/
/*console.log(Object.entries (k, v).map((k, _) => {
if( Number.parseFloat(_) || Number.parseInt(_) ){
return _
} else {
return `"${_}"`
}
}).join(", "))*/
let endpoint = "/api/v1/form?name=core"
instance
.post(endpoint, row)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.error(error)
module.exports = class Core {
constructor(props, current_next_core_id, i, top_depth) {
this._props = props
this._props.top_depth = top_depth
this._current_next_core_id = current_next_core_id
this._i = i
}
find_core_on_deck(curdate, diff) {
curdate = curdate ? curdate : faker.date.recent()
diff = diff ? diff : util.shuffle(CoreDefaults.core_run_duration).pop()
curdate = util.add_date_offset(curdate, diff)
return util.format_date(curdate, "YYYY-MM-DD HH:mm:ss")
}
find_core_type(core_types) {
let types = core_types ? core_types : CoreDefaults.core_types
return util.shuffle(types).pop()
}
find_drilled_length() {
let val = util.shuffle(CoreDefaults.drilled_length).pop() * Math.random()
return util.round(val)
}
find_temperature() {
return Math.random() < 0.005 ? util.round(Math.random(), 4) : 0
}
toJSON() {
Object.keys(this._props).forEach((property) => {
this._props[property] = this[property]
})
//TODO: sleep for a few ms
this._props.core = this.core
return this
}
get hole_id() {
return this._props.hole_id
}
get site_id() {
return this._props.site_id
}
get expedition_id() {
return this._props.site_id
}
get program_id() {
return this._props.program_id
}
get mins() {
return 0
}
get barrel_length() {
return util.shuffle(CoreDefaults.drilled_length).pop()
}
get core_diameter() {
return "HQ"
}
get max_temp() {
return 12.0001
}
get drilled_length() {
return this.find_drilled_length()
}
get recovered_length() {
return
util.frac_above(0.8) * this._props.drilled_length
}
get core_recovery() {
return util.round(this._props.recovered_length)
}
// get top_depth() {
// return util.round(this._props.top_depth)
// }
get core_recovery_pc() {
return Math.abs(
util.round(this._props.recovered_length / this._props.drilled_length)
)
}
get core() {
return this._i + this._current_next_core_id
}
get analyst() {
return util.shuffle(CoreDefaults.curator).pop()
}
get core_ondeck() {
return this.find_core_on_deck()
}
get core_type() {
return this.find_core_type()
}
get continuity() {
return util.shuffle(CoreDefaults.continuity).pop()
}
get last_section() {
return util.shuffle(CoreDefaults.last_section).pop()
}
get core_catcher() {
return 0
}
get core_diameter() {
return this._props.core_diameter
}
get oriented() {
return 0
}
get core_loss_reason() {
return util.shuffle(CoreDefaults.core_loss_reason).pop()
}
get mcd_offset() {
return 0
}
get rqd_abundance() {
return "abundant"
}
get rqd_intensity() {
return "intense"
}
get temperature() {
return this.find_temperature()
}
get comments() {
return faker.name.findName()
}
get igsn() {
return null
}
get fluid_type() {
return util.shuffle(CoreDefaults.fluid_type).pop()
}
get bit_type() {
return Math.random() < 0.005 ? "new" : "used"
}
get barrel_length() {
return util.shuffle(CoreDefaults.barrel_length).pop()
}
get drillers_depth() {
return this._props.bottom_depth
}
get comments_2() {
return "check drillers depth"
}
get mcd_top_depth() {
return parseInt(this._props.top_depth)
} //bug: should be float
get methods_core() {
return util.shuffle(CoreDefaults.methods_core).pop()
}
get igsn_ukbgs() {
return null
}
get bottom_depth() {
let val = this._props.bottom_depth + this._props.drilled_length
return util.round(val)
}
get top_depth() {
let val = this._props.top_depth
return util.round(val)
}
set top_depth(val) {
this._props.top_depth = util.round(val)
}
}
let faker = require("faker")
let moment = require("moment")
let lodash = require("lodash")
function shuffle(array) {
return array.sort(function (a, b) {
......@@ -11,25 +12,31 @@ function add_date_offset(dt, minutes = 0) {
return new Date(dt.getTime() + minutes * 60000)
}
function find_core_on_deck(curdate = faker.date.recent(), diff) {
curdate = add_date_offset(curdate, diff)
return moment(curdate).format("YYYY-MM-DD HH:mm:ss")
function format_date(d, fmt) {
fmt = fmt ? fmt : "YYYY-MM-DD HH:mm:ss"
return moment(d).format(fmt)
}
function find_core_type(
array = ["B", "H", "L", "M", "N", "O", "P", "R", "S", "T", "W", "X", "Z"]
) {
return shuffle(array).pop()
function round(val, digits = 2) {
return Math.round(val * Math.pow(10, digits), digits) / Math.pow(10, digits)
}
function frac_above(x, thresh = 0.5) {
return Math.round(Math.random() * 100, 2) / 100
/**
*
* @param {double} x
* @param {double} thresh x | x > thresh & x <= 1
*/
function frac_above(thresh = 0.5) {
let x = Math.random()
while (!(x > thresh)) {
x = Math.random()
}
return x
}
module.exports = {
shuffle,
add_date_offset,
find_core_on_deck,
find_core_type,
frac_above,
format_date,
round,
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment