Skip to content

Instantly share code, notes, and snippets.

@sdobber
Created December 11, 2022 11:28
Show Gist options
  • Save sdobber/752bbf0126d149c2c4f64bb63463176a to your computer and use it in GitHub Desktop.
Save sdobber/752bbf0126d149c2c4f64bb63463176a to your computer and use it in GitHub Desktop.
JSStore Setup
// from https://github.com/ujjwalguptaofficial/jsstore-examples/tree/master/react
import {
idbCon
} from "./idb_service";
export class BaseService {
get connection() {
return idbCon;
}
}
/* eslint import/no-webpack-loader-syntax: off */
import { DATA_TYPE, Connection } from 'jsstore';
const getWorkerPath = () => {
console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV === 'development') {
return require("file-loader?name=scripts/[name].[hash].js!jsstore/dist/jsstore.worker.js");
}
else {
return require("file-loader?name=scripts/[name].[hash].js!jsstore/dist/jsstore.worker.min.js");
}
};
// This will ensure that we are using only one instance.
// Otherwise due to multiple instance multiple worker will be created.
const workerPath = getWorkerPath().default;
export const idbCon = new Connection(new Worker(workerPath));
export const dbname = 'DanceItJudgeInterfaceDB';
const getDatabase = () => {
const judgingLog = {
name: 'JudgingLog',
columns: {
id: {
primaryKey: true,
autoIncrement: true
},
datetime: {
notNull: true,
dataType: DATA_TYPE.DateTime
},
judgeID: {
dataType: DATA_TYPE.Number,
},
compID: {
notNull: true,
dataType: DATA_TYPE.Number
},
roundID: {
notNull: true,
dataType: DATA_TYPE.Number
},
round: {
notNull: true,
dataType: DATA_TYPE.String
},
competition: {
notNull: true,
dataType: DATA_TYPE.String
},
dance: {
notNull: true,
dataType: DATA_TYPE.String
},
heat: {
notNull: true,
dataType: DATA_TYPE.String
},
mutualexclusive: {
notNull: true,
dataType: DATA_TYPE.Boolean
},
number: {
notNull: true,
dataType: DATA_TYPE.Number
},
score: {
notNull: true,
dataType: DATA_TYPE.Number
},
judgingscheme: {
notNull: true,
dataType: DATA_TYPE.Array
},
}
};
const judgingState = {
name: 'JudgingState',
columns: {
roundId: {
primaryKey: true
},
datetime: {
notNull: true,
dataType: DATA_TYPE.DateTime
},
dance: {
notNull: true,
dataType: DATA_TYPE.String
},
heat: {
notNull: true,
dataType: DATA_TYPE.String
},
dancers: {
notNull: true,
dataType: DATA_TYPE.Array
},
scores: {
notNull: true,
dataType: DATA_TYPE.Object
},
}
};
const dataBase = {
name: dbname,
tables: [judgingLog, judgingState],
version: 2
};
return dataBase;
};
export const initJsStore = () => {
try {
const dataBase = getDatabase();
idbCon.initDb(dataBase);
}
catch (ex) {
console.error(ex);
}
};
import { JudgingLogService } from "./storage_service/judginglog_service";
const Whatever = (props) => {
const logService = new JudgingLogService()
const drawerOpen = async () => {
const judgingLog = await logService.getValidJudgements(props.judgeID)
setChecksumData(processJudgingLog(judgingLog))
setViewChecksums(true)
}
}
import {
BaseService
} from "./base_service";
export class JudgingLogService extends BaseService {
constructor() {
super();
this.tableName = "JudgingLog";
}
getJudgements() {
return this.connection.select({
from: this.tableName,
})
}
addJudgement(judgement) {
return this.connection.insert({
into: this.tableName,
values: [judgement],
return: true // since studentid is autoincrement field and we need id,
// so we are making return true which will return the whole data inserted.
})
}
addJudgements(judgements) {
return this.connection.insert({
into: this.tableName,
values: judgements,
return: true // since studentid is autoincrement field and we need id,
// so we are making return true which will return the whole data inserted.
})
}
getJudgementById(id) {
return this.connection.select({
from: this.tableName,
where: {
id: id
}
})
}
getCurrentJudgements(judgeID) {
const from = new Date()
const to = new Date()
from.setDate(from.getDate() - 3);
to.setDate(to.getDate() + 1);
const allJudgements = this.connection.select({
from: this.tableName,
where: {
judgeID: judgeID,
datetime: {
'-': {
low: from,
high: to
}
},
}
})
return allJudgements
}
async getValidJudgements(judgeID) {
const allJudgements = await this.getCurrentJudgements(judgeID)
const competitions = [...new Set(allJudgements.map(e => e.competition))]
let competitionsDicts = []
competitions.forEach(comp => {
const compRounds = allJudgements.filter((v) => v.competition === comp)
const roundIDs = [...new Set(compRounds.map((e) => e.roundID))]
let rounds = []
roundIDs.forEach(rid => {
const roundData = allJudgements.filter((v) => v.roundID === rid)
const dances = [...new Set(roundData.map(rd => rd.dance))]
const danceDicts = []
dances.forEach(dance => {
let numbers = []
let scores = []
const danceData = roundData.filter((v) => v.dance === dance)
const heats = [...new Set(danceData.map(rd => rd.heat))]
heats.forEach(heat => {
const heatData = danceData.filter((v) => v.heat === heat)
// Only use data with newest timestamp, in case the tournament
// leader has aborted a round
const dts = heatData.map((entry) => entry.datetime)
const maxDate = new Date(Math.max.apply(null, dts));
const finalData = heatData.filter((v) => v.datetime.getTime() === maxDate.getTime())
numbers.push(...finalData.map(v => v.number))
scores.push(...finalData.map(v => v.score))
})
danceDicts.push({
dance: dance,
numbers: numbers,
scores: scores
})
})
rounds.push({
roundID: rid,
round: roundData[0].round,
competition: roundData[0].competition,
results: danceDicts,
mutualexclusive: roundData[0].mutualexclusive,
judgingscheme: roundData[0].judgingscheme
})
}
)
competitionsDicts.push({
name: comp,
rounddata: rounds
})
})
return competitionsDicts
}
removeJudgement(id) {
return this.connection.remove({
from: this.tableName,
where: {
id: id
}
})
}
updateJudgementById(id, updateData) {
return this.connection.update({
in: this.tableName,
set: updateData,
where: {
id: id
}
})
}
clearJudgingLog() {
return this.connection.clear(this.tableName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment