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

v0 for the ranking algorithm stolen from the other implementation, cu… #13

Open
wants to merge 2 commits into
base: develop
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
327 changes: 180 additions & 147 deletions app/records/eyeglassRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,157 +28,190 @@ namespace reimsApp.EyeglassRecords {
}

export class EyeglassRecords {
public static $inject: string[] = ["pouchDB", "$rootScope"];
private localDB: any;
private remoteDB: any;

constructor(private pouchDB: any, $rootScope: any) {
const database_name = "eyeglasses";
console.log("EyeglassRecords services");

this.localDB = pouchDB(database_name);
console.log("Opened local database ", this.localDB);

this.remoteDB = pouchDB("http://localhost:5984/" + database_name);
console.log("Opened remote database ", this.remoteDB);

const syncManager = this.localDB.sync(this.remoteDB, {
live: true,
retry: true
});

syncManager.on("change", function (change) {
// yo, something changed!
console.log("Something changed", change);
$rootScope.$broadcast("sync:change", change);
}).on("paused", function (info) {
// replication was paused, usually because of a lost connection
console.log("Replication paused", info);
$rootScope.$broadcast("sync:pause", info);
}).on("active", function (info) {
// replication was resumed
console.log("Replication back online", info);
$rootScope.$broadcast("sync:active", info);
}).on("error", function (err) {
// totally unhandled error (shouldn"t happen)
console.log("Replicaiton catastrophically failed", err);
$rootScope.$broadcast("sync:failure", err);
}).on("complete", function(err) {
console.log("The sync has ended");
$rootScope.$broadcast("sync:down", err);
});
}

localInfo() {
return this.localDB.info();
}

remoteInfo() {
return this.remoteDB.info();
}

localAllDocs(opts: any) {
return this.localDB.allDocs(opts);
}

addRecord(terms: IEyeglassRecord): any {
console.log(terms);

terms.status = "unfiled";

// input without caring about id
return this.localDB.post(terms);
}

updateDocStatus(doc: any, status: string) {
console.log("Updating status to ", status, doc);
doc.status = status;
this.localDB.put(doc).then(function(res) {
console.log(res);
}).catch(function(err){
console.error(err);
});
}

updateRowsStatus(rows: any[], status: string) {
angular.forEach(rows, (row) => {
this.updateDocStatus(row.doc, status);
});
}

deleteRows(rows: any[]) {
angular.forEach(rows, (row) => {
this.updateDocStatus(row.doc, "deleted");
});
}

getByStatus(status: string) {
const statusIndex = {
_id: "_design/status_index",
views: {
by_status: {
map: function emitStatus(doc) {
if (doc.status) {
emit(doc.status);
} else {
emit("no status");
}
}.toString()
public static $inject: string[] = ["pouchDB", "$rootScope"];
private localDB: any;
private remoteDB: any;

constructor(private pouchDB: any, $rootScope: any) {
const database_name = "eyeglasses";
console.log("EyeglassRecords services");

this.localDB = pouchDB(database_name);
console.log("Opened local database ", this.localDB);

this.remoteDB = pouchDB("http://localhost:5984/" + database_name);
console.log("Opened remote database ", this.remoteDB);

const syncManager = this.localDB.sync(this.remoteDB, {
live: true,
retry: true
});

syncManager.on("change", function (change) {
// yo, something changed!
console.log("Something changed", change);
$rootScope.$broadcast("sync:change", change);
}).on("paused", function (info) {
// replication was paused, usually because of a lost connection
console.log("Replication paused", info);
$rootScope.$broadcast("sync:pause", info);
}).on("active", function (info) {
// replication was resumed
console.log("Replication back online", info);
$rootScope.$broadcast("sync:active", info);
}).on("error", function (err) {
// totally unhandled error (shouldn"t happen)
console.log("Replicaiton catastrophically failed", err);
$rootScope.$broadcast("sync:failure", err);
}).on("complete", function(err) {
console.log("The sync has ended");
$rootScope.$broadcast("sync:down", err);
});
}

localInfo() {
return this.localDB.info();
}

remoteInfo() {
return this.remoteDB.info();
}

localAllDocs(opts: any) {
return this.localDB.allDocs(opts);
}

addRecord(terms: IEyeglassRecord): any {
console.log(terms);

terms.status = "unfiled";

// input without caring about id
return this.localDB.post(terms);
}

updateDocStatus(doc: any, status: string) {
console.log("Updating status to ", status, doc);
doc.status = status;
this.localDB.put(doc).then(function(res) {
console.log(res);
}).catch(function(err){
console.error(err);
});
}

updateRowsStatus(rows: any[], status: string) {
angular.forEach(rows, (row) => {
this.updateDocStatus(row.doc, status);
});
}

deleteRows(rows: any[]) {
angular.forEach(rows, (row) => {
this.updateDocStatus(row.doc, "deleted");
});
}

getByStatus(status: string) {
const statusIndex = {
_id: "_design/status_index",
views: {
by_status: {
map: function emitStatus(doc) {
if (doc.status) {
emit(doc.status);
} else {
emit("no status");
}
}.toString()
}
}
}
};
};

this.localDB.put(statusIndex).then(function() {
console.log("created index");
}).catch(function(err) {
if (err.status !== 409) {
console.error(err);
}
console.log("index already installed");
});

return this.localDB.query("status_index/by_status", {
key: status,
include_docs: true
});
}
this.localDB.put(statusIndex).then(function() {
console.log("created index");
}).catch(function(err) {
if (err.status !== 409) {
console.error(err);
}
console.log("index already installed");
});

return this.localDB.query("status_index/by_status", {
key: status,
include_docs: true
});
}

fullSearch(terms: any, callback: (any)) {
this.getByStatus("filed").then((results) => {
angular.forEach(results.rows, (row) => {
console.log("Eyeglasses running full search", row, terms);
// abs(od_sphere-?) + abs(os_sphere-?) SPHERE_RANK,
const SPHERE_RANK = Math.abs(row.doc.odsphere - (terms.odsphere || 0)) +
Math.abs(row.doc.ossphere - (terms.ossphere || 0));
// abs(IFNULL(od_cylinder,0)-?) + abs(IFNULL(os_cylinder,0)-?) CYLINDER_RANK,
const CYLINDER_RANK = Math.abs(row.doc.odcylinder - (terms.odcylinder || 0)) +
Math.abs(row.doc.oscylinder - (terms.oscylinder || 0));

// abs( (IFNULL(od_cylinder,0)/2 + od_sphere) - (?*0.5+?) ) +
// abs( (IFNULL(os_cylinder,0)/2 + os_sphere) - (?*0.5+?) ) SPHERICAL_EQ_RANK,
const OD_SPHERICAL_EQ_RANK = Math.abs((row.doc.odcylinder || 0) / 2.0 + (1 * row.doc.odsphere)) -
((terms.odcylinder || 0) * 0.5 + (terms.odsphere || 0));
const OS_SPHERICAL_EQ_RANK = Math.abs((row.doc.oscylinder || 0) / 2.0 + (1 * row.doc.ossphere)) -
((terms.oscylinder || 0) * 0.5 + (terms.ossphere || 0));
const SPHERICAL_EQ_RANK = OD_SPHERICAL_EQ_RANK + OS_SPHERICAL_EQ_RANK;

// abs(IFNULL(od_add,0)- 1.00) + abs(IFNULL(os_add,0)- 1.00) ADD_RANK,
const ADD_RANK = Math.abs((row.doc.odadd || 0) - 1.0) + Math.abs((row.doc.osadd || 0) - 1.0);

// (1*IFNULL(SPHERE_RANK,0) + 1*IFNULL(CYLINDER_RANK,0) + 2*IFNULL(SPHERICAL_EQ_RANK,0) + 1*IFNULL(ADD_RANK,0))/5 BIG_RANK
// (1*IFNULL(SPHERE_RANK,0) + 1*IFNULL(CYLINDER_RANK,0) + 1*IFNULL(SPHERICAL_EQ_RANK,0) + 1*IFNULL(ADD_RANK,0))/4 BIG_RANK2
const RANK = SPHERE_RANK + CYLINDER_RANK + 2 * SPHERICAL_EQ_RANK + (ADD_RANK / 5);

console.log(SPHERE_RANK, CYLINDER_RANK, SPHERICAL_EQ_RANK, ADD_RANK, RANK);
row.doc.score = RANK;
});
callback(results);
});
}

getStatusHist() {
// create a design doc
const ddoc = {
_id: "_design/index",
views: {
index: {
map: function mapFun(doc) {
if (doc.status) {
emit(doc.status);
} else {
emit("no status");
}
}.toString(),
reduce: "_count"
}
}
};

getStatusHist() {
// create a design doc
const ddoc = {
_id: "_design/index",
views: {
index: {
map: function mapFun(doc) {
if (doc.status) {
emit(doc.status);
} else {
emit("no status");
}
}.toString(),
reduce: "_count"
// save the design doc
this.localDB.put(ddoc).catch(function (err) {
if (err.status !== 409) {
throw err;
}
}
};

// save the design doc
this.localDB.put(ddoc).catch(function (err) {
if (err.status !== 409) {
throw err;
}
// ignore if doc already exists
}).then(function() {
// find docs where title === "Lisa Says"
}).then(function (result) {
// handle result
}).catch(function (err) {
console.log(err);
});
return this.localDB.query("index", {
keys: ["dispensed", "missing", "filed", "unfiled"],
group: true
});
}
// ignore if doc already exists
}).then(function() {
// find docs where title === "Lisa Says"
}).then(function (result) {
// handle result
}).catch(function (err) {
console.log(err);
});
return this.localDB.query("index", {
keys: ["dispensed", "missing", "filed", "unfiled"],
group: true
});
}
};

const app = angular.module("reimsApp.EyeglassRecords", ["pouchdb"])
Expand Down
11 changes: 9 additions & 2 deletions app/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ namespace reimsApp.Search {

this.$scope.onSubmit = {
name: "Full Search",
func: function(valid: boolean, error: any, terms: any) {
func: (valid: boolean, error: any, terms: any) => {
// ignore validity of form
$scope.fullSearchResults = [{doc: {sku: "success"}}];
console.log("Running full search", this);
this.$scope.loading = true;
EyeglassRecords.fullSearch(terms, (results) => {
console.log("Running callback");
this.$scope.fullSearchResults = results.rows;
this.$scope.loading = false;
});
this.showFull();
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion public/partials/resultRow.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<td><input type="checkbox" ng-model="result.selected"></td>
<td>1</td> <!-- score -->
<td>{{result.doc.score || 0}}</td> <!-- score -->
<td>{{result.doc.sku}}</td>
<td>{{result.doc.type | eyeglassTypeFilter}}</td>
<td>{{result.doc.gender | eyeglassGenderFilter}}</td>
Expand Down
6 changes: 3 additions & 3 deletions public/partials/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
<table class="table table-condensed table-striped table-hover">
<thead reims-result-header></thead>
<tbody ng-hide="displayFull">
<tr reims-result-row ng-repeat="result in allResultRows | filter:{doc: searchTerms} | limitTo: 20" ng-click="Ctrl.selectRow(result)" ng-class="{info: result.selected}"></tr>
<tr reims-result-row ng-repeat="result in allResultRows | filter:{doc: searchTerms} | limitTo: 50" ng-click="Ctrl.selectRow(result)" ng-class="{info: result.selected}"></tr>
</tbody>
<tbody ng-hide="!displayFull">
<tr reims-result-row ng-repeat="result in fullSearchResults"></tr>
<tr reims-result-row ng-repeat="result in fullSearchResults | orderBy: 'doc.score' : 'number' | limitTo: 50"></tr>
</tbody>
</table>
<div ng-if="!allResultRows" class="col-sm-12 text-center">
<div ng-if="!allResultRows || loading" class="col-sm-12 text-center">
<img src="ripple.gif" /> <!-- from loading.io -->
</div>
</div>
Expand Down