@@ -5,6 +5,8 @@ import {nextTick, onMounted, onUnmounted, reactive, ref, watch} from 'vue'
55import ReconnectingWebSocket from ' reconnecting-websocket'
66import {useRoute , useRouter } from ' vue-router'
77import FooterToolBar from ' @/components/FooterToolbar/FooterToolBar.vue'
8+ import nginx_log from ' @/api/nginx_log'
9+ import {debounce } from ' lodash'
810
911const {$gettext} = useGettext ()
1012
@@ -18,7 +20,6 @@ function logType() {
1820}
1921
2022const control = reactive ({
21- fetch: ' new' ,
2223 type: logType (),
2324 conf_name: route .query .conf_name ,
2425 server_idx: parseInt (route .query .server_idx as string ),
@@ -30,26 +31,51 @@ function openWs() {
3031
3132 websocket .onopen = () => {
3233 websocket .send (JSON .stringify ({
33- ... control ,
34- fetch: ' new'
34+ ... control
3535 }))
3636 }
3737
3838 websocket .onmessage = (m : any ) => {
39- const para = document .createElement (' p' )
40- para .appendChild (document .createTextNode (m .data .trim ()));
39+ addLog (m .data )
40+ }
41+ }
4142
42- (logContainer .value as any as Node ).appendChild (para );
43+ function addLog(data : string , prepend : boolean = false ) {
44+ const para = document .createElement (' p' )
45+ para .appendChild (document .createTextNode (data .trim ()))
4346
44- (logContainer .value as any as Element ).scroll ({
45- top: (logContainer .value as any as Element ).scrollHeight ,
46- left: 0 ,
47- behavior: ' smooth'
48- })
47+ const node = (logContainer .value as any as Node )
48+
49+ if (prepend ) {
50+ node .insertBefore (para , node .firstChild )
51+ } else {
52+ node .appendChild (para )
4953 }
54+ const elem = (logContainer .value as any as Element )
55+ elem .scroll ({
56+ top: elem .scrollHeight ,
57+ left: 0 ,
58+ })
59+ }
60+
61+ const page = ref (0 )
62+
63+ function init() {
64+ nginx_log .page (0 , {
65+ conf_name: (route .query .conf_name as string ),
66+ type: logType (),
67+ server_idx: 0 ,
68+ directive_idx: 0
69+ }).then (r => {
70+ page .value = r .page - 1
71+ r .content .split (' \n ' ).forEach ((v : string ) => {
72+ addLog (v )
73+ })
74+ })
5075}
5176
5277onMounted (() => {
78+ init ()
5379 openWs ()
5480})
5581
@@ -66,6 +92,8 @@ watch(auto_refresh, (value) => {
6692})
6793
6894watch (route , () => {
95+ init ()
96+
6997 control .type = logType ();
7098 (logContainer .value as any as Element ).innerHTML = ' '
7199
@@ -88,6 +116,31 @@ onUnmounted(() => {
88116})
89117
90118const router = useRouter ()
119+ const loading = ref (false )
120+
121+ function on_scroll_log() {
122+ if (! loading .value && page .value > 0 ) {
123+ loading .value = true
124+ const elem = (logContainer .value as any as Element )
125+ if (elem .scrollTop / elem .scrollHeight < 0.333 ) {
126+ nginx_log .page (page .value , {
127+ conf_name: (route .query .conf_name as string ),
128+ type: logType (),
129+ server_idx: 0 ,
130+ directive_idx: 0
131+ }).then (r => {
132+ page .value = r .page - 1
133+ r .content .split (' \n ' ).forEach ((v : string ) => {
134+ addLog (v , true )
135+ })
136+ }).finally (() => {
137+ loading .value = false
138+ })
139+ } else {
140+ loading .value = false
141+ }
142+ }
143+ }
91144
92145 </script >
93146
@@ -97,20 +150,11 @@ const router = useRouter()
97150 <a-form-item :label =" $gettext('Auto Refresh')" >
98151 <a-switch v-model:checked =" auto_refresh" />
99152 </a-form-item >
100- <a-form-item :label =" $gettext('Fetch')" >
101- <a-select v-model:value =" control.fetch" style =" max-width : 200px " >
102- <a-select-option value =" all" >
103- <translate >All logs</translate >
104- </a-select-option >
105- <a-select-option value =" new" >
106- <translate >New logs</translate >
107- </a-select-option >
108- </a-select >
109- </a-form-item >
110153 </a-form >
111154
112155 <a-card >
113- <pre class =" nginx-log-container" ref =" logContainer" ></pre >
156+ <pre class =" nginx-log-container" ref =" logContainer"
157+ @scroll =" debounce(on_scroll_log,100, null)()" ></pre >
114158 </a-card >
115159 </a-card >
116160 <footer-tool-bar v-if =" control.type==='site'" >
@@ -125,6 +169,7 @@ const router = useRouter()
125169 height : 60vh ;
126170 overflow : scroll ;
127171 padding : 5px ;
172+ margin-bottom : 0 ;
128173
129174 p {
130175 font-size : 12px ;
0 commit comments