From 89a9262e29aed638e1e399feee1027e46f78e796 Mon Sep 17 00:00:00 2001 From: Shweelan Samson Date: Tue, 15 Sep 2015 14:37:27 +0300 Subject: [PATCH] added tester data model dump script to dump what any device from the db by its --- test/dump-data-model.coffee | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/dump-data-model.coffee diff --git a/test/dump-data-model.coffee b/test/dump-data-model.coffee new file mode 100644 index 0000000..f4f0fcc --- /dev/null +++ b/test/dump-data-model.coffee @@ -0,0 +1,72 @@ +### +# Copyright 2013, 2014, 2015 Zaid Abdulla +# +# GenieACS is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# GenieACS is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with GenieACS. If not, see . +### + +db = require '../lib/db' +fs = require 'fs' + +deviceJson = {} + +f = (object, path) -> + if (not path) + path = '' + + for key in Object.keys(object) + continue if key[0] == '_' + + if (object[key]) + isObject = '_object' of object[key] or '_instance' of object[key] + isValue = '_value' of object[key] + if (isObject and isValue) or (not isObject and not isValue) + throw new Error('Unexpected object') + + if (isValue) + value = object[key]['_value'] + if (typeof value == 'boolean') + if (value) + value = 1 + else + value = 0 + + values = [object[key]['_writable'] or false, value.toString()] + values.push(object[key]['_type']) if (object[key]['_type']) + deviceJson["#{path}#{key}"] = values + continue + + if (isObject) + values = [object[key]['_writable'] or false] + deviceJson["#{path}#{key}."] = values + f(object[key], "#{path}#{key}.") + + +db.connect((err) -> + throw err if (err) + + db.devicesCollection.findOne({_id: process.argv[2]}, {}, (err, device) -> + throw err if (err) + + if (not device) + throw new Error('Device not found') + + f(device) + fs.writeFile("data_model_#{process.argv[2]}.json", JSON.stringify(deviceJson, null, ' '), () -> + throw err if (err) + + console.log("data_model_#{process.argv[2]}.json Saved") + process.exit(0) + ) + ) +)