comparison.vue 5.6 KB

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