UNPKG

@codeplaydata/datasus

Version:

This application decompress the datasus micro data and serve as a gateway class.

119 lines (115 loc) 3.93 kB
// @filename: main.ts /* * Copyright 2025 Pedro Paulo Teixeira dos Santos Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { BasicFTPClient } from "./infra/ftp/BasicFTPClient.js"; import { JobOrchestrator } from "./infra/job/JobOrchestrator.js"; import { DATASUSGenericFTPGateway } from "./interface/gateway/DATASUSGenericFTPGateway.js"; var StateCode; (function (StateCode) { StateCode["Distrito Federal"] = "53"; StateCode["Rio de Janeiro"] = "33"; StateCode["Sao Paulo"] = "35"; StateCode["Acre"] = "12"; StateCode["Alagoas"] = "27"; StateCode["Amazonas"] = "13"; StateCode["Amapa"] = "16"; StateCode["Bahia"] = "29"; StateCode["Ceara"] = "23"; StateCode["Espirito Santo"] = "32"; StateCode["Goais"] = "52"; StateCode["Maranhao"] = "21"; StateCode["Minas Gerais"] = "31"; StateCode["Mato Grosso do Sul"] = "50"; StateCode["Mato Grosso"] = "51"; StateCode["Para"] = "15"; StateCode["Paraiba"] = "25"; StateCode["Pernambuco"] = "26"; StateCode["Piaui"] = "22"; StateCode["Parana"] = "41"; StateCode["Rio Grande do Norte"] = "24"; StateCode["Rondonia"] = "11"; StateCode["Roraima"] = "14"; StateCode["Rio Grande do Sul"] = "43"; StateCode["Santa Catarina"] = "42"; StateCode["Sergipe"] = "28"; StateCode["Tocantins"] = "17"; })(StateCode || (StateCode = {})); class SIASUSService extends JobOrchestrator { } class SIAFTPGateway extends DATASUSGenericFTPGateway { constructor(ftp) { super(ftp, '/dissemin/publicos/SIASUS/200801_/Dados/'); } static async getInstanceOf(ftp) { return new SIAFTPGateway(ftp); } } class SIABasicParser { dictionary; record; constructor(dictionary) { this.dictionary = dictionary; } static instanceOf(dictionary) { return new SIABasicParser(dictionary); } parse(record) { this.record = record; this.record.TPFIN = this.record.TPFIN + this.record.SUBFIN; delete this.record.SUBFIN; for (const [field, value] of Object.entries(this.record)) { const parser = this.dictionary.get(field); if (parser && value !== undefined) { this.record[field] = parser(value); } } return this.record; } } const MAX_CONCURRENT_PROCESSES = 5; const FTP_HOST = 'ftp.datasus.gov.br'; const ftpClient = await BasicFTPClient.connect(FTP_HOST); const gateway = await SIAFTPGateway.getInstanceOf(ftpClient); const BIDictionary = new Map([ ['CNS_PAC', (value) => { const cleanStr = value.trim(); if (!cleanStr) return ''; return Array.from(cleanStr) .map(char => char.charCodeAt(0).toString(16).padStart(2, '0')) .join('') .toUpperCase(); }] ]); const filters = new Map(); filters.set('MUNPAC', "330055"); const sia = SIASUSService.init(gateway, filters, undefined, 'stdout', MAX_CONCURRENT_PROCESSES); const parser = SIABasicParser.instanceOf(BIDictionary); await sia.subset({ src: 'BI', states: ['RJ'], period: { start: { year: 2022, month: '03' }, end: { year: 2022, month: '04' } } }, parser); await sia.exec('./dist/infra/job/job.js').finally(() => { console.log('Done!'); process.exit(0); });