Skip to content

Commit

Permalink
#528 Add tool to get rid of stale orders (#529)
Browse files Browse the repository at this point in the history
* #528 Add script to purge stale orders

* #528 Add purge tool to package.json and update doc
  • Loading branch information
ro31337 authored Feb 26, 2017
1 parent dac4e3f commit 455faa9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/GETTING-STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ When everything's done, it's time to test and run:
* `npm run telegram` - Start Telegram bot.
* `npm run build-production && npm run telegram-production` - Start Telegram bot for production.
* `npm run cli` - Start CLI version of the bot.
* `npm run tools:purge` - Purge stale orders, see [#528](https://github.com/ro31337/libretaxi/issues/528)

## Shutting down

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"cli": "babel-node ./src/app-cli.js",
"telegram": "babel-node ./src/app-telegram.js",
"start": "echo ERR: Type \\\"npm run cli [params]\\\" or \\\"npm run telegram [params]\\\"",
"esdoc": "esdoc -c esdoc.json && rm esdoc/package.json"
"esdoc": "esdoc -c esdoc.json && rm esdoc/package.json",
"tools:purge": "babel-node ./src/tools/purge-stale-orders.js"
},
"repository": {
"type": "git",
Expand Down
73 changes: 73 additions & 0 deletions src/tools/purge-stale-orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable no-console */
import firebase from 'firebase';
import firebaseDB from '../firebase-db';

const ordersDB = firebaseDB.config().ref('orders');
const now = (new Date).getTime();
const objectsToPurge = [];
let objectsToIgnoreCount = 0;

ordersDB.once('value', (snapshot) => {
const orders = snapshot.val();

// identify orders to purge

Object.keys(orders).forEach((key) => {
const createdAt = orders[key].createdAt || 0;
const timeDiff = (now - createdAt) / (1000 * 60 * 60); // hours

if (timeDiff > 6) {
objectsToPurge.push({ key, orderData: orders[key] });
} else {
objectsToIgnoreCount++;
}
});

console.log(`${objectsToPurge.length} orders to purge, ${objectsToIgnoreCount} to ignore`);

// create removeAll callback,
// executed by objectCopiedCallback when all objects have been copied

const removeAll = () => {
let removeCount = 0;
console.log('Everything was copied, time to remove all!');

const objectRemovedCallback = () => {
removeCount++;
console.log(`Orders removed: ${removeCount}`);
if (removeCount === objectsToPurge.length) {
console.log('All done!');
firebase.database().goOffline();
}
};

objectsToPurge.forEach((obj) => {
ordersDB.child(obj.key).remove(objectRemovedCallback);
});
};

// create "copied" callback

let copiedCount = 0;
const objectCopiedCallback = () => {
copiedCount++;
console.log(`Orders copied: ${copiedCount}`);
if (copiedCount === objectsToPurge.length) {
removeAll();
}
};

// copy orders to `staleOrders`

if (objectsToPurge.length > 0) {
objectsToPurge.forEach((obj) => {
const { key, orderData } = obj;
const objectDB = firebaseDB.config().ref(`staleOrders/${key}`);
objectDB.update(orderData, objectCopiedCallback);
});
} else {
firebase.database().goOffline();
}
}, (err) => {
console.log(`ERROR: ${err}`);
});

0 comments on commit 455faa9

Please sign in to comment.