Skip to content

Commit 6489f55

Browse files
committed
Updated fromtend and importer
1 parent dc74d6b commit 6489f55

File tree

13 files changed

+237
-171
lines changed

13 files changed

+237
-171
lines changed
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"log"
8-
"math"
98
"os"
109
"path/filepath"
1110
"time"
@@ -32,15 +31,16 @@ var (
3231
func main() {
3332
flag.Parse()
3433

34+
// Name of tool, logger
35+
name := filepath.Base(flag.CommandLine.Name())
36+
log := logger(name + " ")
37+
3538
// Check number of arguments
3639
if flag.NArg() < 2 {
37-
fmt.Fprintln(os.Stderr, "Usage: importer <sqlite-database> <url>...")
40+
fmt.Fprintf(os.Stderr, "Usage: %v <sqlite-database> <url>...\n", name)
3841
os.Exit(1)
3942
}
4043

41-
// Create log
42-
log := logger(filepath.Base(flag.CommandLine.Name()) + " ")
43-
4444
// Open database
4545
db, err := sqlite3.OpenPathEx(flag.Arg(0), sqlite3.SQLITE_OPEN_CREATE, "")
4646
if err != nil {
@@ -49,7 +49,8 @@ func main() {
4949
}
5050
defer db.Close()
5151

52-
log.Println("database:", db.Filename())
52+
// Report on the database
53+
log.Println("database:", db.Filename(sqlite3.DefaultSchema))
5354

5455
// Create a configuration
5556
config := SQImportConfig{
@@ -74,34 +75,52 @@ func main() {
7475
// Read files
7576
for _, url := range flag.Args()[1:] {
7677
// Create an importer
77-
importer, err := importer.NewImporter(config, url, writer)
78+
importer, err := importer.NewImporter(config, url)
79+
if err != nil {
80+
fmt.Fprintln(os.Stderr, importer.URL(), ": ", err)
81+
continue
82+
}
83+
84+
// Create the decoder
85+
decoder, err := importer.Decoder("")
7886
if err != nil {
7987
fmt.Fprintln(os.Stderr, importer.URL(), ": ", err)
8088
continue
8189
}
90+
defer decoder.Close()
8291

8392
// Reset the counter
84-
log.Println("import:", importer.URL())
85-
mark, start := time.Now(), time.Now()
93+
log.Println(" import:", importer.URL())
94+
log.Println(" ...decoder", decoder)
95+
96+
// Call Begin for writer to get writing function
97+
fn, err := writer.Begin(importer.Name(), sqlite3.DefaultSchema, []string{"continent"})
98+
if err != nil {
99+
fmt.Fprintln(os.Stderr, importer.URL(), ": ", err)
100+
continue
101+
}
86102

87103
// Read and write rows
104+
start, mark := time.Now(), time.Now()
88105
for {
89-
if err := importer.ReadWrite(); err == io.EOF {
106+
if err := importer.ReadWrite(decoder, fn); err == io.EOF {
107+
writer.End(true) // commit
90108
break
91109
} else if err != nil {
110+
writer.End(false) // rollback
92111
fmt.Fprintln(os.Stderr, importer.URL(), ": ", err)
93112
break
94113
}
95114
if time.Since(mark) > 5*time.Second {
96-
log.Printf(" ...written %d rows", writer.Count())
115+
log.Printf(" ...written %d rows", 0)
97116
mark = time.Now()
98117
}
99118
}
100119

101120
// Report
102121
since := time.Since(start)
103-
ops_per_sec := math.Round(float64(writer.Count()) * 1000 / float64(since.Milliseconds()))
104-
log.Printf(" ...written %d rows in %v (%.0f ops/s)", writer.Count(), since.Truncate(time.Millisecond), ops_per_sec)
122+
//ops_per_sec := math.Round(float64(writer.Count()) * 1000 / float64(since.Milliseconds()))
123+
log.Printf(" ...written %d rows in %v (%.0f ops/s)", 0, since.Truncate(time.Millisecond), 0)
105124
}
106125
}
107126

npm/sqlite3/html/index.html

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,40 @@
3636
<!-- BODY CONTENT (SchemaView) -->
3737
<div class="_title bg-light rounded p-2 mb-2">
3838
<h1 class="_name">[schema]</h1>
39-
<small class="_filename">[filename]</small>
39+
<small class="_filename">[filename]</small>
4040
</div>
4141
<ul class="nav nav-tabs" id="schema-table-list">
4242
<li class="nav-item _template">
43-
<a class="nav-link _name" aria-current="page" href="#">[table]</a>
43+
<a class="nav-link _name" aria-current="page" href="#">[table]</a>
4444
</li>
45-
</ul>
45+
</ul>
46+
<!-- TABLE CONTENT (TableView) -->
47+
<div class="m-2">
48+
<table class="table table-hover table-sm table-striped" id="schema-table-data">
49+
<thead>
50+
<tr>
51+
<th>[col]</th>
52+
</tr>
53+
</thead>
54+
<tbody>
55+
<tr>
56+
<td>[row]</td>
57+
</tr>
58+
<tr>
59+
<td>[row]</td>
60+
</tr>
61+
<tr>
62+
<td>[row]</td>
63+
</tr>
64+
</tbody>
65+
</table>
66+
</div>
4667
</div>
4768
<div class="col-md-2 d-md-block d-none bg-light border-start" id="database">
4869
<!-- BODY CONTENT (DatabaseView) -->
4970
<h5 class="_version pt-1"></h5>
71+
<span class="badge bg-secondary badge-sm _pool">[pool]</span>
72+
<hr>
5073
<button class="btn btn-primary btn-sm m-1 action action-modules">List Modules</button>
5174
<button class="btn btn-primary btn-sm m-1 action">Import</button>
5275
<table class="table table-hover table-sm">
@@ -74,7 +97,9 @@ <h5 class="modal-title">Modules</h5>
7497
<form>
7598
<table class="table table-sm">
7699
<tbody id="database-modules-list">
77-
<tr class="_template"><td class="module">[Module]</td></tr>
100+
<tr class="_template">
101+
<td class="module">[Module]</td>
102+
</tr>
78103
</tbody>
79104
</table>
80105
</form>

npm/sqlite3/js/controller/app.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const API_STATIC_PREFIX = '/api/static';
1717
const API_SQLITE_PREFIX = '/api/sqlite';
1818
const API_SQLITE_DELTA = 30 * 1000;
1919
const API_STATIC_DELTA = 30 * 1000;
20+
const DEFAULT_SCHEMA = 'main';
2021

2122
export default class App extends Controller {
2223
constructor() {
@@ -49,18 +50,20 @@ export default class App extends Controller {
4950
const schemaNode = document.querySelector('#schema');
5051
super.define('schemaview', new SchemaView(schemaNode));
5152
this.schemaview.addEventListener(['schema:change'], () => {
52-
// TODO: Set active as the first node
5353
console.log('schema:change');
54+
55+
// Set active table to the first table in the schema
5456
});
5557
this.schemaview.addEventListener(['schema:click'], (sender, target) => {
58+
console.log(`schema:click ${target}`);
59+
5660
// Set active
5761
this.schemaview.active = target;
62+
5863
// Load the table data
59-
console.log(`schema:click ${target}`);
6064
this.tabledata.do(`/${target.schema}/${target.name}`);
6165
});
6266

63-
6467
// PROVIDERS
6568
super.define('static', new Provider(Endpoint, API_STATIC_PREFIX));
6669
if (this.static) {
@@ -80,8 +83,17 @@ export default class App extends Controller {
8083
this.sqlite.addEventListener('provider:error', (sender, error) => {
8184
this.toast.show(error);
8285
});
83-
this.sqlite.addEventListener(['provider:added', 'provider:changed'], (sender, database) => {
84-
console.log(`sqlite added or changed: ${database}`);
86+
this.sqlite.addEventListener(['provider:added'], (sender, database) => {
87+
console.log(`sqlite added: ${database}`);
88+
89+
// Set the database view
90+
this.databaseview.database = database;
91+
92+
// Load the 'main' schema
93+
this.schema.request(`/${DEFAULT_SCHEMA}`, null, API_SQLITE_DELTA);
94+
});
95+
this.sqlite.addEventListener(['provider:changed'], (sender, database) => {
96+
console.log(`sqlite changed: ${database}`);
8597
this.databaseview.database = database;
8698
});
8799
this.sqlite.addEventListener('provider:deleted', (sender, database) => {

npm/sqlite3/js/model/sqlite3/database.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Model } from '@djthorpe/js-framework';
2+
import { Pool } from './pool';
23

34
export default class Database extends Model {
45
static define() {
56
super.define(Database, {
67
version: 'string',
78
modules: '[]string',
89
schemas: '[]string',
10+
pool: 'Pool',
911
}, 'Database');
1012
}
1113
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Model } from '@djthorpe/js-framework';
2+
3+
export default class Pool extends Model {
4+
static define() {
5+
super.define(Pool, {
6+
cur: 'number',
7+
max: 'number',
8+
}, 'Pool');
9+
}
10+
}
11+
12+
Pool.define();

npm/sqlite3/js/model/sqlite3/row.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Model } from '@djthorpe/js-framework';
33
export default class Row extends Model {
44
constructor(data) {
55
super({});
6-
console.log(data);
6+
this.$data = data;
77
}
88

99
static define() {

npm/sqlite3/js/view/sqlite3/database.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ const EVENT_CLICK = 'view:click';
88
export default class DatabaseView extends View {
99
constructor(node) {
1010
super(node);
11+
1112
// Add view for list of schemas
1213
const nodeSchemas = this.query('#database-schemas');
1314
if (nodeSchemas) {
1415
this.$schemas = new List(nodeSchemas, '_template');
1516
}
17+
1618
// Add modal for list of modules
1719
const nodeModules = this.query('#database-modules');
1820
if (nodeModules) {
1921
this.$modules = new Form(nodeModules);
2022
}
23+
2124
// Add list for list of modules
2225
const nodeModulesList = this.query('#database-modules-list');
2326
if (nodeModulesList) {
@@ -55,6 +58,10 @@ export default class DatabaseView extends View {
5558
this.$modulelist.set(module).replace('.module', module);
5659
});
5760
}
61+
// Add pool information
62+
if (v.pool) {
63+
this.pool = v.pool;
64+
}
5865
}
5966

6067
/**
@@ -64,6 +71,18 @@ export default class DatabaseView extends View {
6471
this.replace('._version', v);
6572
}
6673

74+
/**
75+
* @param {Pool} pool
76+
*/
77+
set pool(pool) {
78+
if (pool) {
79+
this.replace('._pool', `Pool ${pool.cur}/${pool.max}`);
80+
} else {
81+
this.replace('._pool', '');
82+
}
83+
}
84+
85+
/* Show the modules modal */
6786
showModules() {
6887
this.$modules.show();
6988
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {
2+
View,
3+
} from '@djthorpe/js-framework';
4+
5+
export default class TableView extends View { }

0 commit comments

Comments
 (0)