comparison.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <!-- 对应页面:设备统计-平台对比 -->
  3. <view class="fix-top-window">
  4. <view class="uni-header">
  5. <uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
  6. <view class="uni-group hide-on-phone">
  7. <view class="uni-sub-title">多个指标在不同平台数据的占比,可以直观看出各个平台引流的效果</view>
  8. </view>
  9. </view>
  10. <view class="uni-container">
  11. <view class="uni-stat--x flex mb-m" style="padding: 0px 15px;">
  12. <view class="uni-stat--app-select">
  13. <uni-data-select collection="opendb-app-list" field="appid as value, name as text" orderby="text asc" :defItem="1" label="应用选择" v-model="query.appid" :clear="false" />
  14. <uni-data-select collection="opendb-app-versions" :where="versionQuery" class="ml-m" field="_id as value, version as text, uni_platform as label, create_date as date" format="{label} - {text}" orderby="date desc" label="版本选择" v-model="query.version_id" />
  15. </view>
  16. <view class="flex" style="flex: 1;">
  17. <view class="ml-m label-text hide-on-phone">日期选择:</view>
  18. <uni-datetime-picker type="date" v-model="query.start_time" returnType="timestamp"
  19. :clearIcon="false" class="uni-stat-datetime-picker"
  20. :class="{'uni-stat__actived': !!query.start_time}" />
  21. </view>
  22. </view>
  23. <view class="dispaly-grid">
  24. <view v-for="(item,index) in chartsData" :key="index" class="uni-stat--x uni-charts-box1">
  25. <view class="label-text" style="margin: 5px 0 20px 0;">{{chartsData[index].title}}</view>
  26. <qiun-data-charts type="ring" :chartData="chartsData[index]" echartsH5 echartsApp :errorMessage="errorMessage"/>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- #ifndef H5 -->
  31. <fix-window />
  32. <!-- #endif -->
  33. </view>
  34. </template>
  35. <script>
  36. import {
  37. mapfields,
  38. stringifyQuery,
  39. getTimeOfSomeDayAgo,
  40. division,
  41. format,
  42. debounce
  43. } from '@/js_sdk/uni-stat/util.js'
  44. export default {
  45. data() {
  46. return {
  47. query: {
  48. dimension: "day",
  49. appid: '',
  50. version_id: '',
  51. start_time: getTimeOfSomeDayAgo(0),
  52. },
  53. platforms: [],
  54. dayChartsData: [],
  55. monChartsData: [],
  56. errorMessage: "",
  57. }
  58. },
  59. created() {
  60. this.debounceGet = debounce(() => {
  61. this.getAllData(this.query);
  62. }, 300);
  63. },
  64. watch: {
  65. query: {
  66. deep: true,
  67. handler(val) {
  68. this.debounceGet()
  69. }
  70. }
  71. },
  72. computed: {
  73. chartsData() {
  74. return [...this.dayChartsData, ...this.monChartsData]
  75. },
  76. versionQuery() {
  77. const {
  78. appid
  79. } = this.query
  80. const query = stringifyQuery({
  81. appid
  82. })
  83. return query
  84. }
  85. },
  86. methods: {
  87. getAllData(query) {
  88. if (!query.appid) {
  89. this.errorMessage = "请先选择应用";
  90. return; // 如果appid为空,则不进行查询
  91. }
  92. this.errorMessage = "";
  93. this.getChartData(query)
  94. this.getRangeCountData(query, 'month')
  95. },
  96. // 获取天的数据
  97. getChartData(query, type = 'day') {
  98. query = JSON.parse(JSON.stringify(query))
  99. const today = getTimeOfSomeDayAgo(0)
  100. if (query.start_time >= today) {
  101. const now = new Date().getTime()
  102. query.start_time = [today, now]
  103. query = stringifyQuery(query, true)
  104. } else {
  105. query = stringifyQuery(query)
  106. }
  107. const db = uniCloud.database()
  108. db.collection('uni-stat-result')
  109. .where(query)
  110. .field(
  111. `active_device_count,new_device_count,total_devices,platform_id`
  112. )
  113. .groupBy(`platform_id`)
  114. .groupField(
  115. `sum(active_device_count) as ${type}_active_device_count, sum(new_device_count) as ${type}_new_device_count, max(total_devices) as ${type}_total_devices`
  116. )
  117. .get()
  118. .then(res => {
  119. const data = res.result.data
  120. this.initChartOption(data, 'dayChartsData')
  121. })
  122. },
  123. // 获取月的数据
  124. getRangeCountData(query, type) {
  125. query = stringifyQuery(query)
  126. const db = uniCloud.database()
  127. const sub = db.collection('uni-stat-result')
  128. .where(query)
  129. .field(
  130. `active_device_count, new_device_count, platform_id, ${type}(add(new Date(0),start_time), "Asia/Shanghai") as ${type},year(add(new Date(0),start_time), "Asia/Shanghai") as year`
  131. )
  132. .groupBy(`year, ${type ? type + ',' : ''}platform_id`)
  133. .groupField(
  134. `sum(active_device_count) as ${type}_active_device_count, sum(new_device_count) as ${type}_new_device_count`
  135. )
  136. .orderBy(`year asc, ${type} asc`)
  137. .get()
  138. .then(res => {
  139. const data = res.result.data
  140. this.initChartOption(data, 'monChartsData', 'month')
  141. })
  142. },
  143. initChartOption(data, goal, type = 'day') {
  144. const db = uniCloud.database()
  145. db.collection('uni-stat-app-platforms').get().then(res => {
  146. const options = [{
  147. field: `${type}_new_device_count`,
  148. title: `${type === 'day' ? '日' : '月'}新增设备对比`,
  149. series: [{
  150. data: []
  151. }]
  152. }, {
  153. field: `${type}_active_device_count`,
  154. title: `${type === 'day' ? '日' : '月'}活跃设备对比`,
  155. series: [{
  156. data: []
  157. }]
  158. }]
  159. if (type === 'day') {
  160. options.unshift({
  161. field: `day_total_devices`,
  162. title: `总设备数对比`,
  163. series: [{
  164. data: [],
  165. }]
  166. })
  167. }
  168. this[goal] = options
  169. const platformsData = res.result.data
  170. const platforms = {}
  171. platformsData.forEach(p => {
  172. platforms[p._id] = p.name
  173. })
  174. for (const chart of this[goal]) {
  175. const pie = chart.series[0].data
  176. const p = JSON.parse(JSON.stringify(platforms))
  177. for (const item of data) {
  178. for (const key in item) {
  179. if (chart.field === key) {
  180. const id = item.platform_id
  181. const slice = {
  182. name: p[id],
  183. value: item[key]
  184. }
  185. pie.push(slice)
  186. delete p[id]
  187. }
  188. }
  189. }
  190. for (const key in p) {
  191. const slice = {
  192. name: p[key],
  193. value: 0
  194. }
  195. pie.push(slice)
  196. }
  197. }
  198. })
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss">
  204. .uni-charts-box1 {
  205. padding: 10px;
  206. height: 420px;
  207. }
  208. </style>