Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
61d6896
worker in place, data in place, working on redis
davirk21 Oct 26, 2017
7e00672
ready for deployment
davirk21 Nov 1, 2017
d29a3a3
fixed node to latest and database pg
davirk21 Nov 1, 2017
6626f40
update node enginer packagejson
davirk21 Nov 1, 2017
efb194f
fix pg issue
davirk21 Nov 1, 2017
86b73a4
fix pg issue
davirk21 Nov 1, 2017
b6ce059
fix clock.js
davirk21 Nov 1, 2017
1baa784
fix clock.js
davirk21 Nov 1, 2017
b58d002
add console.log in worker
davirk21 Nov 1, 2017
c394dba
edit package json
davirk21 Nov 1, 2017
cb16f88
editing procfile
davirk21 Nov 1, 2017
080ee11
editing clock.js
davirk21 Nov 1, 2017
a4edde5
editing clock.js
davirk21 Nov 1, 2017
b1e611e
change order for database grandulaity
davirk21 Nov 1, 2017
e2e08c1
fixed the date by asc
davirk21 Nov 1, 2017
9c1a991
check inspectionheroku is working
davirk21 Nov 1, 2017
b7c313c
fix graphite error servie
davirk21 Nov 1, 2017
0cdd08c
fix graphite statusD
davirk21 Nov 1, 2017
30bc846
fix server index.js
davirk21 Nov 2, 2017
4ea0f3b
fixed caching to req.query
davirk21 Nov 2, 2017
9625b9f
load tester for heroku
davirk21 Nov 4, 2017
d069ed6
fixed granularity error on server/index.js
davirk21 Nov 7, 2017
ea07d01
fixed granularity error on server/index.js
davirk21 Nov 7, 2017
ef0c081
Clean Code
davirk21 Apr 12, 2018
01d1216
Clean Code
davirk21 Apr 12, 2018
8eac772
code cleanup
davirk21 Apr 12, 2018
6cb4dfe
Code Clean up
davirk21 Apr 12, 2018
d028373
Code Clean Up
davirk21 Apr 12, 2018
628f2dc
Code Clean up
davirk21 Apr 12, 2018
78c8c2f
Dont Need
davirk21 Apr 12, 2018
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ typings/
# dotenv environment variables file
.env

redis*

config*

amazon_EC2*

3 changes: 3 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: node server/index.js
clock: node clock.js
loader: node server/loadtester.js
15 changes: 15 additions & 0 deletions clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var CronJob = require('cron').CronJob;
var gov = require('./helper/gov.js');
var gen = require('./generate/generator.js')

// gov.worker1();

var job = new CronJob({
cronTime: '00 00 00 * * 1-5',
onTick: gov.worker1(),
start: true,
timeZone: 'America/Los_Angeles'
});

job.start()
console.log('fire instance clock job status', job.running);
147 changes: 147 additions & 0 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const pg = require('pg')
const pgp = require('pg-promise')();

const connectionString = process.env.DATABASE_URL || 'postgresql://localhost:5432/test';
const clientPromise = pgp(process.env.DATABASE_URL || 'postgresql://localhost:5432/test');

const client = new pg.Client({
connectionString: connectionString,
})
client.connect()

//check database if inspecction data for particular date exists
var checkRowCountForDate = function (date, callback){
var query = `SELECT count(*) FROM restaurantscore WHERE inspection_date = '${date}'`;
return clientPromise.query(query)
.then(data => {
return data;
})
.catch(err => {
console.log("eer", err);
})
}

// save inpection data to the database
var save = function(result, callback) {
result.map((entry) => {
var zip = entry.business_postal_code;
var inspectionId = entry.inspection_id;
var inspectionDate = entry.inspection_date.includes('T') ? changeDate(entry.inspection_date) : entry.inspection_date;
var inspectionType = entry.inspection_type;
var risk = entry.risk_category;

var query = `INSERT INTO restaurantscore (
business_zip,
inspection_id,
inspection_date,
inspection_type,
risk_category
)
VALUES
($1,$2,$3,$4,$5)
`

client.query(query,[zip,inspectionId,inspectionDate, inspectionType, risk], function(err, res){
if(err){
console.log("errhhh", err)
callback(err, null)
} else {
callback("success into database")
}
})
})
}

//save single inspection to the database (used for data generator for testing)
var saveSingle = function(entry, callback) {
var zip = entry.business_postal_code;
var inspectionId = entry.inspection_id;
var inspectionDate = entry.inspection_date.includes('T') ? changeDate(entry.inspection_date) : entry.inspection_date;
var inspectionType = entry.inspection_type;
var risk = entry.risk_category;
var query = `INSERT INTO restaurantscore (
business_zip,
inspection_id,
inspection_date,
inspection_type,
risk_category
)
VALUES
($1,$2,$3,$4,$5)
`
client.query(query,[zip,inspectionId,inspectionDate, inspectionType, risk], function(err, res){
if(err){
callback(err, null)
} else {
callback("success into database")
}
})
}

//changes the inspection date to correct formate '2017-09-26 00:00:00' from '2017-09-26T00:00:00.000'
var changeDate = function(string){
var chars = string.split("");
var letter = "T"
chars.splice(chars.indexOf(letter),1," ");
chars.splice(-4,chars.length);
return chars.join("");
}

//get information by granduarlity with zip
var getByZipByGran = function(zip, startDate, endDate, granularity, callback) {
console.log("datbase stuff",zip, startDate, endDate, granularity);
var query =
`select date_trunc('${granularity}', inspection_date) as ${granularity},
sum((risk_category = 'High Risk')::int) as High,
sum((risk_category = 'Moderate Risk')::int) as moderate,
sum((risk_category = 'Low Risk')::int) as Low

from restaurantscore
where business_zip = '${zip}' and inspection_date > '${startDate}' and inspection_date < '${endDate}'
group by ${granularity}
order by ${granularity};`

client.query(query, function (err, res) {
if(err){
console.log("THIS ERROR", err);
console.log('this was the query', query);
callback(err, null)

}else {
callback(null,res.rows)
}
})
}

//get inspection datat for last 3 months with zip
var getlastThreeMonths = function (zip, callback) {
var query =
`SELECT business_zip,
sum(case when risk_category = 'High Risk' then 1 else 0 end) as HighRiskInspections,
sum(case when risk_category = 'Moderate Risk' then 1 else 0 end) as ModerateRiskInpections,
sum(case when risk_category = 'Low Risk' then 1 else 0 end) as LowRiskInspections
from restaurantscore
group by business_zip`

client.query(query, function(err, res){
if(err){
callback(err, null)
} else {
console.log("heeee", res.rows)
callback(res.rows);
}
})
}



module.exports = {
client: client,
save,
saveSingle,
getlastThreeMonths,
// getByZip,
getByZipByGran,
checkRowCountForDate
}

34 changes: 34 additions & 0 deletions database/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var redis = require("redis"),
client = redis.createClient(process.env.REDISTOGO_URL);
// if you'd like to select database 3, instead of 0 (default), call
// client.select(3, function() { /* ... */ });
client.on("error", function (err) {
console.log("Error " + err);
});

// var key = "scott"
// client.set(key, "is here", redis.print);

// client.get(key, function(err, reply) {
// // reply is null when the key is missing
// console.log("cache",key +':'+reply);
// });


var getInscpectionData = function (zip, callback) {
client.get(query, (err, result) => {
if(err) {
console.log("Error", err)
callback(err, null);
}
if(result){
callback(null, JSON.parse(result));
}
callback(null, []);
})
}

module.exports = {
client,
}

Loading