Guest User

Untitled

a guest
Nov 3rd, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. import { Observable } from 'rxjs/Observable';
  2. import { Subscriber } from 'rxjs/Subscriber';
  3. import { readFile } from 'fs';
  4. import * as program from 'commander';
  5.  
  6. import {
  7. createConnection,
  8. Connection,
  9. MysqlError,
  10. // format as mysqlFormat
  11. } from 'mysql';
  12.  
  13. // var Rx = require('rxjs');
  14. // var request = require('request');
  15. // var progress = require('request-progress');
  16.  
  17. declare var __VERSION__: string;
  18.  
  19. export function generateObservableOperator<I, O>(callback: (subscriber: Subscriber<O>, input: I) => void): (source: Observable<I>) => Observable<O> {
  20.  
  21. return (source: Observable<I>): Observable<O> => {
  22.  
  23. return new Observable<O>((subscriber: Subscriber<O>) => {
  24.  
  25. source.subscribe({
  26.  
  27. next: input => callback(subscriber, input),
  28. error: error => subscriber.error(error),
  29. complete: () => subscriber.complete(),
  30. });
  31. });
  32. };
  33. }
  34.  
  35. export function parseCliArgs(subscriber: Subscriber<string>, argv: string[]) {
  36.  
  37. program
  38. .version(__VERSION__)
  39. .option('-f, --file <required>', 'le nom du fichier de config.json à importer')
  40. .parse(argv);
  41.  
  42. if (program['file'] === undefined) {
  43.  
  44. program.outputHelp();
  45. subscriber.error('Please specify the config file');
  46. }
  47. else {
  48. subscriber.next(program.file);
  49. }
  50. }
  51.  
  52. export function loadConfigFile(subscriber: Subscriber<any>, filename: string) {
  53.  
  54. readFile(filename, 'utf8', function (err, contents) {
  55. if (err || !contents) {
  56.  
  57. console.log('Error during reading of config file' + err);
  58. console.log('There\'s a problem with your config file : ' + program.file);
  59. subscriber.error();
  60. }
  61. else {
  62. try {
  63. let config = JSON.parse(contents);
  64. subscriber.next(config);
  65. }
  66. catch (error) {
  67. subscriber.error();
  68. }
  69. }
  70. });
  71. }
  72.  
  73. // Connect to the mySQL server.
  74. export function connectToMySQL(subscriber: Subscriber<Connection>, host: string, user: string, password: string, database: string) {
  75.  
  76. let connection: Connection = createConnection({
  77.  
  78. host: host,
  79. user: user,
  80. password: password,
  81. database: database,
  82. });
  83.  
  84. connection.connect(function (err: MysqlError) {
  85.  
  86. if (err) {
  87. console.error('Error connecting to the database : ' + err.stack);
  88. subscriber.error();
  89. }
  90. else {
  91. subscriber.next(connection);
  92. }
  93. });
  94. }
  95.  
  96. // Get the last update timestamp.
  97. export function getLastUpdateDate(subscriber: Subscriber<number>, connection: Connection) {
  98.  
  99. var query = 'SELECT * FROM file_log ORDER BY date DESC LIMIT 1';
  100.  
  101. connection.query(query, (error: MysqlError, results: any) => {
  102. if (error) {
  103. subscriber.error();
  104. return connection.rollback(function () {
  105. throw error;
  106. });
  107. }
  108. else {
  109.  
  110. if (results && results.date) {
  111. return subscriber.next(results.date);
  112. }
  113. }
  114. });
  115. }
  116.  
  117.  
  118. // Get the list of urls from the opendata.gouv.fr webservice.
  119. // Register those urls in the database.
  120. // export function getRessourcesUrls(connection: Connection, config: any, timestamp: number) {
  121.  
  122. // var configDB = config.dev;
  123. // var query: string = '';
  124.  
  125. // connection.beginTransaction((err: MysqlError) => {
  126.  
  127. // if (err) {
  128.  
  129. // console.log('Error during transaction' + err);
  130. // throw err;
  131. // }
  132.  
  133. // // OBSERVABLE CREATE.
  134. // var observable = Rx.Observable.create((subscriber: any) => {
  135.  
  136. // // Connexion to the webservice.
  137. // var options = {
  138. // url: config.openData.webServiceUrl + config.openData.datasetId,
  139. // followAllRedirects: true,
  140. // headers: {
  141. // 'User-Agent': 'request',
  142. // 'X-API-KEY': config.openData.apiKey,
  143. // accept: '*/*'
  144. // }
  145. // };
  146.  
  147. // request(options, (error: any, response: any, body: any) => {
  148.  
  149. // console.log('Getting Urls : START');
  150.  
  151. // if (!error && response.statusCode == 200) {
  152.  
  153. // var info = JSON.parse(body);
  154.  
  155. // info.resources.forEach((item: any) => {
  156.  
  157. // let date = new Date(item.published);
  158. // let published = date.getTime();
  159. // // We only process the file that are labeled update and are more recent that the timestamp given in parameters.
  160. // if (published > timestamp
  161. // && item.format == 'zip'
  162. // && item.title.match(/Sirene : mise à jour quotidienne du/g)) {
  163.  
  164. // item.date = published / 1000;
  165. // subscriber.next(item);
  166. // }
  167. // });
  168. // subscriber.complete();
  169. // }
  170.  
  171. // });
  172. // });
  173.  
  174. // let urls: any[] = [];
  175. // // We process the files in bulk.
  176. // var bufferedObservable = observable.bufferCount(configDB.size);
  177. // bufferedObservable.subscribe({
  178.  
  179. // next: (items: any) => {
  180.  
  181. // var values: any[] = [];
  182. // var count = 0;
  183. // for (let item of items) {
  184.  
  185. // values[count++] = [item.date, item.url];
  186. // urls[count] = item.url;
  187. // }
  188.  
  189. // // we fill the database with the fetched data.
  190. // query = queryInitiateInsert(configDB.tableFile);
  191. // query = mysqlFormat(query, [['date', 'url'], values]);
  192.  
  193. // connection.query(query, (error: MysqlError) => {
  194.  
  195. // if (error) {
  196. // return connection.rollback(function () {
  197. // throw error;
  198. // });
  199. // }
  200. // });
  201. // },
  202.  
  203. // error: (err: any) => {
  204.  
  205. // return connection.rollback(function () {
  206. // console.log('Transaction error');
  207. // throw err;
  208. // });
  209. // },
  210.  
  211. // complete: () => {
  212.  
  213. // console.log('Getting Urls : FINISH');
  214. // connection.commit((err: MysqlError) => {
  215. // if (err) {
  216. // return connection.rollback(function () {
  217. // console.log('Transaction error');
  218. // throw err;
  219. // });
  220. // }
  221.  
  222. // // Now we get the files referenced by the urls.
  223. // getExternalFiles(connection, config, timestamp);
  224. // });
  225. // }
  226. // });
  227.  
  228. // });
  229. // }
Add Comment
Please, Sign In to add comment