Skip to content

Log items that share the same UUID #53

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

Open
wants to merge 17 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
54 changes: 30 additions & 24 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const express = require('express');

const qr = require('qr-image');

const {
loadDatabase,
Expand All @@ -11,20 +10,26 @@ const {

const app = express();

const allScans = [];
const allScans = new Map();

Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because I want to avoid repetitions in the "recent" page.
I want to add one by one unique items on that page

Copy link
Member

Choose a reason for hiding this comment

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

This is not the way to handle changes and features. We create an issue, discuss it and push it.
Hidding secret features in unrelated commits is not appropriate decision workflow and distracting for code review

function logScanned(uuid, fixture) {
// TRICK: fixture tells if the the uuid was found in database or not
function addRecentlyScanned(uuid, item, nbFound = 0) {
// TRICK: only record uuids
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)) {
return;
}
const now = new Date();
if (!fixture) {
allScans.unshift({ time: now, status: 'missing', uuid });
return;

const duplicatedItem = item;

duplicatedItem.time = new Date();
duplicatedItem.duplicated = nbFound > 1;
duplicatedItem.link = item.cellRef;
if (nbFound === 0) {
duplicatedItem.fixture = '';
duplicatedItem.uuid = uuid;
duplicatedItem.status = 'missing';
}
allScans.map(item => item.status = (item.uuid === uuid) ? 'fixed' : item.status);
allScans.unshift({ time: now, fixture, uuid });

allScans.set(uuid, duplicatedItem);
}

app.set('view engine', 'ejs');
Expand All @@ -38,19 +43,17 @@ app.get(['/favicon.ico', '/robots.txt'], (req, res) => {
app.get('/search', (req, res) => {
loadDatabase((allItems) => {
res.render('search', {
matches: searchDatabase(req.query, allItems)
.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1)),
matches: searchDatabase(req.query, allItems).sortByFloor(),
});
});
});

app.get('/qrlist', (req, res) => {
loadDatabase((allItems) => {
const qrList = searchDatabase(req.query, allItems)
.filter(item => item.uuid !== '')
.filter(item => item.uuid !== undefined)
.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1));
qrList.forEach(item => item.qr = qr.imageSync('http://url.coderbunker.com/' + item.uuid, { type: 'svg' }));
let qrList = searchDatabase(req.query, allItems);
qrList = qrList.sortByFloor();
qrList = qrList.filterEmptyUuid();
qrList.forEach(item => item.addQrImg());
res.render('qrList', { matches: qrList });
});
});
Expand All @@ -61,19 +64,22 @@ app.get('/recent', (req, res) => {

app.get('/:uuid', (req, res) => {
loadDatabase((allItems) => {
const match = searchDatabase(req.params, allItems)[0];
if (match.length === 0) {
logScanned(req.params.uuid);
const matches = searchDatabase(req.params, allItems);
if (matches.length === 0) {
addRecentlyScanned(req.params.uuid, {});
res.status(404).render('notFound', {
item: '',
id: req.params.uuid,
});
return;
}
addMarkdown(match);
addSimilarItems(match, allItems);
logScanned(req.params.uuid, match.fixture);
res.render('item', match);
if (matches.length > 1) {
console.log(`Too much matches for uuid ${req.params.uuid} length = ${matches.length}`);
}
addRecentlyScanned(req.params.uuid, matches[0], matches.length);
addMarkdown(matches[0]);
addSimilarItems(matches[0], allItems);
res.render('item', matches[0]);
});
});

Expand Down
46 changes: 36 additions & 10 deletions googleSpreadsheet.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
const google = require('googleapis');
const keys = require('./config/keys');
const marked = require('marked');
const qr = require('qr-image');

function rowToObject(val, lab) {
const o = {};
for (let i = 0; i < lab.length; i += 1) {
o[lab[i]] = val[i];
const spreadsheetDataId = '1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY';
const spreadsheetLink = `https://docs.google.com/spreadsheets/d/${spreadsheetDataId}/edit`;

// creates a dictionary mapping column names with the values
// ex: { floor : 402, business : coworking, etc..}
function spreadsheetValuesToObject(values, columns, index) {
const formatedRow = {};
for (let i = 0; i < columns.length; i += 1) {
formatedRow[columns[i]] = values[i];
}
return o;
formatedRow.addQrImg = function addQrImg() {
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(this.uuid)) {
this.qr = qr.imageSync(this.uuid, { type: 'svg' });
}
};
formatedRow.cellRef = `${spreadsheetLink}#gid=0&range=A${index}:T${index}`;
return formatedRow;
}

function loadDatabase(callback) {
const sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: keys.apiKey,
spreadsheetId: '1QHKa3vUpht7zRl_LEzl3BlUbolz3ZiL8yKHzdBL42dY',
spreadsheetId: spreadsheetDataId,
range: 'Agora inventory!A:Z',
}, (err, response) => {
if (err) {
console.log(`The API returned an error: ${err}`);
return;
}
return callback(response.values.map(row =>
rowToObject(row, response.values[0])).splice(1));
const columns = response.values[0];
// map function transforms a list into another list using the given lambda function
let i = 0;
const formatedRows = response.values.map((row) => {
i += 1;
return spreadsheetValuesToObject(row, columns, i);
});
return callback(formatedRows);
});
}

Expand All @@ -31,17 +49,25 @@ function searchDatabase(query, rows) {
Object.keys(query).map((key) => {
matches = matches.filter(item => item[key] === query[key]);
});
matches.filterEmptyUuid = function filterEmptyUuid() {
return this.filter(item => item.uuid !== '').filter(item => item.uuid !== undefined);
};
matches.sortByFloor = function sortByFloor() {
return this.sort((a, b) => (a.floor === b.floor ? 0 : +(a.floor > b.floor) || -1));
};
return matches;
}

function addSimilarItems(obj, allObj) {
function addSimilarItems(refItem, allObj) {
const obj = refItem;
obj.similarItems = searchDatabase({ fixture: obj.fixture }, allObj)
.filter(item => item.uuid !== obj.uuid)
.splice(0, 3);
return obj;
}

function addMarkdown(obj) {
function addMarkdown(refItem) {
const obj = refItem;
obj.HOWTO = marked(obj.HOWTO);
obj.details = marked(obj.details);
obj.Troubleshooting = marked(obj.Troubleshooting);
Expand Down
45 changes: 22 additions & 23 deletions views/recent.ejs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<% include ./partials/header %>
<div class="resultContainer col-xs-12">
<h1>Recently scanned QR code</h1>
<article class="col-xs-12">
<ul class='recent-list'>
<% for(var i = 0; i <allScans.length; i++){%>
<% if(allScans[i].fixture) { %>
<li>
<a href="/<%- allScans[i].uuid %>"><%- allScans[i].time.toTimeString() %> - <%- allScans[i].fixture %> </a> :<br/>
<%- allScans[i].uuid %>
</li>
<h1>Recently scanned QR codes</h1>
<article class="col-xs-12">
<ul class='recent-list'>
<% for (let item of allScans.values()) { %>
<li>
<%- item.time.toTimeString() %> -
<% if (item.fixture) { %>
<%- item.fixture %>
<% } else { %>
<li>
<a href="/<%- allScans[i].uuid %>"><%- allScans[i].time.toTimeString() %>
</a>
<span style="color:<%- allScans[i].status==='fixed'?'orange':'red'%>" >
:<br/>
<%- allScans[i].uuid %>
</span>
</li>
<%}%>
<%}%>
</ul>
</article>
</div>
???
<% } %>
: <br/>
<a href="/<%- item.uuid %>"><%- item.uuid %></a>
<% if (!item.fixture) { %>
<a href="<%- item.link %>" style="color: <%- item.status === 'fixed' ? 'orange' : 'red' %>">Missing Details!</a>
<% } %>
<% if (item.duplicated) { %>
<a href="<%- item.link %>" style="color: red">Duplicate UUID!</a>
<% } %>
</li>
<% } %>
</ul>
</article>
<% include ./partials/footer %>