can now view graphs

This commit is contained in:
Enrico Fasoli 2015-03-13 21:03:11 +01:00
parent bd1f993373
commit 6b5f5f504d
9 changed files with 98 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
node_modules/
client/bower_components/
test/
lib/
bin/

View File

@ -1,2 +1,3 @@
src/
client/bower_components/
test/

21
client/bower.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "pert-gui",
"version": "0.1.0",
"homepage": "https://github.com/fazo96/pert",
"authors": [
"Enrico Fasoli <fazius2009@gmail.com>"
],
"description": "web ui for pert",
"license": "MIT",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"vis": "~3.11.0"
}
}

20
client/index.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Pert</title>
<link rel="stylesheet" href="bower_components/vis/dist/vis.min.css">
<style>
#pert {
width: 400px;
height: 400px;
border: 1px solid lightgray;
}
</style>
</head>
<body>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/vis/dist/vis.min.js"></script>
<div id="pert"></div>
<script src="pert-ui.js"></script>
</body>
</html>

View File

@ -5,7 +5,7 @@
"main": "lib/pert.js",
"bin": {
"pert": "./bin/pert"
},
},
"scripts": {
"prepublish": "scripts/build.sh",
"build": "scripts/build.sh",
@ -23,7 +23,8 @@
"homepage": "https://github.com/fazo96/pert",
"dependencies": {
"chalk": "^1.0.0",
"commander": "^2.6.0"
"commander": "^2.6.0",
"express": "^4.12.2"
},
"devDependencies": {
"coffee-script": "^1.9.1"

View File

@ -3,3 +3,4 @@ echo '#!/usr/bin/env node' > bin/pert
coffee -b -c --no-header -p src/pert-cli.coffee >> bin/pert
chmod +x bin/pert
coffee -b -c --no-header -p src/pert.coffee > lib/pert.js
coffee -b -c --no-header -p src/pert-ui.coffee > client/pert-ui.js

View File

@ -3,6 +3,7 @@ chalk = require 'chalk'
cli = require 'commander'
fs = require 'fs'
Pert = require '../lib/pert.js'
express = require 'express'
ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
@ -36,6 +37,24 @@ cli
pert = new Pert JSON.parse(content), cli.verbose
console.log pert.calculate options
cli
.command 'graph <file>'
.description 'serve HTTP GUI with pert graph of given JSON document'
.alias 'g'
.action (file) ->
didSomething = yes
fs.readFile file, (error,content) ->
if error then err error
else
pert = new Pert JSON.parse(content), cli.verbose
data = pert.calculate()
app = express()
app.use express.static 'client'
app.get '/data', (req,res) -> res.json data
app.listen 3000
console.log chalk.green('Started Web Server'), 'on port', chalk.bold(3000)
cli.parse process.argv
if !didSomething then console.log chalk.green('Tip:'), 'see', chalk.bold(cli.name()+' --help')

17
src/pert-ui.coffee Normal file
View File

@ -0,0 +1,17 @@
$.get 'data', (data) ->
console.log data
i = 0
nodes = data.days.map (x) -> {id: x, label: ""+x}
connections = []
data.activities.forEach (x) ->
connections.push
from: x.startDay, to: x.endDay
label: x.id+" ("+(if x.permittedDelay > 0 then x.duration+"/"+(x.duration+x.permittedDelay) else x.duration)+")"
if x.permittedDelay > 0
connections.push from: x.endDay, to: x.endDay+x.permittedDelay, color: 'green', label: "("+x.permittedDelay+")"
console.log nodes
console.log connections
options =
edges:
style: 'arrow'
network = new vis.Network (document.getElementById 'pert'), { nodes: nodes, edges: connections }, options

View File

@ -3,6 +3,7 @@ fs = require 'fs'
module.exports = class Pert
constructor: (@list, @verbose) ->
@days = []
log: (x...) -> if @verbose then console.log chalk.bold("Pert:"), x...
err: (x...) -> console.log chalk.bold (chalk.red "Pert:"), x...
@ -25,11 +26,14 @@ module.exports = class Pert
@log "start day of",item.id,"is",item.startDay
item.endDay = item.startDay + item.duration
@log "end day of",item.id,"is",item.endDay
@insertDay item.endDay
return item.endDay
# Find out which day the activity starts
calculateStartDay: (item) =>
if !item.depends? or item.depends.length is 0 then return item.startDay = 0
if !item.depends? or item.depends.length is 0
@insertDay 0
return item.startDay = 0
item.startDay = @maxa item.depends.map(@toActivity).map @calculateEndDay
@log "start day of",item.id,"is",item.startDay
# write max delay time to each depend
@ -41,11 +45,20 @@ module.exports = class Pert
@log "written permittedDelay to dependency", x, "of", item, "as", i.permittedDelay
else @log "aborting permittedDelay: already calculated"
@log "permitted delay of",x,"is",i.permittedDelay
@insertDay item.startDay
return item.startDay
# Find out which activity has the highest id
highestID: => return @maxa(@list.map (x) -> x.id)
# Insert a day to the list of days if it's not there already
insertDay: (day) =>
for d in @days
if day is d then return
@days.push day
calculate: (options) ->
@calculateEndDay @toActivity @highestID()
if options?.json then JSON.stringify @list else @list
results = activities: @list, days: @days
if options?.json then JSON.stringify results else results