Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
1
1111
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
madhu.tr
1111
Commits
4836c5c2
Commit
4836c5c2
authored
Apr 12, 2022
by
madhu.tr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
addd
parent
63e0d6cf
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
174 additions
and
130 deletions
+174
-130
main.py
main.py
+21
-8
scripts/core/handler/task1.py
scripts/core/handler/task1.py
+0
-104
scripts/core/handler/task3.py
scripts/core/handler/task3.py
+100
-0
scripts/services/task1.py
scripts/services/task1.py
+0
-18
scripts/services/task3.py
scripts/services/task3.py
+53
-0
No files found.
main.py
View file @
4836c5c2
import
uvicorn
import
json
from
scripts.services.task1
import
report_router
import
time
from
fastapi
import
FastAPI
app
=
FastAPI
()
import
redis
app
.
include_router
(
report_router
)
if
__name__
==
"__main__"
:
# from scripts.core.handler.task3 import num
uvicorn
.
run
(
"main:app"
,
host
=
"0.0.0.0"
,
port
=
2747
)
r
=
redis
.
Redis
(
host
=
'192.168.0.220'
,
port
=
6379
,
db
=
111
)
num
=
1
while
True
:
data
=
{
"data"
:
{
"patient_id"
:
num
}
}
num
=
num
+
1
r
.
set
(
"num"
,
json
.
dumps
(
data
))
print
(
r
.
get
(
"num"
))
time
.
sleep
(
5
)
scripts/core/handler/task1.py
View file @
4836c5c2
import
pymongo
import
pandas
as
pd
from
pydantic
import
BaseModel
myclient
=
pymongo
.
MongoClient
(
"mongodb://192.168.0.220:2747/"
)
mydb
=
myclient
[
"factory"
]
mycol
=
mydb
[
"status"
]
mycol
.
find
()
class
Employee_details
(
BaseModel
):
id
:
str
certificate_number
:
int
business_name
:
str
date
:
str
result
:
str
sector
:
str
address
:
dict
# #
dblist
=
myclient
.
list_database_names
()
if
"factory"
in
dblist
:
print
(
"The database exists."
)
class
Task1
:
def
read_violation
(
self
):
agg
=
list
(
mycol
.
aggregate
(
[
{
'$project'
:
{
'_id'
:
'$business_name'
,
'result'
:
'Violation Issued'
}
},
{
'$group'
:
{
'_id'
:
'$_id'
,
'heghest'
:
{
'$max'
:
'$result'
}
}
},
{
'$sort'
:
{
'highest'
:
-
1
}
}
]))
return
(
agg
[
0
])
def
read_noviolation
(
self
):
r
=
mycol
.
find
({
"result"
:
"No Violation Issued"
})
lst
=
[]
for
i
in
r
:
lst
.
append
(
i
[
"business_name"
])
return
lst
# class Employee_details(BaseModel):
# )
# id: str
# certificate_number: int
# business_name: str
# date: str
# result: str
# sector: str
# address: dict
def
Fnd
(
self
):
q
=
mycol
.
find
({})
if
q
:
a
=
[]
for
i
in
q
:
d
=
{
'Business Name'
:
i
[
'business_name'
],
'Date'
:
i
[
"date"
],
'Result'
:
i
[
"result"
]}
a
.
append
(
d
)
a
=
pd
.
DataFrame
(
a
)
return
(
a
.
to_excel
(
"incpection.xlsx"
,
sheet_name
=
"mySheet"
,
index
=
False
))
else
:
return
"no data"
def
Insert
(
user
):
mycol
.
insert_one
(
user
.
dict
())
return
"Inserted"
def
check_result
(
business_name
,
record_type
):
x
=
business_name
y
=
record_type
cnt
=
mycol
.
count_documents
({
'$and'
:[{
'business_name'
:
x
},{
'result'
:
y
}]})
return
str
(
cnt
)
+
" "
+
str
(
y
)
\ No newline at end of file
scripts/core/handler/task3.py
0 → 100644
View file @
4836c5c2
import
json
import
time
import
redis
from
paho.mqtt
import
client
as
mqtt_client
r
=
redis
.
Redis
(
host
=
'192.168.0.220'
,
port
=
6379
,
db
=
111
)
broker
=
'192.168.0.220'
port
=
1883
topic
=
[
"doc-1"
,
"doc-2"
,
"doc-3"
]
# generate client ID with pub prefix randomly
client_id
=
f
'["doc-1","doc-2","doc-3"]'
username
=
'Mad'
password
=
'public'
def
connect_mqtt
()
->
mqtt_client
:
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
if
rc
==
0
:
print
(
"Connected to MQTT Broker!"
)
else
:
print
(
"Failed to connect, return code
%
d
\n
"
,
rc
)
client
=
mqtt_client
.
Client
(
client_id
)
client
.
username_pw_set
(
username
,
password
)
client
.
on_connect
=
on_connect
client
.
connect
(
broker
,
port
)
return
client
def
publish
(
client
:
object
)
->
object
:
msg_count
=
0
while
True
:
time
.
sleep
(
1
)
counter
=
0
num
=
1
patients
=
{}
while
True
:
msg
=
{
"patient_id"
:
num
}
result
=
client
.
publish
(
topic
[
int
((
counter
)
%
len
(
topic
))],
str
(
msg
))
print
(
result
)
status
=
result
[
0
]
if
status
==
0
:
print
(
f
"Send `{str(msg)}` to topic `{topic[int((counter)
%
len(topic))]}`"
)
if
topic
[
int
(
counter
%
len
(
topic
))]
not
in
patients
.
keys
():
patients
[
topic
[
int
(
counter
%
len
(
topic
))]]
=
[]
patients
[
topic
[
int
(
counter
%
len
(
topic
))]]
.
append
(
str
(
msg
[
'patient_id'
]))
else
:
if
str
(
msg
[
'patient_id'
])
not
in
patients
[
topic
[
int
(
counter
%
len
(
topic
))]]:
patients
[
topic
[
int
(
counter
%
len
(
topic
))]]
.
append
(
str
(
msg
[
'patient_id'
]))
else
:
print
(
f
"Failed to send message to topic {topic}"
)
# counter = counter + 1
# num = num + 1
if
num
==
5
:
# num=1
if
str
(
msg
[
'patient_id'
])
in
patients
[
topic
[
int
(
counter
%
len
(
topic
))]]:
return
patients
else
:
num
=
num
+
1
counter
=
counter
+
1
print
(
patients
)
time
.
sleep
(
0
)
# patient_id= patient_id+1
# time.sleep(5)
# print(counter)
# msg_count += 1
# def run():
# client = connect_mqtt()
# client.loop_start()
# publish(client)
def
run
():
client
=
connect_mqtt
()
client
.
loop_start
()
data
=
publish
(
client
)
# print(data)
# redis_conn()
return
data
def
redis_conn
():
data
=
run
()
# print(data)
print
(
data
)
if
data
:
r
.
set
(
"token_details"
,
json
.
dumps
(
data
))
return
"data inserted"
# data = run()
redis_conn
()
scripts/services/task1.py
View file @
4836c5c2
from
fastapi
import
APIRouter
from
scripts.core.handler.task1
import
Task1
report_router
=
APIRouter
()
@
report_router
.
get
(
"/getdata1"
)
def
most_violation
():
return
Task1
()
.
read_violation
()
@
report_router
.
get
(
"/getdata2"
)
def
no_violation
():
return
Task1
()
.
read_noviolation
()
@
report_router
.
get
(
'/getdata'
)
def
get_data
():
return
Task1
()
.
Fnd
()
@
report_router
.
get
(
'/getDetails'
)
def
checkdata
(
business_name
,
record_type
):
return
Task1
()
.
check_result
(
business_name
,
record_type
)
\ No newline at end of file
scripts/services/task3.py
0 → 100644
View file @
4836c5c2
import
random
import
time
from
paho.mqtt
import
client
as
mqtt_client
broker
=
'192.168.0.220'
port
=
1883
topic
=
"python/mqtt"
# generate client ID with pub prefix randomly
client_id
=
f
'python-mqtt-{random.randint(0, 1000)}'
username
=
'Mad'
password
=
'public'
def
connect_mqtt
():
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
if
rc
==
0
:
print
(
"Connected to MQTT Broker!"
)
else
:
print
(
"Failed to connect, return code
%
d
\n
"
,
rc
)
client
=
mqtt_client
.
Client
(
client_id
)
client
.
username_pw_set
(
username
,
password
)
client
.
on_connect
=
on_connect
client
.
connect
(
broker
,
port
)
return
client
def
publish
(
client
):
msg_count
=
0
while
True
:
time
.
sleep
(
1
)
msg
=
f
"messages: {msg_count}"
result
=
client
.
publish
(
topic
,
msg
)
# result: [0, 1]
status
=
result
[
0
]
if
status
==
0
:
print
(
f
"Send {msg} to topic {topic}"
)
else
:
print
(
f
"Failed to send message to topic {topic}"
)
msg_count
+=
1
def
run
():
client
=
connect_mqtt
()
client
.
loop_start
()
publish
(
client
)
if
__name__
==
'__main__'
:
run
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment