Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SMPPIR-CheckIn_API
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Harbor Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SMPPIR
SMPPIR-CheckIn_API
Commits
8115d8b6
Commit
8115d8b6
authored
4 years ago
by
ns17
Browse files
Options
Downloads
Patches
Plain Diff
Adding csv download of geolocation data and category
parent
b30f147d
No related branches found
No related tags found
1 merge request
!1
Csv download
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/models/category.js
+61
-0
61 additions, 0 deletions
common/models/category.js
common/models/geolocation.js
+105
-0
105 additions, 0 deletions
common/models/geolocation.js
with
166 additions
and
0 deletions
common/models/category.js
+
61
−
0
View file @
8115d8b6
...
...
@@ -21,4 +21,65 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>.
'
use strict
'
;
module
.
exports
=
function
(
Category
)
{
Category
.
csvexport
=
function
(
type
,
res
,
callback
)
{
var
datetime
=
new
Date
();
var
datetimeFuture
=
new
Date
(
datetime
.
getFullYear
(),
datetime
.
getMonth
(),
datetime
.
getDate
()
+
7
)
res
.
set
(
'
Expires
'
,
datetimeFuture
+
''
);
res
.
set
(
'
Cache-Control
'
,
'
max-age=0, no-cache, must-revalidate, proxy-revalidate
'
);
res
.
set
(
'
Last-Modified
'
,
datetime
+
''
);
res
.
set
(
'
Content-Type
'
,
'
application/force-download
'
);
res
.
set
(
'
Content-Type
'
,
'
application/octet-stream
'
);
res
.
set
(
'
Content-Type
'
,
'
application/download
'
);
res
.
set
(
'
Content-Disposition
'
,
'
attachment;filename=category.csv
'
);
res
.
set
(
'
Content-Transfer-Encoding
'
,
'
binary
'
);
Category
.
find
(
function
(
err
,
categories
)
{
// conversão JSON para CSV
var
itemsFormatted
=
[];
// cabeçalho do csv
var
headers
=
{
category_name
:
"
category_name
"
,
category_description
:
"
category_description
"
,
id
:
"
id
"
};
categories
.
forEach
((
item
)
=>
{
itemsFormatted
.
push
({
category_name
:
item
.
category_name
,
category_description
:
item
.
category_description
,
id
:
item
.
id
});
});
if
(
headers
)
{
itemsFormatted
.
unshift
(
headers
);
}
var
jsonObject
=
JSON
.
stringify
(
itemsFormatted
);
var
array
=
typeof
jsonObject
!=
'
object
'
?
JSON
.
parse
(
jsonObject
)
:
jsonObject
;
var
csv
=
''
;
for
(
var
i
=
0
;
i
<
array
.
length
;
i
++
)
{
var
line
=
''
;
for
(
var
index
in
array
[
i
])
{
if
(
line
!=
''
)
line
+=
'
;
'
// aqui muda o separador do csv
line
+=
array
[
i
][
index
];
}
csv
+=
line
+
'
\r\n
'
;
}
res
.
send
(
csv
);
});
};
Category
.
remoteMethod
(
'
csvexport
'
,
{
accepts
:
[
{
arg
:
'
type
'
,
type
:
'
string
'
,
required
:
false
},
{
arg
:
'
res
'
,
type
:
'
object
'
,
'
http
'
:
{
source
:
'
res
'
}}
],
returns
:
{},
// Para usar o 'type' -> http: {path: '/csv/:type', verb: 'get'}
http
:
{
path
:
'
/csv
'
,
verb
:
'
get
'
},
});
};
This diff is collapsed.
Click to expand it.
common/models/geolocation.js
+
105
−
0
View file @
8115d8b6
...
...
@@ -21,5 +21,110 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>.
'
use strict
'
;
module
.
exports
=
function
(
Geolocation
)
{
Geolocation
.
csvexport
=
function
(
type
,
res
,
callback
)
{
if
(
type
==
"
padr
"
||
type
==
"
andr
"
){
var
datetime
=
new
Date
();
var
datetimeFuture
=
new
Date
(
datetime
.
getFullYear
(),
datetime
.
getMonth
(),
datetime
.
getDate
()
+
7
)
res
.
set
(
'
Expires
'
,
datetimeFuture
+
''
);
res
.
set
(
'
Cache-Control
'
,
'
max-age=0, no-cache, must-revalidate, proxy-revalidate
'
);
res
.
set
(
'
Last-Modified
'
,
datetime
+
''
);
res
.
set
(
'
Content-Type
'
,
'
application/force-download
'
);
res
.
set
(
'
Content-Type
'
,
'
application/octet-stream
'
);
res
.
set
(
'
Content-Type
'
,
'
application/download
'
);
res
.
set
(
'
Content-Disposition
'
,
'
attachment;filename=geolocation.csv
'
);
res
.
set
(
'
Content-Transfer-Encoding
'
,
'
binary
'
);
Geolocation
.
find
(
function
(
err
,
geolocations
)
{
// conversão JSON para CSV
var
itemsFormatted
=
[];
if
(
type
==
"
padr
"
){
// cabeçalho do csv
var
headers
=
{
category_id
:
"
category_id
"
,
geolocation_name
:
"
geolocation_name
"
,
latitude
:
"
latitude
"
,
longitude
:
"
longitude
"
,
when_sent
:
"
when_sent
"
,
description_location
:
"
description_location
"
,
user_id
:
"
user_id
"
,
id
:
"
id
"
};
geolocations
.
forEach
((
item
)
=>
{
itemsFormatted
.
push
({
category_id
:
item
.
category_id
,
geolocation_name
:
item
.
geolocation_name
,
latitude
:
item
.
latitude
,
longitude
:
item
.
longitude
,
when_sent
:
item
.
when_sent
,
description_location
:
item
.
description_location
,
user_id
:
item
.
user_id
,
id
:
item
.
id
});
});
}
if
(
type
==
"
andr
"
){
// cabeçalho do csv
var
headers
=
{
category_id
:
"
category_id
"
,
geolocation_name
:
"
geolocation_name
"
,
latitude
:
"
latitude
"
,
longitude
:
"
longitude
"
,
android_id
:
"
android_id
"
,
when_sent
:
"
when_sent
"
,
description_location
:
"
description_location
"
,
user_id
:
"
user_id
"
,
id
:
"
id
"
};
geolocations
.
forEach
((
item
)
=>
{
itemsFormatted
.
push
({
category_id
:
item
.
category_id
,
geolocation_name
:
item
.
geolocation_name
,
latitude
:
item
.
latitude
,
longitude
:
item
.
longitude
,
android_id
:
item
.
android_id
,
when_sent
:
item
.
when_sent
,
description_location
:
item
.
description_location
,
user_id
:
item
.
user_id
,
id
:
item
.
id
});
});
}
if
(
headers
)
{
itemsFormatted
.
unshift
(
headers
);
}
var
jsonObject
=
JSON
.
stringify
(
itemsFormatted
);
var
array
=
typeof
jsonObject
!=
'
object
'
?
JSON
.
parse
(
jsonObject
)
:
jsonObject
;
var
csv
=
''
;
for
(
var
i
=
0
;
i
<
array
.
length
;
i
++
)
{
var
line
=
''
;
for
(
var
index
in
array
[
i
])
{
if
(
line
!=
''
)
line
+=
'
;
'
// aqui muda o separador do csv
line
+=
array
[
i
][
index
];
}
csv
+=
line
+
'
\r\n
'
;
}
res
.
send
(
csv
);
});
}
else
console
.
log
(
'
Use as rotas /padr ou /andr
'
)
};
Geolocation
.
remoteMethod
(
'
csvexport
'
,
{
accepts
:
[
{
arg
:
'
type
'
,
type
:
'
string
'
},
{
arg
:
'
res
'
,
type
:
'
object
'
,
'
http
'
:
{
source
:
'
res
'
}}
],
returns
:
{},
http
:
{
path
:
'
/csv/:type
'
,
verb
:
'
get
'
}
});
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment