'
88 | $('#myTable > tbody:last-child').append(contents);
89 | recalc()
90 | });
91 |
92 | // can't use $(".deleteMe").click here because the items are dynamically added, not all present at the start.
93 | $(document).on('click', 'button.deleteMe', function () {
94 | $(this).closest('tr').remove();
95 | recalc();
96 | });
97 |
98 | function formatTotal(x) {
99 | x = Math.round(x);
100 | return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
101 | }
102 |
103 | function recalc(){
104 | // iterate through all the values in the table (class="val")
105 | var resultVal = 0.0;
106 | $(".val").each ( function() {
107 | var itemval = $(this).text();
108 | itemval = itemval.replace('', "0");
109 | resultVal += parseFloat ( itemval.replace(/\s/g,'').replace(',','.'));
110 | });
111 | resultVal = formatTotal(resultVal);
112 | document.getElementById('result').innerHTML = "Total Purchase $" + resultVal;
113 | return(resultVal)
114 | }
115 |
116 | });
117 |
--------------------------------------------------------------------------------
/Website/readme.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # Fraud Detection
4 | ## Implemented on SQL Server 2016 R Services and HDInsight Spark
5 |
6 | > Discover more examples at [Microsoft Machine Learning Server](https://github.com/Microsoft/ML-Server)
7 |
8 |
9 | Deploy this solution from Cortana Intelligence Gallery with [SQL Server](https://aka.ms/fraud-detection) or [HDInsight Spark Cluster](https://aka.ms/fraud-detection-hdi).
10 |
11 | For all documentation, visit the [Fraud Detection website](https://microsoft.github.io/r-server-fraud-detection/).
12 |
13 | **NOTE:** Please don't use "Download ZIP" to get this repository, as it will change the line endings in the data files. Use "git clone" to get a local copy of this repository.
14 |
15 | # Contributing
16 |
17 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
18 |
--------------------------------------------------------------------------------
/Website/server.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 | var Connection = require('tedious').Connection;
3 | var Request = require('tedious').Request;
4 | var TYPES = require('tedious').TYPES;
5 |
6 | var fs = require('fs');
7 | var util = require('util');
8 | var logFileName = __dirname + '/debug.log';
9 |
10 | var app = express();
11 | var exphbs = require('express-handlebars');
12 | app.engine('handlebars', exphbs({defaultLayout: 'main'}));
13 | app.set('view engine', 'handlebars');
14 |
15 | app.use(express.static('public'));
16 |
17 |
18 |
19 |
20 | //
21 | // DB Connection
22 | //
23 | var args = process.argv.slice(2);
24 | if (args.length>0) {
25 | var user = args[0];
26 | var pw = args[1];
27 | }
28 | else {
29 | var user = 'XXYOURSQLUSER';
30 | var pw = 'XXYOURSQLPW';
31 | }
32 |
33 |
34 | var con = new Connection({ //fix this with fraud db info
35 | userName: user,
36 | password: pw,
37 | server: 'localhost',
38 | // When you connect to Azure SQL Database, you need encrypt: true
39 | options: { encrypt: true, database: 'Fraud_R' }
40 | });
41 |
42 | con.on('connect', function(err) {
43 | console.log('DB Connection ' + (err ? '~~~ Failure ~~~' : 'Success'));
44 | if (err) console.log(err);
45 | });
46 |
47 | //
48 | // Put your routes here
49 | //
50 |
51 | // Home Page
52 | app.get('/', function (req, res) {
53 | res.render('home')
54 | });
55 |
56 | // Kill the server
57 | app.get('/kill', function (req, res) {
58 | setTimeout(() => process.exit(), 500);
59 | });
60 |
61 |
62 | // predict function, called from scoreClaim.js
63 |
64 | app.get('/predict', function (req, res) {
65 | var request = new Request('ScoreOneTrans', function(err, rowCount) {
66 | if (err) {
67 | console.log(err);
68 | }
69 | // console.log("Rows Returned: " + rowCount )
70 | });
71 |
72 | var record = req.query.record;
73 | console.log (record)
74 | request.on('row', function(col) {
75 | if (col[0].value === null) {
76 | console.log('NULL');
77 | } else {
78 | // values to return - the predicted probability
79 | value = col[0].value;
80 | }
81 |
82 | res.json({ pred: value });
83 | request.on('doneInProc', function(rowCount, more) {
84 | console.log(rowCount + ' rows returned');
85 | });
86 |
87 | });
88 | // pass the entire record to the stored procedure
89 | request.addParameter('inputstring', TYPES.VarChar, record);
90 | con.callProcedure(request);
91 | con.close;
92 |
93 |
94 | });
95 |
96 | //log to file
97 | var logFile = fs.createWriteStream(logFileName, { flags: 'a' });
98 | var logProxy = console.log;
99 | console.log = function (d) { //
100 | logFile.write(util.format(new Date() + ": " + d || '') + '\r\n');
101 | logProxy.apply(this, arguments);
102 | };
103 |
104 | app.listen(3000, function () {
105 | console.log('Example app listening on port 3000!');
106 | });
--------------------------------------------------------------------------------
/Website/startMe.vbs:
--------------------------------------------------------------------------------
1 | user = WScript.Arguments.Item(0)
2 | pw = WScript.Arguments.Item(1)
3 |
4 | cmd = "node server.js " + user + " " + pw
5 |
6 | CreateObject("Wscript.Shell").Run cmd, 0
--------------------------------------------------------------------------------
/Website/views/home.handlebars:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
Your Cart
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
Total Purchase: $0
36 |
37 |
38 |
39 |
40 |
41 |
Processing your order...
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
Select Items to Purchase
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
Black and White Diamond Heart
71 |
Price: $130
72 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
Diamond Pave Earrings
84 |
Price: $569
85 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
Diamond Tennis Bracelet
98 |
Price: $360
99 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
Diamond Engagement Ring
112 |
Price: $2100
113 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
Login to Your Account
147 |
148 |
149 |
150 |
151 |
152 |
Online Fraud Detection Demo
153 |
This site demonstrates how a website might interact with a fraud prediction score. When the user purchases from this site, the transaction is sent first to SQL Server to predict a score. When the score is high, the transaction has a high probability of being fraudulent.
154 |
After you click OK below, try adding some items to your cart and purchase them.
155 | Then use the Login to Your Account button on the shopping page to return here to select a different account, and try the purchase again.
156 |
157 |
158 |
Try an Account
159 |
Here are a few different accounts to try out for this demo.